问题
Is there a way in dplyr to compare groups with each other? Here a concrete example: I would like to apply a t-test to the following combinations: a vs b, a vs c and b vs c
set.seed(1)
tibble(value = c(rnorm(1000, 1, 1), rnorm(1000, 5, 1), rnorm(1000, 10,1)),
group=c(rep("a", 1000), rep("b", 1000), rep("c", 1000))) %>%
nest(value)
# A tibble: 3 x 2
group data
<chr> <list>
1 a <tibble [1,000 × 1]>
2 b <tibble [1,000 × 1]>
3 c <tibble [1,000 × 1]>
If dplyr provides no solution, i would also be happy for other approaches...maybe data.table?
回答1:
Here's a base-R / tidyverse approach (which is somewhat manual, but imho ok for this task):
combn(df$group, 2, FUN = function(g)
t.test(filter(df, group == g[1]) %>% unnest %$% value ,
filter(df, group == g[2]) %>% unnest %$% value ),
simplify = FALSE)
# [[1]]
#
# Welch Two Sample t-test
#
# data: filter(df, group == g[1]) %>% unnest %$% value and filter(df, group == g[2]) %>% unnest %$% value
# t = -86.114, df = 1998, p-value < 2.2e-16
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
# -4.086376 -3.904396
# sample estimates:
# mean of x mean of y
# 0.9883519 4.9837381
#
#
# [[2]]
#
# Welch Two Sample t-test
#
# data: filter(df, group == g[1]) %>% unnest %$% value and filter(df, group == g[2]) %>% unnest %$% value
# t = -195.4, df = 1998, p-value < 2.2e-16
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
# -9.117558 -8.936356
# sample estimates:
# mean of x mean of y
# 0.9883519 10.0153090
#
#
# [[3]]
#
# Welch Two Sample t-test
#
# data: filter(df, group == g[1]) %>% unnest %$% value and filter(df, group == g[2]) %>% unnest %$% value
# t = -108.65, df = 1997.9, p-value < 2.2e-16
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
# -5.122395 -4.940747
# sample estimates:
# mean of x mean of y
# 4.983738 10.015309
来源:https://stackoverflow.com/questions/49367137/compare-groups-with-each-other