2015-02-24 46 views
0

我有兩個矩陣兩組間矩陣:ř比較相關性由統計(t檢驗)

cor_matrices1 = array(sample(100),dim=c(20,10,10)) 

cor_matrices2 = array(sample(100),dim=c(10,10,10)) 

cor_matrices1具有20個文檔和每個文檔中,我們具有由10項10的相關矩陣

cor_matrices2有10個文檔,對於每個文檔我們都有一個10乘10的相關矩陣

相關矩陣10乘10對於所有的都有相同的術語,只是改變這些值。

我想通過t檢驗來比較cor_matrices1和cor_matrices2的術語。

結果是具有t值的一個相關矩陣10x10。

感謝

+0

'cor_matrices1'是10次20×10的矩陣陣列。你打算創建20次10x10矩陣嗎? – DatamineR 2015-02-24 16:42:22

+0

否,cor_matrices1是20x10x10矩陣的數組。我想從20和10個文檔中的兩個數組之間的t值創建一個。 – DemetriusRPaula 2015-02-24 18:30:48

回答

1

所以基本上你需要以下條件:有可能比更好的方式雙重for循環,但在此之際,我希望能有一個雙重for循環,因爲它是更清晰的閱讀,只要你沒有很多矩陣(10x20不是很多),那麼你很好(這隻需要1秒計算)。

你會最終有一個20×10矩陣,因爲第一陣列20點矩陣和第二10

你需要做以下的第一:

數據:

cor_matrices1 = array(sample(100),dim=c(20,10,10)) 
cor_matrices2 = array(sample(100),dim=c(10,10,10)) 

解決方案

#initiate mymatrix as a 20x10 matrix 
mymatrix <- matrix(nrow=dim(cor_matrices1)[1], ncol=dim(cor_matrices2)[1]) 

#run the loop to populate the matrix with the t.tests' statistics 
for (i in 1:dim(cor_matrices1)[1]) { 
    for (j in 1:dim(cor_matrices2)[1]) { 
    mymatrix[i,j] <- t.test(cor_matrices1[i,,], cor_matrices2[j,,])$statistic 
    } 
} 

輸出

> mymatrix 
      [,1]  [,2]  [,3]   [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] 
[1,] 5.5853096 6.1979575 0.9636135 -0.38003497 0.8078331 3.3969073 1.5651130 6.1375357 -0.4856826 3.8526553 
[2,] 6.4835893 7.3883179 1.3936984 -0.06060466 1.2600229 4.1799857 2.0909319 7.2064851 -0.1804566 4.6040987 
[3,] 10.5499600 12.4612833 4.9087509 3.67337581 5.1053177 8.5639508 5.9404200 11.7967193 3.5041504 8.6115299 
[4,] 6.8099365 7.7779495 1.6835736 0.24432968 1.5720800 4.5265863 2.4031794 7.5666095 0.1212386 4.9265904 
[5,] 3.1313201 3.5382502 -1.8716670 -3.58258536 -2.2470268 0.5182614 -1.3648099 3.5951168 -3.6751691 1.1924479 
[6,] -2.1215144 -2.6120967 -7.9545278 -10.82110795 -9.0923043 -6.0319761 -7.8460891 -2.0551756 -10.8493623 -4.5923672 
[7,] 0.5649513 0.5971889 -3.8604264 -5.46406280 -4.2873721 -1.9296771 -3.4898817 0.8036567 -5.5374016 -1.2016093 
[8,] 2.3410299 2.6510613 -2.7448643 -4.57137186 -3.1979002 -0.4049241 -2.2798867 2.7594448 -4.6578399 0.3470886 
[9,] -1.0873533 -1.2121674 -5.2500287 -6.81509337 -5.7070461 -3.5289496 -4.9454641 -0.9462123 -6.8790254 -2.7761814 
[10,] 11.6457024 13.3988664 6.3944924 5.36168846 6.6623806 9.8549618 7.4104395 12.8070217 5.1956504 9.8589128 
[11,] 0.6202717 0.6448094 -3.2380057 -4.55523337 -3.5532787 -1.5135278 -2.8796109 0.8239709 -4.6256268 -0.9112790 
[12,] 0.8312961 0.8860768 -3.4505211 -4.96191790 -3.8328582 -1.5512273 -3.0714358 1.0739395 -5.0362201 -0.8703299 
[13,] 6.0480378 6.6406437 1.6431224 0.41111323 1.5305889 3.9923199 2.2400048 6.5800061 0.3066747 4.4044122 
[14,] 3.7185897 3.9910684 -0.2165576 -1.40970887 -0.4069604 1.7259251 0.2460738 4.0586111 -1.4943193 2.2070542 
[15,] 3.6257669 3.9953318 -0.8952537 -2.33610856 -1.1590954 1.3223841 -0.3895172 4.0462200 -2.4282655 1.8871558 
[16,] -1.1606132 -1.3436360 -6.0074059 -7.99304170 -6.6575927 -4.1054003 -5.7273085 -1.0090346 -8.0535361 -3.1485593 
[17,] -3.6306778 -4.1709216 -8.5889634 -10.86913291 -9.4628367 -6.9216143 -8.4516457 -3.6551922 -10.9088895 -5.7360407 
[18,] -1.6046036 -1.8704075 -6.6534574 -8.81815316 -7.4052207 -4.7544755 -6.4115234 -1.4825538 -8.8725847 -3.6947929 
[19,] 3.2956127 3.5441625 -0.7047402 -1.94573896 -0.9211169 1.2430949 -0.2505359 3.6261504 -2.0294596 1.7518654 
[20,] 5.2945216 5.7533496 1.1045595 -0.09728864 0.9666110 3.2797497 1.6473487 5.7459609 -0.1936377 3.7147684 
+0

這個工作適合你嗎? – LyzandeR 2015-02-27 11:14:17