Home > Back-end >  How to convert a list of tbl_regression objects to a single tbl_regression object?
How to convert a list of tbl_regression objects to a single tbl_regression object?

Time:09-20

I am trying to add significance stars to a gtsummary table that combines multiple models, but I'm getting an error that I don't know how to resolve.

library(gtsummary)
library(tidyverse)

# Create a list of tbl_regression objects
models <-  c("disp", "disp   hp") %>% 
  map(
    ~ paste("vs", .x, sep = " ~ ") %>% 
      as.formula() %>% 
      glm(data = mtcars,
          family = binomial(link = "logit")) %>% 
      tbl_regression(exponentiate = TRUE))

# Try to add significance stars
models %>% 
  add_significance_stars(
    pattern = "{estimate}{stars}",
    thresholds = c(0.001, 0.01, 0.05),
    hide_ci = TRUE,
    hide_p = TRUE,
    hide_se = FALSE
  ) 
#> Error: Error in argument 'x='. Expecting object of class 'tbl_regression', or 'tbl_uvregression'

It seems that the list models is not an object of class tbl_regression, and therefore cannot be passed to add_significance_stars(). How can I fix this problem?

CodePudding user response:

Your models object is a list of gtsummary tables. Hence, as you did when you created the tables you have to use map to loop over the list to add significance stars to each table:

library(gtsummary)
library(tidyverse)

models %>%
  map(
    ~ add_significance_stars(.x,
      pattern = "{estimate}{stars}",
      thresholds = c(0.001, 0.01, 0.05),
      hide_ci = TRUE,
      hide_p = TRUE,
      hide_se = FALSE
    )
  )

CodePudding user response:

As you indicate, models is a list of objects of class tbl_regression. You can just use lapply, like this:

models = lapply(models,
  add_significance_stars,
    pattern = "{estimate}{stars}",
    thresholds = c(0.001, 0.01, 0.05),
    hide_ci = TRUE,
    hide_p = TRUE,
    hide_se = FALSE
  ) 

Or you could include the call to add_significance_stars() in the original map()

  • Related