data(tecator)
absorp=tecator$absorp.fdata
ind=1:129
x=absorp[ind,]
y=tecator$y$Fat[ind]
newx=absorp[-ind,]
newy=matrix(tecator$y$Fat[-ind],ncol=1)
# Functional PC regression
res.pc=fregre.pc(x,y,1:6)
summary(res.pc)
pred.pc=predict.fregre.fd(res.pc,newx)
# Functional nonparametric regression
# Functional nonparametric regression with other semimetric.
res.np=fregre.np(x,y,Ker=AKer.tri,metric=semimetric.deriv)
summary(res.np)
pred.np=predict.fregre.fd(res.np,newx)
# Functional regression with basis representation
res.basis=fregre.basis.cv(x,y)
summary(res.basis)
pred.basis=predict.fregre.fd(res.basis,newx)
#x.d=fdata.deriv(x,nbasis=19,nderiv=1)
#res.basis2=fregre.basis.cv(x.d,y)
#summary(res.basis2)
#newx.d=fdata.deriv(newx,nbasis=19,nderiv=1)
#pred.basis2=predict.fregre.fd(res.basis2,newx.d)
dev.new()
plot(pred.pc-newy)
points(pred.np-newy,col=2,pch=2)
points(pred.basis-newy,col=4,pch=4)
sum((pred.pc-newy)^2,na.rm=TRUE)/sum((newy-mean(newy))^2,na.rm=TRUE)
sum((pred.np-newy)^2,na.rm=TRUE)/sum((newy-mean(newy))^2,na.rm=TRUE)
sum((pred.basis-newy)^2,na.rm=TRUE)/sum((newy-mean(newy))^2,na.rm=TRUE)Run the code above in your browser using DataLab