Auresm
Auresm

Reputation: 139

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. The end goal is to have an easily readable table as an output that can be presented to a reader.

Currently I am using individual t-tests that give results one at a time, such as the code below.

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. 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.

Is there a way to run each of these tests simultaneously that stores the results in a table, i.e. I would get table that lists the result of a t-test of Condition 0 against Condition 1, Condition 0 against Condition 2, etc.

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,  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, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0,  0, 0, 1, 0, 1, 1, 0,
                                         0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,  0, 0, 0, 0, 0), condition =
                           structure(c(5L, 4L, 2L, 5L, 1L, 1L,  2L, 3L, 2L, 2L, 5L, 2L, 3L, 1L,
                                        5L, 4L, 5L, 2L, 4L, 4L, 1L, 2L,  3L, 5L, 2L, 4L, 5L, 4L, 5L, 5L, 5L,
                                        2L, 1L, 4L, 3L, 5L, 2L, 5L,  1L, 4L, 2L, 3L, 2L, 5L, 1L, 2L, 1L, 2L,
                                        3L, 1L, 2L, 4L, 3L, 5L,  3L, 4L, 1L, 5L, 1L, 2L, 4L, 2L, 2L, 3L, 4L,
                                        3L, 1L, 2L, 3L, 2L,  4L, 2L, 1L, 5L, 4L, 1L, 3L, 5L, 4L, 3L, 2L, 4L,
                                        5L, 3L, 4L, 2L,  4L, 2L, 4L, 3L, 5L, 2L, 3L, 1L, 1L, 1L, 3L, 5L, 5L,
                                        3L, 1L, 3L,  2L, 3L, 4L, 5L, 2L, 2L, 1L, 1L, 5L, 5L, 2L, 4L, 5L, 3L,
                                        1L, 4L,  5L, 3L, 4L, 1L, 5L, 3L, 1L, 2L, 1L, 3L, 5L, 3L, 1L, 2L, 4L,
                                        4L,  1L, 3L, 4L, 5L, 3L, 3L, 5L, 4L, 2L, 3L, 5L, 4L, 1L, 5L, 3L, 4L, 
                                        2L, 4L, 5L, 3L, 4L, 2L, 4L, 5L, 3L, 2L, 1L, 2L, 4L, 1L, 3L, 5L,  2L,
                                        1L, 3L, 4L, 1L, 2L, 4L, 5L, 2L, 2L, 3L, 3L, 5L, 1L, 2L, 5L,  2L, 3L,
                                        4L, 2L, 4L, 1L, 3L, 4L, 1L, 4L, 1L, 5L, 4L, 2L, 2L, 5L,  1L, 4L, 5L,
                                        3L, 1L, 1L, 4L, 5L, 3L, 2L, 1L, 1L, 5L, 1L, 4L, 5L,  3L, 4L, 5L, 3L,
                                        1L, 5L, 2L, 4L, 5L, 1L, 4L, 1L, 3L, 2L, 4L, 3L,  5L, 5L, 1L, 4L, 1L,
                                        3L, 4L, 5L, 1L, 3L, 1L, 3L, 4L, 2L, 3L, 3L,  2L, 3L, 2L, 4L, 5L, 2L,
                                        4L, 5L, 1L, 2L, 5L, 3L, 2L, 3L, 5L, 4L,  1L, 3L, 4L, 5L, 1L, 2L, 5L,
                                        5L, 3L, 1L, 4L, 5L, 3L, 2L, 1L, 1L,  4L, 5L, 1L, 2L, 1L, 3L, 1L, 5L,
                                        2L, 2L, 5L, 1L, 3L, 4L, 3L, 1L,  3L, 2L, 1L, 2L, 5L, 3L, 1L, 4L, 2L,
                                        3L, 1L, 2L, 3L, 4L, 1L, 3L,  2L, 5L, 1L, 4L, 5L, 1L, 2L, 1L, 2L, 4L,
                                        5L, 5L, 3L, 5L, 4L, 2L,  4L, 3L, 5L, 2L), .Label = c("0", "1", "2",
                                                                                                "3", "4"), class = "factor"), 
                           partisan_guess = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 
                                                                1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 
                                                                1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
                                                                1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 
                                                                2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 
                                                                2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
                                                                2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 
                                                                2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
                                                                2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
                                                                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))
 

Upvotes: 0

Views: 75

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389115

We can write a function to apply t.test for every condition against condition 0.

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>

Upvotes: 1

Related Questions