# Shows the effects of three levels of smoothing
# where the size of the third derivative is penalized.
# The null space contains quadratic functions.
x <- seq(-1,1,0.02)
y <- x + 3*exp(-6*x^2) + rnorm(rep(1,101))*0.2
# set up a saturated B-spline basis
basisobj <- create.bspline.basis(c(-1,1),101)
lambda <- 1
fdParobj <- fdPar(basisobj, 2, lambda)
result1 <- smooth.basis(x, y, fdParobj)
yfd1 <- result1$fd
c(result1$df,result1$gcv) # display df and gcv measures
lambda <- 1e-4
fdParobj <- fdPar(basisobj, 2, lambda)
result2 <- smooth.basis(x, y, fdParobj)
yfd2 <- result2$fd
c(result2$df,result2$gcv)
lambda <- 0
fdParobj <- fdPar(basisobj, 2, lambda)
result3 <- smooth.basis(x, y, fdParobj)
yfd3 <- result3$fd
c(result3$df,result3$gcv)
plot(x,y) # plot the data
lines(yfd1, lty=2) # add heavily penalized smooth
lines(yfd2, lty=1) # add reasonably penalized smooth
lines(yfd3, lty=3) # add smooth without any penalty
legend(-1,3,c("1","0.0001","0"),lty=c(2,1,3))
plotfit.fd(y, x, yfd2) # plot data and smoothRun the code above in your browser using DataLab