Gtsummary add_difference for categorical in same table with add_p

Viewed 294

I am trying to make tables with both the SMD and the p value on the same table. First issue is add_p and add_difference cannot be in same table code without the error

`add_difference()` cannot be run after `add_p()`, and vice versa

Furthermore, the code for the SMD as it stands does not work with categorical variables. See code

library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)


trial2 %>%
  tbl_summary(by = trt) %>%
  add_difference(list(all_continuous() ~ "cohens_d",
                 all_categorical() ~ "cohens_d")) %>% 
  add_overall() %>%
  add_n() %>%
  modify_header(label ~ "**Variable**") %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
  modify_footnote(
    all_stat_cols() ~ "Median (IQR) or Frequency (%)"
  ) %>%
  modify_caption("**Table 1. Patient Characteristics**") %>%
  bold_labels()

returns

Variable(s) 'grade' are summary type 'categorical' and not compatible with `add_difference()`.

could the SMD be calculated similar to the tableone package or using with ‘stddiff’ package so that it works with categorical variables?

1 Answers

The add_difference() function with standardized means does not include a p-value by default. But you can calculate one using add_p() in a separate table and merge them with tbl_merge(). Example below!

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

tbl1 <-
  trial %>%
  select(trt, age) %>%
  tbl_summary(by = trt, missing = "no") %>%
  add_difference(list(all_continuous() ~ "cohens_d"))

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

tbl_final <-
  tbl_merge(list(tbl1, tbl2)) %>%
  modify_spanning_header(everything() ~ NA)

enter image description here Created on 2021-08-10 by the reprex package (v2.0.1)

Regarding standardized differences for categorical data: you can add any statistic you like using the generic add_stat() function. Categorical standardized differences are not avialable in gtsummary out of the box.

Related