Why Compare Two Group Differences?
When two groups are compared across a second category, their difference may not be the same in each category. For example, a training program’s fitted outcome difference between participants and nonparticipants might differ between two age categories. The interaction is the change in that group difference from one category to the other.
A convenient way to express that change is to subtract one group difference from the other. This is called a difference-in-differences. The name describes the arithmetic: first take a difference between groups within each category, then take the difference between those two results. The calculation uses four fitted cell means, one for each combination of the two binary factors.
The contrast is a description of fitted outcomes. By itself, it does not show that one group’s status caused an outcome change. The interpretation must match how the data were collected and what the fitted model supports.
Define the Four Fitted Cell Means
Let factor \(A\) have levels 0 and 1, and factor \(B\) have levels 0 and 1. Let \(\widehat m_{ab}\) denote the fitted mean outcome for observations in the cell with \(A=a\) and \(B=b\). Thus, \(\widehat m_{10}\) is the fitted mean when \(A=1\) and \(B=0\). The first index always refers to \(A\), and the second to \(B\).
The order matters. This definition first compares \(A=1\) with \(A=0\), then compares the difference at \(B=1\) with the difference at \(B=0\). Reversing either comparison changes the sign. The size and direction of the result therefore make sense only when the comparison order is stated.
| \(B=0\) | \(B=1\) | |
|---|---|---|
| \(A=0\) | \(\widehat m_{00}\) | \(\widehat m_{01}\) |
| \(A=1\) | \(\widehat m_{10}\) | \(\widehat m_{11}\) |
Deriving the Four-Cell Contrast
Start with the difference between the two \(A\)-group fitted means at \(B=1\). Subtract the corresponding difference at \(B=0\). Distributing the minus sign gives an equivalent expression involving all four cells:
This identity is algebraic: it does not depend on a particular fitting procedure. It provides a useful check on a calculation. The two-difference form emphasizes the comparison across categories; the four-term form makes each cell’s sign explicit. In the stated order, the \(11\) and \(00\) means have positive signs, and the \(01\) and \(10\) means have negative signs.
Worked Example: Difference Between Two Program Gaps
Suppose a hypothetical analysis of a program and a second binary category gives fitted mean scores, in points, of \(\widehat m_{00}=62\), \(\widehat m_{10}=68\), \(\widehat m_{01}=70\), and \(\widehat m_{11}=81\). Calculate the difference-in-differences, comparing \(A=1\) with \(A=0\) at \(B=1\) and then at \(B=0\).
At \(B=1\), the fitted difference for \(A=1\) versus \(A=0\) is \(81-70=11\) points. At \(B=0\), that difference is \(68-62=6\) points. Therefore,
The four-cell calculation gives the same answer: \(81-70-68+62=5\) points. In this fitted model, the \(A=1\) versus \(A=0\) difference is 5 points larger at \(B=1\) than at \(B=0\). This is an interaction contrast, not evidence by itself that the program caused a score increase.
Connection to a Two-by-Two Regression Model
With zero-one coding for both factors, a two-by-two model with an interaction can be written as \(Y=\beta_0+\beta_A A+\beta_B B+\beta_{AB}AB+\varepsilon\). Here \(Y\) is the outcome, \(A\) and \(B\) are coded 0 or 1, and \(AB\) is their product. The model allows the fitted mean to differ in each of the four cells.
The model’s conditional mean is assumed to have this form, with residuals having mean zero within each cell. To interpret the model-based fitted means in the usual observational framework, observations must be appropriately represented by the model; standard regression inference also relies on its relevant sampling and error assumptions. The algebra below identifies the fitted contrast and does not require a test or an assumption about equal cell sizes.
Substitute each cell’s codes into the model. At \(A=0,B=0\), the product is 0 and the fitted mean is \(\widehat m_{00}=\widehat\beta_0\). At \(A=1,B=0\), it is \(\widehat m_{10}=\widehat\beta_0+\widehat\beta_A\). At \(A=0,B=1\), it is \(\widehat m_{01}=\widehat\beta_0+\widehat\beta_B\). At \(A=1,B=1\), it is \(\widehat m_{11}=\widehat\beta_0+\widehat\beta_A+\widehat\beta_B+\widehat\beta_{AB}\). Using these fitted cell means in the contrast gives:
The intercept and both main-effect terms cancel, leaving the product-term coefficient. Thus, under this particular zero-one coding, the interaction coefficient equals the difference-in-differences in fitted cell means. This equality depends on the stated coding and model. If factor coding or the comparison order changes, coefficient interpretation and signs must be checked rather than assumed.
Worked Example: Recovering the Interaction Coefficient
Suppose a hypothetical fitted regression model for an outcome measured in minutes is \(\widehat Y=20+3A+5B+4AB\), with both factors coded 0 or 1. Find the four fitted cell means and verify the difference-in-differences.
Substitute the cell codes into the equation. For \(A=0,B=0\), \(\widehat m_{00}=20\). For \(A=1,B=0\), \(\widehat m_{10}=20+3=23\). For \(A=0,B=1\), \(\widehat m_{01}=20+5=25\). For \(A=1,B=1\), \(\widehat m_{11}=20+3+5+4=32\). All fitted means are in minutes.
The group difference at \(B=1\) is \(32-25=7\) minutes. At \(B=0\), it is \(23-20=3\) minutes. Their difference is \(7-3=4\) minutes. Directly, \(32-25-23+20=4\) minutes, which equals the fitted interaction coefficient \(\widehat\beta_{AB}=4\).
The conclusion is that the fitted \(A=1\) versus \(A=0\) difference is 4 minutes greater at \(B=1\) than at \(B=0\). The individual fitted differences are 7 and 3 minutes; the interaction contrast is their difference, not either one alone.
Calculating From Fitted Means in Practice
A fitted cell mean is the model’s predicted average outcome for a specified combination of factor levels. In a two-by-two analysis, write the four fitted means in a table and label the rows and columns before calculating. This prevents a common indexing error, especially when the factor names are not naturally numbered 0 and 1.
In an ordinary least-squares model that gives each of the four cells its own mean and includes no additional predictors, the fitted mean in each cell equals that cell’s observed sample mean, provided each cell contains observations. This follows because the model fits a separate constant to each cell: the sum of squared residuals within a cell is minimized when that constant is the cell’s sample mean. If the model includes other predictors or constraints, fitted cell means may not equal the raw cell averages. The difference-in-differences formula still applies to the four fitted means being compared.
Worked Example: Computing From Four Fitted Cell Means
Suppose a hypothetical model gives fitted monthly costs, in dollars, for two service options (\(A=0\) and \(A=1\)) in two customer categories (\(B=0\) and \(B=1\)). The fitted means are \(\widehat m_{00}=\$120\), \(\widehat m_{10}=\$114\), \(\widehat m_{01}=\$150\), and \(\widehat m_{11}=\$138\). Calculate and interpret the difference-in-differences.
At \(B=1\), the \(A=1\) versus \(A=0\) difference is \(138-150=-\$12\) per month. At \(B=0\), it is \(114-120=-\$6\) per month. Thus,
The four-term version checks the result: \(138-150-114+120=-\$6\) per month. The fitted difference for the two service options is 6 dollars more negative at \(B=1\) than at \(B=0\). That is a statement about the fitted differences, not a conclusion about why the costs differ or whether the options caused those differences.
Common Mistakes and Why the Contrast Matters
- Subtracting the differences in the reverse order. If the specified comparison is the \(A\) difference at \(B=1\), minus the \(A\) difference at \(B=0\), calculate \((\widehat m_{11}-\widehat m_{01})-(\widehat m_{10}-\widehat m_{00})\). Reversing the outer subtraction changes the sign.
- Calling one group difference the interaction. A difference such as \(\widehat m_{11}-\widehat m_{01}\) is the \(A\)-group difference at \(B=1\). The interaction contrast compares it with the corresponding difference at \(B=0\).
- Assuming the contrast is automatically causal. The arithmetic describes how fitted group differences vary across categories. A causal interpretation requires an appropriate study design and assumptions; the contrast alone does not establish them.
- Reading the product coefficient without checking coding. The coefficient equals the stated difference-in-differences for the zero-one coding and ordering shown here. Always connect a coefficient to the cell means before interpreting its sign.
The contrast is useful because it summarizes how a group difference changes across a second category in one outcome-scale quantity. Calculating it from cell means keeps the comparison visible: readers can see both group differences and how much they differ from each other.
Check Your Understanding
Use the stated order of comparisons in each question.
- Write the difference-in-differences comparing the \(A=1\) versus \(A=0\) difference at \(B=1\) with that difference at \(B=0\), using the four fitted cell means.
- Suppose \(\widehat m_{00}=10\), \(\widehat m_{10}=13\), \(\widehat m_{01}=15\), and \(\widehat m_{11}=21\). Calculate both group differences and their difference-in-differences.
- For \(\widehat Y=8+2A-3B+5AB\), with \(A\) and \(B\) coded 0 or 1, calculate the four fitted cell means and verify the interaction contrast.
- If the difference-in-differences is \(-4\) points, state what that means about the two fitted group differences in the order defined here.
- Why does a difference-in-differences computed from fitted means not, by itself, show that a group or program caused the outcome difference?