Generalized Linear Model

Before You Start

Basic Procedure

Report Results

Summary and Practice

Syntax

The examples use the scored version of the simulated dataset described in Example Data. The code runs when the site is built, so the table below it is its output.

Logistic regression

library(dplyr)
library(tidyr)
library(gtsummary)
theme_gtsummary_compact()

mydata <- read.csv("data/mydata_scored.csv")
data_complete <- mydata %>%
  drop_na(y1, y2, y3, C_total, gender, education, income, A_s1, A_s2, A_s3, B_s1, B_s2, B_s3) %>%
  mutate(across(c(education, income), factor))

gt_r1 <- glm(y1 ~ C_total + gender + education + income + A_s1 + A_s2 + A_s3 + B_s1 + B_s2 + B_s3, data_complete, family = binomial) %>%
  tbl_regression(exponentiate = TRUE,
                 pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>%
  add_significance_stars(
    hide_p = FALSE,
    pattern = "{p.value}{stars}"
  )

gt_r2 <- glm(y2 ~ C_total + gender + education + income + A_s1 + A_s2 + A_s3 + B_s1 + B_s2 + B_s3, data_complete, family = binomial) %>%
  tbl_regression(exponentiate = TRUE,
                 pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>%
  add_significance_stars(
    hide_p = FALSE,
    pattern = "{p.value}{stars}"
  )

gt_r3 <- glm(y3 ~ C_total + gender + education + income + A_s1 + A_s2 + A_s3 + B_s1 + B_s2 + B_s3, data_complete, family = binomial) %>%
  tbl_regression(exponentiate = TRUE,
                 pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>%
  add_significance_stars(
    hide_p = FALSE,
    pattern = "{p.value}{stars}"
  )

tbl_merge(
  list(gt_r1, gt_r2, gt_r3),
  tab_spanner = c("**Outcome 1**", "**Outcome 2**", "**Outcome 3**")
)
Characteristic
Outcome 1
Outcome 2
Outcome 3
OR SE p-value1 OR SE p-value1 OR SE p-value1
C_total 1.24 0.056 <0.001*** 1.20 0.059 0.003** 1.01 0.060 0.811
gender








    female — —
— —
— —
    male 1.18 0.229 0.479 0.97 0.257 0.891 0.73 0.259 0.232
education








    1 — —
— —
— —
    2 1.11 0.320 0.752 1.27 0.363 0.507 1.35 0.333 0.368
    3 1.02 0.327 0.946 0.81 0.384 0.591 0.58 0.373 0.151
    4 1.02 0.307 0.951 1.65 0.339 0.139 0.77 0.340 0.452
income








    1 — —
— —
— —
    2 1.02 0.363 0.947 1.44 0.394 0.355 0.92 0.421 0.836
    3 1.25 0.360 0.540 1.03 0.394 0.944 1.28 0.399 0.534
    4 0.89 0.362 0.743 0.67 0.416 0.345 1.49 0.392 0.312
    5 1.21 0.333 0.567 1.17 0.373 0.681 1.58 0.372 0.219
A_s1 0.99 0.025 0.636 1.00 0.028 0.874 1.05 0.028 0.081
A_s2 1.07 0.024 0.006** 0.97 0.026 0.285 0.98 0.026 0.342
A_s3 1.02 0.025 0.410 0.99 0.029 0.845 1.04 0.028 0.200
B_s1 1.0 0.031 0.863 1.13 0.035 <0.001*** 1.0 0.034 0.875
B_s2 1.04 0.023 0.096 1.02 0.025 0.541 1.01 0.025 0.637
B_s3 0.96 0.031 0.248 1.05 0.035 0.154 1.12 0.035 0.002**
1 *p<0.05; **p<0.01; ***p<0.001
Abbreviations: CI = Confidence Interval, OR = Odds Ratio, SE = Standard Error

Resources & References