Introduction
A regression model can represent the same factor levels in more than one way. One coding might make a chosen level the reference; another might use contrasts that compare several levels. The coefficient values, and what each coefficient means, can change with that choice. The model’s fitted mean for each factor level need not change at all.
This distinction is useful when reading model output or comparing software settings. The coefficients are coordinates for expressing the fitted pattern across levels. To check whether two codings describe the same fitted pattern, compare the fitted means—not the coefficient values one by one.
A Factor-Only Model
Consider a response \(Y_i\) and a factor with \(K\) levels. Write \(g_i\) for the level observed for case \(i\). A factor-only model for the conditional mean is
Here \(\mu_g\) is the conditional mean for level \(g\), and \(\varepsilon_i\) is the difference between the observed response and that level’s mean. For this tutorial, the fitted model uses ordinary least squares (OLS) and includes enough factor columns to represent an arbitrary mean for every observed level. OLS chooses coefficients to minimize the sum of squared residuals. No normality assumption is needed for the coding-invariance result below.
With reference-level coding, one level is represented by the intercept, and the other levels have indicator columns. For three levels A, B, and C, taking A as the reference gives
Thus \(\beta_0\) is the fitted mean for A, while \(\beta_1\) and \(\beta_2\) are differences from A. These coefficient meanings follow from this coding choice; they are not definitions that apply to every coding.
Worked Example: Fitted Means With a Reference Level
Suppose hypothetical responses are recorded in three groups: A has values 10 and 14, B has values 15 and 17, and C has values 18 and 22. Fit a factor-only OLS model using A as the reference, and find each group’s fitted mean.
In a factor-only model with a separate fitted mean for each level, the OLS fitted mean for a level is its sample mean. The calculations are
Because A is the reference, \(\beta_0=12\), \(\beta_1=16-12=4\), and \(\beta_2=20-12=8\). Substitution gives \(\widehat{\mu}_A=12\), \(\widehat{\mu}_B=12+4=16\), and \(\widehat{\mu}_C=12+8=20\). These are fitted means in the response’s units.
Re-expressing the Same Three Means
Now represent the same three levels with two contrast columns. Assign the values \((-1,-1)\) to A, \((1,-1)\) to B, and \((0,2)\) to C. Let the coefficients be \(\gamma_0,\gamma_1,\gamma_2\), so the fitted means are
These equations are a different representation of the three fitted means. Solving them for the coefficients gives
The values assigned to the levels determine the coefficient meanings. Here \(\gamma_0\) is the equally weighted average of the three fitted level means. The other coefficients encode contrasts among those means. The predictions can be recovered from the equations regardless of how the coefficients themselves are interpreted.
Worked Example: Reproducing Three Means With Contrasts
Use the same hypothetical group means, 12 for A, 16 for B, and 20 for C, with the contrast values just defined. Calculate the contrast coefficients and verify all three fitted means.
First calculate the coefficients:
Substitute these values into the level equations:
The coefficients changed from \((12,4,8)\) under reference coding to \((16,2,2)\) under these contrasts. Every group’s fitted mean stayed the same.
Why the Fitted Values Stay the Same
The general reason is a property of OLS under an invertible change of predictor coordinates. Let \(X\) be the design matrix for one coding, and let \(\boldsymbol{\beta}\) be its coefficient vector. Suppose another coding has design matrix \(Z=XA\), where \(A\) is a nonsingular square matrix. “Nonsingular” means that \(A\) has an inverse. If the original coefficients are \(\boldsymbol{\beta}\), set the new coefficients to \(\boldsymbol{\theta}=A^{-1}\boldsymbol{\beta}\). Then
So any fitted-value vector available under the first coding is also available under the second. The reverse holds as well, because the change can be undone with \(A^{-1}\). The two codings therefore allow exactly the same fitted-value vectors. OLS minimizes the same sum of squared residuals over that same set, so the fitted values are identical. If the design matrix has full column rank, the coefficient estimates are uniquely related by \(\widehat{\boldsymbol{\theta}}=A^{-1}\widehat{\boldsymbol{\beta}}\).
For a factor with all \(K\) levels represented, an intercept and \(K-1\) suitable factor columns can express any set of \(K\) level means. Changing from one such coding to another changes the coordinates used to express those means, but not which sets of means can be represented. The derivation concerns the model’s fitted values; it does not require a probability distribution for the errors.
Worked Example: Changing the Reference Level
Return to fitted means of 12 for A, 16 for B, and 20 for C. Under A-reference coding, the coefficients were \(\beta_0=12\), \(\beta_1=4\), and \(\beta_2=8\). Change the reference level to B and find the new coefficients.
With B as the reference, the intercept is B’s fitted mean. The other two coefficients are differences from B:
The predictions are therefore
The intercept changed from 12 to 16, and the other coefficients now compare A and C with B rather than with A. Yet the fitted mean for each group is unchanged.
Coefficient Meaning Depends on Coding
Even a two-level factor illustrates why coefficient values should not be compared without checking the coding. Suppose a hypothetical outcome has fitted means of 30 units for group A and 36 units for group B. With A as the reference, the intercept is 30 and the indicator coefficient for B is \(36-30=6\). The coefficient describes B minus A.
Instead, assign a single predictor \(x=-1\) to A and \(x=1\) to B, and write the fitted mean as \(\gamma_0+\gamma_1x\). To reproduce the two means, solve
Adding the equations gives \(2\gamma_0=66\), so \(\gamma_0=33\). Subtracting the first equation from the second gives \(2\gamma_1=6\), so \(\gamma_1=3\). The predictions are \(33-3=30\) for A and \(33+3=36\) for B. The slope coefficient is now half the difference between the groups, not the full difference. Both codings yield the same fitted means of 30 and 36 units.
A coefficient is therefore not a fitted mean unless its coding makes it one, as the reference-level intercept does. Nor is a coefficient necessarily the difference between two particular levels; that depends on how the contrast values were assigned. To interpret any coefficient, first identify the coding and substitute its values into the model equation.
Common Mistake and Practical Check
A common mistake is to conclude that changing the reference level has changed the model’s predictions because the intercept or factor coefficients changed. That reasoning confuses the model’s representation with its fitted values. A changed coefficient can simply reflect a new baseline or a different scale for a contrast.
A reliable check is to calculate the fitted mean for every factor level under each coding. If all levels receive the same fitted means, the two representations agree on those predictions. For an OLS model using an intercept and a full set of contrasts for the factor, an invertible recoding preserves the available fitted values and hence the fitted values obtained by minimizing squared residuals. This statement assumes the recoding represents the same model; changing the model’s terms or restrictions is not merely changing the coding.
Check Your Understanding
Use the model equations and fitted means to answer these questions.
- A factor has fitted means 8, 11, and 17 under reference coding. What is the intercept if the second level becomes the reference?
- With contrast values \((-1,1)\) for A and \((1,1)\) for B, a model is \(\widehat{\mu}=\gamma_0+\gamma_1x_1+\gamma_2x_2\). Write the fitted mean for each level.
- Why does an invertible change of coding leave the OLS fitted-value set unchanged?
- Under a two-level coding with values \(-1\) and \(1\), what does the slope represent in terms of the two fitted means?
- A coefficient changes after switching the reference level. What calculation would establish whether the fitted means changed?