修改
因此,在我打电话给library(reshape)
时,似乎打破了因素的标签。这不包含在最小示例中,但现在将添加。创建示例不需要它,但需要重新创建该问题。我需要库来使我的数据形状均匀factanal()
。任何想法重塑的部分是打破它以及如何解决它?
原始问题
我一直在对我的数据进行因子分析,并且在打印结果的方式上一直存在间歇性问题。
如果我创建如下数据集:
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)
)
并使用
运行因子分析factanal(mock, factors = 2)
我将输出打印出非常漂亮的项目名称作为行的标签,例如:
# 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
这种输出正是我想要的。但是,当我对自己的数据运行相同类型的分析时(我在这里为长度道歉):
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(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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)
结果不那么漂亮,例如:
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
因此,我现在得到索引,而不是将好的项目名称作为加载的标签。虽然这对我来说没什么问题,但明天我会和一位不太熟悉R的教授一起工作,并且很可能因为缺乏标签而感到沮丧。那么在第二种情况下标签会发生什么?我怎样才能让他们回来?
答案 0 :(得分:4)
问题是miniset
是cast_df
和factanal
来电as.matrix(x)
。 as.matrix.cast_df
方法使用rrownames
和rcolnames
(所有reshape
函数)来提取“特殊维度名称”。
对于miniset
,这些是NULL
(因此丢失了rownames)。在不知道你如何构建miniset
的情况下,我在这里无法进一步帮助。 (您必须在创建reshape
对象时使用miniset
来构建cast_df
。
好消息是
factanal(as.data.frame(miniset))
按您的意愿工作