## PRELIMINARY CODE FOR HOMEWORK ## You need to write an R function ## that fits a smoothing spline with k ## nodes. ## Arguments x, y , number of nodes or nodes. ## The function needs to return a list with ## two components (1) X matrix (2) Vector of coefficients. ## To make it realy good you also need (3) Predicted values ## (4) Predicted values on a testing set prespecified as an ## extra argument of the function. x = runif(30) y = sin(x*9)+rnorm(30)/10 nod = seq(min(x),max(x), length=7)[-c(1,7)] X = cbind(1,x,x^2) for(i in 1:5) { u = x*0 u[ x > nod[i]] = (x[x>nod[i]]-nod[i])^3 X = cbind(X,u) } z = lsfit(X,y,int=F) plot(x,y) points(x,y- z$resid,pch=16,col=2) i = sort.list(x) lines(x,y- z$resid,col=2) plot(x,y) lines(x[i],(y- z$resid)[i],col=2)