Tutorials › ANOVA › Coding Factorial Fixed Effects in Mixed Models

Mixed-Effects Factorial Models · Tutorial 388 of 595

Coding Factorial Fixed Effects in Mixed Models

You will translate between two coding schemes for a two-factor mixed model, recover its fitted cell means, and express factor and interaction hypotheses as coefficient contrasts.

Advanced 11 min read

What You'll Learn

  • Write reference-cell and sum-to-zero fixed-effects terms for two factors and their interaction
  • Recover each fitted cell mean from either coding scheme
  • Translate reference-cell coefficients into sum-to-zero coefficients
  • Interpret main-effect coefficients in the presence of an interaction
  • Express interaction and factor hypotheses as explicit joint tests
  • Recognize why recoding changes coefficient meanings but not fitted cell means

Why Coding Matters in a Mixed Model

A factorial mixed model separates the structure of the observations from the way its fixed treatment means are labelled. The random-effects part describes dependence, such as shared variation among observations in the same block. The fixed-effects part represents the treatment combinations. Coding determines how the coefficients in that fixed part combine to produce each treatment-combination mean.

The same set of cell means can be represented using a reference cell or using sum-to-zero contrasts. The fitted means do not change when the coding is changed, but individual coefficients do. This matters when interpreting output or stating a hypothesis: a single reference-coded coefficient may describe an effect at one level of the other factor, not an overall main effect.

Definition: In a factorial fixed-effects model, coding assigns numerical predictor values to factor levels so that model coefficients determine the fitted mean for every factor combination. Reference-cell coding compares levels with designated reference levels. Sum-to-zero coding uses contrast columns whose entries sum to zero across the levels of a factor.

The Mixed Model and Reference-Cell Coding

Consider two fixed factors, \(A\) and \(B\). Let \(A\) have levels \(A_0,A_1\), and let \(B\) have levels \(B_0,B_1,B_2\). Suppose observations are collected in blocks, with each block containing observations for all six treatment combinations. A random-intercept block model is

$$ Y_{ijk}=\text{fixed mean for }(A_j,B_k)+u_i+\varepsilon_{ijk}, \qquad u_i\sim N(0,\sigma^2_{\text{block}}), \qquad \varepsilon_{ijk}\sim N(0,\sigma^2). $$

Here \(i\) indexes blocks, \(j\) indexes the levels of \(A\), and \(k\) indexes the levels of \(B\). The random block effects are independent across blocks; the errors are independent across observations and independent of the block effects. The model assumes normally distributed random effects and errors with the stated variances, and a common residual variance. The factors are treated as fixed: their levels are the specific levels whose means are being compared. Coding changes only the fixed-mean representation, not these random-effects assumptions.

Choose \(A_0\) and \(B_0\) as reference levels. The reference-cell model is

$$ \begin{aligned} \mu_{00}&=\beta_0,\\ \mu_{01}&=\beta_0+\beta_{B1}, \qquad \mu_{02}=\beta_0+\beta_{B2},\\ \mu_{10}&=\beta_0+\beta_A,\\ \mu_{11}&=\beta_0+\beta_A+\beta_{B1}+\beta_{AB1},\\ \mu_{12}&=\beta_0+\beta_A+\beta_{B2}+\beta_{AB2}. \end{aligned} $$

The symbol \(\mu_{jk}\) denotes the fixed mean for cell \((A_j,B_k)\). Thus \(\beta_0\) is the reference-cell mean, \(\beta_A\) is the difference between \(A_1\) and \(A_0\) at \(B_0\), and \(\beta_{B1}\) and \(\beta_{B2}\) are differences from \(B_0\) at \(A_0\). Each interaction coefficient is a difference-in-differences. For example, \(\beta_{AB1}\) measures how much the \(A_1\)-versus-\(A_0\) difference at \(B_1\) departs from that difference at \(B_0\).

Worked Example: Reading Reference-Cell Coefficients

Suppose a hypothetical mixed-model fit gives \(\beta_0=10\), \(\beta_A=2\), \(\beta_{B1}=2\), \(\beta_{B2}=5\), \(\beta_{AB1}=0\), and \(\beta_{AB2}=1\). Calculate the six fitted cell means.

At the reference cell, \(\hat{\mu}_{00}=\beta_0=10\). For \(A_0\), the other means are \(\hat{\mu}_{01}=10+2=12\) and \(\hat{\mu}_{02}=10+5=15\).

At \(B_0\), changing from \(A_0\) to \(A_1\) gives \(\hat{\mu}_{10}=10+2=12\). At \(B_1\), \(\hat{\mu}_{11}=10+2+2+0=14\). At \(B_2\), \(\hat{\mu}_{12}=10+2+5+1=18\).

The fitted means are therefore 10, 12, 15 for \(A_0\), and 12, 14, 18 for \(A_1\). The \(A_1\)-versus-\(A_0\) differences across \(B_0,B_1,B_2\) are 2, 2, and 3. The interaction coefficients correctly record departures from the reference difference: zero at \(B_1\) and one at \(B_2\).

Sum-to-Zero Coding

For \(A\), assign scores \(-1\) to \(A_0\) and \(+1\) to \(A_1\). For \(B\), use two contrast columns with level scores \((1,0)\) for \(B_0\), \((0,1)\) for \(B_1\), and \((-1,-1)\) for \(B_2\). Each contrast column sums to zero across its factor levels. Denote the \(A\) score by \(x_A\), and the two \(B\) scores by \(z_1,z_2\). The fixed mean is

$$ \mu_{jk} =\gamma_0+\gamma_A x_A+\gamma_{B1}z_1+\gamma_{B2}z_2 +\gamma_{AB1}x_Az_1+\gamma_{AB2}x_Az_2. $$

The interaction columns are products of the corresponding factor scores. Because the scores sum to zero, \(\gamma_0\) is the equally weighted average of the six cell means. The coefficient \(\gamma_A\) is half the difference between the equally weighted \(A_1\) and \(A_0\) means. The \(B\) coefficients describe marginal departures from the grand mean for \(B_0\) and \(B_1\); the departure for \(B_2\) is \(-\gamma_{B1}-\gamma_{B2}\). These equal-weight interpretations concern the model’s cell means, not the raw proportions of observations in each cell.

The interaction coefficients measure departures from an additive pattern. For example, at \(B_0\), the \(A_1\)-versus-\(A_0\) difference is \(2(\gamma_A+\gamma_{AB1})\). At \(B_2\), it is \(2(\gamma_A-\gamma_{AB1}-\gamma_{AB2})\). Thus the interaction terms allow the \(A\) difference to vary across the levels of \(B\).

Worked Example: Converting the Cell Means to Sum-to-Zero Coefficients

Use the six hypothetical fitted means from the previous example: \(10,12,15\) for \(A_0\) and \(12,14,18\) for \(A_1\), in order of \(B_0,B_1,B_2\). Calculate the sum-to-zero coefficients directly from these means.

The average across all six cells is

$$ \gamma_0=\frac{10+12+15+12+14+18}{6} =\frac{81}{6}=13.5. $$

The \(A_0\) and \(A_1\) marginal means are \(37/3\) and \(44/3\), respectively. Since the \(A\) score is \(-1\) for \(A_0\) and \(+1\) for \(A_1\), the coefficient is half their difference:

$$ \gamma_A =\frac{1}{2}\left(\frac{44}{3}-\frac{37}{3}\right) =\frac{7}{6}\approx 1.167. $$

The marginal means for \(B_0,B_1,B_2\) are \(11,13,16.5\). Therefore \(\gamma_{B1}=11-13.5=-2.5\), and \(\gamma_{B2}=13-13.5=-0.5\). The implied \(B_2\) departure is \(-(-2.5)-(-0.5)=3\), agreeing with \(16.5-13.5=3\).

At \(B_0\), the \(A\) difference is \(12-10=2\). The sum-to-zero model gives that difference as \(2(\gamma_A+\gamma_{AB1})\), so

$$ \gamma_{AB1}=\frac{2}{2}-\frac{7}{6} =1-\frac{7}{6}=-\frac{1}{6}\approx -0.167. $$

At \(B_1\), the \(A\) difference is also \(14-12=2\), so \(\gamma_{AB2}=1-\frac{7}{6}=-\frac{1}{6}\). The implied interaction departure at \(B_2\) is \(-\gamma_{AB1}-\gamma_{AB2}=1/3\). The \(A\) difference there is \(2(\frac{7}{6}+\frac{1}{3})=3\), as required.

Why the Fitted Means Do Not Depend on the Coding

A coding scheme is a way to express the same six cell means using six coefficients. Each scheme has one intercept, one coefficient for \(A\), two for \(B\), and two for the interaction. The mapping from coefficients to cell means is one-to-one: the reference coefficients can be recovered by taking cell differences, and the sum-to-zero coefficients can be recovered by taking averages and contrasts. Consequently, if the same full factorial fixed-effects model is fitted with either coding, changing coding is a reparameterization, not a change in the set of possible fitted cell means.

For the example, the reference coefficients can be recovered from the cell means as follows:

$$ \begin{aligned} \beta_0&=\mu_{00}=10, & \beta_A&=\mu_{10}-\mu_{00}=2,\\ \beta_{B1}&=\mu_{01}-\mu_{00}=2, & \beta_{B2}&=\mu_{02}-\mu_{00}=5,\\ \beta_{AB1}&=\mu_{11}-\mu_{10}-\mu_{01}+\mu_{00}=0, & \beta_{AB2}&=\mu_{12}-\mu_{10}-\mu_{02}+\mu_{00}=1. \end{aligned} $$

Conversely, the sum-to-zero intercept and coefficients are determined by the equal-weight averages and contrasts just calculated. Substituting them into the sum-to-zero equation reproduces all six means. For instance, at \(A_0,B_0\), the scores are \(-1,1,0\), giving \(13.5-\frac{7}{6}-2.5+\frac{1}{6}=10\). This agreement is a direct check that the conversion has preserved the fitted cell mean.

State Hypotheses in Terms of the Question

A coefficient test has a meaning only after its coding and its target comparison are specified. Under reference coding, \(H_0:\beta_A=0\) tests whether the \(A_1\)-versus-\(A_0\) difference is zero specifically at \(B_0\). It does not by itself test whether \(A\) has no effect at every level of \(B\). The latter question requires the joint null

$$ H_0:\beta_A=\beta_{AB1}=\beta_{AB2}=0. $$

The interaction null asks whether the \(A\) difference is the same at all levels of \(B\). In reference coding it is

$$ H_0:\beta_{AB1}=\beta_{AB2}=0. $$

This is equivalent to the sum-to-zero interaction null \(H_0:\gamma_{AB1}=\gamma_{AB2}=0\). In either coding, the alternative is that at least one of the interaction contrasts is nonzero.

Worked Example: Writing the Main-Effect and Interaction Tests

Suppose the scientific questions concern (1) whether the \(A\) difference varies across \(B\), and (2) whether the equally weighted average difference between \(A_1\) and \(A_0\) is zero. State the corresponding hypotheses using each coding scheme.

For the interaction question, use the two reference-coded interaction coefficients: \(H_0:\beta_{AB1}=\beta_{AB2}=0\), with the alternative that at least one is nonzero. The equivalent sum-to-zero null is \(H_0:\gamma_{AB1}=\gamma_{AB2}=0\).

For the equally weighted average \(A\) difference, sum-to-zero coding makes the target explicit: the average difference is \(2\gamma_A\), so the null is \(H_0:\gamma_A=0\). Under reference coding, that same average difference is

$$ \frac{(\mu_{10}-\mu_{00})+(\mu_{11}-\mu_{01})+(\mu_{12}-\mu_{02})}{3} =\beta_A+\frac{\beta_{AB1}+\beta_{AB2}}{3}. $$

Thus the equivalent reference-coded hypothesis is \(H_0:\beta_A+(\beta_{AB1}+\beta_{AB2})/3=0\), not simply \(H_0:\beta_A=0\). For the example, the average difference is \((2+2+3)/3=7/3\), and the sum-to-zero expression gives \(2\gamma_A=2(7/6)=7/3\). These hypotheses specify contrasts of the fixed means; the model’s mixed-effects inference procedure must account for the random block structure when calculating uncertainty and a test.

Common Mistake: Treating a Main-Effect Coefficient as Universal

A common error is to read \(\beta_A\) from a reference-coded model as “the effect of \(A\)” without qualification. With an interaction in the model, \(\beta_A\) is the \(A\) difference only at the reference level \(B_0\). The differences at \(B_1\) and \(B_2\) are \(\beta_A+\beta_{AB1}\) and \(\beta_A+\beta_{AB2}\). To test an average difference, state the corresponding average contrast; to test no \(A\) difference at any \(B\) level, test the relevant coefficients jointly.

A second error is to assume that coding changes the model’s treatment predictions. It changes the coefficient values and their direct interpretations, but a complete factorial model with both main effects and their interaction still represents the same six cell means. If fitted means change after recoding, check whether the fixed-effects terms, data, or model-fitting choices also changed.

Key takeaway: Read a coefficient through its coding. Reference-cell coefficients describe comparisons anchored at the reference levels; sum-to-zero coefficients express equal-weight contrasts. State hypotheses as the contrasts of cell means that answer the scientific question, rather than relying on a coefficient label alone.

Check Your Understanding

Use the definitions and equations above to answer these questions.

  1. Under reference coding, what comparison does \(\beta_{B2}\) represent, and at which level of \(A\) is it evaluated?
  2. For the sum-to-zero scores used here, what is the implied marginal departure for \(B_2\) in terms of \(\gamma_{B1}\) and \(\gamma_{B2}\)?
  3. Write the reference-coded joint null hypothesis that \(A\) has no difference at any level of \(B\).
  4. Why does \(H_0:\beta_A=0\) not generally test whether the equally weighted average \(A\) difference is zero when interactions are included?
  5. Using the six example means, calculate the \(A_1\)-versus-\(A_0\) difference at \(B_2\), and verify it using the sum-to-zero coefficients.
Clinical Biostats