Sometimes you will want to have univariate and multivariable analyses in the same table. The example below looks at the impact of variables on survival and then only selects those with a certain p-value (in this case 0.02) to include in the multivariable analysis.

It involves creating two separate tables and then joining at the end using the tbl_merge function from gtsummary.

There is an intermediate step where a table, filtered by p-value, is created. This allows the use of those selected variables with a p-value below the threshold to be used in the multivariable analysis. You could manually specify which variables to include and avoid this step but it may become cumbersome if there were a large number of variables to be included.

Finally the table is converted to a “gt” table so that some additional formatting can be done.

Code
library(titanic)
library(tidyverse)
library(gtsummary)

theme_gtsummary_compact()

table_uni <- titanic_train %>% # Dataset used
  select(-c(Name, PassengerId, Ticket, Cabin, Embarked, Parch)) %>% # Keeping only the variables we want
  mutate( # Making death 1 and survival 0, easier to interpret in graph
    Survived = as.numeric(Survived!= '1'),
    Pclass = factor(Pclass) # Class should be categorical
  ) %>%
  tbl_uvregression(method=glm,
                   y = Survived,
    method.args = list(family = binomial),
    exponentiate = TRUE,
    hide_n = T
  ) %>%
  add_nevent(location="level") %>%
  add_global_p() %>%    #omnibus test
  bold_p() %>% # bold p-values  (default 0.05)
  bold_labels() %>% 
  italicize_levels() %>% 
  modify_header(stat_nevent = "**Deaths**")


p <- titanic_train %>% # Dataset used
  # select(Pclass, Sex, Age, Survived) %>% # Keeping only the variables we want
  select(-c(Name, PassengerId, Ticket, Cabin, Embarked, Parch)) %>% 
  mutate( # Making death 1 and survival 0, easier to interpret in graph
    Survived = as.numeric(Survived!= '1'),
    Pclass = factor(Pclass) # Class should be categorical
  ) %>%
  tbl_uvregression(method=glm,
                   y = Survived,
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) %>% 
  filter_p(t = 0.02)


table_multi <- titanic_train %>% # Dataset used
  select(Survived, p$table_body$variable) %>% # Keeping only the variables we want
  mutate( # Making death 1 and survival 0, easier to interpret in graph
    Survived = as.numeric(Survived!= '1'),
    Pclass = factor(Pclass) # Class should be categorical
  ) %>%
  glm(Survived ~ ., family = binomial(), data = .) %>% 
  tbl_regression(
    exponentiate = T
  ) %>%
  add_global_p() %>%    #omnibus test
  bold_p() %>% # bold p-values  (default 0.05)
  bold_labels() %>% 
  italicize_levels()

tbl_merge(
  list(table_uni, table_multi),
  tab_spanner = c("**Univariate analysis**", "**Multivariable analysis**")) %>%
  as_gt() %>% 
gt::tab_options(table.font.names = "Times New Roman") %>% 
  gt::opt_horizontal_padding(scale = 2.5)
Characteristic Univariate analysis Multivariable analysis
Deaths OR1 95% CI1 p-value OR1 95% CI1 p-value
Pclass <0.001 <0.001
    1 80
    2 97 1.90 1.27, 2.83 2.09 1.23, 3.56
    3 372 5.31 3.78, 7.53 5.99 3.66, 9.85
Sex <0.001 <0.001
    female 81
    male 468 12.4 8.94, 17.2 13.8 9.66, 20.0
Age 424 1.01 1.00, 1.02 0.038
SibSp 549 1.07 0.95, 1.22 0.3
Fare 549 0.98 0.98, 0.99 <0.001 1.00 0.99, 1.00 0.4
1 OR = Odds Ratio, CI = Confidence Interval