计算相关性 - cor() - 仅用于列的子集

时间:2010-08-26 03:47:11

标签: r correlation

我有一个数据框,想要计算correlation(使用Spearman,数据是分类和排名),但仅限于列的子集。我尝试了所有,但R的cor()函数只接受数字数据(x必须是数字,表示错误信息),即使使用了Spearman。

一种粗暴的方法是从数据框中删除非数字列。这不是那么优雅,对于速度我仍然不想计算所有列之间的相关性。

我希望有一种方法可以简单地说“计算列x,y,z的相关性”。列引用可以按编号或按名称进行。我认为提供它们的灵活方式是通过矢量。

任何建议都表示赞赏。

4 个答案:

答案 0 :(得分:59)

如果您有一个数据框,其中某些列是数字的,而另一些是其他(字符或因子),并且您只想对数字列执行相关,则可以执行以下操作:

set.seed(10)

x = as.data.frame(matrix(rnorm(100), ncol = 10))
x$L1 = letters[1:10]
x$L2 = letters[11:20]

cor(x)

Error in cor(x) : 'x' must be numeric

cor(x[sapply(x, is.numeric)])

             V1         V2          V3          V4          V5          V6          V7
V1   1.00000000  0.3025766 -0.22473884 -0.72468776  0.18890578  0.14466161  0.05325308
V2   0.30257657  1.0000000 -0.27871430 -0.29075170  0.16095258  0.10538468 -0.15008158
V3  -0.22473884 -0.2787143  1.00000000 -0.22644156  0.07276013 -0.35725182 -0.05859479
V4  -0.72468776 -0.2907517 -0.22644156  1.00000000 -0.19305921  0.16948333 -0.01025698
V5   0.18890578  0.1609526  0.07276013 -0.19305921  1.00000000  0.07339531 -0.31837954
V6   0.14466161  0.1053847 -0.35725182  0.16948333  0.07339531  1.00000000  0.02514081
V7   0.05325308 -0.1500816 -0.05859479 -0.01025698 -0.31837954  0.02514081  1.00000000
V8   0.44705527  0.1698571  0.39970105 -0.42461411  0.63951574  0.23065830 -0.28967977
V9   0.21006372 -0.4418132 -0.18623823 -0.25272860  0.15921890  0.36182579 -0.18437981
V10  0.02326108  0.4618036 -0.25205899 -0.05117037  0.02408278  0.47630138 -0.38592733
              V8           V9         V10
V1   0.447055266  0.210063724  0.02326108
V2   0.169857120 -0.441813231  0.46180357
V3   0.399701054 -0.186238233 -0.25205899
V4  -0.424614107 -0.252728595 -0.05117037
V5   0.639515737  0.159218895  0.02408278
V6   0.230658298  0.361825786  0.47630138
V7  -0.289679766 -0.184379813 -0.38592733
V8   1.000000000  0.001023392  0.11436143
V9   0.001023392  1.000000000  0.15301699
V10  0.114361431  0.153016985  1.00000000

答案 1 :(得分:15)

对于数值数据,您有解决方案。但你说,这是分类数据。然后生活变得有点复杂......

那么,首先:两个分类变量之间的关联量不是用Spearman等级相关来测量的,而是用卡方检验来衡量的。实际上这是逻辑。排名表示您的数据中有一些订单。现在告诉我哪个更大,黄色还是红色?我知道,有时R会对分类数据执行spearman等级相关。如果我将黄色1和红色2编码,则R会认为红色大于黄色。

所以,忘记Spearman的分类数据。我将演示chisq-test以及如何使用combn()选择列。但是你可以从Agresti的书中获得更多的时间: http://www.amazon.com/Categorical-Analysis-Wiley-Probability-Statistics/dp/0471360937

set.seed(1234)
X <- rep(c("A","B"),20)
Y <- sample(c("C","D"),40,replace=T)

table(X,Y)
chisq.test(table(X,Y),correct=F)
# I don't use Yates continuity correction

#Let's make a matrix with tons of columns

Data <- as.data.frame(
          matrix(
            sample(letters[1:3],2000,replace=T),
            ncol=25
          )
        )

# You want to select which columns to use
columns <- c(3,7,11,24)
vars <- names(Data)[columns]

# say you need to know which ones are associated with each other.
out <-  apply( combn(columns,2),2,function(x){
          chisq.test(table(Data[,x[1]],Data[,x[2]]),correct=F)$p.value
        })

out <- cbind(as.data.frame(t(combn(vars,2))),out)

然后你应该得到:

> out
   V1  V2       out
1  V3  V7 0.8116733
2  V3 V11 0.1096903
3  V3 V24 0.1653670
4  V7 V11 0.3629871
5  V7 V24 0.4947797
6 V11 V24 0.7259321

V1和V2表示它在哪些变量之间,“out”表示关联的p值。这里所有变量都是独立的。你可以期待,因为我随机创建了数据。

答案 2 :(得分:2)

通过查看Rattle生成的R脚本,我找到了一种更简单的方法。它看起来如下:

correlations <- cor(mydata[,c(1,3,5:87,89:90,94:98)], use="pairwise", method="spearman")

答案 3 :(得分:0)

另一种选择是只使用出色的corrr软件包https://github.com/drsimonj/corrr并这样做

require(corrr)
require(dplyr)

myData %>% 
   select(x,y,z) %>%  # or do negative or range selections here
   correlate() %>%
   rearrange() %>%  # rearrange by correlations
   shave() # Shave off the upper triangle for a cleaner result

第3步和第4步完全是可选的,只是用来说明该软件包的用处。

相关问题