Logistic regression with two binary predictors

1 Two predictors in additive and multiplicative models

The previous logistic regression chapters considered one predictor at a time. Clinical questions often ask whether a treatment contrast looks different in a baseline subgroup. Here we model the probability of a treatment response using:

  • treatment group (trt); and
  • a binary baseline PHQ-9 category (phq_cat).

Our research question is:

Within each PHQ-9 category, how does the estimated probability of treatment response differ between therapist-guided treatment and waitlist?

We will first fit an additive logistic model and then a model with trt * phq_cat. Results will be presented using the probability difference, log-odds and odds ratios.

2 Packages and data

library(tidyverse)
library(here)
library(broom)
library(marginaleffects)

options(
  marginaleffects_print_omit = c("s.value"),
  marginaleffects_print_style = ifelse(interactive(), "summary", "tinytable")
)
df_binary <- read_csv2(here("data", "steps_clean.csv")) |>
  filter(!is.na(lsas_screen), !is.na(lsas_post)) |>
  mutate(
    # The second outcome 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")
    ),
    phq_cat = factor(
      case_when(
        phq9_screen < 10 ~ "< 10",
        phq9_screen >= 10 ~ ">= 10"
      ),
      levels = c("< 10", ">= 10")
    )
  ) |>
  filter(trt %in% c("waitlist", "therapist-guided")) |>
  droplevels()

df_binary |>
  count(phq_cat, lsas_response, .drop = FALSE)
phq_cat lsas_response n
< 10 No response 40
< 10 Response 21
>= 10 No response 35
>= 10 Response 16

phq_cat explicitly divides baseline PHQ-9 scores into < 10 and >= 10. It is a convenient binary moderator for this lesson. Logistic regression with a binary and numeric predictor keeps PHQ-9 continuous. The outcome is treatment response, defined as a reduction of at least 30% from baseline LSAS.

Waitlist is the reference treatment group, < 10 is the reference PHQ-9 category, and Response is the modeled outcome event.

3 Additive logistic model

An additive model has treatment and PHQ-9-category terms but no product term:

\[ \operatorname{logit}\left[P(Y_i = \mathrm{Response})\right] = \beta_0 + \beta_1 trt_i + \beta_2 phq_i \]

On the log-odds scale, this model says that the treatment odds ratio is the same in both PHQ-9 categories.

mod_additive <- glm(
  lsas_response ~ trt + phq_cat,
  family = binomial,
  data = df_binary
)

summary(mod_additive)

Call:
glm(formula = lsas_response ~ trt + phq_cat, family = binomial, 
    data = df_binary)

Coefficients:
                    Estimate Std. Error z value Pr(>|z|)    
(Intercept)         -2.14895    0.48573  -4.424 9.68e-06 ***
trttherapist-guided  2.45679    0.51206   4.798 1.60e-06 ***
phq_cat>= 10        -0.02193    0.46766  -0.047    0.963    
---
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 109  degrees of freedom
AIC: 118.25

Number of Fisher Scoring iterations: 4

We can get the ORs for therapist-guided versus waitlist and for PHQ-9 >= 10 versus < 10 by exponentiating the coefficients.

b_additive <- coef(mod_additive)
exp(b_additive[["trttherapist-guided"]]) # OR therapist-guided vs waitlist
[1] 11.66727
exp(b_additive[["phq_cat>= 10"]]) # OR PHQ-9 >= 10 vs < 10
[1] 0.9783106

The fact that the odds ratio is the same in both PHQ-9 categories does not guarantee identical probability differences: changing a probability by a fixed odds ratio can produce different percentage-point changes at different baseline probabilities.

additive_probabilities <- predictions(
  mod_additive,
  newdata = datagrid(trt = unique, phq_cat = unique)
)

additive_treatment_effects <- avg_comparisons(
  mod_additive,
  variables = list(trt = c("waitlist", "therapist-guided")),
  by = "phq_cat",
  type = "response"
)

additive_treatment_low_phq <- additive_treatment_effects |>
  filter(phq_cat == "< 10")
additive_treatment_high_phq <- additive_treatment_effects |>
  filter(phq_cat == ">= 10")

additive_odds_ratio <- tidy(
  mod_additive,
  conf.int = TRUE,
  exponentiate = TRUE
) |>
  filter(term == "trttherapist-guided")

additive_probabilities
trt phq_cat Estimate Pr(>|z|) 2.5 % 97.5 %
Type: invlink(link)
waitlist < 10 0.104 <0.001 0.0431 0.232
waitlist >= 10 0.102 <0.001 0.0414 0.231
therapist-guided < 10 0.576 0.365 0.4113 0.726
therapist-guided >= 10 0.571 0.457 0.3853 0.739
additive_treatment_effects
phq_cat Estimate Std. Error z Pr(>|z|) 2.5 % 97.5 %
Term: trt
Comparison: therapist-guided - waitlist
Type: response
< 10 0.472 0.0832 5.68 <0.001 0.309 0.635
>= 10 0.469 0.0894 5.24 <0.001 0.293 0.644

Compared with waitlist, therapist-guided treatment has an estimated probability of treatment response that is higher by 47.2 percentage points among participants with baseline PHQ-9 < 10 (95% CI 30.9 to 63.5 percentage points). Among participants with baseline PHQ-9 >= 10, it is higher by 46.9 percentage points (95% CI 29.3 to 64.4 percentage points).

Because this is an additive logistic model, its therapist-guided-versus-waitlist odds ratio is 11.67 (95% CI 4.53 to 34.62) in both PHQ-9 categories, adjusted for PHQ-9 category.

plot_predictions(
  mod_additive,
  condition = c("trt", "phq_cat")
) +
  labs(
    title = "Additive logistic model",
    subtitle = "Probabilities by treatment and baseline PHQ-9 category",
    x = "Treatment group",
    y = "Estimated probability of treatment response (95% CI)",
    color = "Baseline PHQ-9"
  ) +
  theme_minimal()

Additive-model estimated probabilities of treatment response. Error bars are 95% confidence intervals.

4 Interaction model

NoteEffect modification, interaction, and moderation

These terms are often used interchangeably, but they mean different things.

  • Effect modification: the effect of one intervention (here, treatment) differs across levels of another variable (here, baseline PHQ-9 category). Only the first variable needs to be something we could intervene on; the second is a characteristic we condition on (VanderWeele 2009).
  • Causal interaction: the effect of intervening on both variables together differs from what their separate effects would imply. Both variables must be things we could, at least in principle, intervene on (VanderWeele 2009).
  • Moderation: the term most used in psychology for a variable that changes the direction or strength of a relationship (Baron and Kenny 1986). In randomized trials, Kraemer et al. (2002) define a moderator as a characteristic measured before randomization, and therefore unrelated to treatment, that identifies subgroups with different treatment effects. This is close to effect modification.

Treatment is randomized, but PHQ-9 is not, so this chapter studies effect modification. We can estimate whether the treatment effect differs between PHQ-9 categories, but not what would happen if we changed a participant’s depression, and a difference between the categories may reflect other characteristics that go with higher PHQ-9 scores.

All three depend on the scale. A model can show no interaction on one scale and an interaction on another (VanderWeele and Knol 2014). The additive model above is an example: it has no interaction on the log-odds scale, yet its probability differences differ between the PHQ-9 categories (47.2 versus 46.9 percentage points). Here the difference is tiny because PHQ-9 category barely predicts response (odds ratio 0.98), so both categories start from almost the same probability under waitlist. With baseline PHQ-9 kept continuous, the probability difference in the additive model changes visibly across scores; see logistic regression with a binary and numeric predictor. When you report effect modification, name the scale, for example the probability difference (an additive scale) or the odds ratio (a multiplicative scale).

The interaction model adds a product term, written trt * phq_cat in the model formula:

\[ \operatorname{logit}\left[P(Y_i = \mathrm{Response})\right] = \beta_0 + \beta_1 trt_i + \beta_2 phq_i + \beta_3(trt_i \times phq_i). \]

The product term lets the treatment association with the outcome differ by baseline PHQ-9 category. Because both predictors are indicators (0/1), the product \(trt_i \times phq_i\) equals 1 only for therapist-guided participants with PHQ-9 >= 10, so \(\beta_3\) applies to that group alone. The treatment coefficient \(\beta_1\) is now the treatment log-odds difference in the reference category, PHQ-9 < 10; it is not an overall treatment effect.

mod_interaction <- glm(
  lsas_response ~ trt * phq_cat,
  family = binomial,
  data = df_binary
)

summary(mod_interaction)

Call:
glm(formula = lsas_response ~ trt * phq_cat, family = binomial, 
    data = df_binary)

Coefficients:
                                 Estimate Std. Error z value Pr(>|z|)    
(Intercept)                      -2.19722    0.60853  -3.611 0.000305 ***
trttherapist-guided               2.52265    0.70908   3.558 0.000374 ***
phq_cat>= 10                      0.07696    0.86233   0.089 0.928885    
trttherapist-guided:phq_cat>= 10 -0.14002    1.02617  -0.136 0.891466    
---
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.23  on 108  degrees of freedom
AIC: 120.23

Number of Fisher Scoring iterations: 4

We can exponentiate the coefficients to express them on the odds-ratio scale. In an interaction model, however, the treatment coefficient now represents the treatment effect when PHQ-9 is in its reference category (< 10). Thus, \(\exp(\beta_1)\) is the OR for therapist-guided treatment versus waitlist among participants with PHQ-9 < 10.

The interaction coefficient tells us how the treatment OR changes when moving from PHQ-9 < 10 to PHQ-9 >= 10. Here, \(\exp(\beta_3)\) is the ratio of the two treatment ORs. To obtain the treatment OR among participants with PHQ-9 >= 10, we therefore combine the treatment and interaction coefficients: \(\exp(\beta_1 + \beta_3)\).

b_interaction <- coef(mod_interaction)
b1 <- b_interaction[["trttherapist-guided"]]
b3 <- b_interaction[["trttherapist-guided:phq_cat>= 10"]]

exp(b1) # treatment OR when PHQ-9 < 10
[1] 12.46154
exp(b3) # ratio of treatment ORs
[1] 0.8693416
exp(b1 + b3) # treatment OR when PHQ-9 >= 10
[1] 10.83333

To go from the odds scale to the probability scale, we can again use functions from the marginaleffects package:

interaction_probabilities <- predictions(
  mod_interaction,
  newdata = datagrid(trt = unique, phq_cat = unique)
)

interaction_probabilities
trt phq_cat Estimate Pr(>|z|) 2.5 % 97.5 %
Type: invlink(link)
waitlist < 10 0.100 <0.001 0.0326 0.268
waitlist >= 10 0.107 <0.001 0.0350 0.284
therapist-guided < 10 0.581 0.371 0.4042 0.739
therapist-guided >= 10 0.565 0.533 0.3631 0.748
interaction_treatment_effects <- avg_comparisons(
  mod_interaction,
  variables = list(trt = c("waitlist", "therapist-guided")),
  by = "phq_cat",
  type = "response"
)

interaction_treatment_effects
phq_cat Estimate Std. Error z Pr(>|z|) 2.5 % 97.5 %
Term: trt
Comparison: therapist-guided - waitlist
Type: response
< 10 0.481 0.104 4.61 <0.001 0.276 0.685
>= 10 0.458 0.119 3.86 <0.001 0.225 0.691

Within the baseline PHQ-9 < 10 category, therapist-guided treatment has an estimated probability of treatment response that is higher by 48.1 percentage points than waitlist (95% CI 27.6 to 68.5 percentage points). Within the baseline PHQ-9 >= 10 category, the corresponding probability is higher by 45.8 percentage points (95% CI 22.5 to 69.1 percentage points).

The associated therapist-guided-versus-waitlist odds ratios are 12.46 (95% CI 3.10 to 50.02) for PHQ-9 < 10 and 10.83 (95% CI 2.53 to 46.36) for PHQ-9 >= 10.

interaction_probabilities |>
  ggplot(aes(x = trt, y = estimate, group = phq_cat, color = phq_cat)) +
  geom_point(position = position_dodge(width = 0.1)) +
  geom_line(position = position_dodge(width = 0.1)) +
  geom_errorbar(
    aes(ymin = conf.low, ymax = conf.high),
    width = 0.1,
    position = position_dodge(width = 0.1)
  ) +
  labs(
    title = "Interaction logistic model",
    subtitle = "Treatment probabilities may differ by baseline PHQ-9 category",
    x = "Treatment group",
    y = "Estimated probability of treatment response (95% CI)",
    color = "Baseline PHQ-9"
  ) +
  theme_minimal()

Interaction-model estimated probabilities of treatment response. Error bars are 95% confidence intervals.
plot_comparisons(
  mod_interaction,
  variables = list(trt = c("waitlist", "therapist-guided")),
  by = "phq_cat",
  type = "response"
) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  labs(
    title = "Conditional treatment effects on the probability scale",
    x = "Baseline PHQ-9 category",
    y = "Therapist-guided minus waitlist probability difference (95% CI)"
  ) +
  theme_minimal()

Conditional therapist-guided-minus-waitlist probability differences from the interaction model. Error bars are 95% confidence intervals.

4.1 Do the two probability differences differ?

It might be tempting to compare the two intervals in the plot by eye. But overlapping intervals, or one interval excluding zero while the other does not, do not tell us whether the two treatment effects differ (Gelman and Stern 2006). To answer that question, estimate the difference between the two probability differences directly. hypothesis = difference ~ reference subtracts the first row of the comparisons, the reference PHQ-9 category < 10, from each other row, here PHQ-9 >= 10.

interaction_rd_difference <- avg_comparisons(
  mod_interaction,
  variables = list(trt = c("waitlist", "therapist-guided")),
  by = "phq_cat",
  type = "response",
  hypothesis = difference ~ reference
)
interaction_rd_difference
Hypothesis Estimate Std. Error z Pr(>|z|) 2.5 % 97.5 %
Type: response
(>= 10) - (< 10) -0.0226 0.158 -0.143 0.886 -0.332 0.287
# Ratio of treatment ORs: PHQ-9 >= 10 divided by PHQ-9 < 10
interaction_or_ratio <- tidy(
  mod_interaction,
  conf.int = TRUE,
  exponentiate = TRUE
) |>
  filter(term == "trttherapist-guided:phq_cat>= 10")
interaction_or_ratio
term estimate std.error statistic p.value conf.low conf.high
trttherapist-guided:phq_cat>= 10 0.8693416 1.026166 -0.1364489 0.8914664 0.1110342 6.814128

The therapist-guided-minus-waitlist probability difference is 2.3 percentage points smaller among participants with baseline PHQ-9 >= 10 than among those with PHQ-9 < 10 (95% CI -33.2 to 28.7 percentage points).

This is the interaction on the probability scale: a difference between two probability differences. The interval is wide enough to include large differences in either direction, so these data cannot tell us whether the treatment effect differs between the PHQ-9 categories.

On the odds-ratio scale, the treatment OR for PHQ-9 >= 10 divided by the treatment OR for PHQ-9 < 10 is 0.87 (95% CI 0.11 to 6.81).

CautionInteraction tests need large samples

Effect modification is estimated from the smallest cells of the design.

interaction_cell_counts <- df_binary |>
  count(trt, phq_cat, lsas_response) |>
  pivot_wider(names_from = lsas_response, values_from = n)
interaction_cell_counts
trt phq_cat No response Response
waitlist < 10 27 3
waitlist >= 10 25 3
therapist-guided < 10 13 18
therapist-guided >= 10 10 13

In the waitlist arm, only 3 participants responded in the PHQ-9 < 10 category and 3 in the >= 10 category. Estimates based on so few events are unstable, and a cell with no events at all would leave the model unable to estimate its log-odds (a problem known as separation). More generally, an interaction needs far more data than a main effect of the same size: roughly four times the sample size in a two-arm trial with equally sized subgroups (Brookes et al. 2004). A trial powered for its overall treatment effect is therefore rarely powered to detect effect modification, and a non-significant interaction is weak evidence that none exists.

5 Interpreting the interaction

An interaction model can describe different conditional estimates, but it does not automatically establish a clinically meaningful subgroup. Before presenting a subgroup finding, ask:

  1. Were the moderator and the interaction planned before looking at the data?
  2. Are the conditional probability differences precise enough to distinguish meaningful patterns from sampling variation?
  3. Does the interaction plot show a consistent, clinically interpretable pattern rather than an isolated coefficient or p-value?
  4. Would the conclusion be different if PHQ-9 were modeled continuously? See logistic regression with a binary and numeric predictor.

The interaction coefficient is a difference of log-odds contrasts. It is not itself the difference between the two probability differences; that difference is the hypothesis estimate above. avg_comparisons() gives the quantities we want to communicate: therapist-guided-minus-waitlist probability differences within the explicitly stated PHQ-9 categories.

6 Summary

In this chapter you learned to:

  • derive phq_cat explicitly as baseline PHQ-9 < 10 versus >= 10;
  • fit additive and trt * phq_cat binary logistic models with glm(..., family = binomial);
  • report treatment contrasts as conditional probability differences in percentage points with 95% CIs;
  • distinguish a conditional odds ratio from a probability difference;
  • distinguish effect modification, causal interaction, and moderation, and name the scale on which effect modification is assessed;
  • estimate the difference between two conditional probability differences directly, and explain why interaction tests need large samples; and
  • use predicted-probability and conditional-effect plots to inspect a proposed subgroup pattern.

References

Baron, Reuben M., and David A. Kenny. 1986. “The Moderator-Mediator Variable Distinction in Social Psychological Research: Conceptual, Strategic, and Statistical Considerations.” Journal of Personality and Social Psychology 51 (6): 1173–82. https://doi.org/10.1037/0022-3514.51.6.1173.
Brookes, Sara T., Elise Whitely, Matthias Egger, George Davey Smith, Paul A. Mulheran, and Tim J. Peters. 2004. “Subgroup Analyses in Randomized Trials: Risks of Subgroup-Specific Analyses; Power and Sample Size for the Interaction Test.” Journal of Clinical Epidemiology 57 (3): 229–36. https://doi.org/10.1016/j.jclinepi.2003.08.009.
Gelman, Andrew, and Hal Stern. 2006. “The Difference Between ‘Significant’ and ‘Not Significant’ Is Not Itself Statistically Significant.” The American Statistician 60 (4): 328–31. https://doi.org/10.1198/000313006X152649.
Kraemer, Helena Chmura, G. Terence Wilson, Christopher G. Fairburn, and W. Stewart Agras. 2002. “Mediators and Moderators of Treatment Effects in Randomized Clinical Trials.” Archives of General Psychiatry 59 (10): 877–83. https://doi.org/10.1001/archpsyc.59.10.877.
VanderWeele, Tyler J. 2009. “On the Distinction Between Interaction and Effect Modification.” Epidemiology 20 (6): 863–71. https://doi.org/10.1097/EDE.0b013e3181ba333c.
VanderWeele, Tyler J., and Mirjam J. Knol. 2014. “A Tutorial on Interaction.” Epidemiologic Methods 3 (1): 33–72. https://doi.org/10.1515/em-2013-0005.