Tutorials › Generalized Linear Models › Difference-in-Differences in a Two-by-Two Model

Factors Contrasts and Interactions · Tutorial 147 of 981

Difference-in-Differences in a Two-by-Two Model

See how a two-by-two interaction coefficient is formed from four cell predictors, and why its value need not equal a difference-in-differences on the response scale.

Intermediate 10 min read

What You'll Learn

  • Write the reference-coded linear predictor for two binary factors and identify its four cell values.
  • Derive the interaction coefficient as a difference between two factor contrasts.
  • Recover all four reference-coded coefficients from the cell linear predictors.
  • Calculate the interaction on the linear-predictor scale in identity-link and log-link examples.
  • Distinguish a logit-scale interaction from a difference-in-differences in probabilities.
  • Recognize why the same coefficient value can have different interpretations under different links.

Four Cells and One Interaction Contrast

A two-by-two model has two factors, each with two levels. For example, a study might compare two groups across two periods. An interaction asks whether the contrast between the levels of one factor changes across the levels of the other. In a generalized linear model, the interaction coefficient records that change on the linear-predictor scale.

Let \(A\) and \(B\) be binary indicators, each taking values 0 or 1. Use reference coding: \(A=0\) and \(B=0\) are the reference levels. The two-factor model with an interaction is

$$ \eta(A,B)=\beta_0+\beta_A A+\beta_B B+\beta_{AB}AB. $$

Here \(\eta\) is the linear predictor. In a GLM with link \(g\), the mean \(\mu(A,B)\) satisfies \(\eta(A,B)=g\{\mu(A,B)\}\). The four cell predictors are obtained by substituting the four possible pairs of indicator values.

$$ \begin{aligned} \eta_{00}&=\beta_0,\\ \eta_{10}&=\beta_0+\beta_A,\\ \eta_{01}&=\beta_0+\beta_B,\\ \eta_{11}&=\beta_0+\beta_A+\beta_B+\beta_{AB}. \end{aligned} $$

The subscripts give the values of \(A\) and \(B\), in that order. For instance, \(\eta_{10}\) is the predictor when \(A=1\) and \(B=0\). Keeping this ordering explicit helps prevent a common error: the interaction is not one cell predictor by itself.

Definition: In the reference-coded two-by-two model, the linear-predictor contrast for changing \(B\) from 0 to 1 at \(A=0\) is \(\eta_{01}-\eta_{00}\). The corresponding contrast at \(A=1\) is \(\eta_{11}-\eta_{10}\). Their difference, \[ (\eta_{11}-\eta_{10})-(\eta_{01}-\eta_{00}), \] is the difference-in-differences on the linear-predictor scale.

Deriving the Interaction Coefficient

The interaction coefficient equals this difference-in-differences. This follows directly from the four cell predictors; no assumption about a particular distribution or link is needed for the algebra.

Theorem: For the reference-coded model \(\eta(A,B)=\beta_0+\beta_A A+\beta_B B+\beta_{AB}AB\), \[ \beta_{AB}=(\eta_{11}-\eta_{10})-(\eta_{01}-\eta_{00}) =\eta_{11}-\eta_{10}-\eta_{01}+\eta_{00}. \]

Proof. At \(A=0\), changing \(B\) from 0 to 1 changes the predictor by \[ \eta_{01}-\eta_{00}=(\beta_0+\beta_B)-\beta_0=\beta_B. \] At \(A=1\), the same change gives \[ \eta_{11}-\eta_{10} =(\beta_0+\beta_A+\beta_B+\beta_{AB})-(\beta_0+\beta_A) =\beta_B+\beta_{AB}. \] Subtracting the first contrast from the second leaves \(\beta_{AB}\). Rearranging the contrast gives the stated four-cell formula. \(\square\)

The same result holds if the contrasts are taken in the other order. The contrast for changing \(A\) at \(B=0\) is \(\eta_{10}-\eta_{00}=\beta_A\), while at \(B=1\) it is \(\eta_{11}-\eta_{01}=\beta_A+\beta_{AB}\). The change in that contrast is again \(\beta_{AB}\).

Cell Predictors and Coefficients

The cell-predictor representation also gives a direct way to recover every coefficient. The reference cell determines the intercept; contrasts from that cell determine the two main-effect coefficients; the remaining difference determines the interaction.

Result: Given four cell predictors, the unique reference-coded coefficients are \[ \beta_0=\eta_{00},\qquad \beta_A=\eta_{10}-\eta_{00},\qquad \beta_B=\eta_{01}-\eta_{00},\qquad \beta_{AB}=\eta_{11}-\eta_{10}-\eta_{01}+\eta_{00}. \]

Derivation. The equation for \(\eta_{00}\) gives \(\beta_0=\eta_{00}\). Subtracting it from \(\eta_{10}=\beta_0+\beta_A\) gives \(\beta_A=\eta_{10}-\eta_{00}\); subtracting it from \(\eta_{01}=\beta_0+\beta_B\) gives \(\beta_B=\eta_{01}-\eta_{00}\). Finally, solving the equation for \(\eta_{11}\) gives \(\beta_{AB}=\eta_{11}-\beta_0-\beta_A-\beta_B\). Substituting the first three expressions yields the formula above. These expressions both recover the coefficients and verify that any four specified cell predictors determine exactly one set of reference-coded coefficients. \(\square\)

Worked Example: A Difference-in-Differences With an Identity Link

Suppose the four fitted means are 50 for \(A=0,B=0\), 54 for \(A=0,B=1\), 48 for \(A=1,B=0\), and 57 for \(A=1,B=1\). With an identity link, the linear predictor equals the mean, so these are also the four cell predictors.

The change from \(B=0\) to \(B=1\) at \(A=0\) is \(54-50=4\). At \(A=1\), it is \(57-48=9\). Their difference is \[ 9-4=5. \] Equivalently, the four-cell formula gives \[ 57-48-54+50=5. \] Thus \(\beta_{AB}=5\) in the identity-link model. For a check, the other coefficients are \(\beta_0=50\), \(\beta_A=48-50=-2\), and \(\beta_B=54-50=4\); the model predicts \(\eta_{11}=50-2+4+5=57\), as required.

What Changes Under a Non-Identity Link?

The four-cell identity remains true for the linear predictor, but the predictor may no longer equal the mean. For a log link, the contrast is a difference of log means, which can be written as a log ratio of ratios. For a logit link, it is a difference of log odds contrasts. In general, applying the inverse link to the cell predictors does not preserve differences, so a difference-in-differences of means or probabilities need not equal \(\beta_{AB}\).

Worked Example: A Log-Link Model for Rates

Suppose a Poisson GLM uses a log link and has fitted rates \(\mu_{00}=2\), \(\mu_{01}=3\), \(\mu_{10}=4\), and \(\mu_{11}=5\). The cell predictors are their logarithms: \(\eta_{ab}=\log(\mu_{ab})\).

The contrast for changing \(B\) at \(A=0\) is \[ \log(3)-\log(2)=\log(3/2). \] At \(A=1\), it is \[ \log(5)-\log(4)=\log(5/4). \] Therefore the interaction coefficient is \[ \beta_{AB}=\log(5/4)-\log(3/2) =\log\left(\frac{5/4}{3/2}\right) =\log(5/6)\approx -0.1823. \] The four-cell formula gives the same check: \[ \log(5)-\log(4)-\log(3)+\log(2)=\log(10/12)=\log(5/6). \] Here the interaction is a difference between log-rate contrasts, equivalently the logarithm of a ratio of rate ratios. It is not the raw rate difference-in-differences: \((5-4)-(3-2)=0\).

Worked Example: A Logit-Scale Interaction Is Not a Probability Difference-in-Differences

Consider a logistic regression with \(\beta_0=-2\), \(\beta_A=0.7\), \(\beta_B=0.4\), and \(\beta_{AB}=-0.6\). Its four linear predictors are \[ \eta_{00}=-2,\quad \eta_{10}=-1.3,\quad \eta_{01}=-1.6,\quad \eta_{11}=-1.5. \] The change in log odds for \(B\) is \(-1.6-(-2)=0.4\) at \(A=0\), and \(-1.5-(-1.3)=-0.2\) at \(A=1\). Their difference is \(-0.2-0.4=-0.6=\beta_{AB}\).

To compare with the probability scale, use the inverse-logit function \(\operatorname{expit}(x)=1/(1+e^{-x})\). The four fitted probabilities are approximately \[ \operatorname{expit}(-2)=0.1192,\quad \operatorname{expit}(-1.6)=0.1680,\quad \operatorname{expit}(-1.3)=0.2142,\quad \operatorname{expit}(-1.5)=0.1824. \] The probability contrast for changing \(B\) at \(A=0\) is \[ \operatorname{expit}(-1.6)-\operatorname{expit}(-2) \approx 0.1680-0.1192=0.0488. \] At \(A=1\), it is \[ \operatorname{expit}(-1.5)-\operatorname{expit}(-1.3) \approx -0.0317. \] Using the unrounded probabilities, the probability-scale difference-in-differences is \[ [\operatorname{expit}(-1.5)-\operatorname{expit}(-1.3)] - [\operatorname{expit}(-1.6)-\operatorname{expit}(-2)] \approx -0.0805. \] This is not \(-0.6\). The coefficient is the difference-in-differences in log odds; the probability-scale contrasts are obtained after applying the nonlinear inverse-logit function.

Common Pitfall and Practical Meaning

A frequent mistake is to calculate a difference-in-differences from response-scale values and call it the interaction coefficient in any GLM. That identification is correct for an identity-link model, but not generally for nonlinear links. In the logistic example, the response-scale quantity is about \(-0.0805\), whereas the interaction coefficient is \(-0.6\). They answer related but distinct questions on different scales.

A second check is to keep the order of subtraction consistent. The formula here is “the \(B\) contrast at \(A=1\), minus the \(B\) contrast at \(A=0\).” Reversing the order changes the sign. Whichever factor is treated as the first contrast, the coefficient is the change in that contrast across the other factor, provided the subtraction order is stated clearly.

The algebra describes a model parameter; by itself it does not establish a causal effect. A causal interpretation of a difference-in-differences requires a study design and assumptions beyond this two-by-two coefficient calculation. For a GLM, always name the scale of the contrast, identify the four cells, and apply the stated link before interpreting the coefficient.

Key takeaway: In a reference-coded two-by-two model, \(\beta_{AB}=\eta_{11}-\eta_{10}-\eta_{01}+\eta_{00}\). It is the difference-in-differences of the four cell values on the linear-predictor scale; only with an identity link is that automatically the corresponding difference-in-differences of means.

Check Your Understanding

For each question, state the scale of the contrast explicitly.

  1. Write the four cell predictors for the reference-coded two-by-two model and derive the expression for \(\beta_{AB}\).
  2. If \(\eta_{00}=1\), \(\eta_{10}=3\), \(\eta_{01}=2\), and \(\eta_{11}=5\), calculate \(\beta_A\), \(\beta_B\), and \(\beta_{AB}\).
  3. Under a log link, what ratio of mean ratios is represented by exponentiating \(\beta_{AB}\)?
  4. Why does a logistic-regression interaction coefficient generally differ from a probability-scale difference-in-differences?
  5. What changes if the order of the two contrasts in the difference-in-differences is reversed?
Clinical Biostats