我需要以编程方式从psych::principal
重现自动(varimax)旋转以进行测试。
事实证明,对于某些数据,我无法从psych
重现该轮换,因为显然,输出中组件的顺序得到了轮流改变了。
考虑这个可重复的例子:
# some dataset from psych
library(psych)
data("Thurstone")
principal.unrotated <- principal(r = Thurstone, nfactors = 4, rotate = "none")$loa # calculate unrotated loadings
principal.varimax <- principal(r = Thurstone, nfactors = 4, rotate = "varimax")$loa # calculate varimax rotated loadings
rot.mat.varimax <- varimax(x = principal.unrotated)$rotmat # manually calculate varimax rotmat on unrotated loadings
round(x = unclass(principal.unrotated) %*% rot.mat.varimax, digits = 12) == round(x = unclass(principal.varimax), digits = 12) # works as expected
#> [,1] [,2] [,3] [,4]
#> Sentences TRUE TRUE TRUE TRUE
#> Vocabulary TRUE TRUE TRUE TRUE
#> Sent.Completion TRUE TRUE TRUE TRUE
#> First.Letters TRUE TRUE TRUE TRUE
#> 4.Letter.Words TRUE TRUE TRUE TRUE
#> Suffixes TRUE TRUE TRUE TRUE
#> Letter.Series TRUE TRUE TRUE TRUE
#> Pedigrees TRUE TRUE TRUE TRUE
#> Letter.Group TRUE TRUE TRUE TRUE
# same procedure using some dataset from another package
library(qmethod)
data("lipset")
Lipset <- cor(x = lipset[[1]], method = "pearson") # must calculate cor matrix first
principal.unrotated <- principal(r = Lipset, nfactors = 4, rotate = "none")$loa # calculate unrotated loadings
principal.varimax <- principal(r = Lipset, nfactors = 4, rotate = "varimax")$loa # calculate varimax rotated loadings
rot.mat.varimax <- varimax(x = principal.unrotated)$rotmat # manually calculate varimax rotmat on unrotated loadings
round(x = unclass(principal.unrotated) %*% rot.mat.varimax, digits = 12) == round(x = unclass(principal.varimax), digits = 12) # fails
#> [,1] [,2] [,3] [,4]
#> US1 FALSE FALSE TRUE TRUE
#> US2 FALSE FALSE TRUE TRUE
#> US3 FALSE FALSE TRUE TRUE
#> US4 FALSE FALSE TRUE TRUE
#> JP5 FALSE FALSE TRUE TRUE
#> CA6 FALSE FALSE TRUE TRUE
#> UK7 FALSE FALSE TRUE TRUE
#> US8 FALSE FALSE TRUE TRUE
#> FR9 FALSE FALSE TRUE TRUE
round(unclass(principal.varimax)[, c(2,1,3,4)], 12) == round(unclass(principal.unrotated) %*% rot.mat.varimax, 12) # seems like the ORDER of components is reversed
#> PC1 PC2 PC3 PC4
#> US1 TRUE TRUE TRUE TRUE
#> US2 TRUE TRUE TRUE TRUE
#> US3 TRUE TRUE TRUE TRUE
#> US4 TRUE TRUE TRUE TRUE
#> JP5 TRUE TRUE TRUE TRUE
#> CA6 TRUE TRUE TRUE TRUE
#> UK7 TRUE TRUE TRUE TRUE
#> US8 TRUE TRUE TRUE TRUE
#> FR9 TRUE TRUE TRUE TRUE
更新
只是一小部分:旋转矩阵在两个程序中实际上是相同的:
principal.varimax$rot.mat == rot.mat.varimax
这意味着(有点违反直觉)rot.mat
已应用于原始顺序中主要组件的过去版本。
答案 0 :(得分:0)
这实际上是psych::principal
(和psych::fa
)中返回加载顺序的{{3>} 重复,因此会被标记。
以下是通过应用相同的odering从psych::principal
重现varimax旋转的方法:
# using code from within psych::principal, as per this answer: https://stackoverflow.com/questions/16896959/psychprincipal-explanation-for-the-order-and-naming-of-rotated-principal-c
ev.rotated <- diag(t(manual.varimax) %*% manual.varimax) # find eigenvalues
ev.order <- order(ev.rotated, decreasing = TRUE) # order by eigenvalues
manual.varimax <- manual.varimax[, ev.order]
round(manual.varimax, 14) == round(principal.varimax, 14) # works now
#> [,1] [,2] [,3] [,4]
#> US1 TRUE TRUE TRUE TRUE
#> US2 TRUE TRUE TRUE TRUE
#> US3 TRUE TRUE TRUE TRUE
#> US4 TRUE TRUE TRUE TRUE
#> JP5 TRUE TRUE TRUE TRUE
#> CA6 TRUE TRUE TRUE TRUE
#> UK7 TRUE TRUE TRUE TRUE
#> US8 TRUE TRUE TRUE TRUE
#> FR9 TRUE TRUE TRUE TRUE