Dear all I'm unable to find an example of extracting the rotated scores of a principal components analysis. I can do this easily for the un-rotated version. data(mtcars) .PC <- princomp(~am+carb+cyl+disp+drat+gear+hp+mpg, cor=TRUE, data=mtcars) unclass(loadings(.PC)) # component loadings summary(.PC) # proportions of variance mtcars$PC1 <- .PC$scores[,1] # extract un-rotated scores of 1st principal component mtcars$PC2 <- .PC$scores[,2] # extract un-rotated scores of 2nd principal component head(mtcars[, c('PC1', 'PC2')]) However, I no longer understand how to do so if I want to use ?principal in 'psych' and any of the GPArotation methods. For example, require(psych) r <- cor(mtcars[,c("am","carb","cyl","disp","drat","gear","hp","mpg")]) pca <- principal(r, nfactors = 8, residuals = T, rotate="none") # or 'varimax' or any other GPArotation supported rotation pca I've turned the 'pca' object and ?principal help page upside down and I still cannot find anything that would resemble a 'scores' value. I'm pretty sure it's one matrix computation away, but I cannot find which one. Ideas? Thank you Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
Take 2 on this. Below I'm pasting the code to perform PCA in R (without any rotation), manually; using ?princomp; and using ?principal. I also point out some differences in teh output and terminology of the two functions. In short, I found how to compute the scores of principal components when no rotation is used. However, I'm still confused on how to compute the scores when rotations (such as 'varimax' or other methods in GPArotation) are applied. Any ideas on how to obtain (non-scale'd) scores of the rotated principal components? Thank you Liviu ### require(psych) data(mtcars) rawd <- mtcars[,c("am","carb","cyl","disp","drat","gear","hp","mpg")] ## compute acp .PC <- princomp(~am+carb+cyl+disp+drat+gear+hp+mpg, cor=TRUE, data=mtcars) pca <- principal(rawd, nfactors = 8, residuals = T, rotate="none", scores=T) ## eigenvectors of the correlation matrix of raw data eigens <- eigen(cor(rawd)) eigens$vectors unclass(loadings(.PC)) # component 'loadings' in ?princomp parlance #not sure if ?principal outputs this ## correlation matrix between raw data and unrotated component scores ## 'loadings' in ?principal parlance and 'component matrix' in SPSS eigens$vectors %*% diag(sqrt(eigens$values)) cor(cbind(rawd, .PC$scores)) unclass(pca$loadings) ## extract un-rotated scores of principal components head(scale(rawd) %*% eigens$vectors) # app, but very similar results head(.PC$scores) head(pca$scores) # scale'd, and obtained via regression on scale'd data head(factor.scores(scale(rawd), unclass(pca$loadings))) # same scores as from ?principal #for differeneces between ?princomp and ?principal scores #see last paragraph of Details in ?principal On Tue, Nov 30, 2010 at 10:22 AM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all > I'm unable to find an example of extracting the rotated scores of a > principal components analysis. I can do this easily for the un-rotated > version. > > data(mtcars) > .PC <- princomp(~am+carb+cyl+disp+drat+gear+hp+mpg, cor=TRUE, data=mtcars) > unclass(loadings(.PC)) ?# component loadings > summary(.PC) # proportions of variance > mtcars$PC1 <- .PC$scores[,1] # extract un-rotated scores of 1st > principal component > mtcars$PC2 <- .PC$scores[,2] # extract un-rotated scores of 2nd > principal component > head(mtcars[, c('PC1', 'PC2')]) > > However, I no longer understand how to do so if I want to use > ?principal in 'psych' and any of the GPArotation methods. For example, > require(psych) > r <- cor(mtcars[,c("am","carb","cyl","disp","drat","gear","hp","mpg")]) > pca <- principal(r, nfactors = 8, residuals = T, rotate="none") # or > 'varimax' or any other GPArotation supported rotation > pca > > I've turned the 'pca' object and ?principal help page upside down and > I still cannot find anything that would resemble a 'scores' value. I'm > pretty sure it's one matrix computation away, but I cannot find which > one. > > Ideas? Thank you > Liviu > > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail >-- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
Hi, I am also doing PCA. Is the following right for extracting the scores? library(psych) pca<-principal(data,nfactors=,rotate="varimax",scores=T) pca$loadings pca$score Best regards, He On Tue, Nov 30, 2010 at 10:22 AM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all > I'm unable to find an example of extracting the rotated scores of a > principal components analysis. I can do this easily for the un-rotated > version. > > data(mtcars) > .PC <- princomp(~am+carb+cyl+disp+drat+gear+hp+mpg, cor=TRUE, data=mtcars) > unclass(loadings(.PC)) ?# component loadings > summary(.PC) # proportions of variance > mtcars$PC1 <- .PC$scores[,1] # extract un-rotated scores of 1st > principal component > mtcars$PC2 <- .PC$scores[,2] # extract un-rotated scores of 2nd > principal component > head(mtcars[, c('PC1', 'PC2')]) > > However, I no longer understand how to do so if I want to use > ?principal in 'psych' and any of the GPArotation methods. For example, > require(psych) > r <- cor(mtcars[,c("am","carb","cyl","disp","drat","gear","hp","mpg")]) > pca <- principal(r, nfactors = 8, residuals = T, rotate="none") # or > 'varimax' or any other GPArotation supported rotation > pca > > I've turned the 'pca' object and ?principal help page upside down and > I still cannot find anything that would resemble a 'scores' value. I'm > pretty sure it's one matrix computation away, but I cannot find which > one. > > Ideas? Thank you > Liviu > > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > 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. >
Hi He Zhang,>> Is the following right for extracting the scores? >> ... >> pca$loadings >> pca$scoreYes. But you should be aware that the function principal() in package psych is standardizing your data internally, which you might not want. That is, the analysis is being based on the correlation matrix rather than on the covariance matrix. The two analyses (and the "default" biplots that issue from them) have somewhat different properties. You should know about these. Regards, Mark. -- View this message in context: http://r.789695.n4.nabble.com/pca-analysis-extract-rotated-scores-tp3065061p3067370.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- PCA - scores
- Bug in by() function which works for some FUN argument and does not work for others
- Bug in by() function which works for some FUN argument and does not work for others
- Bug in by() function which works for some FUN argument and does not work for others
- Apply same linear model to subset of dataframe