Why Compare Differences Across Two Factors?
When a generalized linear model includes two factors and their interaction, the effect of one factor can depend on the level of the other. A useful way to quantify that dependence is to compare the effect of the first factor at each level of the second. The difference between those two effects is a difference-in-differences (often abbreviated DiD).
Here the comparison is made on the model’s link scale. That qualification matters: a contrast on the link scale is not generally the same as a difference between response means or probabilities. This tutorial derives the two-by-two contrast, shows how it equals the interaction coefficient, and interprets it for logistic and log-link models. These interpretations are aligned with the SOA Exam PA (Predictive Analytics) syllabus.
The Two-by-Two Model and Its Contrast
Use treatment coding, with \(A=0\) and \(B=0\) as the reference levels. For a two-by-two factor model with an interaction, write the linear predictor as
Here \(\beta_0\) is the linear predictor at the reference cell, \(\beta_A\) is the change for \(A\) when \(B=0\), \(\beta_B\) is the change for \(B\) when \(A=0\), and \(\beta_{AB}\) is the interaction coefficient. This parameterization describes the four cell predictors as follows:
The contrast follows by subtracting the \(A\)-effect at \(B=0\) from the \(A\)-effect at \(B=1\). Substitution gives
This derivation explains why the interaction coefficient is the difference-in-differences on the link scale. The effect of changing \(A\) from 0 to 1 is \(\beta_A\) when \(B=0\), and \(\beta_A+\beta_{AB}\) when \(B=1\). Their difference is exactly \(\beta_{AB}\). The conclusion assumes this specified treatment-coded model; with another coding scheme, the coefficient’s interpretation may differ, although the cell-based contrast can still be calculated directly.
Worked Examples
Worked Example: Logistic Interaction and Odds
A hypothetical insurer models whether a submitted device claim is approved. Let \(A=1\) indicate that the file contains a particular documentation feature and \(B=1\) indicate that the claim is from a particular review channel. A logistic GLM has \(\operatorname{logit}(p_{ab})=\beta_0+\beta_Aa+\beta_Bb+\beta_{AB}ab\), with \(\beta_0=-2.0\), \(\beta_A=0.5\), \(\beta_B=0.4\), and \(\beta_{AB}=0.6\). Calculate the link-scale DiD, the corresponding odds-ratio comparison, and the four fitted probabilities. The model assumes the conditional log odds follow the stated linear predictor; the hypothetical coefficients do not establish a causal effect.
Method. First calculate all four linear predictors, then form the contrast. The logistic link is \(g(p)=\log[p/(1-p)]\), so exponentiating a log-odds contrast gives the corresponding odds ratio.
Cell predictors. The four values are \(\eta_{00}=-2.0\), \(\eta_{10}=-2.0+0.5=-1.5\), \(\eta_{01}=-2.0+0.4=-1.6\), and \(\eta_{11}=-2.0+0.5+0.4+0.6=-0.5\).
Link-scale DiD. The change in log odds for \(A\) when \(B=1\) is \(\eta_{11}-\eta_{01}=-0.5-(-1.6)=1.1\). The change when \(B=0\) is \(\eta_{10}-\eta_{00}=-1.5-(-2.0)=0.5\). Therefore the difference is \(1.1-0.5=0.6\), which agrees with \(\beta_{AB}\).
Odds-ratio comparison. The odds ratio for changing \(A\) at \(B=0\) is \(\exp(0.5)=1.6487\). At \(B=1\), it is \(\exp(1.1)=3.0042\). Their ratio is \(\exp(1.1)/\exp(0.5)=\exp(0.6)=1.8221\). Thus the \(A\)-associated odds multiplier is 1.8221 times as large at \(B=1\) as at \(B=0\).
Fitted probabilities. Convert each predictor using \(p=\exp(\eta)/[1+\exp(\eta)]\). The results are \(p_{00}=0.1192\), \(p_{10}=0.1824\), \(p_{01}=0.1680\), and \(p_{11}=0.3775\), rounded to four decimal places.
Check and common mistake. The cell contrast is \([(-0.5)-(-1.6)]-[(-1.5)-(-2.0)]=1.1-0.5=0.6\), as required. A common mistake is to call the difference in probability changes the interaction coefficient: those changes are \(0.1824-0.1192=0.0632\) and \(0.3775-0.1680=0.2095\), whose difference is \(0.1463\), not \(0.6\). The coefficient is the DiD on the logit scale, not on the probability scale.
Worked Example: Log-Link Interaction and Rate Ratios
Suppose a hypothetical Poisson GLM models event counts over equal exposure periods for four combinations of two binary rating features. Assume independent counts with conditional means specified by a log link, \(\log(\mu_{ab})=\beta_0+\beta_Aa+\beta_Bb+\beta_{AB}ab\). Let \(\beta_0=\log(2)\), \(\beta_A=\log(1.5)\), \(\beta_B=\log(2)\), and \(\beta_{AB}=\log(1.2)\). Find the four fitted mean counts and interpret the interaction.
Method. Calculate each log mean from the model and exponentiate. Under a log link, a change in the linear predictor corresponds to a multiplicative change in the mean.
Cell means. At \((0,0)\), \(\mu_{00}=\exp(\log 2)=2\). At \((1,0)\), \(\eta_{10}=\log 2+\log 1.5=\log 3\), so \(\mu_{10}=3\). At \((0,1)\), \(\eta_{01}=\log 2+\log 2=\log 4\), so \(\mu_{01}=4\). At \((1,1)\), \(\eta_{11}=\log 2+\log 1.5+\log 2+\log 1.2=\log(7.2)\), so \(\mu_{11}=7.2\).
Difference-in-differences. The \(A\)-associated rate ratio at \(B=0\) is \(\mu_{10}/\mu_{00}=3/2=1.5\). At \(B=1\), it is \(\mu_{11}/\mu_{01}=7.2/4=1.8\). Their ratio is \(1.8/1.5=1.2\), and the link-scale contrast is \(\log(1.8)-\log(1.5)=\log(1.2)=\beta_{AB}\).
Check and common mistake. The fitted combined mean is also \(2\times1.5\times2\times1.2=7.2\), agreeing with the cell calculation. It would be incorrect to describe \(\beta_{AB}=\log(1.2)\) as an added 1.2 events. It is a log rate-ratio contrast; exponentiating it gives the 1.2 ratio of rate ratios.
Worked Example: Recovering an Interaction From Fitted Means
A hypothetical portfolio analysis uses a Gamma GLM with a log link for positive claim costs. Suppose the fitted conditional mean costs, in dollars, are \(\mu_{00}=500\), \(\mu_{10}=600\), \(\mu_{01}=750\), and \(\mu_{11}=1{,}050\). Find the link-scale DiD, the interaction multiplier, and the combined mean predicted if the interaction were zero. Assume these fitted means refer to comparable covariate settings and that the log-link mean model applies.
Method. For a log link, \(\eta_{ab}=\log(\mu_{ab})\). Use the cell contrast, then exponentiate it to obtain the ratio of the two \(A\)-associated mean ratios.
Calculate the two effects. At \(B=0\), the mean ratio for changing \(A\) is \(600/500=1.2\), so its link-scale effect is \(\log(1.2)\). At \(B=1\), the mean ratio is \(1{,}050/750=1.4\), so its link-scale effect is \(\log(1.4)\).
Contrast and interpretation. The DiD is \(\log(1.4)-\log(1.2)=\log(1.4/1.2)=\log(7/6)\), approximately \(0.1542\). Exponentiating gives \(7/6\), or approximately \(1.1667\). The mean ratio associated with \(A\) is therefore \(1.1667\) times as large when \(B=1\) as when \(B=0\).
Zero-interaction check. If the interaction were zero, the combined mean under the log-link model would be \(\mu_{10}\mu_{01}/\mu_{00}=(600)(750)/500=900\). The observed fitted mean, \(1{,}050\), exceeds that value. Equivalently, the separate response-scale increments are \(600-500=100\) and \(1{,}050-750=300\), a difference of \(200\) dollars. That \(200\)-dollar response-scale contrast is not the interaction coefficient; the coefficient is approximately \(0.1542\) on the log scale. The zero-interaction calculation and the fitted cell means are consistent because \(900\times(7/6)=1{,}050\).
What the Contrast Does and Does Not Say
The link function determines the response-scale meaning of the contrast. If \(g\) is the link and \(\mu_{ab}\) is the conditional mean, then \(\eta_{ab}=g(\mu_{ab})\). The DiD compares changes in \(g(\mu)\), not necessarily changes in \(\mu\). For common links:
| Model link | Link-scale DiD | Exponentiated contrast |
|---|---|---|
| Logit, \(g(p)=\log[p/(1-p)]\) | Difference between two changes in log odds | Ratio of odds ratios |
| Log, \(g(\mu)=\log(\mu)\) | Difference between two changes in log means | Ratio of mean ratios |
| Identity, \(g(\mu)=\mu\) | Difference between two changes in means | No exponentiation is needed |
A zero interaction means the effects are additive on the model’s link scale. With a log link, it means the two rate ratios are equal; with a logit link, it means the two odds ratios are equal. It does not generally mean the response-scale effect is constant. For a nonlinear link, the same link-scale change can correspond to different changes in means or probabilities at different starting values.
Also distinguish the statistical contrast from a causal claim. A fitted interaction describes how modeled conditional outcomes differ across the four factor combinations. By itself, it does not show that changing either factor causes the outcome to change. Causal interpretation would require an appropriate study design and additional assumptions.
Common Exam Traps
- Subtracting in the wrong order. State which effect is being compared with which. Reversing the order changes the sign: \((\eta_{10}-\eta_{00})-(\eta_{11}-\eta_{01})=-\beta_{AB}\).
- Using response values in place of link values. For a logistic model, use log odds for this coefficient contrast, not probability differences. For a log-link model, use log means, not raw mean increments.
- Exponentiating too early. First calculate the link-scale contrast. Exponentiation then gives a ratio of odds ratios or mean ratios when the link is logit or log.
- Overstating what an interaction means. An interaction describes dependence between effects on the specified model scale. It is not automatically evidence of causation or of an interaction on every other scale.
A reliable calculation is to write the four fitted linear predictors, calculate the two simple effects on the link scale, and subtract them in a stated order. This approach works whether the cell values are supplied directly or must be obtained from model coefficients.
Check Your Understanding
For each problem, identify the scale of the requested contrast before calculating.
- A treatment-coded log-link model has \(\beta_A=0.25\) and \(\beta_{AB}=-0.30\). Find the effect of \(A\) on the link scale at \(B=0\) and \(B=1\), then give the ratio of the corresponding mean ratios. (Answer: link effects \(0.25\) and \(-0.05\); ratio of mean ratios \(\exp(-0.30)=0.7408\).)
- A logistic model has \(\beta_{AB}=0.40\). State the link-scale DiD and the ratio of odds ratios, and explain what a value above 1 means. (Answer: DiD \(0.40\); ratio of odds ratios \(\exp(0.40)=1.4918\), so the \(A\)-associated odds ratio is 1.4918 times as large at \(B=1\) as at \(B=0\).)
- For a log-link model, fitted means are \(\mu_{00}=10\), \(\mu_{10}=15\), \(\mu_{01}=20\), and \(\mu_{11}=36\). Calculate the DiD and exponentiated contrast. (Answer: \(\log(36/20)-\log(15/10)=\log(1.2)\approx0.1823\); exponentiated contrast \(1.2\).)
- A model has cell predictors \(\eta_{00}=-1.2\), \(\eta_{10}=-0.7\), \(\eta_{01}=-0.9\), and \(\eta_{11}=-0.1\). Calculate the DiD using the defined order. (Answer: \([(-0.1)-(-0.9)]-[(-0.7)-(-1.2)]=0.8-0.5=0.3\).)
- In a log-link model with no interaction, fitted means are \(\mu_{00}=8\), \(\mu_{10}=12\), and \(\mu_{01}=10\). Find the combined mean implied for \((A,B)=(1,1)\), and name the assumption used. (Answer: \(12\times10/8=15\); the assumption is zero interaction on the log scale, so the two mean ratios multiply.)