[R] pca analysis: extract rotated scores?
Liviu Andronic
landronimirc at gmail.com
Tue Nov 30 22:44:04 CET 2010
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
More information about the R-help
mailing list