计算 R tidyverse 环境中 2 个列表元素的所有组合的多个 t 检验

calculate multiple t-tests for all the combinations of 2 list elements in R tidyverse environment

我正在寻找一种方法来计算 R tidyverse 环境中 2 个列表元素的所有组合的多个 t 检验。

我想测试基于变速箱的 Miles/(US) gallon 的平均值,用于 cyl 和 vs 的每个组合。我的工作示例是这段代码:

mtcars %>%
  filter(cyl==8 & vs == 0) %>%
  mutate(am = as.factor(am)) %>%

  # independent t-test
  t.test(mpg ~ am, data = ., paired = FALSE)%>%
  broom::tidy() %>%

  mutate(cyl = 8) %>%
  mutate(vs  = 0)   %>%
  select(cyl, vs, everything())

这段代码是我写的:

cyl_list <- list(unique(mtcars$cyl)) # 6 4 8
vs_list  <- list(unique(mtcars$vs))  # 0 1


complete_t_test <- function(cyl_par, vs_par){

  mtcars %>%
    filter(cyl=={cyl_par} & vs == {vs_par}) %>%
    mutate(am = as.factor(am)) %>%

    # independent t-test
    t.test(mpg ~ am, data = ., paired = FALSE) %>%
    broom::tidy() %>%

    mutate(cyl = {cyl_par}) %>%
    mutate(vs  = {vs_par})   %>%
    select(cyl, vs, everything())}

我在想类似于 purrr::map2(cyl_list, vs_list, complete_t_test) 的东西 但是没有用。

写一个函数计算一个组合之间的t.test。使用 cross_df 创建所有组合并将函数 complete_t_test 应用于每个组合。

library(tidyverse)

complete_t_test <- function(cyl_par, vs_par) {
   tryCatch({
      mtcars %>%
       filter(cyl== cyl_par & vs == vs_par) %>%
       t.test(mpg ~ am, data = ., paired = FALSE) %>%
       broom::tidy()
   }, error = function(e) return(NA))
}

cyl_list <- unique(mtcars$cyl)
vs_list  <- unique(mtcars$vs)

cross_df(list(a = cyl_list, b = vs_list)) %>%
   mutate(t_test = map2(a, b, complete_t_test))

#     a     b   t_test           
#   <dbl> <dbl> <list>           
#1     6     0 <lgl [1]>        
#2     4     0 <lgl [1]>        
#3     8     0 <tibble [1 × 10]>
#4     6     1 <lgl [1]>        
#5     4     1 <tibble [1 × 10]>
#6     8     1 <lgl [1]>       

列表列可能是一个可行的解决方案(参见书籍 R for Data Science, chapter 25)。我使用 nest() 创建一个列表列,然后进行 t 检验,然后再次 unnest() 查看结果。

注意:您的示例对于 mtcars 数据中的几种组合都失败了,因此我使用 possibly() 仅在适当的数据可用时才进行 t 检验。

library("tidyverse")

f1 <- possibly(~t.test(mpg ~ am, data = .x), otherwise = NULL)

mtcars %>% 
    group_by(cyl, vs) %>% 
    nest() %>%                                 # create list columns
    mutate(res = map(data, ~f1(.x))) %>%       # do t-tests
    mutate(res = map(res, broom::tidy)) %>%    # tidy()
    unnest(res) %>%                            # unnest list columns
    select(1:8)                                # show some columns for Whosebug
#> # A tibble: 2 x 8
#> # Groups:   cyl, vs [2]
#>     cyl    vs           data estimate estimate1 estimate2 statistic p.value
#>   <dbl> <dbl> <list<df[,9]>>    <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
#> 1     4     1       [10 x 9]   -5.47       22.9      28.4    -2.76   0.0254
#> 2     8     0       [14 x 9]   -0.350      15.0      15.4    -0.391  0.704

reprex package (v0.3.0)

于 2019-11-04 创建