【发布时间】:2014-06-22 07:54:18
【问题描述】:
我是 R 新手,我正在寻找一种方法来选择性地将 cor.test 应用于数据框中的一组变量。我自己制作了一个小脚本来查看,然后从 cor(dataframe) 结果中选择一组相关性。接下来我想做的是对所有这些系数的绝对值大于 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 的绝对值。现在我想将这些列名对(对于 0.5,然后从 Betaproteobacteria/axis1 到 Solibacteres/axis3)提供给第一个数据帧上的 cor.test。
我对 R 编程真的很陌生,不同的“应用”函数等的数量已经令人困惑了。我想其中一个应该是使用+ 或者我应该使用某种 for 循环?
【问题讨论】:
-
我认为使用包
psych中的corr.test会更容易。您可以将数据框作为参数传递并获取所有成对相关性。 -
谢谢 - 我想我会使用 psych 来完成最紧迫的任务。然后有一天我仍然必须在这里解决这个问题。它似乎具有我需要的大部分功能 - (虽然倾向于可怕地缩写列名,但我可以在那里使用一些易于识别的代号,更大的问题是它似乎只产生带两个小数点的 p 值)跨度>
标签: r apply correlation