Add multiple model statistics with add_glance_table() horizontaly (and not verticaly) in tbl_regression

429 views Asked by At

I used tbl_regression and add_glance_table() from gtsummary to build a table with model statistic:

library(gtsumary)
coxph(Surv(time, event) ~ score, data = dat) %>%
  tbl_regression(exponentiate = TRUE) %>%
  add_glance_table(concordance)

1st question: How can I move the model statistic horizontaly, to the right?

cox

Because, in the end, I want to display multiple model statistic, with C index in the last column, like this:

tbl_uvregression(
  dat_score,
  method=survival::coxph,
  y = Surv(time, event),
  exponentiate = TRUE) 

cox2

2nd question: How do I add add_glance_table in tbl_uvregression?

1

There are 1 answers

3
Daniel D. Sjoberg On

You can merge any additional columns/statistics into a gtsummary using the modify_table_body() function (the table_body is an internal data frame that is styled and printed as the summary table).

It's possible to add the c-index in a tbl_uvregression() setting. But I think it requires a higher understanding of the internals of a tbl_uvregression() object. In the example below, I estimate each univariable model separately, summarize the model with tbl_regression(), merge in the c-index, then stack all the tbls with tbl_stack().

Happy Programming!

library(gtsummary)
library(tidyverse)
library(survival)
packageVersion("gtsummary")
#> [1] '1.5.2'

covariates <- c("age", "marker")

# iterate over the covariates
tbl <-
  covariates %>%
  map(
    function(varname) {
      # build regression model
      mod <- 
        str_glue("Surv(ttdeath, death) ~ {varname}") %>%
        as.formula() %>%
        coxph(data = trial)
      # calculate and format c-index. adding variable column to merge in the next step
      df_cindex <- 
        broom::glance(mod) %>% 
        select(concordance) %>% 
        mutate(
          concordance = style_sigfig(concordance, digits = 3),
          variable = varname
        )
      
      # summarize model
      tbl_regression(mod, exponentiate = TRUE) %>%
        # merge in the c-index
        modify_table_body(~left_join(.x, df_cindex, by = "variable")) %>%
        modify_header(concordance = "**c-index**") # assigning a header label unhides the column
    }
  ) %>%
  #stack all tbls
  tbl_stack()

enter image description here Created on 2022-04-09 by the reprex package (v2.0.1)