##### Octane data example #####
data(gasoline)
# Create the requisite functional data objects
bbasis = create.bspline.basis(c(900, 1700), 40)
wavelengths = 2*450:850
nir = matrix(NA, 401, 60)
for (i in 1:60) nir[ , i] = gasoline$NIR[i, ]
# Why not just take transpose of gasoline$NIR above?
# Because for some reason it leads to an error in the next statement
gas.fd = smooth.basisPar(wavelengths, nir, bbasis)$fd
# Method 1: Call fpcr with fdobj argument
gasmod1 = fpcr(gasoline$octane, fdobj = gas.fd, nc = 30)
# Method 2: Call fpcr with explicit signal, basis, and penalty matrices
# --the following lines use functions from package 'fda' to derive
# these matrices so as to yield identical results to the above
mybasismat = eval.basis(wavelengths, bbasis)
bb2 = quadset(basisobj = bbasis)
myhalfpen = crossprod(diag(sqrt(bb2$quadvals[ , 2])), eval.basis(bb2, bb2$quadvals[ , 1], 2))
gasmod2 = fpcr(gasoline$octane, sigmat = gasoline$NIR, basismat = mybasismat,
halfpens = list(myhalfpen), nc = 30)
# Check that both calls yield identical estimates
plot(wavelengths, gasmod1$fhat, type='l', xlab='Wavelength',
ylab='Coefficient function')
lines(wavelengths, gasmod2$fhat, col=2)
Run the code above in your browser using DataLab