pca_kcv | R Documentation |
Principal component analysis k-fold cross-validation
pca_kcv(X, ks = 2, npc.max = ncol(X))
X |
Matrix to be subjected to svd |
ks |
Number of k-fold groups to use. Default=2. (see |
npc.max |
The maximum number of principal components to test. Default=ncol(X) |
Matrix of square error values for each element in X
http://stats.stackexchange.com/a/115477/10675
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.