Skip to content
Prev 301256 / 398503 Next

Obtain residuals from a Principal Component Analysis

I need to correct myself.  I thought that predict.princomp allowed you
to specify the number of principal components to use, but that is not
the case.

A bit more detail is in order.

Suppose we have a matrix X,
X <- data.frame(E1= c(50, 55, 65, 50, 60, 65, 75.),
     E2= c(67, 71, 76, 80, 82, 89, 95),
     E3= c(90, 93, 95, 102, 97, 106, 117),
     E4= c(98, 102, 105, 130, 135, 137, 133),
     E5= c(120, 129, 134, 138, 151, 153, 155))
     rownames(B) <- c("G1","G2","G3","G4","G5","G6","G7")

Typically, X is scaled so that each column has mean 0 and variance 1.
X <- scale(X)

We use 'princomp' to provide a decomposition of X = SCORES %*%
t(LOADINGS).  Would be nice if the help page of princomp said this!
m1 <- princomp(X)

You can verify this decomposition.  This gives a matrix of zeros:
round(X - m1$scores %*% t(m1$loadings),8)

We can create a lower-rank approximation of X by using the first 2 (or
however many) principal components:
m1$scores[,1:2] %*% t(m1$loadings[,1:2])

I think of "residuals" as being the difference between the original
matrix X and this lower-rank approximation of X:
round(X - m1$scores[,1:2] %*% t(m1$loadings[,1:2]),4)

Kevin Wright
On Wed, Jul 25, 2012 at 5:02 AM, petohtalrayn <h643306 at rtrtr.com> wrote: