Last active
May 15, 2022 20:45
-
-
Save ddsjoberg/0ca26a9b2da0269b7d405cd3e2744209 to your computer and use it in GitHub Desktop.
bigglm() gtsummary example, step by step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gtsummary) | |
library(broom.helpers) | |
library(tidyverse) | |
packageVersion("gtsummary") | |
#> [1] '1.6.0' | |
# build model | |
mod <- biglm::bigglm(response ~ age + trt, data = trial, family = binomial()) | |
# build a fancy tidy data frame one step at a time | |
# remove any steps you don't need OR ones that are too much computation | |
mod_tidied <- | |
mod %>% | |
tidy_and_attach(tidy_fun = tidy_parameters, conf.int = TRUE, exponentiate = TRUE) %>% | |
tidy_identify_variables() %>% | |
tidy_add_contrasts() %>% | |
tidy_add_reference_rows() %>% # this does not work for bigglm(), filed an issue to get support | |
tidy_add_estimate_to_reference_rows() %>% | |
tidy_add_variable_labels() %>% | |
tidy_add_term_labels() %>% | |
tidy_add_header_rows() %>% | |
tidy_add_n() %>% | |
tidy_remove_intercept() %>% | |
tidy_select_variables() %>% | |
tidy_add_coefficients_type() %>% | |
tidy_detach_model() | |
# convert tidy data frame to a gtsummary object | |
# all the styling needs to be added manually | |
tbl <- | |
mod_tidied %>% | |
# add a column that is used in gtsummary | |
mutate(row_type = ifelse(header_row, "label", "level")) %>% | |
.create_gtsummary_object() %>% | |
modify_cols_merge(pattern = "{conf.low}, {conf.high}", rows = !is.na(conf.low)) %>% | |
modify_header( | |
label = "**Characteristic**", estimate = "**OR**", | |
conf.low = "**95% CI**", p.value = "**p-value**" | |
) %>% | |
modify_fmt_fun(list(c(estimate, conf.low, conf.high) ~ style_ratio, | |
p.value ~ style_pvalue)) %>% | |
modify_footnote( | |
estimate = "OR = Odds Ratio", | |
conf.low = "CI = Confidence Interval", | |
abbreviation = TRUE | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment