Why Two Reference Levels Matter
When a model includes two categorical predictors and their interaction, interpreting a coefficient requires knowing the reference level of each predictor. Together, those two reference levels identify one baseline cell: the combination of categories represented by the intercept. Main-effect coefficients compare cells that differ from this baseline along one factor, while an interaction coefficient describes how a difference along one factor changes across levels of the other.
The same fitted cell means can be represented with different reference levels and different coefficients. The predictions do not change; the comparisons attached to the coefficients do. This distinction is useful when explaining a model to a business audience and when checking whether a reported coefficient answers the intended question.
The Two-Factor Treatment-Coded Model
First consider two factors with two levels each: \(A\) has reference level \(a_0\) and alternative level \(a_1\); \(B\) has reference level \(b_0\) and alternative level \(b_1\). Define \(I_A=1\) when \(A=a_1\), and \(I_B=1\) when \(B=b_1\); otherwise each indicator is zero. The treatment-coded linear model is
This is an identity-link model for a numerical response. The fitted means and coefficient interpretations below follow from the model’s coding. For ordinary least-squares inference, the usual assumptions concern the errors: conditional mean zero, independent observations, and constant error variance for the standard variance estimates. Those assumptions matter for inference, not for the algebra that translates coefficients into fitted means. With a different link function, the same coding applies to the linear predictor, but coefficients generally describe changes on the link scale rather than direct differences in response means.
The four fitted means follow by substituting the indicator values:
Thus, \(\beta_A\) is the \(A\)-level difference when \(B=b_0\), and \(\beta_B\) is the \(B\)-level difference when \(A=a_0\). Neither main effect is automatically an overall or average difference across all levels of the other factor.
Why the Interaction Is a Difference in Differences
The interaction coefficient can be derived directly from the four cell means. At \(B=b_0\), the difference between the \(A\) levels is \(\widehat{\mu}_{a_1b_0}-\widehat{\mu}_{a_0b_0}=\beta_A\). At \(B=b_1\), the corresponding difference is \(\widehat{\mu}_{a_1b_1}-\widehat{\mu}_{a_0b_1}=\beta_A+\beta_{AB}\). Subtracting the first difference from the second cancels \(\beta_A\), leaving the interaction:
This difference-in-differences form is a useful check: if the effect of moving from \(a_0\) to \(a_1\) is the same at both levels of \(B\), the interaction is zero. For factors with more than two levels, each non-reference level has its own indicator; each interaction coefficient compares the relevant pair of level changes with the baseline cell.
Worked Example: Finding the Baseline and Interaction
A hypothetical service team models mean claim-handling cost, in dollars, by office region and service tier. The reference levels are East and Standard. The four fitted cell means are East/Standard: $80, West/Standard: $86, East/Premium: $75, and West/Premium: $79. Find the intercept, both main effects, and the interaction coefficient. Then check the Premium cell for West.
Method. Use East/Standard as the baseline, then calculate each coefficient as the cell difference implied by the treatment-coded model.
Intercept. The baseline fitted mean is $80, so \(\beta_0=80\).
West main effect. Compare West with East while Standard is held fixed: \(86-80=6\). Therefore, \(\beta_{\text{West}}=6\).
Premium main effect. Compare Premium with Standard while East is held fixed: \(75-80=-5\). Therefore, \(\beta_{\text{Premium}}=-5\).
Interaction. The West-minus-East difference is \(86-80=6\) under Standard and \(79-75=4\) under Premium. The change in that difference is \(4-6=-2\), so \(\beta_{\text{West:Premium}}=-2\). Equivalently, \(79-75-86+80=-2\).
Check the fitted West/Premium cell. The model gives \(80+6-5-2=79\), matching the supplied fitted mean. The method avoids treating the difference between the West/Premium and baseline cells, \(79-80=-1\), as the interaction; that difference combines both main effects and the interaction.
More Than Two Levels
If a factor has several levels, treatment coding creates one indicator for each non-reference level. In a two-factor model, every combination of non-reference indicators also has an interaction term. The intercept remains the baseline cell mean. A main effect for a particular level is the difference from the reference level of that factor when the other factor is at its reference level. An interaction coefficient is the additional adjustment for the joint combination beyond adding those two baseline comparisons.
Worked Example: Interpreting Several Interaction Coefficients
For a hypothetical predictive model of average processing cost, let Segment A be the reference level for segment and Standard the reference level for processing method. The fitted cell means are: A/Standard $40, B/Standard $46, C/Standard $35, A/Assisted $34, B/Assisted $37, and C/Assisted $31. Calculate the baseline, main effects, and interactions, then reconstruct the two Assisted means that are not baseline cells.
Baseline and main effects. The baseline is A/Standard, so \(\beta_0=40\). The Segment B effect is \(46-40=6\); the Segment C effect is \(35-40=-5\). The Assisted main effect is \(34-40=-6\). Each comparison changes just one factor from the baseline.
Segment B interaction. Under Standard, B minus A is \(46-40=6\). Under Assisted, B minus A is \(37-34=3\). The interaction is \(3-6=-3\). Directly, the same value is \(37-34-46+40=-3\).
Segment C interaction. Under Standard, C minus A is \(35-40=-5\). Under Assisted, C minus A is \(31-34=-3\). The interaction is \(-3-(-5)=2\). Directly, \(31-34-35+40=2\).
Reconstruct and check. For B/Assisted, the fitted mean is \(40+6-6-3=37\), as supplied. For C/Assisted, it is \(40-5-6+2=31\), also as supplied. The two interaction coefficients differ because the B-versus-A and C-versus-A contrasts change by different amounts across methods.
Changing the Reference Levels
Changing either reference level changes what the intercept and main effects compare. It does not change the fitted values for the cells when the model is refit with the same interaction structure. In a two-by-two table, changing just one reference factor reverses the sign of the corresponding interaction coefficient; changing both reference factors leaves its sign unchanged. These statements follow from the difference-in-differences expression: reversing one contrast reverses the expression’s sign, while reversing both contrasts preserves it.
Worked Example: Re-expressing the Same Cell Means
Use the following hypothetical fitted mean costs, in dollars: North/Basic $100, South/Basic $112, North/Plus $94, and South/Plus $101. First use North and Basic as the references; then change both references to South and Plus. Give the coefficients under each coding and verify one cell prediction after the change.
North/Basic references. The intercept is \(100\). South’s main effect is \(112-100=12\), and Plus’s main effect is \(94-100=-6\). The South-by-Plus interaction is \((101-94)-(112-100)=7-12=-5\). Direct check: \(101-94-112+100=-5\).
South/Plus references. The intercept is now the South/Plus mean, \(101\). North’s main effect at Plus is \(94-101=-7\). Basic’s main effect at South is \(112-101=11\). The North-by-Basic interaction is \(100-112-94+101=-5\).
Prediction check. Under the new coding, North/Basic is \(101-7+11-5=100\), matching the original cell mean. The coefficients have changed their comparison meanings, but the predicted cell mean has not. The interaction remains \(-5\) because both reference contrasts were reversed.
Using Reference Levels in R
In R, a formula such as lm(y ~ A * B, data = d) includes the two main effects and their interaction. For unordered factors using treatment contrasts, the factor reference levels determine the baseline cell. Set them deliberately before fitting, for example with relevel(A, ref = "East") and relevel(B, ref = "Standard"). The exact level names must match the data.
When interpreting output, first confirm the factor levels and their references, then identify the baseline combination. Do not infer the baseline from the order in which categories appear in a report or plot. The model’s design matrix determines the coding; a changed reference can alter coefficient labels and values without changing the fitted cell predictions.
Write the baseline cell explicitly as the chosen level of \(A\) combined with the chosen level of \(B\).
The intercept is the baseline fitted mean; each main effect changes one factor while holding the other at its reference.
Compare a factor difference across the other factor’s levels, or calculate the equivalent difference in differences.
Add the intercept, applicable main effects, and the applicable interaction coefficient; compare the result with the fitted cell value.
Common Exam Trap
A frequent error is to call the difference between a non-baseline cell and the baseline cell an interaction. That is a total cell difference: it includes both main effects and the interaction. For the first worked example, West/Premium minus East/Standard is \(79-80=-1\). The interaction is not \(-1\); it is \(-2\), found by comparing the West-versus-East difference across Premium and Standard: \((79-75)-(86-80)=4-6=-2\).
Another error is to interpret a main effect as applying uniformly across the other factor. Here, the West effect is \(6\) under Standard but \(4\) under Premium. The coefficient \(6\) is specifically the West effect at the Standard reference level; the interaction supplies the adjustment at Premium. State the reference levels when describing a main effect, and verify an interaction with the relevant four cells.
Check Your Understanding
For each problem, use the stated references and show which cell comparisons determine the coefficients.
- A two-factor model has reference levels A and X. Its fitted means are A/X = 50, B/X = 58, A/Y = 47, and B/Y = 53. Find the intercept, B main effect, Y main effect, and B-by-Y interaction. (Answer: intercept \(=50\), B \(=8\), Y \(=-3\), interaction \(=-2\).)
- With references Lake and Basic, a model’s fitted means are Lake/Basic \(=120\), Ridge/Basic \(=126\), Lake/Enhanced \(=111\), and Ridge/Enhanced \(=120\). Find the two main effects and interaction, then check the Ridge/Enhanced fitted mean. (Answer: Ridge \(=6\), Enhanced \(=-9\), interaction \(=3\); check: \(120+6-9+3=120\).)
- Factor A has reference level Red and Factor B has reference level Day. Cell means are Red/Day \(=30\), Blue/Day \(=35\), Red/Night \(=34\), and Blue/Night \(=41\). Find the interaction and state whether the Blue effect is the same by day and night. (Answer: interaction \(=(41-34)-(35-30)=2\); Blue effect is \(5\) by day and \(7\) at night, so it is not the same.)
- For references East and Standard, the fitted means are East/Standard \(=70\), West/Standard \(=76\), East/Premium \(=68\), and West/Premium \(=72\). Find the interaction, then explain why West/Premium minus East/Standard is not the interaction. (Answer: interaction \(=(72-68)-(76-70)= -2\); the baseline-cell difference is \(72-70=2\) and combines effects.)
- A two-by-two model has an interaction of \(-4\) when the references are North and Basic. What is the interaction coefficient’s sign if only the North/South factor reference is switched to South, with the same fitted cell means and model structure? (Answer: \(+4\), because reversing exactly one of the two contrasts reverses the difference-in-differences sign.)