Initial data

Just like in the previous example we will load the Titanic dataset and make the same manipulations to the data. Then we will use logistic regression with death as the outcome before creating a table of the model coefficients.

Code
library(tidyverse)
library(skimr)
library(titanic)
library(janitor)
library(gtsummary)
library(bstfun)

df_titanic <- titanic::titanic_train %>%
  select(-c(PassengerId, Name, Ticket, Parch, Cabin)) %>% # Removing unwanted variables
  mutate( # First converting SibSp into a factor variable and then collapsing it to 3 levels and an "other" level
    SibSp = factor(SibSp), 
    SibSp = fct_lump_n(SibSp, n = 3),
    SibSp = fct_recode(SibSp, ">=3" = "Other"), # Renaming the new level
    Sex = fct_recode(Sex, # Renaming levels of categorical variables
      "Female" = "female",
      "Male" = "male"
    ),
    Embarked = fct_recode(Embarked,
                          "Cherbourg" = "C",
                          "Southhampton" = "S",
                          "Cobh" = "Q")
    ) %>% 
  rename( # Renaming variables
    "Passenger class" = Pclass,
    "Number of siblings" = SibSp,
    "Ticket price" = Fare,
    "City of embarkation" = Embarked
  )

Creating the model

Standard logistic regression model with death as the outcome. The “.” after the “~” indicates that we will use all available variables to predict the outcome. Here we have subtracted the “passenger_id” variable from the equation as we don’t want to use this to predict death.

Code
model_titanic <- glm(survived ~ . - passenger_id, data = df_titanic, family = "binomial")

Creating the table

Code
model_titanic %>% 
  tbl_regression(exponentiate = TRUE) %>%
  italicize_levels() %>%
  bold_labels() %>%
  bold_p() %>% 
  as_gt() %>%
  gt::tab_options(table.font.names = "Times New Roman")
Characteristic OR1 95% CI1 p-value
Class
1
2 3.33 1.76, 6.37 <0.001
3 11.2 5.80, 22.1 <0.001
Sex
Female
Male 13.7 9.01, 21.2 <0.001
Age 1.04 1.03, 1.06 <0.001
Number of siblings 1.46 1.15, 1.88 0.002
Ticket price 1.00 0.99, 1.00 0.6
City of embarkation
Cherbourg
Queenstown (Cobh) 2.26 0.71, 7.49 0.2
Southampton 1.50 0.87, 2.57 0.14
1 OR = Odds Ratio, CI = Confidence Interval

Creating the table with an inline forest plot

Code
model_titanic %>% 
  tbl_regression(exponentiate = TRUE) %>%
  italicize_levels() %>%
  bold_labels() %>%
  bold_p() %>% 
  add_inline_forest_plot() %>%
  as_gt() %>%
  gt::tab_options(table.font.names = "Times New Roman")
Characteristic Forest Plot OR1 95% CI1 p-value
Class
1
2 3.33 1.76, 6.37 <0.001
3 11.2 5.80, 22.1 <0.001
Sex
Female
Male 13.7 9.01, 21.2 <0.001
Age 1.04 1.03, 1.06 <0.001
Number of siblings 1.46 1.15, 1.88 0.002
Ticket price 1.00 0.99, 1.00 0.6
City of embarkation
Cherbourg
Queenstown (Cobh) 2.26 0.71, 7.49 0.2
Southampton 1.50 0.87, 2.57 0.14
1 OR = Odds Ratio, CI = Confidence Interval