有没有办法 运行 多个 t.tests 产生的结果可以很容易地以 table 格式存储?
Is there a way to run multiple t.tests that produce results that can be easily stored in table format?
我正在处理一组数据,我想对其进行子集化和比较 t 检验。最终目标是将易于阅读的 table 作为输出呈现给 reader.
目前我正在使用一次给出一个结果的单独 t 检验,例如下面的代码。
t.test(survey$numericDV[survey$condition == 0 & survey$partisan_guess == "Republican"], survey$numericDV[survey$condition == 1 & survey$partisan_guess == "Republican"])
$condition 是一个因子变量,有 5 个水平,从 0 到 4,$partisan_guess 是一个有 2 个水平的因子。目标是 运行 t 测试将条件 == 0 与其他 4 个级别进行比较,并能够指定要使用 partisan_guess 的哪个级别。
有没有办法同时 运行 这些测试中的每一个,将结果存储在 table 中,即我会得到 table,其中列出了 t 测试的结果条件 0 对条件 1,条件 0 对条件 2,等等
在此先感谢您的帮助!
最小可复制数据:
survey <- structure(list(numericDVedu = c(0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0,
1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1,
0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0,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, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("Democrat", "Republican"
), class = "factor")), class = "data.frame", row.names = c(NA, -330L))
我们可以编写一个函数来针对条件 0 对每个条件应用 t.test
。
run_t_test <- function(data, partisan_guess) {
data <- subset(data, partisan_guess == partisan_guess)
zero_data <- data$numericDV[survey$condition == 0]
purrr::map_df(1:4, function(x) broom::tidy(t.test(
zero_data, data$numericDV[survey$condition == x])), .id = 'condition')
}
run_t_test(survey, 'Republican')
# A tibble: 4 x 11
# condition estimate estimate1 estimate2 statistic p.value parameter conf.low
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 -0.0490 0.333 0.382 -0.588 0.557 132. -0.214
#2 2 -0.113 0.333 0.446 -1.32 0.188 128. -0.282
#3 3 0.0635 0.333 0.270 0.782 0.436 127. -0.0972
#4 4 0.0980 0.333 0.235 1.25 0.212 130. -0.0565
# … with 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>
run_t_test(survey, 'Democrat')
# A tibble: 4 x 11
# condition estimate estimate1 estimate2 statistic p.value parameter conf.low
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 -0.0490 0.333 0.382 -0.588 0.557 132. -0.214
#2 2 -0.113 0.333 0.446 -1.32 0.188 128. -0.282
#3 3 0.0635 0.333 0.270 0.782 0.436 127. -0.0972
#4 4 0.0980 0.333 0.235 1.25 0.212 130. -0.0565
# … with 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>
我正在处理一组数据,我想对其进行子集化和比较 t 检验。最终目标是将易于阅读的 table 作为输出呈现给 reader.
目前我正在使用一次给出一个结果的单独 t 检验,例如下面的代码。
t.test(survey$numericDV[survey$condition == 0 & survey$partisan_guess == "Republican"], survey$numericDV[survey$condition == 1 & survey$partisan_guess == "Republican"])
$condition 是一个因子变量,有 5 个水平,从 0 到 4,$partisan_guess 是一个有 2 个水平的因子。目标是 运行 t 测试将条件 == 0 与其他 4 个级别进行比较,并能够指定要使用 partisan_guess 的哪个级别。
有没有办法同时 运行 这些测试中的每一个,将结果存储在 table 中,即我会得到 table,其中列出了 t 测试的结果条件 0 对条件 1,条件 0 对条件 2,等等
在此先感谢您的帮助!
最小可复制数据:
survey <- structure(list(numericDVedu = c(0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0,
1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1,
0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 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L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("Democrat", "Republican"
), class = "factor")), class = "data.frame", row.names = c(NA, -330L))
我们可以编写一个函数来针对条件 0 对每个条件应用 t.test
。
run_t_test <- function(data, partisan_guess) {
data <- subset(data, partisan_guess == partisan_guess)
zero_data <- data$numericDV[survey$condition == 0]
purrr::map_df(1:4, function(x) broom::tidy(t.test(
zero_data, data$numericDV[survey$condition == x])), .id = 'condition')
}
run_t_test(survey, 'Republican')
# A tibble: 4 x 11
# condition estimate estimate1 estimate2 statistic p.value parameter conf.low
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 -0.0490 0.333 0.382 -0.588 0.557 132. -0.214
#2 2 -0.113 0.333 0.446 -1.32 0.188 128. -0.282
#3 3 0.0635 0.333 0.270 0.782 0.436 127. -0.0972
#4 4 0.0980 0.333 0.235 1.25 0.212 130. -0.0565
# … with 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>
run_t_test(survey, 'Democrat')
# A tibble: 4 x 11
# condition estimate estimate1 estimate2 statistic p.value parameter conf.low
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 -0.0490 0.333 0.382 -0.588 0.557 132. -0.214
#2 2 -0.113 0.333 0.446 -1.32 0.188 128. -0.282
#3 3 0.0635 0.333 0.270 0.782 0.436 127. -0.0972
#4 4 0.0980 0.333 0.235 1.25 0.212 130. -0.0565
# … with 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>