对多个组中的同一响应变量执行多个 t 检验
Performing multiple t-tests on the same response variable across many groups
我看过很多类似的问题(比如 一个),但在我的例子中,治疗组没有保存为单独的向量,而且我没有成功地将我的变量名替换成任何我在这个主题上看到的其他代码。
我想比较 "before" 和 "after" 治疗对同一变量(测试分数)在多个位置的均值。
我的数据是这样的:
> head(my.df, n=15)
Location TestScore Treatment
1 4 0.7167641 Before
2 4 0.7998261 Before
3 4 0.8165880 After
4 4 0.8078955 After
5 7 0.6993413 Before
6 7 0.8404255 Before
7 7 0.7803164 Before
8 7 0.8383867 After
9 7 0.7930419 After
10 8 0.8504963 Before
11 8 0.7734653 Before
12 8 0.8408432 After
13 8 0.7980454 After
14 8 0.8407756 After
15 8 0.7837427 After
请注意,"before" 和 "after" 回复的数量在不同地点内和不同地点之间是不同的。
我知道我可以使用以下代码比较所有位置的治疗前后:
t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
但是,我想比较每个位置的前后值(因为我有 100 多个位置),而不是一次比较所有位置。理想情况下,我可以生成一个列表或 table 个 p 值,而不必每次都编写新的代码行。我想我可以做一些简单的事情,比如添加 "group_by",如下所示:
my.df %>% group_by(Location) %>% do(tidy(t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
但是当我 运行 这段代码时,我得到每个位置的 p 值都相同的输出(即使数据不同),如下所示:
# A tibble: 10 x 11
# Groups: Location [10]
Location estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 4 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
2 7 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
3 8 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
4 9 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
5 10 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
6 12 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
7 14 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
8 16 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
9 21 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
10 27 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
我怎样才能得到单独的 p 值来比较每个位置的处理前后?任何帮助是极大的赞赏!
你的大部分代码都是正确的,在 group_by 之后,要处理每个组内的数据,你需要使用 data = .
而不是 'data=my.df'
:
my.df %>% group_by(Location) %>%
do(tidy(t.test(TestScore ~ Treatment, data = ., var.equal = FALSE)))
例如:
library(dplyr)
library(broom)
my.df = data.frame(Location=sample(c(4,7,8),100,replace=TRUE),
TestScore=rnorm(100,10,1),
Treatment=sample(c("Before","After"),100,replace=TRUE)
# A tibble: 3 x 11
# Groups: Location [3]
Location estimate estimate1 estimate2 statistic p.value parameter conf.low
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 0.660 10.0 9.38 1.74 0.0926 31.0 -0.116
2 7 0.191 10.2 10.0 0.620 0.541 24.7 -0.445
3 8 -0.0720 10.1 10.2 -0.198 0.844 32.0 -0.813
我看过很多类似的问题(比如
我想比较 "before" 和 "after" 治疗对同一变量(测试分数)在多个位置的均值。
我的数据是这样的:
> head(my.df, n=15)
Location TestScore Treatment
1 4 0.7167641 Before
2 4 0.7998261 Before
3 4 0.8165880 After
4 4 0.8078955 After
5 7 0.6993413 Before
6 7 0.8404255 Before
7 7 0.7803164 Before
8 7 0.8383867 After
9 7 0.7930419 After
10 8 0.8504963 Before
11 8 0.7734653 Before
12 8 0.8408432 After
13 8 0.7980454 After
14 8 0.8407756 After
15 8 0.7837427 After
请注意,"before" 和 "after" 回复的数量在不同地点内和不同地点之间是不同的。
我知道我可以使用以下代码比较所有位置的治疗前后:
t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
但是,我想比较每个位置的前后值(因为我有 100 多个位置),而不是一次比较所有位置。理想情况下,我可以生成一个列表或 table 个 p 值,而不必每次都编写新的代码行。我想我可以做一些简单的事情,比如添加 "group_by",如下所示:
my.df %>% group_by(Location) %>% do(tidy(t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
但是当我 运行 这段代码时,我得到每个位置的 p 值都相同的输出(即使数据不同),如下所示:
# A tibble: 10 x 11
# Groups: Location [10]
Location estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 4 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
2 7 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
3 8 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
4 9 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
5 10 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
6 12 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
7 14 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
8 16 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
9 21 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
10 27 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
我怎样才能得到单独的 p 值来比较每个位置的处理前后?任何帮助是极大的赞赏!
你的大部分代码都是正确的,在 group_by 之后,要处理每个组内的数据,你需要使用 data = .
而不是 'data=my.df'
:
my.df %>% group_by(Location) %>%
do(tidy(t.test(TestScore ~ Treatment, data = ., var.equal = FALSE)))
例如:
library(dplyr)
library(broom)
my.df = data.frame(Location=sample(c(4,7,8),100,replace=TRUE),
TestScore=rnorm(100,10,1),
Treatment=sample(c("Before","After"),100,replace=TRUE)
# A tibble: 3 x 11
# Groups: Location [3]
Location estimate estimate1 estimate2 statistic p.value parameter conf.low
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 0.660 10.0 9.38 1.74 0.0926 31.0 -0.116
2 7 0.191 10.2 10.0 0.620 0.541 24.7 -0.445
3 8 -0.0720 10.1 10.2 -0.198 0.844 32.0 -0.813