Hi everyone, I am relatively new to R, and I need to perform the principal components analysis of a data matrix. I know that there are a bunch of methods to do it (dudi.pca, princomp, prcomp...) but I have not managed to find a method that can return the residuals obtained by retaining X principal components of the original data, as this MATLAB function can do: http://is.gd/6WeUFF Suggestions? Please, help me to find a way to do it, any information you can give will be highly appreciated. Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Obtain-residuals-from-a-Principal-Component-Analysis-tp4637754.html Sent from the R help mailing list archive at Nabble.com.
Obtain residuals from a Principal Component Analysis
3 messages · petohtalrayn, Kevin Wright
Just use 'predict' on the fitted model and subtract the predicted values from the data. Kevin Wright
On Wed, Jul 25, 2012 at 5:02 AM, petohtalrayn <h643306 at rtrtr.com> wrote:
Hi everyone, I am relatively new to R, and I need to perform the principal components analysis of a data matrix. I know that there are a bunch of methods to do it (dudi.pca, princomp, prcomp...) but I have not managed to find a method that can return the residuals obtained by retaining X principal components of the original data, as this MATLAB function can do: http://is.gd/6WeUFF Suggestions? Please, help me to find a way to do it, any information you can give will be highly appreciated. Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Obtain-residuals-from-a-Principal-Component-Analysis-tp4637754.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Kevin Wright
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:
Hi everyone, I am relatively new to R, and I need to perform the principal components analysis of a data matrix. I know that there are a bunch of methods to do it (dudi.pca, princomp, prcomp...) but I have not managed to find a method that can return the residuals obtained by retaining X principal components of the original data, as this MATLAB function can do: http://is.gd/6WeUFF Suggestions? Please, help me to find a way to do it, any information you can give will be highly appreciated. Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Obtain-residuals-from-a-Principal-Component-Analysis-tp4637754.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Kevin Wright