R组中的矛头相关性

use*_*166 8 r

你如何按照R中的组计算Spearman相关性.我发现以下链接按组分别谈论Pearson相关性.但是当我尝试用spearman替换类型时,它不起作用.

https://stats.stackexchange.com/questions/4040/r-compute-correlation-by-group

Jos*_*ien 18

对于基本R解决方案,这个怎么样:

df <- data.frame(group = rep(c("G1", "G2"), each = 10),
                 var1 = rnorm(20),
                 var2 = rnorm(20))

r <- by(df, df$group, FUN = function(X) cor(X$var1, X$var2, method = "spearman"))
# df$group: G1
# [1] 0.4060606
# ------------------------------------------------------------ 
# df$group: G2
# [1] 0.1272727
Run Code Online (Sandbox Code Playgroud)

然后,如果你想以data.frame的形式得到结果:

data.frame(group = dimnames(r)[[1]], corr = as.vector(r))
#   group      corr
# 1    G1 0.4060606
# 2    G2 0.1272727
Run Code Online (Sandbox Code Playgroud)

编辑:如果您更喜欢plyr基于解决方案,这里有一个:

library(plyr)
ddply(df, .(group), summarise, "corr" = cor(var1, var2, method = "spearman"))
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 7

很老的问题,但是这个tidy&broom解决方案非常简单。因此,我必须分享方法:

set.seed(123)
df <- data.frame(group = rep(c("G1", "G2"), each = 10),
                 var1 = rnorm(20),
                 var2 = rnorm(20))

library(tidyverse)
library(broom)

df  %>% 
  group_by(group) %>%
  summarize(correlation = cor(var1, var2,, method = "sp"))
# A tibble: 2 x 2
  group correlation
  <fct>       <dbl>
1 G1        -0.200 
2 G2         0.0545

# with pvalues and further stats
df %>% 
  nest(-group) %>% 
  mutate(cor=map(data,~cor.test(.x$var1, .x$var2, method = "sp"))) %>%
  mutate(tidied = map(cor, tidy)) %>% 
  unnest(tidied, .drop = T)
# A tibble: 2 x 6
  group estimate statistic p.value method                          alternative
  <fct>    <dbl>     <dbl>   <dbl> <chr>                           <chr>      
1 G1     -0.200        198   0.584 Spearman's rank correlation rho two.sided  
2 G2      0.0545       156   0.892 Spearman's rank correlation rho two.sided 
Run Code Online (Sandbox Code Playgroud)

从某个时间/dplyr版本开始,您需要编写此代码以获得上述结果并且没有错误:

df %>% 
  nest(data = -group) %>%
  mutate(cor=map(data,~cor.test(.x$var1, .x$var2, method = "sp"))) %>%
  mutate(tidied = map(cor, tidy)) %>% 
  unnest(tidied) %>% 
  select(-data, -cor)
Run Code Online (Sandbox Code Playgroud)


Das*_*son 5

这是另一种方法:

# split the data by group then apply spearman correlation
# to each element of that list
j <- lapply(split(df, df$group), function(x){cor(x[,2], x[,3], method = "spearman")})

# Bring it together
data.frame(group = names(j), corr = unlist(j), row.names = NULL)
Run Code Online (Sandbox Code Playgroud)

比较我的方法,Josh的方法,以及使用rbenchmark的plyr解决方案:

Dason <- function(){
    # split the data by group then apply spearman correlation
    # to each element of that list
    j <- lapply(split(df, df$group), function(x){cor(x[,2], x[,3], method = "spearman")})

    # Bring it together
    data.frame(group = names(j), corr = unlist(j), row.names = NULL)
}

Josh <- function(){
    r <- by(df, df$group, FUN = function(X) cor(X$var1, X$var2, method = "spearman"))
    data.frame(group = attributes(r)$dimnames[[1]], corr = as.vector(r))
}

plyr <- function(){
    ddply(df, .(group), summarise, "corr" = cor(var1, var2, method = "spearman"))
}


library(rbenchmark)
benchmark(Dason(), Josh(), plyr())
Run Code Online (Sandbox Code Playgroud)

这给出了输出

> benchmark(Dason(), Josh(), plyr())
     test replications elapsed relative user.self sys.self user.child sys.child
1 Dason()          100    0.19 1.000000      0.19        0         NA        NA
2  Josh()          100    0.24 1.263158      0.22        0         NA        NA
3  plyr()          100    0.51 2.684211      0.52        0         NA        NA
Run Code Online (Sandbox Code Playgroud)

所以看起来我的方法稍快但不是很多.我认为Josh的方法更直观一些.plyr解决方案是最容易编码的,但它不是最快的(但它确实更方便)!


mne*_*nel 5

如果您想要为大量群体提供有效的解决方案,那么这data.table就是您的最佳选择。

library(data.table)
DT <- as.data.table(df)
setkey(DT, group)
DT[,list(corr = cor(var1,var2,method = 'spearman')), by = group]
Run Code Online (Sandbox Code Playgroud)