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 binary predictor
1 Introduction
This notebook extends the treatment example from logistic regression foundations. We focus on the two arms that make a binary treatment predictor: waitlist and therapist-guided.
Our question is:
Among participants in these two arms, how does the estimated probability of treatment response differ for therapist-guided treatment versus waitlist?
The target is the therapist-guided minus waitlist probability difference. We will use the corresponding odds ratio to read logistic regression output, but the probability difference is the primary clinical result.
2 Packages and data
df_binary <- 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")
)
) |>
filter(trt %in% c("waitlist", "therapist-guided")) |>
droplevels()
df_binary |>
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 |
Response means that LSAS decreases by at least 30% from baseline, as in logistic regression foundations.
We explicitly make waitlist the reference group. The treatment contrast shown by the coefficient will therefore be log-odds of response for the therapist-guided group minus the log-odds for treatment response in the waitlist group.
levels(df_binary$trt)[1] "waitlist" "therapist-guided"
contrasts(df_binary$trt) therapist-guided
waitlist 0
therapist-guided 1
3 A logistic model for two treatment groups
For participant \(i\), let \(Y_i\) indicate treatment response. 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}\}. \]
Here, \(\beta_0\) is the log-odds of response in the waitlist group, and \(\beta_1\) is the therapist-guided-versus-waitlist log-odds difference. These are the quantities that glm() estimates directly. They are not yet the most clinically intelligible quantities.
3.1 Fit and interpret the model
We fit the model with the glm() function, using family = binomial to tell R that this is a logistic model. We can then use the summary() function to see the model coefficients.
mod_binary_trt <- glm(
lsas_response ~ trt, # model formula
family = binomial, # using the binomial family to get a logistic regression
data = df_binary # using our dataset df_binary
)
summary(mod_binary_trt)
Call:
glm(formula = lsas_response ~ trt, family = binomial, data = df_binary)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.1595 0.4311 -5.009 5.48e-07 ***
trttherapist-guided 2.4580 0.5115 4.806 1.54e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 142.11 on 111 degrees of freedom
Residual deviance: 112.25 on 110 degrees of freedom
AIC: 116.25
Number of Fisher Scoring iterations: 4
# Store the coefficients for the calculations below
b0 <- coef(mod_binary_trt)[["(Intercept)"]]
b1 <- coef(mod_binary_trt)[["trttherapist-guided"]]The output from summary() shows that the log-odds of response in the waitlist group is -2.16 (\(\beta_0\)), and that the log-odds in the therapist-guided group is 2.46 (\(\beta_1\)) higher, that is, \(\beta_0 + \beta_1\).
By exponentiating the linear predictors (added coefficients), we get the odds for each group. By exponentiating only the coefficient for the therapist-guided group (\(\beta_1\); the difference in log-odds between the therapist-guided and the waitlist group), we get the odds ratio of response in the therapist-guided vs. the waitlist group.
exp(b0) # odds of response in waitlist group[1] 0.1153846
exp(b0 + b1) # odds of response in therapist-guided group[1] 1.347826
exp(b1) # odds ratio of response, therapist-guided vs. waitlist[1] 11.68116
To go from the linear predictors of the log-odds to the probability, we can use the inverse logit function:
\[\operatorname{logit}^{-1}(x) = \frac{e^x}{1 + e^x}\]
| Treatment group | Linear predictor | Model-derived probability |
|---|---|---|
| waitlist | \(\beta_0\) | \(\operatorname{logit}^{-1}(\beta_0)\) |
| therapist-guided | \(\beta_0 + \beta_1\) | \(\operatorname{logit}^{-1}(\beta_0 + \beta_1)\) |
exp(b0) / (1 + exp(b0)) # probability of response in waitlist group[1] 0.1034483
exp(b0 + b1) / (1 + exp(b0 + b1)) # probability, therapist-guided group[1] 0.5740741
3.2 Get probabilities and the probability difference from the model
You can also get these predicted probabilities using the marginaleffects package.
p_binary_trt <- predictions(
mod_binary_trt,
newdata = datagrid(trt = unique)
)
diff_binary_trt <- avg_comparisons(
mod_binary_trt,
variables = list(trt = c("waitlist", "therapist-guided")),
type = "response"
)
or_binary_trt <- tidy(
mod_binary_trt,
conf.int = TRUE,
exponentiate = TRUE
) |>
filter(term == "trttherapist-guided")
p_binary_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.278 | 0.4401 | 0.698 |
diff_binary_trt| Estimate | Std. Error | z | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|---|
| Term: trt | |||||
| Comparison: therapist-guided - waitlist | |||||
| Type: response | |||||
| 0.471 | 0.0783 | 6.01 | <0.001 | 0.317 | 0.624 |
The estimated probability difference is calculated from the model’s predicted probabilities rather than from its coefficients. It answers a different question from the printed log-odds coefficient: how many percentage points higher or lower is the modeled probability in one group than the other?
The estimated probability of treatment response is 10.3% for waitlist (95% CI 4.7% to 21.2%) and 57.4% for therapist-guided treatment (95% CI 44.0% to 69.8%).
Compared with waitlist, therapist-guided treatment increases the estimated probability by 47.1 percentage points (therapist-guided minus waitlist = 47.1 percentage points; 95% CI 31.7 to 62.4 percentage points). This is an unadjusted contrast within the restricted two-arm sample.
The associated therapist-guided-versus-waitlist odds ratio is 11.68 (95% CI 4.54 to 34.62). It is a ratio of the odds of response, not a probability ratio and not a 47.1-percentage-point change.
3.3 Visualize the group probabilities
plot_predictions(mod_binary_trt, condition = "trt") +
labs(
title = "Estimated probability of treatment response",
subtitle = "Waitlist vs. therapist-guided; 95% confidence intervals",
x = "Treatment group",
y = "Probability of treatment response"
) +
theme_minimal()
4 Log-odds and odds ratios
The logistic model is linear on the log-odds scale. Exponentiating \(\beta_1\) gives the therapist-guided-versus-waitlist odds ratio:
\[ \exp(\beta_1) = \frac{\text{odds of response with therapist-guided treatment}} {\text{odds of response with waitlist}}. \]
or_binary_trt| term | estimate | std.error | statistic | p.value | conf.low | conf.high |
|---|---|---|---|---|---|---|
| trttherapist-guided | 11.68116 | 0.5114807 | 4.805611 | 1.5e-06 | 4.542373 | 34.6221 |
An odds ratio larger than 1 means higher odds of response in the therapist-guided group. It cannot be read as a percentage-point increase, because the same odds ratio implies different probability differences at different baseline probabilities. Report the model-derived probabilities and their probability difference when the clinical question concerns chances of an outcome.
5 Summary
In this chapter you learned to:
- restrict the STePS data to a binary treatment comparison and set waitlist as the reference category;
- fit a binary logistic model with
glm(..., family = binomial); - report treatment-specific probabilities and the therapist-guided-minus-waitlist probability difference with 95% CIs; and
- interpret the associated odds ratio as a ratio of odds rather than a probability difference.