show r squared, aic, bic, and deviance for multiple models using gtsummary

Viewed 330

looking to have the r squared, aic, bic, and deviance values presented for each of the four models here in the merged output

mod0 <- lm(surv_time ~ Age + Gender + Education + `Standardized MoCA`, data = surv_tbldata_converters)
mod1 <- lm(surv_time ~ Age + Gender + Education + `Global Composite`, data = surv_tbldata_converters)

mod2 <- lm(surv_time ~ Age + Gender + Education + `Standardized MoCA`, data = surv_tbldata)
mod3 <- lm(surv_time ~ Age + Gender + Education + `Global Composite`, data = surv_tbldata)

mod0_tbl <- mod0 %>%
  tbl_regression(exponentiate = F) %>%
  add_glance_source_note(include = c(r.squared, AIC, BIC, deviance))

mod1_tbl <- mod1 %>%
  tbl_regression(exponentiate = F) %>%
  add_glance_source_note(include = c(r.squared, AIC, BIC, deviance))

mod2_tbl <- mod2 %>%
  tbl_regression(exponentiate = F) %>%
  add_glance_source_note(include = c(r.squared, AIC, BIC, deviance))

mod3_tbl <- mod3 %>%
  tbl_regression(exponentiate = F) %>%
  add_glance_source_note(include = c(r.squared, AIC, BIC, deviance))

# merge tables 
tbl_merge(tbls = list(mod2_tbl, mod3_tbl, mod0_tbl, mod1_tbl),
          tab_spanner = c("**MoCA Model (Full Sample)**", "**Global Composite Model (Full Sample)**", "**MoCA Model (Converters Only)**", "**Global Composite Model (Converters Only)**")) %>%
  modify_header(label = "**Predictor**") %>%
  modify_caption("**Table 3. Study One Model Comparisons**")  %>%
  bold_labels() 
1 Answers

The add_glance_source_note() function adds the statistics as a source note, and the table may only have one source note. Use the add_glance_table() function to add the statistics to the bottom of the table, and you'll be able to merge the tables without issue. Example below!

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

lm1 <- lm(age ~ trt + grade, trial) 
lm2 <- lm(marker ~ trt + grade, trial)

tbl1 <-
  tbl_regression(lm1) %>%
  add_glance_table(include = c(r.squared, AIC, BIC, deviance))
tbl2 <-
  tbl_regression(lm2) %>%
  add_glance_table(include = c(r.squared, AIC, BIC, deviance))


tbl_all <- tbl_merge(list(tbl1, tbl2))

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

Related