I want to understand how the principal() function in psych package calculate the $score element.
I want to try the covariance matrix rather than correlation matrix.
model <- principal(mtcars[8:11],nfactors=4, rotate='none', scores=T, cov=T)
Basically, the scores of the PCA should be linear combination of original centered data, using loading matrix as the weights, so I tried:
test <- scale(mtcars[8:11], center=T, scale=F) %*% model$loadings / model$scores
I understand that the principal()
function use some sort of scaling on loadings, however, the ratio should still be the same for each column, which is not the case here for test
.
If I use the correlation matrix, this will not be a problem. For example:
model <- principal(mtcars[8:11],nfactors=4, rotate='none', scores=T, cov=F)
test <- scale(mtcars[8:11], center=T, scale=T) %*% model$loadings / model$scores
The help document uses the terminology of factor analysis which confused me more. Hope somebody can enlighten me here.
Thank you in advance!
You have discovered a bug in the psych
package. Scores are found incorrectly for the unstandardized (covariance) solution. This will be fixed in the next release (which will not be out for at least a month). In the interim, you can find the scores by hand using the loadings matrix and the (centered) raw data.
model <- principal(mtcars[8:11],nfactors=4, rotate='none', scores=T, cov=T)
L <- model$loadings # Just get the loadings matrix
S <- model$scores # This gives an incorrect answer in the current version
d <- mtcars[8:11] # get your data
dc <- scale(d,scale=FALSE) # center the data but do not standardize it
sc <- dc %*% L # scores are the centered data times the loadings
lowerCor(sc) #These scores, being principal components
# should be orthogonal
PC1 PC2 PC3 PC4
PC1 1
PC2 0 1
PC3 0 0 1
PC4 0 0 0 1
When you find a problem with psych
that is not answered here, it is useful to write me (the package developer).
Note that for the unrotated solution, the component loadings are also orthogonal (as they should be).
factor.congruence(L,L)
PC1 PC2 PC3 PC4
PC1 1 0 0 0
PC2 0 1 0 0
PC3 0 0 1 0
PC4 0 0 0 1
(Burt's measure of Factor congruence, also known as Tucker's coefficient, takes the inner products of the (uncentered) loadings and then divides by the sums of squares of the respective columns). You could also find the cross products of the Loadings
round( t(L) %*% L,3)
PC1 PC2 PC3 PC4
PC1 2.742 0.000 0.000 0.000
PC2 0.000 0.721 0.000 0.000
PC3 0.000 0.000 0.142 0.000
PC4 0.000 0.000 0.000 0.051