Mean and covariance estimation for functional data analysis
devtools::install_github("linulysses/mcfda")
library(mcfda)
set.seed(1)
mu <- function(s) sin(2pis)
cov <- synfd::matern
D <- synfd::irreg.fd(mu=mu, X=synfd::gaussian.process(cov), n=100, m=5)
mu.obj <- meanfunc(D$t,D$y,newt=NULL,method='FOURIER', tuning='cv',weig=NULL, domain=c(0,1))
plot(mu.obj)
lines(regular.grid(),mu(regular.grid()))
mu.obj <- meanfunc(D$t,D$y,newt=NULL,method='PACE', tuning='cv',weig=NULL,kernel='epanechnikov',deg=1)
plot(mu.obj)
lines(regular.grid(),mu(regular.grid()))
cov.obj <- covfunc(D$t,D$y,newt=NULL,mu=mu.obj,method='PACE', tuning='GCV',weig=NULL,kernel='epanechnikov',delta=Inf)
grid <- regular.grid()
cov.hat <- predict(cov.obj,grid)
cov0 <- cov(grid)
mean((cov.hat-cov0)^2) / mean(cov0^2)
cov.obj <- covfunc(D$t,D$y,newt=NULL,mu=mu.obj,method='FOURIER', tuning='lle',weig=NULL)
cov.hat <- predict(cov.obj,grid)
mean((cov.hat-cov0)^2) / mean(cov0^2)
cov.obj <- covfunc(D$t,D$y,newt=NULL,method='SP',weig=NULL)
cov.hat <- predict(cov.obj,grid)
mean((cov.hat-cov0)^2) / mean(cov0^2)
sig2 <- sigma2(D$t,D$y)
sig2
abs(sig2 - attr(D,'sig')^2)
FPCA via fdapace package
Once the mean and covariance functions are obtained, the fdapace package can used with the options userCov, userMu and userSigma2 to perform FPCA:
error <- ifelse(sig2==0,yes=FALSE,no=TRUE)
grid <- regular.grid(h=0)
optns <- list(userCov=list(t=grid,cov=predict(cov.obj,grid)), userMu=list(t=grid,mu=predict(mu.obj,grid)), userSigma2 = sig2, error=error, verbose=T)
R <- fdapace::FPCA(D$y,D$t,optns)
plot(R$phi[,1])
Lin, Z. and Wang, J.-L. (2020+). Mean and covariance estimation for functional snippets. Journal of the American Statistical Association. to appear.
Lin, Z., Wang, J.-L. and Zhong, Q. (2020+). Basis expansions for functional snippets. preprint.
Yao, F., Müller, H.-G. and Wang, J.-L. (2005). Functional Data Analysis for Sparse Longitudinal Data. Journal Journal of the American Statistical Association. 100(470): 577-590.