ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.06k stars 128 forks source link

How can I add the degrees of freedom and test-statistic in the table_summary? #932

Closed loukesio closed 3 years ago

loukesio commented 3 years ago

Dear all, I am trying to report the degrees of freedom in the tbl_summary from gtsummary package in R but I can not find anything.

I would appreciate any help or guidance. Here is an example data set

iris %>% tbl_summary( by=Species, statistic = all_continuous() ~ "{mean} ({sd})" ) %>% add_p()

ddsjoberg commented 3 years ago

Can you give more details please? You want to report the degrees of freedom of what? Are you referring to a t-test?

loukesio commented 3 years ago

Thank you so much for the prompt reply. Yes I want to report for a t.test (or Kruskal W., Wilcoxon test) the degrees of freedom, and test statistic. It might be a naive question but I could not figure it out

ddsjoberg commented 3 years ago

here's an example! please re-open the issue if this does not answer your question

library(gtsummary)
#> #BlackLivesMatter

# build summary table
tbl <-
  trial %>%
  select(age, marker, trt) %>%
  tbl_summary(by = trt,
              missing = "no") %>%
  add_p(everything() ~ "t.test") 

# the df and t-statistic are hidden by default, by they are in the data frame
tbl$table_body
#> # A tibble: 2 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    continu~ Age       label    Age   46 (3~ 48 (3~ <named lis~
#> 2 marker   t.test    continu~ Marker L~ label    Mark~ 0.84 ~ 0.52 ~ <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>

# unhide them by assigning a header label
tbl %>%
  modify_header(
    statistic = "**t-statistic**",
    parameter = "**df**"
  ) %>%
  # add formatting function to round the stats
  modify_fmt_fun(
    c(statistic, parameter) ~ style_sigfig
  ) %>%
  # convert to kable to display on GitHub
  as_kable()
Characteristic Drug A, N = 98 Drug B, N = 102 t-statistic df p-value
Age 46 (37, 59) 48 (39, 56) -0.21 184 0.8
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 1.6 185 0.12

Created on 2021-07-01 by the reprex package (v2.0.0)

loukesio commented 3 years ago

Dear Daniel,

Thank you so much for the answer. I appreciate so much your prompt reply. I have one extra question although the issue is pretty much closed.

If I replace the t.test with aov:

tbl <-
  trial %>%
  select(age, marker, trt) %>%
  tbl_summary(by = trt,
              missing = "no") %>%
  add_p(everything() ~ "aov")  %>%
  modify_header(
    statistic = "**statistic**",
    parameter = "**df**"
  ) %>%
  # add formatting function to round the stats
  modify_fmt_fun(
    c(statistic, parameter) ~ style_sigfig
  )

I am taking the following error Error: Predicate functions must return a single TRUE or FALSE, not a logical vector of length 0.

Do you have an idea why am I taking this error? Overall thank you for your time

ddsjoberg commented 3 years ago

Inspect the columns in .$table_body, like in the previous example. You can only print the columns that already exist. If a column isn't there, you'll need to add it with the add_stat function.

loukesio commented 3 years ago

Thank you so much Daniel