# A toy example that creates problems with
# data2fd: (0,0) -> (0.5, -0.25) -> (1,1)
b2.3 <- create.bspline.basis(norder=2, breaks=c(0, .5, 1))
# 3 bases, order 2 = degree 1 =
# continuous, bounded, locally linear
fdPar2 <- fdPar(b2.3, Lfdobj=2, lambda=1)
# Penalize excessive slope Lfdobj=1;
# second derivative Lfdobj=2 is discontinuous.
#fd2.3s0 <- smooth.basis(0:1, 0:1, fdPar2)
b3.4 <- create.bspline.basis(norder=3, breaks=c(0, .5, 1))
# 4 bases, order 3 = degree 2 =
# continuous, bounded, locally quadratic
fdPar3 <- fdPar(b3.4, lambda=1)
# Penalize excessive slope Lfdobj=1;
# second derivative Lfdobj=2 is discontinuous.
fd3.4s0 <- smooth.basis(0:1, 0:1, fdPar3)
plot(fd3.4s0$fd)
# 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)
fdParobj <- fdPar(basisobj, 2, lambda=1)
result1 <- smooth.basis(x, y, fdParobj)
yfd1 <- result1$fd
with(result1, c(df, gcv, SSE))
fdParobj <- fdPar(basisobj, 2, lambda=1e-4)
result2 <- smooth.basis(x, y, fdParobj)
yfd2 <- result2$fd
with(result2, c(df, gcv, SSE))
fdParobj <- fdPar(basisobj, 2, lambda=0)
result3 <- smooth.basis(x, y, fdParobj)
yfd3 <- result3$fd
with(result3, c(df, gcv, SSE))
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 smooth
Run the code above in your browser using DataLab