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
prcomp eigenvalues
4 messages · Rebecca Young, Sundar Dorai-Raj, Brian Ripley +1 more
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
The eigenvalues are the squares of the singular values (although you need to watch the scalings used, in particular n vs n-1). (This is standard theory.) Since both are non-negative, given one you can get the other.
On Tue, 2 Aug 2005, Sundar Dorai-Raj wrote:
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
______________________________________________ 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
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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/