[英]Is there a way to run multiple t.tests that produce results that can be easily stored in table format?
I'm working with a set of data that I want to subset and compare t-tests for.我正在处理一组我想对其进行子集化和比较 t 检验的数据。 The end goal is to have an easily readable table as an output that can be presented to a reader.
最终目标是拥有一个易于阅读的表格,如 output 可以呈现给读者。
Currently I am using individual t-tests that give results one at a time, such as the code below.目前我正在使用单独的 t 检验,一次给出一个结果,例如下面的代码。
t.test(survey$numericDV[survey$condition == 0 & survey$partisan_guess == "Republican"], survey$numericDV[survey$condition == 1 & survey$partisan_guess == "Republican"])
$condition is a factor variable with 5 levels from 0 to 4,and $partisan_guess is a factor with 2 levels. $condition 是一个从 0 到 4 的 5 个级别的因子变量,$partisan_guess 是一个有 2 个级别的因子。 The goal is to run t-tests comparing condition == 0 against the other 4 levels, with the ability to specify which level of partisan_guess to use.
目标是运行 t-tests 比较 condition == 0 与其他 4 个级别,并能够指定使用哪个级别的 partisan_guess。
Is there a way to run each of these tests simultaneously that stores the results in a table, ie I would get table that lists the result of a t-test of Condition 0 against Condition 1, Condition 0 against Condition 2, etc.有没有办法同时运行这些测试中的每一个,将结果存储在一个表中,即我会得到一个表,其中列出了条件 0 对条件 1、条件 0 对条件 2 等的 t 检验结果。
Thanks in advance for the help!在此先感谢您的帮助!
Minimum replicable data:最小可复制数据:
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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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))
We can write a function to apply t.test
for every condition against condition 0.我们可以编写一个 function 来针对条件 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.