简体   繁体   English

使用项目标签从factanal()打印因子分析

[英]Print factor analysis from factanal() with item labels

EDIT 编辑

So it looks like it's something in my call to library(reshape) that's breaking the labeling of factors. 因此,看起来好像是我对library(reshape)调用打破了因素的标签。 This was not included in the minimal example, but will be added now. 最小示例中未包括此功能,但现在将添加。 It's not needed to create the example, but it's needed to recreate the issue. 创建示例不是必需的,但是重新创建问题是必需的。 I need the library to get my data in shape to even do factanal() . 我需要图书馆来factanal()我的数据,甚至可以进行factanal() Any ideas what part of reshape is breaking it and how to fix it? 有什么想法重塑的一部分正在破坏它,以及如何修复它?


Original question 原始问题

I have been running factor analyses on my data and have been having an intermittent issue with the way results are printed. 我一直在对数据进行因子分析,并且在打印结果的方式时遇到间歇性问题。

If I create a data set like the following: 如果我创建如下数据集:

library(reshape)
mock <- data.frame(
  sample_name1 = sample(1:100),
  sample_name2 = sample(1:100),
  sample_name3 = sample(1:100),
  s_amplename_4 = sample(1:100),
  samplename5 = sample(1:100),
  sa_mplen_a_me_6 = sample(1:100),
  samplename7 = sample(1:100),
  samplename8 = sample(1:100)
)

and run a factor analysis with 并使用

factanal(mock, factors = 2)

I get the output to print out very prettily with item names as labels for the rows, eg: 我得到的输出非常漂亮地打印出来,项目名称作为行的标签,例如:

# Snip snip
Loadings:
                Factor1 Factor2
sample_name1    -0.126  -0.105 
sample_name2    -0.414         
sample_name3     0.665         
s_amplename_4           -0.314 
samplename5              0.850 
sa_mplen_a_me_6 -0.117         
samplename7      0.442         
samplename8     -0.139 

This kind of output is exactly what I am looking for. 这种输出正是我想要的。 However, when I run the same type of analysis on my own data (and I apologize for the length here): 但是,当我对自己的数据进行相同类型的分析时(在这里为长度表示歉意):

miniset <- structure(list(`clarity1` = c(2, 2, 2, 3, 4.5, 1.5, 1.5, 3.5, 
                                           2, 6, 2.5, 4, 1, 1.5, 6, 2, 5.5, 2, 2, 3, 1.5, 5, 3.5, 2, 1.5, 
                                           2.5, 3, 3, 2, 1), 
                          `clarity2` = c(1.5, 2, 2, 2, 3.5, 5, 3, 5, 
                                           2, 4, 2, 2.5, 1, 1.5, 2, 4, 5, 2, 2, 3.5, 6, 1, 2, 1.5, 1, 2, 
                                           2, 3, 6.5, 1), 
                          `clarity3` = c(3, 3.5, 2, 3.5, 5.5, 4, 6, 5.5, 
                                           2, 3, 3, 3.5, 1, 2.5, 2, 5, 5, 5, 2, 6.5, 5.5, 5, 5.5, 6, 3, 
                                           2, 2, 5, 4.5, 5.5), 
                          `detail1` = c(3, 4, 2, 6, 5, 6.5, 5.5, 
                                          4, 3, 6, 2.5, 4, 1, 4, 2, 4.5, 7, 6.5, 2, 6.5, 6, 2, 6, 5, 2.5, 
                                          5.5, 4, 5.5, 6, 1.5), 
                          `detail2` = c(3.5, 4, 4, 6.5, 4.5, 6, 
                                          4, 4.5, 2, 6, 2.5, 5, 2, 4, 3, 6, 7, 7, 2, 6.5, 6, 3, 6, 6, 2.5, 
                                          6, 3, 5, 6.5, 2.5), 
                          `detail3` = c(2.5, 4, 2, 6, 5, 6, 6, 4, 
                                          2, 6, 2, 5, 2, 3, 3, 5, 6.5, 6, 2, 6.5, 7, 7, 5.5, 5, 3.5, 2, 
                                          3, 5, 6, 2), 
                          `complete1` = c(2, 2.5, 2, 3, 3.5, 5.5, 2.5, 2.5, 
                                            2, 3, 3, 3.5, 2, 4, 3, 3, 7, 4, 2, 3, 6, 3, 5.5, 2, 3, 2, 2, 
                                            3, 6, 3), 
                          `complete2` = c(3, 4.5, 2, 3, 4.5, 6, 6, 4.5, 3, 
                                            3, 3.5, 4, 2, 5, 3, 4, 7, 4, 2, 6, 7, 5, 5, 6, 3, 3, 5, 5, 6, 
                                            2), 
                          `complete3` = c(3, 4.5, 2, 2.5, 4.5, 6.5, 5, 5, 2, 6.5, 
                                            3.5, 3.5, 1, 3, 3, 2.5, 7, 4, 2, 6, 1.5, 7, 5.5, 6.5, 3.5, 5.5, 
                                            3, 3, 2.5, 1), 
                          `truthful1` = c(2.5, 2, 2, 3, 3.5, 2, 2, 2.5, 
                                            2, 3, 3, 2.5, 2, 3, 2, 2, 3.5, 3, 2, 3.5, 1.5, 1, 3.5, 2.5, 3, 
                                            2, 2, 3, 1.5, 1.5), 
                          `truthful2` = c(2.5, 1.5, 2, 2, 3, 1.5, 
                                            2, 1, 1, 5.5, 3, 3.5, 1, 4.5, 2, 2, 5, 2, 2, 1.5, 4.5, 1, 3.5, 
                                            2, 3.5, 2.5, 2, 2, 4.5, 1), 
                          `truthful3` = c(2, 1.5, 2, 3.5, 
                                            2.5, 2, 2, 2.5, 2, 2, 3.5, 2.5, 1, 1.5, 3, 2, 5, 3, 3, 2, 3.5, 
                                            1, 2, 1, 3.5, 2, 2, 2.5, 4.5, 1), 
                          `relevant1` = c(1.5, 1.5, 
                                            2, 5, 2.5, 1.5, 2, 3.5, 2, 4.5, 2.5, 3.5, 1, 3.5, 3, 1.5, 5.5, 
                                            3.5, 2, 2, 6, 3, 3.5, 3, 1.5, 2, 3, 3, 6, 1), 
                          `relevant2` = c(1.5, 
                                            3, 2, 2, 3.5, 1.5, 2.5, 5.5, 1, 2, 3.5, 2, 1, 1.5, 2, 4, 5.5, 
                                            2, 3, 5.5, 5.5, 1, 4, 5, 1.5, 2, 3, 2.5, 3, 1), 
                          `relevant3` = c(1.5, 
                                            2, 2, 3, 2, 1, 2, 2, 1, 2, 1.5, 2.5, 1, 1.5, 2, 1.5, 5.5, 5, 
                                            2, 1, 7, 1, 1, 2, 1, 2, 3, 3, 2.5, 1)), 
                     .Names = c("clarity1", 
                                "clarity2", "clarity3", "detail1", "detail2", "detail3", 
                                "complete1", "complete2", "complete3", "truthful1", "truthful2", 
                                "truthful3", "relevant1", "relevant2", "relevant3"), 
                     row.names = c(NA, 30L), class = c("cast_df", "data.frame"))

factanal(miniset, factors = 3)

the result is much less pretty, eg: 结果不那么漂亮,例如:

Loadings:
      Factor1 Factor2 Factor3
 [1,]          0.222   0.664 
 [2,]  0.559   0.524         
 [3,]  0.824                 
 [4,]  0.740   0.361   0.282 
 [5,]  0.698   0.374   0.251 
 [6,]  0.783   0.278   0.265 
 [7,]  0.498   0.598   0.140 
 [8,]  0.796   0.227   0.204 
 [9,]  0.490  -0.240   0.835 
[10,]  0.147   0.156   0.348 
[11,]          0.697   0.324 
[12,]          0.756         
[13,]  0.319   0.811   0.204 
[14,]  0.567   0.252   0.108 
[15,]  0.320   0.690 

So rather than having the nice item names as labels for the loadings, I now get indices. 因此,我现在不用索引了,而是用漂亮的项目名称作为加载的标签。 While that's fine for me, I'll be working with a professor tomorrow who is less familiar with R and will probably get frustrated by the lack of labels. 虽然对我来说很好,但是我明天将与一位对R不太熟悉的教授一起工作,并且可能会因缺少标签而感到沮丧。 So what happens to the labels in the second case? 那么在第二种情况下标签会发生什么呢? And how can I get them back? 我又该如何找回他们?

The issue is that miniset is a cast_df and factanal calls as.matrix(x) . 问题是minisetcast_dffactanal调用为as.matrix(x) The as.matrix.cast_df method uses rrownames and rcolnames (all reshape functions) to extract "special dimension names". as.matrix.cast_df方法使用rrownamesrcolnames (所有reshape函数)提取“特殊尺寸名称”。

For miniset these are NULL (hence the rownames are lost). 对于miniset这些为NULL (因此,行miniset丢失)。 Without knowing how you constructed miniset I can't help further here. 不知道您如何构造miniset在这里我无济于事。 (You must have used reshape to construct miniset at some point as you have created a cast_df object. (您必须在创建cast_df对象时使用reshape构造miniset cast_df

Good news is that 好消息是

factanal(as.data.frame(miniset))

Works as you wish 如你所愿

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

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