HWS9

ST 380: Probability and Statistics for the Physical Sciences
Solution to Homework Assignment - 9
Prepared by Liwei Wang
Fall, 2009
Ch.3-Ex.1: (a) R code:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
data(attenu)
xa<-attenu$dist
ya<-attenu$accel
plot(xa,ya,xlab=’Distance’,ylab=’Acceleration’,main=’(a)’,pch=’.’)
data(airquality)
temp<-airquality$Temp
ozone<-airquality$Ozone
good=which(ozone>0)
xb=temp[good]
yb=ozone[good]
plot(xb,yb,xlab=’temperature’,ylab=’ozone’,main=’(b)’,pch=’.’)
data(faithful)
xd<-faithful$eruptions
yd<-faithful$waiting
plot(xd,yd,xlab=’eruptions’,ylab=’waiting’,main=’(d)’,pch=’.’)
We get the same graphics shown in Figures 3.1(a), (b) and (d).
ST 380: HWS9
Page 1
c
°Sujit
Ghosh, NCSU Statistics
(b)
150
50
100
ozone
0.6
0.4
0.2
0
0.0
Acceleration
0.8
(a)
0
100
200
300
Distance
60
70
80
90
temperature
70
50
60
waiting
80
90
(d)
1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
eruptions
(b) Use lines() to add lowess and supsmu fits.
R codes:
>
>
>
>
>
>
>
>
>
>
>
>
ST 380: HWS9
plot(xa,ya,xlab=’Distance’,ylab=’Acceleration’,main=’(a)’,pch=’.’)
lines(lowess(xa,ya),col=2)
lines(supsmu(xa,ya),lty=2,col=3)
legend(200,0.8, c(paste(c(’lowess’,’supsmu’))),col=2:3,lty=1:2)
plot(xb,yb,xlab=’temperature’,ylab=’ozone’,main=’(b)’,pch=’.’)
lines(lowess(xb,yb),col=2)
lines(supsmu(xb,yb),lty=2,col=3)
legend(60,150, c(paste(c(’lowess’,’supsmu’))),col=2:3,lty=1:2)
plot(xd,yd,xlab=’eruptions’,ylab=’waiting’,main=’(d)’,pch=’.’)
lines(lowess(xd,yd),col=2)
lines(supsmu(xd,yd),lty=2,col=3)
legend(2,90, c(paste(c(’lowess’,’supsmu’))),col=2:3,lty=1:2)
Page 2
c
°Sujit
Ghosh, NCSU Statistics
(b)
0.2
150
50
0.4
100
lowess
supsmu
ozone
0.6
lowess
supsmu
0
0.0
Acceleration
0.8
(a)
0
100
200
300
Distance
60
70
80
90
temperature
lowess
supsmu
70
50
60
waiting
80
90
(d)
1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
eruptions
(c) For lowess, the tuning parameter is f. For supsmu, the tuning parameter is
span. Let try several different values on attenu dataset.
>
>
>
>
>
>
>
>
>
>
>
ST 380: HWS9
par(mfrow=c(1,2))
plot(xa,ya,xlab=’Distance’,ylab=’Acceleration’,main=’(a)lowess’,pch=’.’)
lines(lowess(xa,ya),col=2)
lines(lowess(xa,ya,f=0.2),lty=2,col=3)
lines(lowess(xa,ya,f=0.8),lty=3,col=4)
legend(200,0.8, c(paste(’f=’,c(’2/3’,’0.2’,’0.8’))),col=2:4,lty=1:3)
plot(xa,ya,xlab=’Distance’,ylab=’Acceleration’,main=’(a)supsmu’,pch=’.’)
lines(supsmu(xa,ya),col=2)
lines(supsmu(xa,ya,span=0.3),lty=2,col=3)
lines(supsmu(xa,ya,span=0.4),lty=3,col=4)
legend(200,0.8, c(paste(’span=’,c(’CV’,’0.3’,’0.4’))),col=2:4,lty=1:3)
Page 3
c
°Sujit
Ghosh, NCSU Statistics
0.8
(a)supsmu
0.8
(a)lowess
0.4
Acceleration
0.0
0.2
0.4
0.0
0.2
Acceleration
0.6
span= CV
span= 0.3
span= 0.4
0.6
f= 2/3
f= 0.2
f= 0.8
0
100
200
300
0
100
200
Distance
300
Distance
Ch.3-Ex.2 R codes:
>
>
>
>
>
>
>
data(mtcars)
x<-mtcars$wt
y<-mtcars$disp
plot(x,y,xlab=’Weight’,ylab=’Displacement’,main=’mtcars’)
lines(lowess(x,y))
plot(y,x,ylab=’Weight’,xlab=’Displacement’,main=’mtcars’)
lines(lowess(y,x))
mtcars
100
2
200
3
Weight
300
Displacement
4
400
5
mtcars
2
3
4
5
Weight
100
200
300
400
Displacement
From the plots above, we can see that it does matters much which we think of
as X and which as Y. However, there is always one way more natural than the
other. For instance, in this problem, weight is easier to measure. Hence, it is more
sensible that we consider weight as predict variable, i.e. X, and displacement as
dependent variable, i.e. Y.
Ch.3-Ex.3 To read data into R, I first save the data in a txt file. Then I change my working
directory to the folder where I save the txt file. After that, ’read.table’ in R is
ST 380: HWS9
Page 4
c
°Sujit
Ghosh, NCSU Statistics
used.
R codes:
draft<-read.table(’draft.txt’,header=T)
xdraft<-draft$Day_of_year
ydraft<-draft$Draft_No.
plot(xdraft,ydraft,xlab=’Day of year’,ylab=’Draft number’)
lines(lowess(xdraft,ydraft))
lines(supsmu(xdraft,ydraft),lty=2)
200
0
100
Draft number
300
>
>
>
>
>
>
0
100
200
300
Day of year
>
>
>
>
>
>
>
>
plot(xdraft,ydraft,xlab=’Day of year’,ylab=’Draft number’)
lines(lowess(xdraft,ydraft))
lines(lowess(xdraft,ydraft,f=0.8),col=2,lty=2)
legend(200,350, c(paste(’f=’,c(’2/3’,’0.8’))),col=1:2,lty=1:2)
plot(xdraft,ydraft,xlab=’Day of year’,ylab=’Draft number’)
lines(supsmu(xdraft,ydraft))
lines(supsmu(xdraft,ydraft,span=0.1),col=2,lty=2)
legend(200,350, c(paste(’span=’,c(’CV’,’0.1’))),col=1:2,lty=1:2)
ST 380: HWS9
Page 5
c
°Sujit
Ghosh, NCSU Statistics
By setting f = 0.8 and span = 0.1, I get smoother and wigglier smoothers.
200
Draft number
0
100
200
0
100
Draft number
300
span= CV
span= 0.1
300
f= 2/3
f= 0.8
0
100
200
300
0
Day of year
ST 380: HWS9
100
200
300
Day of year
Page 6
c
°Sujit
Ghosh, NCSU Statistics