繁体   English   中英

从psych :: fa对象获取标准化加载(“模式矩阵”)

[英]Obtain standardized loadings (“pattern matrix”) from psych::fa object

psych :: print.psych()函数为psych :: fa()生成的因子分析对象产生漂亮的输出。 我想获得表格后面的文本“基于相关矩阵的标准化载荷(模式矩阵)”作为数据框,而无需剪切和粘贴。

library(psych)
my.fa <- fa(Harman74.cor$cov, 4)
my.fa  #Equivalent to print.psych(my.fa)

产生以下内容(我在这里显示前四个项目):

Factor Analysis using method =  minres
Call: fa(r = Harman74.cor$cov, nfactors = 4)
Standardized loadings (pattern matrix) based upon correlation matrix
                         MR1   MR3   MR2   MR4   h2   u2 com
VisualPerception        0.04  0.69  0.04  0.06 0.55 0.45 1.0
Cubes                   0.05  0.46 -0.02  0.01 0.23 0.77 1.0
PaperFormBoard          0.09  0.54 -0.15  0.06 0.34 0.66 1.2
Flags                   0.18  0.52 -0.04 -0.02 0.35 0.65 1.2

我尝试检查print.psych的源代码(在RStudio中使用View(print.psych) ),但只能找到一个部分来打印用于“按组进行因子分析”的标准化加载。

my.fa$weights没有标准化,并且该表缺少h2,u2和com列。 如果可以将它们标准化,则以下代码可以工作:

library(data.table)
library(psych)
my.fa <- fa(Harman74.cor$cov,4)
my.fa.table <- data.table(dimnames(Harman74.cor$cov)[[1]],
    my.fa$weights, my.fa$communalities, my.fa$uniquenesses, my.fa$complexity)
setnames(my.fa.table, old = c("V1", "V3", "V4", "V5"), 
                      new = c("item", "h2", "u2", "com"))

打印my.fa.table给出以下内容(我显示前四行),这表明$ weights不正确:

                     item          MR1         MR3          MR2          MR4        h2        u2      com
 1:       VisualPerception -0.021000973  0.28028576  0.006002429 -0.001855021 0.5501829 0.4498201 1.028593
 2:                  Cubes -0.003545975  0.11022570 -0.009545919 -0.012565221 0.2298420 0.7701563 1.033828
 3:         PaperFormBoard  0.028562047  0.13244895 -0.019162262  0.014448449 0.3384722 0.6615293 1.224154
 4:                  Flags  0.009187032  0.14430196 -0.025374834 -0.033737089 0.3497962 0.6502043 1.246102

$loadings替换$weights会出现以下错误消息:

Error in as.data.frame.default(x, ...) : 
  cannot coerce class ‘"loadings"’ to a data.frame

更新:

添加[,]解决了类问题:

library(data.table)
library(psych)
my.fa <- fa(Harman74.cor$cov,4)

my.fa.table <- data.table(dimnames(Harman74.cor$cov)[[1]],
    my.fa$loadings[,], my.fa$communalities, my.fa$uniquenesses, my.fa$complexity)
setnames(my.fa.table, old = c("V1", "V3", "V4", "V5"), 
                      new = c("item", "h2", "u2", "com"))
my.fa.table

                      item         MR1          MR3          MR2         MR4        h2        u2      com
 1:       VisualPerception  0.04224875  0.686002901  0.041831185  0.05624303 0.5501829 0.4498201 1.028593
 2:                  Cubes  0.05309628  0.455343417 -0.022143990  0.01372376 0.2298420 0.7701563 1.033828
 3:         PaperFormBoard  0.08733001  0.543848733 -0.147686005  0.05523805 0.3384722 0.6615293 1.224154
 4:                  Flags  0.17641395  0.517235582 -0.038878915 -0.02229273 0.3497962 0.6502043 1.246102

我仍然很乐意得到一个更优雅的答案,或者解释为什么它不是内置的。

它不是内置的,因为每个人都想要稍微不同的东西。 如您所见,您可以通过组合fa中的四个对象来创建表:加载,社区,唯一性和复杂性。

   df <- data.frame(unclass(f$loadings), h2=f$communalities, u2= f$uniqueness,com=f$complexity)
 round(df,2)

因此,对于瑟斯顿相关矩阵:

  f <- fa(Thurstone,3)
 df <- data.frame(unclass(f$loadings), h2=f$communalities, u2= f$uniqueness,com=f$complexity)
 round(df,2)

产生

                    MR1   MR2   MR3   h2   u2  com
Sentences          0.90 -0.03  0.04 0.82 0.18 1.01
Vocabulary         0.89  0.06 -0.03 0.84 0.16 1.01
Sent.Completion    0.84  0.03  0.00 0.74 0.26 1.00
First.Letters      0.00  0.85  0.00 0.73 0.27 1.00
Four.Letter.Words -0.02  0.75  0.10 0.63 0.37 1.04
Suffixes           0.18  0.63 -0.08 0.50 0.50 1.20
Letter.Series      0.03 -0.01  0.84 0.73 0.27 1.00
Pedigrees          0.38 -0.05  0.46 0.51 0.49 1.96
Letter.Group      -0.06  0.21  0.63 0.52 0.48 1.25

或者,您可以尝试使用fa2latex进行基于LaTex的漂亮格式化。

fa2latex(f)

产生准APA风格的LateX表格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM