Is it possible to extract the full output for the statistical tests performed in gtsummary?

Viewed 187

Background/aim

I am using gtsummary to present tables that also include statistical tests performed by the package. I want to supplement the table with additional information about the statistical test in my manuscript text. For example, gtsummary has tested mean differences between groups with anova and I want to report the F value and degrees of value of the test in the text. Is there any way to extract this information from the gtsummary object or do I have to run the statistical tests again separately and extract it from there? I.e., run a aov() and copy paste from output?

Example situation with anova as the statistical test

library(tidyverse)
library(gtsummary)
theme_gtsummary_mean_sd()

gtTable <- mtcars %>% 
  select(cyl, mpg) %>% 
  tbl_summary(by = cyl) %>% 
  add_p()

oneWay <- aov(mpg ~ cyl, data = mtcars)
summary(oneWay)
#>             Df Sum Sq Mean Sq F value   Pr(>F)    
#> cyl          1  817.7   817.7   79.56 6.11e-10 ***
#> Residuals   30  308.3    10.3                     
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Table

enter image description here

1 Answers

This should be possible, but unfortunately it is not current possible with the aov() results. I will make it possible in the next release, and you can follow this GitHub Issue to track the progress of the release. https://github.com/ddsjoberg/gtsummary/issues/956

Here is an example using a t-test, where it currently is possible.

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'

tbl <-
  trial %>%
  select(age, trt) %>%
  tbl_summary(by = trt,
              missing = "no") %>%
  add_p(all_continuous() ~ "t.test")

# report any statistics in `tbl$table_body` with `inline_text()`
tbl$table_body
#> # A tibble: 1 x 18
#>   variable test_name var_type   var_label row_type label stat_1 stat_2 test_result
#>   <chr>    <chr>     <chr>      <chr>     <chr>    <chr> <chr>  <chr>  <list>     
#> 1 age      t.test    continuous Age       label    Age   46 (3~ 48 (3~ <named lis~
#> # ... with 9 more variables: estimate <dbl>, statistic <dbl>, parameter <dbl>,
#> #   conf.low <dbl>, conf.high <dbl>, p.value <dbl>, estimate1 <dbl>,
#> #   estimate2 <dbl>, alternative <chr>

# the columns about the t-test come from `t.test(...) %>% broom::tidy()`
t.test(age ~  trt, data = trial) %>% 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   -0.438      47.0      47.4    -0.209   0.834      184.    -4.57      3.69
#> # ... with 2 more variables: method <chr>, alternative <chr>

# t-statistic
inline_text(tbl, variable = "age", column = "statistic") %>% style_sigfig()
#>       t 
#> "-0.21"
# degrees of freedom
inline_text(tbl, variable = "age", column = "parameter") %>% style_number()
#>    df 
#> "184"

Created on 2021-08-10 by the reprex package (v2.0.1)

Related