I would like to create a table one of baseline characteristics, stratified by an exposure, that shows SMDs between the exposure groups for every level of the categorical variables. Currently, adding SMDs via add_difference only gives an overall SMD for the variable as shown with Grade below.
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.2.3
tbl <-
trial %>%
select(trt, age, response, grade) %>%
tbl_summary(by = trt, missing = "no") %>%
add_difference(everything() ~ "smd")
tbl
I cannot figure out how to make them as a custom function within gtsummary. Alternatively, bal.tab()
from the cobalt package calculates them by level, but I also can't figure out how to add them to my tbl_summary from that package.
I appreciate any help!
UPDATE: The function is now incorporated into a package, which can be loaded with:
install.packages("devtools")
devtools::install_github("zheer-kejlberg/Z.gtsummary.addons")
library(Z.gtsummary.addons)
I've written some code to do just this with gtsummary tables at https://github.com/zheer-kejlberg/gtsummary-SMDs.
Use %>% add_SMD(location = "level")
to add SMDs to each level of all categorical variables or %>% add_SMD(location = "both")
to get both the "overall SMDs" (Mahalanobis distances) and the per-level SMDs.
Behind the scenes, the function uses dummy coding for the per-level SMDs.
There's not as yet support for confidence intervals on SMDs, but that's in the pipeline. See the output here:
EDIT (Nov 12 2023):
The function has now been updated with support for confidence intervals (the ci
argument) and custom number of decimals (the decimals
argument).