{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# hw5_R -...

This preview shows pages 1–2. Sign up to view the full content.

abalone=read.table("http://www- stat.stanford.edu/~hastie/stats305/Data/abalone.305",sep=",",head=T,row.names=1) attach(abalone) tps=function(x,knots,d=3){ B1=outer(x,1:d,"^") B2=outer(x,knots,"-")^d B2[B2<0]=0 cbind(1,B1,B2) } cB=function(B,C){ qrC=qr(t(C)) qC=qr.Q(qrC,complete=T)[,-seq(nrow(C))] B%*%qC } pdf(file="hw5_plots.pdf") Rings.grid=seq(from=0,to=25,by=.1) fit1=lm(Shucked.wt~tps(Rings,knots=c(5,10,15,20),d=1)-1) plot(Rings,Shucked.wt,xlim=c(0,25),ylim=c(-.5,1.4),main="Linear Spline") matlines(Rings.grid,predict(fit1,list(Rings=Rings.grid),interval="conf"),col=1,lty= c(1,2,2)) abline(v=c(5,10,15,20),lty=2) abline(h=0) fit2=lm(Shucked.wt~tps(Rings,knots=c(5,10,15,20))-1) plot(Rings,Shucked.wt,xlim=c(0,25),ylim=c(-.5,1.4),main="Cubic Spline") matlines(Rings.grid,predict(fit2,list(Rings=Rings.grid),interval="conf"),col=1,lty= c(1,2,2)) abline(v=c(5,10,15,20),lty=2) abline(h=0) C1=rbind( c(0,1,0,0,0,0), c(0,0,1,1,1,1) ) fit3=lm(Shucked.wt~cB(tps(Rings,knots=c(5,10,15,20),d=1),C1)-1) plot(Rings,Shucked.wt,xlim=c(0,25),ylim=c(-.5,1.4),main="Natural Linear Spline") matlines(Rings.grid,predict(fit3,list(Rings=Rings.grid),interval="conf"),col=1,lty= c(1,2,2)) abline(v=c(5,10,15,20),lty=2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}