Skip to content
Prev 199634 / 398503 Next

prcomp - principal components in R

The output of summary prcomp displays the cumulative amount of variance explained relative to the total variance explained by the principal components PRESENT in the object.  So, it is always guaranteed to be at 100% for the last principal component present.  You can see this from the code in summary.prcomp() (see this code with getAnywhere("summary.prcomp")).

Here's how to get the output you want (the last line in the transcript below):
Importance of components:
                         PC1   PC2   PC3   PC4   PC5
Standard deviation     1.175 1.058 0.976 0.916 0.850
Proportion of Variance 0.275 0.223 0.190 0.167 0.144
Cumulative Proportion  0.275 0.498 0.688 0.856 1.000
Importance of components:
                        PC1   PC2   PC3
Standard deviation     1.17 1.058 0.976
Proportion of Variance 0.40 0.324 0.276
Cumulative Proportion  0.40 0.724 1.000
[1] 1.1749061 1.0581362 0.9759016
[1] 1.1749061 1.0581362 0.9759016 0.9164905 0.8503122
[1] 1.1749061 1.0581362 0.9759016 0.9164905 0.8503122
[1] 0.2752317 0.4984734 0.6883643 0.8558386 1.0000000
[1] 0.2752317 0.4984734 0.6883643
It's probably better to get prcomp to compute all the components in the first place, because the SVD is the bulk of the computation anyway (so doing it again will be slower for large matrices.)  Then just look at the most important principal components.  However, there may be a shortcut for computing the values of D in the SVD of a matrix -- you could look for that if you have demanding computations (e.g., the sqrts of the eigen values of the covariance matrix of scaled x: sqrt(eigen(var(scale(x, center=T, scale=F)), only.values=T)$values)).

-- Tony Plate
zubin wrote: