library(tidyverse)
library(here)
library(marginaleffects)
options(
marginaleffects_print_omit = c("s.value"),
marginaleffects_print_style = ifelse(interactive(), "summary", "tinytable")
)
df_clean <- read_csv2(here("data", "steps_clean.csv")) |>
filter(!is.na(lsas_screen), !is.na(lsas_post), !is.na(gad_screen)) |>
mutate(
# The second factor 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")
)
)Logistic regression with a numeric predictor
1 Numeric predictors and probabilities
A numeric predictor can change the probability of an outcome continuously. Here we model the probability of treatment response as a function of baseline GAD-7 score (gad_screen). The logistic model is linear on the log-odds scale, but its probability curve is generally curved.
mod_gad <- glm(
lsas_response ~ gad_screen,
family = binomial,
data = df_clean
)
summary(mod_gad)
Call:
glm(formula = lsas_response ~ gad_screen, family = binomial,
data = df_clean)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.48997 0.42024 -1.166 0.244
gad_screen -0.01725 0.03622 -0.476 0.634
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 216.05 on 168 degrees of freedom
Residual deviance: 215.83 on 167 degrees of freedom
AIC: 219.83
Number of Fisher Scoring iterations: 4
The model coefficients tell us that the estimated log-odds when GAD-7 is 0 (Intercept) is -0.49, and that the estimated log-odds changes by -0.017 for each one-point increase in GAD-7.
If we exponentiate the gad_screen estimate, we get the odds ratio of treatment response for each one-unit increase in GAD-7 at screening.
exp(b_gad)[1] 0.9828997
# Estimate and 95% CI on the log-odds scale, then exponentiate
or_gad <- comparisons(
mod_gad,
variables = list(gad_screen = c(10, 11)),
newdata = datagrid(gad_screen = 10),
type = "link",
transform = exp
)
or_gad| gad_screen | Estimate | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|
| Term: gad_screen | ||||
| Comparison: 11 - 10 | ||||
| Type: link | ||||
| 10 | 0.983 | 0.634 | 0.916 | 1.06 |
We can also get the estimated odds ratio for an x-point increase in GAD-7 at screening, by using the same procedure while multiplying the coefficient by x. For example, if we wanted the odds ratio for a 10-point increase in GAD-7, we could do:
exp(b_gad * 10) # OR for response for a 10-point increase in GAD-7[1] 0.8415737
Using the avg_slopes() function from the marginaleffects package, we can get these estimates on the probability scale, instead of the log-odds scale. It computes the slope of the probability curve (its derivative) at each participant’s GAD-7 score and averages these slopes across participants.
gad_slope <- avg_slopes(mod_gad, variables = "gad_screen", type = "response")
gad_slope| Estimate | Std. Error | z | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|---|
| Term: gad_screen | |||||
| Comparison: dY/dX | |||||
| Type: response | |||||
| -0.00385 | 0.00807 | -0.477 | 0.633 | -0.0197 | 0.012 |
On average, a one-point higher baseline GAD-7 score is associated with a change of -0.39 percentage points in the estimated probability of treatment response (95% CI -1.97 to 1.20 percentage points).
The odds ratio for a one-point higher GAD-7 score is 0.98 (95% CI 0.92 to 1.06). The linear log-odds model assumes this OR is constant across GAD-7 scores; the probability-scale slope is not.
GAD-7 was not randomized, so this is an association and not the effect of changing a participant’s anxiety. Because the probability curve is not a straight line, its slope differs across GAD-7 values; the average slope summarizes them, so interpreting it as the change for a one-point increase is an approximation that holds for small changes. The coefficient in summary() is a log-odds change; exponentiating it gives an odds ratio, not this probability-scale slope.
plot_predictions(mod_gad, condition = "gad_screen") +
labs(
title = "Estimated probability of treatment response",
subtitle = "Across baseline GAD-7 scores; 95% confidence interval",
x = "GAD-7 score at screening",
y = "Probability of treatment response"
) +
theme_minimal()
2 From a continuous score to a curve
A linear predictor does not imply a straight line on the probability scale. For this reason, plots and predictions at clinically meaningful values are usually more informative than reporting a coefficient alone.
gad_values <- quantile(
df_clean$gad_screen,
probs = c(.25, .50, .75),
na.rm = TRUE
)
predictions(
mod_gad,
newdata = datagrid(gad_screen = gad_values)
)| gad_screen | Estimate | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|
| Type: invlink(link) | ||||
| 7 | 0.352 | 0.00376 | 0.264 | 0.451 |
| 10 | 0.340 | < 0.001 | 0.272 | 0.416 |
| 14 | 0.325 | < 0.001 | 0.245 | 0.417 |
3 Is the log-odds linear in GAD-7?
The logistic model assumes that the log-odds of response change linearly with GAD-7. As in Course 2, we can relax this assumption with a natural cubic spline and compare the two models with a likelihood-ratio test.
library(splines)
mod_gad_spline <- glm(
lsas_response ~ ns(gad_screen, df = 3),
family = binomial,
data = df_clean
)
lrt_gad <- anova(mod_gad, mod_gad_spline, test = "LRT")
lrt_gad| Resid. Df | Resid. Dev | Df | Deviance | Pr(>Chi) |
|---|---|---|---|---|
| 167 | 215.8267 | NA | NA | NA |
| 165 | 215.1503 | 2 | 0.6764077 | 0.7130499 |
gad_spline_slope <- avg_slopes(
mod_gad_spline,
variables = "gad_screen",
type = "response"
)
gad_spline_slope| Estimate | Std. Error | z | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|---|
| Term: gad_screen | |||||
| Comparison: dY/dX | |||||
| Type: response | |||||
| -0.00664 | 0.01 | -0.664 | 0.506 | -0.0262 | 0.013 |
# A spline has no single constant OR: compare GAD-7 = 11 versus 10
or_gad_spline_10_11 <- comparisons(
mod_gad_spline,
variables = list(gad_screen = c(10, 11)),
newdata = datagrid(gad_screen = 10),
type = "link",
transform = exp
)
or_gad_spline_10_11| gad_screen | Estimate | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|
| Term: gad_screen | ||||
| Comparison: 11 - 10 | ||||
| Type: link | ||||
| 10 | 0.998 | 0.986 | 0.819 | 1.22 |
Allowing a curved relationship does not improve the fit: the likelihood-ratio test comparing the spline model with the linear model gives \(\chi^2\)(2) = 0.68, p = 0.71.
In the spline model, a one-point higher baseline GAD-7 score is associated with an average change of -0.66 percentage points in the estimated probability of treatment response (95% CI -2.62 to 1.30 percentage points), compared with -0.39 percentage points in the linear model. In the spline model, the OR comparing GAD-7 = 11 with GAD-7 = 10 is 1.00 (95% CI 0.82 to 1.22), versus the constant one-point OR of 0.98 in the linear log-odds model. Other starting scores can give different spline ORs.
A non-significant test does not prove that the relationship is linear, but these data give no reason to prefer the more flexible model.
gad_grid <- datagrid(
newdata = df_clean,
gad_screen = seq(
min(df_clean$gad_screen),
max(df_clean$gad_screen),
length.out = 100
)
)
pred_gad_linear <- predictions(
mod_gad,
newdata = gad_grid
) |>
mutate(model = "Linear in the log-odds")
pred_gad_spline <- predictions(
mod_gad_spline,
newdata = gad_grid
) |>
mutate(model = "Natural cubic spline (df = 3)")
bind_rows(pred_gad_linear, pred_gad_spline) |>
ggplot(aes(x = gad_screen, y = estimate, color = model, fill = model)) +
geom_ribbon(
aes(ymin = conf.low, ymax = conf.high),
alpha = 0.15,
color = NA
) +
geom_line(linewidth = 1) +
labs(
title = "Estimated probability of treatment response",
subtitle = "Linear and spline models; 95% confidence intervals",
x = "GAD-7 score at screening",
y = "Probability of treatment response",
color = "Model",
fill = "Model"
) +
theme_minimal()
The spline curve bends upward at low GAD-7 scores, but only 10 participants scored 4 or lower, and its confidence band is wide there. Bends at the edges of a spline often reflect a handful of observations rather than a real pattern, so we keep the simpler linear model.
4 The average prediction is not the prediction at the average
Suppose a clinic manager asks what proportion of patients can be expected to respond to treatment. A tempting answer is to take a “typical” patient, with every covariate at its sample mean, and report that patient’s predicted probability. Several widely used tools and many published papers report predictions this way. In a logistic model, however, the prediction for a typical patient is generally not the expected proportion of responders, and the two drift further apart the more patients differ from one another. Knowing which of the two you are looking at matters throughout this course. It is why we report average predictions and comparisons, and it explains why odds ratios change when covariates are added to a model, a point we return to in logistic regression with a binary and numeric predictor.
The two summaries sound alike but answer different questions:
- the average prediction: predict every participant’s probability, then average the predictions; and
- the prediction at the average: set GAD-7 to its sample mean, then predict once.
A made-up example shows why they differ and which question each one answers. Imagine a clinic where half the patients have mild anxiety (GAD-7 = 4) and half have severe anxiety (GAD-7 = 14). Suppose the probability of response falls steeply with GAD-7, with log-odds of response equal to \(3 - 0.4 \times \text{GAD-7}\). The numbers are invented to make the pattern easy to see, and plogis() is R’s inverse logit.
# A made-up model: log-odds of response = 3 - 0.4 * GAD-7
toy_logit <- function(gad) 3 - 0.4 * gad
gad_patients <- c(mild = 4, severe = 14)
p_patients <- plogis(toy_logit(gad_patients))
p_patients mild severe
0.80218389 0.06913842
# Average prediction: expected proportion of the clinic's patients who respond
p_clinic_average <- mean(p_patients)
p_clinic_average[1] 0.4356612
# Prediction at the average: a patient with the clinic's mean GAD-7
p_at_average_gad <- plogis(toy_logit(mean(gad_patients)))
p_at_average_gad[1] 0.3543437
A mild patient has an estimated 80% chance of response and a severe patient 7%. If the clinic treats equal numbers of each, we expect 44% of its patients to respond; this is the average prediction. The prediction at the average describes a patient with GAD-7 = 9, the clinic’s mean score, and gives 35%. Nobody in this clinic has that score, so the second number answers a question about a hypothetical “average patient”, not about the clinic’s patients.
The figure shows where the gap comes from. On the log-odds scale (left), the model is a straight line, so the point halfway between the two patients is also the model’s prediction at GAD-7 = 9: on this scale, averaging and predicting at the average agree. Transforming to probabilities (right) bends the line into a curve. The prediction at the average follows the curve down to 35%, but the average of the two patients’ probabilities lies halfway along the straight dashed line between them, at 44%. Translated back to log-odds, the average prediction sits above the straight line in the left panel: averaging probabilities is not the same as averaging log-odds.1
Show the plot code
# Express log-odds on both scales, one row per scale
both_scales <- function(data) {
data |>
mutate(
`Log-odds scale` = log_odds,
`Probability scale` = plogis(log_odds)
) |>
select(-log_odds) |>
pivot_longer(
c(`Log-odds scale`, `Probability scale`),
names_to = "scale",
values_to = "value"
)
}
toy_curve <- tibble(gad = seq(0, 21, by = 0.1), log_odds = toy_logit(gad)) |>
both_scales()
toy_patients <- tibble(gad = gad_patients, log_odds = toy_logit(gad)) |>
both_scales()
toy_summaries <- tibble(
gad = mean(gad_patients),
log_odds = c(qlogis(p_clinic_average), toy_logit(mean(gad_patients))),
summary = c("Average prediction", "Prediction at the average")
) |>
both_scales()
ggplot(toy_curve, aes(x = gad, y = value)) +
geom_vline(xintercept = mean(gad_patients), linetype = "dotted") +
geom_line(color = "grey50") +
geom_line(data = toy_patients, linetype = "dashed") +
geom_point(data = toy_patients, size = 3) +
geom_point(data = toy_summaries, aes(color = summary), size = 3) +
facet_wrap(~scale, scales = "free_y") +
labs(
title = "Average prediction versus prediction at the average",
subtitle = "Made-up model; black points: mild and severe patients",
x = "GAD-7 score",
y = NULL,
color = NULL
) +
theme_minimal() +
theme(legend.position = "bottom")
marginaleffects has a function for each summary. avg_predictions() gives the average prediction: it predicts every participant’s probability and averages the predictions. predictions() gives the prediction at chosen predictor values; newdata = "mean" sets GAD-7 to its sample mean. In the GAD-7 model, the two summaries are:
p_gad_average <- avg_predictions(mod_gad)
p_gad_at_mean <- predictions(mod_gad, newdata = "mean")
p_gad_average| Estimate | Std. Error | z | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|---|---|
| Type: response | |||||
| 0.337 | 0.0363 | 9.28 | <0.001 | 0.266 | 0.409 |
p_gad_at_mean| Estimate | Pr(>|z|) | 2.5 % | 97.5 % |
|---|---|---|---|
| Type: invlink(link) | |||
| 0.336 | <0.001 | 0.269 | 0.411 |
Here they are almost identical (0.3373 versus 0.3363). The gap grows with how much the predicted log-odds vary between participants, and GAD-7 barely moves them: across the observed range of GAD-7, the predicted log-odds change by only 0.34. Treatment changes the log-odds far more, so the distinction matters in the models with treatment later in the course.
5 Summary
- Logistic regression can model a binary outcome using a continuous predictor.
- Probability curves are usually easier to communicate than log-odds coefficients.
- An average probability slope is an interpretable summary, but it can conceal variation across the predictor range.
- A natural cubic spline and a likelihood-ratio test check whether the log-odds are linear in a numeric predictor.
- The average of predicted probabilities is not the predicted probability at the average covariate value; the gap grows with how much the predictions vary.
Footnotes
The general result is Jensen’s inequality. The inverse logit is convex for negative log-odds (probabilities below 0.5) and concave for positive log-odds, so the direction of the gap depends on where the predictions lie.↩︎