R通过统计比较两组间的相关矩阵(t检验)

时间:2015-02-24 16:35:48

标签: r matrix statistics correlation

我有两个矩阵:

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-test比较cor_matrices1和cor_matrices2的术语。

结果是一个相关矩阵10x10与t值。

由于

1 个答案:

答案 0 :(得分:1)

所以基本上你需要以下内容:可能有比双循环更好的方法,但在这种情况下我更喜欢双循环,因为它更清晰,只要你没有很多矩阵(10x20并不多)然后你很好(这只需要1秒钟来计算)。

首先,你将得到一个20x10矩阵,因为第一个数组有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