Change the default Statistical test performed by "add_p()" function in gtsummary summary tables

Viewed 351

I am using gtsummary package to generate summary tables.

I would like to do the following:

  1. That the "add_p" function performs a two-proportions z-test for the proportions in the "by" variable instead of chi-square test for independence. Using stats::prop.test
  2. Displays on the footnote that the "Statistical tests performed" are "2-sample test for equality of proportions with continuity correction"

How can I do that within this example code?

trial2 <- trial %>% select(trt, grade)
trial3 <- trial2[-which(trial2$grade == "III"),]
trial4 <- droplevels(trial3)

trial4 %>%
  tbl_summary(
    by = trt,
    statistic = list(all_continuous() ~ "{mean} ({sd})",
                     all_categorical() ~ "{n} / {N} ({p}%)"),
    digits = all_continuous() ~ 2,
    label = grade ~ "Tumor Grade"
  ) %>% add_p()

Thank you!

1 Answers

You can have two options. First, build a custom p-value function to calculate the p-value based off of prop.test(). There is an example of this in the add_p.tbl_summary() help file.

The second option (and easier option) is to download the current development version of the package from GitHub. In this version, the prop.test() option is already built in. Example below!

remotes::install_github("ddsjoberg/gtsummary")
library(gtsummary)
packageVersion("gtsummary")
#> [1] ‘1.3.5.9017’

trial %>%
  select(response, death, trt) %>%
  tbl_summary(by = trt) %>%
  add_p(test = everything() ~ "prop.test") %>%
  modify_footnote(p.value ~ "2-sample test for equality of proportions with continuity correction")

enter image description here

You may also want to check out the new function add_difference() that also reports the prop.test() p-value along with differences between groups.

trial %>%
  select(trt, response, death) %>%
  tbl_summary(by = trt,
              statistic = all_dichotomous() ~ "{p}%",
              missing = "no") %>%
  modify_footnote(all_stat_cols() ~ NA) %>%
  add_n() %>%
  add_difference(estimate_fun = ~paste0(style_sigfig(. * 100), "%"))

enter image description here

Related