Tutorials › Regression Analysis (Undergraduate) › Interactions With More Than Two Levels in Both Factors

Interactions Among Groups · Tutorial 357 of 991

Interactions With More Than Two Levels in Both Factors

Use fitted cell means and contrasts to describe how the association between one categorical factor and an outcome varies across levels of another factor.

Intermediate 10 min read

What You'll Learn

  • Represent two multi-level categorical factors with cell means and an interaction model
  • Translate reference-level coefficients into fitted means for each factor combination
  • Calculate difference-in-differences contrasts from cell means
  • Identify the condition for no interaction across all factor levels
  • Distinguish conditional effects from pooled comparisons

Why Cell Means Matter

When two categorical factors are related to an outcome, the difference between levels of one factor may depend on the level of the other. For example, a training method might be associated with different test scores across experience groups. A single overall difference between methods would hide that variation. Fitted cell means make the outcome for each combination of factor levels explicit, and contrasts between those means describe how group differences change across the other factor.

This tutorial considers two factors, each with more than two levels. The basic idea extends the two-by-two difference-in-differences calculation: compare a difference between levels of one factor in one stratum with the corresponding difference in another stratum. With multiple levels, there are several such comparisons, and the collection of them describes the interaction.

The Model for Two Categorical Factors

Let factor \(A\) have \(a\) levels, indexed by \(i=1,\ldots,a\), and factor \(B\) have \(b\) levels, indexed by \(j=1,\ldots,b\). Let \(Y_{ijk}\) be the outcome for observation \(k\) in the cell formed by levels \(i\) and \(j\). The cell mean \(\mu_{ij}\) is the mean outcome for that combination of factor levels.

Definition: A two-factor cell-means model is \(Y_{ijk}=\mu_{ij}+\varepsilon_{ijk}\), where \(\mu_{ij}\) is the mean outcome in cell \((i,j)\) and \(\varepsilon_{ijk}\) is the observation's deviation from that cell mean. The model allows a separate mean for every combination of factor levels.

For the usual least-squares analysis, the model assumes that observations are independent, that each cell has a well-defined mean, and that the errors have mean zero and a common variance across cells. For procedures that use a normal-theory distribution, the errors are also assumed to be normally distributed. The fitted cell means themselves do not require that additional normality assumption. In practice, independence comes from the study design; residual plots and knowledge of the data-collection process help assess the other assumptions.

If each combination of levels is observed, least squares fits each cell mean by the sample mean in that cell. In particular, if cell \((i,j)\) contains \(n_{ij}\) observations, then \(\hat{\mu}_{ij}=\bar{Y}_{ij}\), the average of those observations. The cell-means model is saturated for these cells: it does not impose a pattern such as equal differences or a straight-line trend across the factor levels.

Reference-Level Coefficients and Fitted Means

The cell-means representation is direct, but regression software often writes the same model using reference levels. Take level 1 of each factor as the reference. Let \(\alpha_i\) describe the difference between level \(i\) of \(A\) and its reference when \(B\) is at its reference; let \(\beta_j\) describe the corresponding difference for \(B\); and let \(\gamma_{ij}\) describe the additional departure for the combination of nonreference levels.

$$ \mu_{ij}=\mu_{11}+\alpha_i+\beta_j+\gamma_{ij} $$

The reference-level terms are set to zero: \(\alpha_1=0\), \(\beta_1=0\), \(\gamma_{i1}=0\), and \(\gamma_{1j}=0\). Thus \(\mu_{11}\) is the reference-cell mean, and the equation reconstructs every other cell mean. A main-effect coefficient such as \(\alpha_i\) is therefore a conditional difference at the reference level of \(B\), not necessarily an overall difference averaged across all levels of \(B\).

The interaction term can be written as a difference-in-differences. For any two levels \(i,i'\) of \(A\) and two levels \(j,j'\) of \(B\), define the rectangular contrast

$$ C=(\mu_{ij}-\mu_{i'j})-(\mu_{ij'}-\mu_{i'j'}) $$

It compares the difference between \(i\) and \(i'\) at level \(j\) with that same difference at level \(j'\). If the result is zero, that particular pairwise difference is the same in those two strata. If it is nonzero, the difference changes across the two levels.

Theorem: There is no interaction between the two factors if and only if every rectangular contrast \(C\) is zero. Equivalently, all cell means can be written as \(\mu_{ij}=\mu+\alpha_i+\beta_j\), with no additional interaction term.

To justify the equivalence, first suppose the means are additive, so \(\mu_{ij}=\mu+\alpha_i+\beta_j\). Substitution into the contrast gives

$$ [(\mu+\alpha_i+\beta_j)-(\mu+\alpha_{i'}+\beta_j)] - [(\mu+\alpha_i+\beta_{j'})-(\mu+\alpha_{i'}+\beta_{j'})]=0. $$

Conversely, suppose every rectangular contrast is zero. Choose reference levels 1 and 1, and compare cell \((i,j)\) to the reference row and column. The zero contrast condition gives \(\mu_{ij}-\mu_{1j}=\mu_{i1}-\mu_{11}\). Rearranging,

$$ \mu_{ij}=\mu_{i1}+\mu_{1j}-\mu_{11} =\mu_{11}+(\mu_{i1}-\mu_{11})+(\mu_{1j}-\mu_{11}). $$

This is an additive expression with no interaction. Consequently, checking the reference-level interactions \(\gamma_{ij}\) for all nonreference pairs is enough: there are \((a-1)(b-1)\) such terms, and all are zero exactly when the model is additive.

Worked Example: A Three-Level Factor Across Two Clinics

Suppose a hypothetical program compares three approaches to reducing systolic blood pressure at two clinics. The outcome is reduction in mmHg, so larger values indicate greater reductions. The fitted cell means are as follows.

ApproachClinic 1Clinic 2
A8 mmHg6 mmHg
B10 mmHg8 mmHg
C7 mmHg5 mmHg

Assume independent observations within the six approach-by-clinic cells, a common error variance, and a mean-zero error in each cell. These are assumptions of the model, not facts proven by the table. If the data came from independent participants and the within-cell residual spreads were reasonably similar, those features would support the assumptions; the means alone cannot assess the residual spreads.

Take approach A and Clinic 1 as reference. The reference mean is \(\hat{\mu}_{11}=8\) mmHg. At Clinic 1, the fitted difference for B versus A is \(10-8=2\) mmHg, so \(\hat{\alpha}_B=2\) mmHg. For C versus A it is \(7-8=-1\) mmHg, so \(\hat{\alpha}_C=-1\) mmHg. For approach A, Clinic 2 versus Clinic 1 is \(6-8=-2\) mmHg, so \(\hat{\beta}_2=-2\) mmHg.

The interaction for B at Clinic 2 is the extra departure from adding those reference differences. The model predicts \(8+2-2=8\) mmHg, which equals the fitted mean for B at Clinic 2; hence \(\hat{\gamma}_{B,2}=8-8=0\). For C at Clinic 2, the additive prediction is \(8-1-2=5\) mmHg, again equal to the fitted mean, so \(\hat{\gamma}_{C,2}=0\).

The cell-mean contrasts tell the same story directly. B exceeds A by \(10-8=2\) mmHg at Clinic 1 and by \(8-6=2\) mmHg at Clinic 2, giving a difference-in-differences of \(2-2=0\) mmHg. C is 1 mmHg below A at both clinics, also giving zero. These fitted means show no interaction pattern for these approaches and clinics: the approach differences are constant across clinics.

More Than One Interaction Contrast

With three or more levels in both factors, a single comparison cannot describe the whole interaction. One convenient approach is to select reference levels and calculate each nonreference interaction coefficient. Another is to choose comparisons that answer a particular question. Both methods use fitted cell means; changing the reference levels changes the coefficient values, but not the fitted means or the underlying pattern of contrasts.

Worked Example: Reading a Three-by-Three Interaction

In a hypothetical study, three teaching methods are compared across three prior-experience groups. The outcome is a test score in points. Consider these fitted cell means.

MethodNoviceIntermediateAdvanced
A505560
B545761
C495359

Use Method A and Novice as references. The reference mean is 50 points. At Novice experience, B differs from A by \(54-50=4\) points and C differs by \(49-50=-1\) point. For Method A, Intermediate differs from Novice by \(55-50=5\) points, and Advanced differs by \(60-50=10\) points.

For B and Intermediate, the additive prediction is \(50+4+5=59\) points. The fitted mean is 57, so the interaction coefficient is \(57-59=-2\) points. In contrast form, B exceeds A by \(57-55=2\) points at Intermediate, compared with \(54-50=4\) points at Novice. The difference-in-differences is \(2-4=-2\) points.

For C and Intermediate, the additive prediction is \(50+(-1)+5=54\) points. The fitted mean is 53, so the interaction is \(53-54=-1\) point. Equivalently, C is \(53-55=-2\) points below A at Intermediate and \(49-50=-1\) point below A at Novice; the change in that difference is \(-2-(-1)=-1\) point.

The other reference-level interactions can be checked in the same way. For B and Advanced, the prediction is \(50+4+10=64\), compared with a fitted mean of 61, giving \(-3\) points. For C and Advanced, the prediction is \(50-1+10=59\), exactly the fitted mean, giving 0 points. Since several interaction coefficients are not zero, the method differences are not constant across experience groups.

Worked Example: Choosing a Contrast to Answer a Specific Question

Consider a different hypothetical comparison of three work schedules across three shifts. The outcome is completed units per worker per hour, and the fitted means are:

ScheduleMorningAfternoonNight
A202422
B232527
C192623

Assume workers' observations are independent, the cell means are meaningful for each schedule-shift combination, and the errors have mean zero and a common variance. These assumptions would need support from the hypothetical sampling or assignment process and an assessment of residual variability; the table of means does not establish them.

Suppose the question is whether the difference between schedules B and A changes from Morning to Afternoon. At Morning, the fitted difference is \(23-20=3\) units per worker-hour. At Afternoon it is \(25-24=1\). Therefore the contrast is \(1-3=-2\) units per worker-hour. The B-versus-A difference is 2 units smaller in the Afternoon than in the Morning.

For the same schedule comparison, Night versus Morning gives \((27-22)-(23-20)=5-3=2\) units per worker-hour. Thus the difference is 2 units larger at Night than in the Morning. These two contrasts describe how the B-versus-A difference varies across the shifts; neither alone summarizes every schedule comparison. For example, comparing C with A would require its own contrasts.

What the Interaction Does—and Does Not—Say

An interaction describes variation in conditional differences: it says that a difference between levels of one factor changes across levels of the other factor. It does not by itself identify a cause. A causal interpretation would require a study design and assumptions that justify causal conclusions. Nor does a nonzero fitted contrast by itself establish that the corresponding population contrast differs from zero; uncertainty and an appropriate inferential procedure are needed for that conclusion.

The fitted cell means are often the clearest starting point because they stay interpretable regardless of the coding used for the regression. Coefficients are compact summaries relative to a chosen reference, while rectangular contrasts express comparisons directly in the outcome's units. Report which levels are being compared and the units, rather than describing an interaction only as “positive” or “negative.”

  • Use cell means to show the fitted outcome at every factor combination.
  • Use a contrast to state how a specified group difference changes between specified levels of the other factor.
  • Check all relevant contrasts before concluding that there is no interaction across multi-level factors.
Common pitfall: A learner sees a positive coefficient for Method B and concludes that B has a positive advantage at every experience level. Under reference coding, that coefficient describes the B-versus-reference-method difference only at the reference experience level. At other levels, the fitted difference also includes the relevant interaction coefficient. For instance, in the three-by-three example, B exceeds A by 4 points for Novices, but by only 2 points for Intermediate learners because the interaction contrast is \(-2\) points. Read conditional differences from the fitted cell means or include the interaction terms; do not interpret one main-effect coefficient as universal.

Check Your Understanding

Use fitted cell means and rectangular contrasts to answer these questions.

  1. For factors with three and four levels, how many reference-level interaction coefficients are there?
  2. In a two-by-two comparison, the difference between A1 and A2 is 6 points at B1 and 2 points at B2. Calculate the rectangular contrast using the order “difference at B1 minus difference at B2,” and interpret it.
  3. What does a zero interaction coefficient for one nonreference cell mean under reference-level coding? Does it alone prove there is no interaction across all levels?
  4. Why can changing reference levels alter individual coefficient values without altering fitted cell means?
  5. What additional information, beyond a table of fitted cell means, would help assess whether a common error variance assumption is reasonable?