【问题标题】:dplyr summarize across ttestdplyr 跨 ttest 汇总
【发布时间】:2021-09-24 02:08:32
【问题描述】:

我正在尝试跨多个变量运行 t 检验。假设我想按am 分组,然后我想看看mpg 是否与vs 在统计上不同

这是一个使用 summarize_eachold answer,但我正在尝试使用 dplyr 包中的 across

library(tidyverse)
library(broom)

mtcars %>%
  group_by(am) %>%
  summarise_each(funs(
    t.test(.[vs == 0], .[vs == 1])$p.value,
    t.test(.[vs == 0], .[vs == 1])$conf.int[1],
    t.test(.[vs == 0], .[vs == 1])$conf.int[2]
  ),
  vars = mpg)
#> Warning: `summarise_each_()` was deprecated in dplyr 0.7.0.
#> Please use `across()` instead.
#> Warning: `funs()` was deprecated in dplyr 0.8.0.
#> Please use a list of either functions or lambdas: 
#> 
#>   # Simple named list: 
#>   list(mean = mean, median = median)
#> 
#>   # Auto named with `tibble::lst()`: 
#>   tibble::lst(mean, median)
#> 
#>   # Using lambdas
#>   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> # A tibble: 2 x 4
#>      am `vars_$` `vars_[..2` `vars_[..3`
#>   <dbl>    <dbl>       <dbl>       <dbl>
#> 1     0 0.000395       -8.33       -3.05
#> 2     1 0.00459       -14.0        -3.27

## clean names via broom
t.test(mtcars %>% filter(am == 0) %>% filter(vs == 0) %>% pull(mpg), mtcars %>% filter(am == 0) %>% filter(vs == 1)%>% pull(mpg)) %>% broom::tidy()
#> # A tibble: 1 x 10
#>   estimate estimate1 estimate2 statistic  p.value parameter conf.low conf.high
#>      <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl>
#> 1    -5.69      15.0      20.7     -4.63 0.000395      14.0    -8.33     -3.05
#> # ... with 2 more variables: method <chr>, alternative <chr>
t.test(mtcars %>% filter(am == 1) %>% filter(vs == 0) %>% pull(mpg), mtcars %>% filter(am == 1) %>% filter(vs == 1) %>% pull(mpg)) %>% broom::tidy()
#> # A tibble: 1 x 10
#>   estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
#>      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
#> 1    -8.62      19.8      28.4     -3.55 0.00459      11.0    -14.0     -3.27
#> # ... with 2 more variables: method <chr>, alternative <chr>

## how to pass functions into .fns??
mtcars  %>%
  group_by(am) %>%
  summarise(across(
    .cols = mpg,
    .fns = list(
      t.test(.[vs == 0], .[vs == 1])$p.value,
      t.test(.[vs == 0], .[vs == 1])$conf.int[1],
      t.test(.[vs == 0], .[vs == 1])$conf.int[2]
    )
  ))
#> Error: Problem with `summarise()` input `..1`.
#> i `..1 = across(...)`.
#> x Must subset columns with a valid subscript vector.
#> i Logical subscripts must match the size of the indexed input.
#> x Input has size 11 but subscript `i` has size 19.
#> i The error occurred in group 1: am = 0.

reprex package (v2.0.1) 于 2021 年 9 月 23 日创建

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    如果我们使用tidy

    library(dplyr)
    library(broom)
    library(tidyr)
    mtcars %>% 
       group_by(am) %>% 
       summarise(across(
        .cols = mpg,
          ~ list(tidy(t.test(.[vs == 0], .[vs == 1])) %>%
                 select(p.value, conf.low, conf.high))
        )) %>% 
       unnest(mpg) 
    

    -输出

    # A tibble: 2 x 4
         am  p.value conf.low conf.high
      <dbl>    <dbl>    <dbl>     <dbl>
    1     0 0.000395    -8.33     -3.05
    2     1 0.00459    -14.0      -3.27
    

    在 OP 的代码中,我们需要 list 中的 lambda 函数

    mtcars  %>%
      group_by(am) %>%
      summarise(across(
        .cols = mpg,
        .fns =  list( 
          p.value = ~ t.test(.[vs == 0], .[vs == 1])$p.value,
          conf.low = ~ t.test(.[vs == 0], .[vs == 1])$conf.int[1],
          conf.high =~ t.test(.[vs == 0], .[vs == 1])$conf.int[2]
        )
      )) 
    

    -输出

    # A tibble: 2 x 4
         am mpg_p.value mpg_conf.low mpg_conf.high
      <dbl>       <dbl>        <dbl>         <dbl>
    1     0    0.000395        -8.33         -3.05
    2     1    0.00459        -14.0          -3.27
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-23
      • 2021-01-20
      • 2015-06-27
      • 2015-09-18
      • 1970-01-01
      • 2018-10-22
      • 2018-10-06
      • 2017-03-30
      相关资源
      最近更新 更多