pca_kcv: Principal component analysis k-fold cross-validation

View source: R/pca_kcv.R

pca_kcvR Documentation

Principal component analysis k-fold cross-validation

Description

Principal component analysis k-fold cross-validation

Usage

pca_kcv(X, ks = 2, npc.max = ncol(X))

Arguments

X

Matrix to be subjected to svd

ks

Number of k-fold groups to use. Default=2. (see kfold)

npc.max

The maximum number of principal components to test. Default=ncol(X)

Value

Matrix of square error values for each element in X

References

http://stats.stackexchange.com/a/115477/10675

Examples

dat <- as.matrix(iris[,1:4])
res <- pca_kcv(dat, ks=10)
res2 <- lapply(res, colSums)
res2

COL <- 2:4
LTY <- 1:3
op <- par(mar=c(4,4,2,1), tcl=-0.25, mgp=c(2.5,0.5,0))
for(i in seq(res)){
  if(i==1) {
    plot(res2[[i]], t="n", ylim=range(unlist(res2)), 
     main="iris", xlab="n PCs", ylab="PRESS")
    grid()
  } 
  lines(res2[[i]], t="b", bg=c(NaN,COL[i])[(res2[[i]]==min(res2[[i]])) + 1],
   col=COL[i], lty=LTY[i], pch=21)
}
legend("topright", legend=c("naive", "approximate", "pseudoinverse"),
 col=COL, lty=LTY, pch=21, bty="n")
par(op)



marchtaylor/sinkr documentation built on July 4, 2022, 5:48 p.m.