相互比较组 [英] Compare groups with each other
本文介绍了相互比较组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dplyr中是否有一种方法可以相互比较组?这是一个具体示例:我想对以下组合应用t检验:a vs b,a vs c和b vs c
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]>
如果dplyr没有提供解决方案,我也会对其他方法感到满意...也许是data.table?
If dplyr provides no solution, i would also be happy for other approaches...maybe data.table?
推荐答案
这是base-R/tidyverse方法(虽然有些手动,但是对此任务没什么好感的):
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
这篇关于相互比较组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文