reconstruct_fd_from_PCA: Obtain the reconstructed curves after PCA

Description Usage Arguments Value Examples

View source: R/misc.R

Description

This function reconstructs the functional curves from a score vector using the basis obtained after applying functional PCA. This allows the user to draw estimations from the joint density of the FPCA scores and reconstruct the curves for those new scores.

Usage

1
reconstruct_fd_from_PCA(pca_struct, scores, centerfns = T)

Arguments

pca_struct

List obtained after calling function pca.fd.

scores

Numerical vector that contains the scores of the fPCA decomposition for one functional observation.

centerfns

Logical value specifying wheter the FPCA performed used centerfns = T or centerfns = F. By default centerfns = T.

Value

Returns a object of type fd that contains the reconstructed curve.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Example 1

# Simulate fd
nobs <- 200
dv <- 10
basis<-fda::create.bspline.basis(rangeval=c(0,1),nbasis=10)
set.seed(5)
C <- matrix(rnorm(nobs*dv), ncol =  dv, nrow = nobs)
fd_sim <- fda::fd(coef=t(C),basis)

# Perform FPCA
pca_struct <- fda::pca.fd(fd_sim,nharm = 6)

# Reconstruct first curve
fd_rec <- reconstruct_fd_from_PCA(pca_struct = pca_struct, scores = pca_struct$scores[1,])
plot(fd_sim[1])
plot(fd_rec, add = TRUE, col = "red")
legend("topright",
       legend = c("Real Curve", "PCA Reconstructed"),
       col = c("black","red"),
       lty = 1)

# Example 2 (Perfect reconstruction)

# Simulate fd
nobs <- 200
dv <- 7
basis<-fda::create.bspline.basis(rangeval=c(0,1),nbasis=dv)
set.seed(5)
C <- matrix(rnorm(nobs*dv), ncol =  dv, nrow = nobs)
fd_sim <- fda::fd(coef=t(C),basis)

# Perform FPCA
pca_struct <- fda::pca.fd(fd_sim,nharm = dv)

# Reconstruct first curve
fd_rec <- reconstruct_fd_from_PCA(pca_struct = pca_struct, scores = pca_struct$scores[1,])
plot(fd_sim[1])
plot(fd_rec, add = TRUE, col = "red")
legend("topright",
       legend = c("Real Curve", "PCA Reconstructed"),
       col = c("black","red"),
       lty = 1)

Example output

[1] "done"
[1] "done"
[1] "done"
[1] "done"

fdaACF documentation built on Oct. 23, 2020, 8:05 p.m.