How to add number of observations to merged regression table?

Viewed 58

I've been using the gtsummary package to create tables for my logistic regressions. I've successfully created two separate tables based on two different models (males and female separately), and I've managed to include number of observations per level in both of them. However when I merge the tables, the number of observations (N) disappear from both models. The code I've been using to create the individual regression tables are these:

#simple log reg with only males
log_gender_M <- glm(SRH_05_2gr ~ smoking_05,
                           data = Male,
                           family = "binomial") 
#regression table for male log reg
model_M <- tbl_regression(log_gender_M, exponentiate = TRUE)
model_M %>%
  modify_header(update = label ~ "**Characteristic (N = {n})**") %>%
  bold_labels()  %>%
  add_n(location = "level") %>%
  as_gt() %>%
  gt::tab_source_note(gt::md("*Data drawn from the 2005 Scania Survey*")) %>%
  gt::tab_header("Males")

The issue with (N) occurs when I merge the regressions

#merging log reg models for males and females
tbl_merge(
  tbls = list(model_M, model_F),
  tab_spanner = c("Males", "Females")) %>%
  add_n(location = "level")

When I use the function add_n() with the above code I receive the following response:

Error in UseMethod("add_n") : 
 no applicable method for 'add_n' applied to an object of class "c('tbl_merge', 'gtsummary')"

Does anyone know how this might be fixed? I'm quite new to this. Thanks a lot in advance!

1 Answers

Here are a few examples of where you can place the model N that will persist after merging.

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

tbl <- 
  glm(response ~ grade, trial, family = binomial) %>% 
  tbl_regression(exponentiate = TRUE) %>%
  # add N to the bottom of table
  add_glance_table(include = nobs) %>%
  # add new column with Ns
  add_n(location = c("label", "level"))


tbl_final <- 
  tbl_merge(list(tbl, tbl), tab_spanner = c("**Men**", "**Women**"))

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

Happy Programming!

Related