2013-08-07 40 views
0

編輯打印因素分析標籤

所以它看起來像它的東西,在我呼籲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(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) 

結果是更漂亮,如:

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的教授合作,並且可能會因缺乏標籤而感到沮喪。那麼第二種情況下的標籤會發生什麼?我怎樣才能讓他們回來?

+2

我不能重現此。當我運行你的代碼,輸出與標籤和所有內容完全「相當」。 – joran

+0

@joran我進一步麻煩了這個問題並相應地更新了這個問題。 – Ryan

回答

4

問題是minisetcast_dffactanal調用as.matrix(x)as.matrix.cast_df方法使用rrownamesrcolnames(全部reshape函數)來提取「特殊維名稱」。

對於miniset這些是NULL(因此rownames丟失)。不知道你是如何構造的miniset我不禁在這裏進一步。 (您必須使用reshape在一些點來構建miniset爲您創建一個cast_df對象。

好消息是,如你所願

factanal(as.data.frame(miniset)) 

作品

+0

謝謝!我加載它時仍然很奇怪沒有加載'reshape'包的結構,但之後加載重塑失敗。它是否恢復爲'as.matrix.data。沒有'cast_df'方法時找到框架? – Ryan

+1

@Ryan - 完全 - 'as.matrix.cast.df'是'reshape'包中的一個函數。 – mnel