Search code examples
gtsummary

tbl_regression sample size in model - gtsummary


Is there a way to add the sample size in the tbl_regression output. Meaning the actual sample used for the model?

#example
tbl_regression(model, exponentiate = TRUE)

Thanks,

#gtsummary


Solution

  • There are two ways to add the model N to a tbl_regression() table.

    1. Use modify_header() to include the N in a column header. Requires gtsummary v1.3.6 or higher.
    2. Use add_n() to add a column to the table with the number of observations. Requires the current dev version of gtsummary. Install the dev version with remotes::install_github("ddsjoberg/gtsummary")
    library(gtsummary)
    packageVersion("gtsummary")
    #> '1.3.6.9013'
    
    glm(response ~ age, trial, family = binomial) %>%
      tbl_regression(exponentiate = TRUE) %>%
      modify_header(label ~ "**Characteristic, N = {N}**") %>% # Add N to the column header
      add_n() %>%                                              # Add N column to table
      add_nevent()                                             # Add Event N column to table
    

    enter image description here