# simplest call
girlGrowthSm <- with(growth, smooth.basisPar(argvals=age, y=hgtf))
plot(girlGrowthSm$fd, xlab="age", ylab="height (cm)",
main="Girls in Berkeley Growth Study" )
plot(deriv(girlGrowthSm$fd), xlab="age", ylab="growth rate (cm / year)",
main="Girls in Berkeley Growth Study" )
plot(deriv(girlGrowthSm$fd, 2), xlab="age",
ylab="growth acceleration (cm / year^2)",
main="Girls in Berkeley Growth Study" )
# 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) + sin(1:101)/2
# sin not rnorm to make it easier to compare
# results across platforms
# 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)
result1 <- smooth.basisPar(argvals=x, y=y,
fdobj=basisobj, Lfdobj=2, lambda=1)
all.equal(result1, result1.)
# TRUE
with(result1, c(df, gcv)) # display df and gcv measures
fdParobj <- fdPar(basisobj, 2, lambda=1e-4)
result2 <- smooth.basisPar(x, y, basisobj, 2, lambda=1e-4)
with(result2, c(df, gcv)) # display df and gcv measures
result3 <- smooth.basisPar(x, y, basisobj, 2, lambda=0)
with(result3, c(df, gcv)) # display df and gcv measures
plot(x,y) # plot the data
lines(result1$fd, lty=2) # add heavily penalized smooth
lines(result2$fd, lty=1) # add reasonably penalized smooth
lines(result3$fd, lty=3) # add smooth without any penalty
legend(-1,3,c("1","0.0001","0"),lty=c(2,1,3))
plotfit.fd(y, x, result2$fd) # plot data and smooth
Run the code above in your browser using DataLab