library(tidyverse)
library(here)
library(broom)
library(marginaleffects)
options(
marginaleffects_print_omit = c("s.value"),
marginaleffects_print_style = ifelse(interactive(), "summary", "tinytable")
)Logistic regression with a categorical predictor
1 Introduction
A treatment variable with three arms is a categorical predictor, not a single binary treatment indicator. In the STePS data, the arms are waitlist, therapist-guided treatment, and self-guided treatment.
Our primary questions are:
- What is the model-derived probability of treatment response in each treatment arm?
- How do those probabilities differ between clinically relevant pairs of arms?
As before, we will communicate probabilities and probability differences with 95% CIs. Log-odds coefficients and odds ratios help us read model output, but they are not probability differences.
2 Packages and data
df_categorical <- read_csv2(here("data", "steps_clean.csv")) |>
filter(!is.na(lsas_screen), !is.na(lsas_post)) |>
mutate(
# The second level is the event modeled by binomial glm().
lsas_response = factor(
if_else(
(lsas_screen - lsas_post) / lsas_screen >= 0.30,
"Response",
"No response"
),
levels = c("No response", "Response")
),
trt = factor(
trt,
levels = c("waitlist", "therapist-guided", "self-guided")
)
)
df_categorical |>
count(trt, lsas_response, .drop = FALSE)| trt | lsas_response | n |
|---|---|---|
| waitlist | No response | 52 |
| waitlist | Response | 6 |
| therapist-guided | No response | 23 |
| therapist-guided | Response | 31 |
| self-guided | No response | 37 |
| self-guided | Response | 20 |
Response is a reduction of at least 30% from baseline LSAS; it is not a diagnostic or remission definition. We order the outcome so that Response is the event in the binomial model, and we explicitly set waitlist as the treatment reference category.
levels(df_categorical$trt)[1] "waitlist" "therapist-guided" "self-guided"
contrasts(df_categorical$trt) therapist-guided self-guided
waitlist 0 0
therapist-guided 1 0
self-guided 0 1
3 Reference categories and dummy variables
With waitlist as the reference, R uses two indicator variables. The model is
\[ \operatorname{logit}\left[P(Y_i = \text{Response} \mid trt_i)\right] = \beta_0 + \beta_1\,\mathbf{1}\{trt_i = \text{therapist-guided}\} + \beta_2\,\mathbf{1}\{trt_i = \text{self-guided}\}. \]
| Treatment group | Linear predictor | Coefficient comparison |
|---|---|---|
| waitlist | \(\beta_0\) | reference |
| therapist-guided | \(\beta_0 + \beta_1\) | therapist-guided minus waitlist |
| self-guided | \(\beta_0 + \beta_2\) | self-guided minus waitlist |
Thus, the coefficient table supplies two log-odds comparisons with waitlist. It does not directly show the self-guided-versus-therapist-guided comparison; we obtain that, and all other pairwise probability differences, from the fitted model.
4 Fit the three-arm logistic model
mod_categorical_trt <- glm(
lsas_response ~ trt,
family = binomial,
data = df_categorical
)
p_categorical_trt <- predictions(
mod_categorical_trt,
newdata = datagrid(trt = unique)
)
diff_guided_waitlist <- avg_comparisons(
mod_categorical_trt,
variables = list(trt = c("waitlist", "therapist-guided")),
type = "response"
)
diff_self_waitlist <- avg_comparisons(
mod_categorical_trt,
variables = list(trt = c("waitlist", "self-guided")),
type = "response"
)
or_categorical_trt <- tidy(
mod_categorical_trt,
conf.int = TRUE,
exponentiate = TRUE
) |>
filter(term != "(Intercept)")5 Predicted probabilities by treatment arm
p_categorical_trt| trt | Estimate | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|
| Type: invlink(link) | ||||
| waitlist | 0.103 | <0.001 | 0.0472 | 0.212 |
| therapist-guided | 0.574 | 0.2781 | 0.4401 | 0.698 |
| self-guided | 0.351 | 0.0267 | 0.2388 | 0.482 |
The three predicted probabilities are response-scale summaries of the same logistic model. They are often a more direct starting point for a clinical discussion than its intercept and two log-odds coefficients.
plot_predictions(mod_categorical_trt, condition = "trt") +
labs(
title = "Estimated probability of treatment response",
subtitle = "Three-arm logistic regression; 95% confidence intervals",
x = "Treatment group",
y = "Probability of treatment response"
) +
theme_minimal()
The estimated probability of treatment response is 10.3% for waitlist (95% CI 4.7% to 21.2%), 57.4% for therapist-guided treatment (95% CI 44.0% to 69.8%), and 35.1% for self-guided treatment (95% CI 23.9% to 48.2%).
Relative to waitlist, therapist-guided treatment increases the estimated probability by 47.1 percentage points (95% CI 31.7 to 62.4 percentage points). Self-guided treatment increases it by 24.7 percentage points relative to waitlist (95% CI 10.1 to 39.4 percentage points). These are unadjusted contrasts in the three-arm analysis sample.
The corresponding odds ratios versus waitlist are 11.68 for therapist-guided treatment (95% CI 4.54 to 34.62) and 4.68 for self-guided treatment (95% CI 1.80 to 13.85). Each is a ratio of odds, not a probability ratio or a percentage-point probability difference.
6 Probability differences between treatment arms
There is no single treatment contrast with three arms. Below, we calculate all pairwise differences to see the full picture. A positive difference means that the first-named group has the higher estimated probability of treatment response.
pairwise_differences <- avg_comparisons(
mod_categorical_trt,
variables = list(trt = "pairwise"),
type = "response"
)
pairwise_differences| Contrast | Estimate | Std. Error | z | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|---|---|
| Term: trt | ||||||
| Type: response | ||||||
| self-guided - therapist-guided | -0.223 | 0.0923 | -2.42 | 0.0156 | -0.404 | -0.0422 |
| self-guided - waitlist | 0.247 | 0.0748 | 3.31 | <0.001 | 0.101 | 0.3940 |
| therapist-guided - waitlist | 0.471 | 0.0783 | 6.01 | <0.001 | 0.317 | 0.6240 |
pairwise_differences |>
mutate(
estimate_pp = 100 * estimate,
conf.low_pp = 100 * conf.low,
conf.high_pp = 100 * conf.high
) |>
ggplot(aes(y = contrast, x = estimate_pp)) +
geom_vline(xintercept = 0, linetype = "dashed") +
geom_errorbar(aes(xmin = conf.low_pp, xmax = conf.high_pp), width = 0.1) +
geom_point(size = 2.5) +
labs(
title = "Pairwise differences in probability of treatment response",
subtitle = "First-named minus second-named arm; 95% confidence intervals",
x = "Probability difference (percentage points)",
y = NULL
) +
theme_minimal()
7 Odds ratios as a secondary summary
or_categorical_trt| term | estimate | std.error | statistic | p.value | conf.low | conf.high |
|---|---|---|---|---|---|---|
| trttherapist-guided | 11.681159 | 0.5114807 | 4.805611 | 0.0000015 | 4.542373 | 34.62210 |
| trtself-guided | 4.684685 | 0.5127409 | 3.011850 | 0.0025966 | 1.801915 | 13.85084 |
The odds ratios above compare each active arm with waitlist because waitlist is the reference group.
8 Summary
In this chapter you learned to:
- define an explicit reference category for a three-level treatment predictor;
- fit a three-arm binary logistic model with
glm(..., family = binomial); - obtain estimated probabilities for every treatment arm and pairwise probability differences with 95% CIs; and
- distinguish odds ratios from the percentage-point probability differences needed for a clinical interpretation.