dplyr summarize across ttest

4

我正在尝试在多个变量间运行t检验。比如说我想要按照am分组,然后想要查看对于vsmpg是否在统计学上不同。

这里有一个旧回答,使用了summarize_each,但是我正在尝试使用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.

本文创建于2021年9月23日,使用reprex软件包(版本2.0.1)

1个回答

6
如果我们正在使用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的代码中,我们需要将lambda函数放在list内部。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接