Hello, Can you get eigenvalues in addition to eigevectors using prcomp? If so how? I am unable to use princomp due to small sample sizes. Thank you in advance for your help! Rebecca Young -- Rebecca Young Graduate Student Ecology & Evolutionary Biology, Badyaev Lab University of Arizona 1041 E Lowell Tucson, AZ 85721-0088 Office: 425BSW rlyoung at email.arizona.edu (520) 621-4005
I donn't think you can get it directly from prcomp ,but you can get it though svd.In fact,the prcomp use the svd to do the principal components analysis . ======= 2005-08-03 10:06:56 伳侜佋佢伬伌佇伵佒佇佇伌伒伬仯伜======>Hello, > >Can you get eigenvalues in addition to eigevectors using prcomp? If so how? >I am unable to use princomp due to small sample sizes. >Thank you in advance for your help! >Rebecca Young > >-- >Rebecca Young >Graduate Student >Ecology & Evolutionary Biology, Badyaev Lab >University of Arizona >1041 E Lowell >Tucson, AZ 85721-0088 >Office: 425BSW >rlyoung at email.arizona.edu >(520) 621-4005 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html= = = = = = = = = = = = = = = = = = = 2005-08-03 ------ Deparment of Sociology Fudan University Blog:http://sociology.yculblog.com
Rebecca Young wrote:> Hello, > > Can you get eigenvalues in addition to eigevectors using prcomp? If so how? > I am unable to use princomp due to small sample sizes. > Thank you in advance for your help! > Rebecca Young >Hi, Rebecca, From ?prcomp: The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix, not by using 'eigen' on the covariance matrix. This is generally the preferred method for numerical accuracy. ... So you can get the singular values, but not the eigenvalues. You could use ?princomp if you really want the eigenvalues. In either case, you read the code to see how this is done. x <- matrix(rnorm(1000), 100, 10) # eigenvalues v <- cov.wt(x) ev <- eigen(v$cov * (1 - 1/v$n.obs), symmetric = TRUE)$values ev[ev < 0] <- 0 princomp(x)$sdev sqrt(ev) # singular values sv <- svd(scale(x, center = TRUE, scale = FALSE), nu = 0) prcomp(x)$sdev sv$d/sqrt(max(1, nrow(x) - 1)) HTH, --sundar
On Tue, 2005-08-02 at 19:06 -0700, Rebecca Young wrote:> Hello, > > Can you get eigenvalues in addition to eigevectors using prcomp? If so how? > I am unable to use princomp due to small sample sizes. > Thank you in advance for your help! > Rebecca Young >Rebecca, This answer is similar as some others, but this is simpler. You have two separate problems: running PCA and getting eigenvalues. The first is easy to solve: use prcomp instead of princomp (which only exists for historic reasons). Function prcomp can handle cases with more columns than rows. pc <- prcomp(x) Above I assumed that your data are called x (or you can first make x, say: x <- rcauchy(200); dim(x) <- c(20,10) -- which puts a funny twist to comments on variances and standard deviations below). This saves something that are called 'sdev' or standard deviations, and you can get values that are (proportional to) eigenvalues simply by taking their squares: ev <- pc$sdev^2 These may be good enough for you (they would be good enough for me). However, if you want to exactly replicate the numbers in some other piece of software, you may need to multiply these by some constant. If you don't need this, you may stop reading here. The eigenvalues above are related to usual 'unbiased' variance so that the following results are approximately equal: sum(ev) sum(apply(x, 2, var)) If you want to get eigenvalues related to biased estimate of variance, you can do eb <- (1-1/nrow(x))*ev Function princomp uses these, as do some other software, but prcomp works hard and carefully to get the eigenvalues it uses instead of biased values (that would come naturally and directly in the algorithm it uses). Some programs relate their eigenvalues to the sum of squares, and you can get these by es <- (nrow(x) - 1) * ev Finally, some popular programs in ecology (your affiliation) use proportional eigenvalues which you can get with: ev/sum(ev) cheers, jari oksanen -- Jari Oksanen -- Dept Biology, Univ Oulu, 90014 Oulu, Finland email jari.oksanen at oulu.fi, homepage http://cc.oulu.fi/~jarioksa/