首页 文章

Cor.test到数据框中的变量子集

提问于
浏览
0

我是R的新手,我正在寻找一种方法将cor.test有选择地应用于数据框中的一组变量 . 我自己制作了一个小脚本来查看,然后从cor(数据帧)结果中选择一组相关性 . 我接下来要做的是对所有这些具有绝对值大于X的系数的结果运行cor.test .

我的剧本

sortedcorlist <- function(mydataframe,method,numtoreport)
  {
   vahe<-cor(mydataframe, method=method)
   diag(vahe) <- 0
  vahe2 <- as.data.frame(vahe)
  vahe2 <- vahe2[c("axis1", "axis2", "axis3")]
  vahe2 <- as.matrix(vahe2)
  vahe2 <- as.table(vahe2)
  vahe2 <- as.data.frame(vahe2)
  head(vahe2[order(abs(vahe2[3]),decreasing=T),],n=numtoreport)
  }
If I run it on a dataframe like.

axis1   axis2   axis3   Alphaproteobacteria Actinobacteria  Gammaproteobacteria Solibacteres    Deltaproteobacteria
-0.118764   -0.028032   0.16921 15.5712530713   5.5282555283    5.773955774 11.9164619165   7.2788697789
-0.277526   0.081097    -0.079291   15.6943303205   12.2432210353   8.5456039441    5.7518488085    5.9983566146
-0.049546   0.002888    0.108965    17.9294117647   7.937254902 6.0235294118    13.0039215686   4.9098039216
-0.225758   0.043167    -0.022499   13.6838868389   12.5768757688   6.2423124231    7.3800738007    6.2115621156
0.004122    -0.017673   -0.020766   16.6099387338   11.708645337    6.3308373043    6.6712049013    5.1055139551
0.194926    -0.140736   -0.105162   17.6307007786   9.1768631813    8.1757508343    6.1179087875    3.5595105673
0.036636    0.001613    0.097292    17.1144859813   10.8644859813   6.4836448598    8.8785046729    6.4252336449
0.227766    0.321532    0.0225  17.8297278437   11.5143056525   6.5945568737    12.805303559    3.5589672017
-0.013657   -0.049475   0.145208    15.5555555556   5.7023060797    6.1635220126    12.2431865828   6.750524109
0.143307    -0.040705   0.104411    20.9752839011   7.4816299265    3.7408149633    12.4248496994   5.3440213761

我可以获得我感兴趣的相关性的列出输出 .

sortedcorlist(klassmuld,"kendall",30)
                  Var1  Var2       Freq
9   Betaproteobacteria axis1 -0.7333333
10       Acidobacteria axis1  0.7333333
37      Actinobacteria axis3 -0.6888889
12      Spartobacteria axis1 -0.6000000
38 Gammaproteobacteria axis3 -0.5555556
4  Alphaproteobacteria axis1  0.5111111
39        Solibacteres axis3  0.5111111
29       Phycisphaerae axis2 -0.4666667
8  Deltaproteobacteria axis1 -0.4222222
21      Actinobacteria axis2  0.4222222
11     Sphingobacteria axis1 -0.3777778
26       Acidobacteria axis2 -0.3777778
40 Deltaproteobacteria axis3  0.3777778
45       Phycisphaerae axis3  0.3777778
2                axis2 axis1 -0.2888889
15               SJA.4 axis1 -0.2888889
17               axis1 axis2 -0.2888889
22 Gammaproteobacteria axis2  0.2888889
25  Betaproteobacteria axis2  0.2888889
7         Solibacteres axis1  0.2444444
47               SJA.4 axis3  0.2444444
19               axis3 axis2 -0.2000000
24 Deltaproteobacteria axis2 -0.2000000
32             Bacilli axis2 -0.2000000
34               axis2 axis3 -0.2000000
6  Gammaproteobacteria axis1  0.1555556
14    Verrucomicrobiae axis1  0.1555556
28      Spartobacteria axis2  0.1555556
43     Sphingobacteria axis3  0.1555556
46    Verrucomicrobiae axis3  0.1555556

所以我可以看到哪些相关性(对于我感兴趣的轴1,2,3)的绝对值大于让我们说0.5 . 现在我想将这些对的列名称(从Betaproteobacteria / axis1到Solibacteres / axis3为0.5)到第一个数据帧的cor.test .

我是R编程的新手,不同的“应用”功能等等已经让人困惑 . 我想其中一个应该是使用的那个或者我应该使用某种for循环?

1 回答

  • 0

    如果将 sortedcorlist(klassmuld,"kendall",30) 的值分配给名称,例如"sorted.cors",则可以存储结果,然后选择符合规范的行并在成对列上运行cor.test:

    sorted.cors <- sortedcorlist(klassmuld,"kendall",30)
    targets <- sorted.cors[sorted.cors[['Freq']] > 0.5 , ]
    apply(targets, 1, function(ro) cor.test( klassmuld[[ ro[1] ]],
                                             klassmuld[[ ro[2] ]])$statistic )
    # With your example data I get:
           4       23 
    2.897042 4.180236
    

    请注意,rownames成为结果向量的名称 .

相关问题