Liu, Feng
2010-May-05 22:19 UTC
[R] How could I get the percent of variance explained by each axis if I use prcomp to predict new dataset?
Dear list, I am trying to use a PCA to predict new dataset, I know how to get the variance explained for the original PCA, but how could I get the percent of variance explained by each axis for this new PCA ordination? Thanks a lot. x1 <- matrix(rnorm(30), 6, 5) x2 <- matrix (rnorm(40), 8, 5) pca1<-prcomp(x1, retx=TRUE) pca1$sdev pred <- predict(pca1, x2) Feng Liu
Paul Hiemstra
2010-May-06 07:51 UTC
[R] How could I get the percent of variance explained by each axis if I use prcomp to predict new dataset?
Liu, Feng wrote:> Dear list, > > I am trying to use a PCA to predict new dataset, I know how to get the > variance explained for the original PCA, but how could I get the > percent of variance explained by each axis for this new PCA ordination? > > Thanks a lot. > > x1 <- matrix(rnorm(30), 6, 5) > x2 <- matrix (rnorm(40), 8, 5) > pca1<-prcomp(x1, retx=TRUE) > pca1$sdev > pred <- predict(pca1, x2) > > > Feng Liu > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.I think the following will give you what you want: (pca1$sdev)^2 / sum(pca1$sdev^2) # Or cumulative cumsum((pca1$sdev)^2) / sum(pca1$sdev^2) The $sdev part of the prcomp object gives the standard deviation of the pca axis, taking the square gives the variance. cheers, Paul -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770