#create a data matrix
t <- seq(0,4*pi,len=50)
X.org <- matrix(cbind(cos(t),sin(2*t)),length(t),2) %*%
matrix(rnorm(20),2,10)
#add some normal errors
X <- X.org + .25*rnorm(length(X.org))
#and mark some data as missing
X[runif(length(X))<.25] <- NA
#compute two smooth basis functions
res <- SVD.smooth(X, n.basis=2, niter=100)
#plot the two smooth basis functions
par(mfcol=c(3,2),mar=c(4,4,.5,.5))
plot(t,res[,1],ylim=range(res),type="l")
lines(t,res[,2],col=2)
#and some of the data fitted to the smooths
for(i in 1:5){
plot(t,X[,i])
lines(t,predict.lm(lm(X[,i]~res),data.frame(res)))
lines(t,X.org[,i],col=2)
}
Run the code above in your browser using DataLab