Tutorials › Regression Analysis (Undergraduate) › Why Recoding Leaves Predictions Unchanged

Coding Categorical Predictors · Tutorial 305 of 991

Why Recoding Leaves Predictions Unchanged

Learn how to transform reference-coded coefficients when the reference group changes, and verify that the recoding preserves every fitted category mean.

Beginner 9 min read

What You'll Learn

  • Write a reference-coded model for a categorical predictor with multiple groups.
  • Convert the intercept and indicator coefficients from one reference group to another.
  • Verify that the transformation preserves the fitted mean for every category.
  • Explain why ordinary least squares gives the same fitted values under either reference coding.
  • Avoid interpreting a changed coefficient as a changed group mean or changed prediction.

Why the Coefficients Can Change While the Fit Does Not

A reference category gives meaning to the coefficients in a regression model with a categorical predictor. If the reference group changes, the intercept and indicator coefficients change too. That change is a change of coordinates: the same group means can be described relative to a different starting group.

This distinction matters when reading or comparing regression output. A coefficient under reference coding is a difference from the chosen reference group, not the fitted mean for that category. Changing the reference group changes which differences are reported. It does not, by itself, change the model's fitted mean for any category.

Definition: Suppose a categorical predictor has \(K\) observed groups. Under reference-cell coding, one group is designated the reference group, and the model includes an indicator for each of the other \(K-1\) groups. The intercept represents the fitted mean for the reference group; each indicator coefficient represents the fitted mean for its group minus the fitted mean for the reference group.

The Model and Its Coefficients

Consider observations \(Y_i\) from individuals in one of \(K\) groups, denoted \(G_i\). For this tutorial, use a model with a categorical predictor only: each group has its own mean, and there are no other predictors. If group \(A\) is the reference, the model is

$$ Y_i=\beta_0^{(A)}+\sum_{g\ne A}\beta_g^{(A)}I(G_i=g)+\varepsilon_i. $$

Here \(I(G_i=g)\) is 1 when observation \(i\) belongs to group \(g\), and 0 otherwise. The error \(\varepsilon_i\) is the difference between the observed outcome and its group mean in the model. The superscript \((A)\) labels the coding that uses \(A\) as the reference; it is not an exponent. The coefficient \(\beta_g^{(A)}\) exists for each nonreference group \(g\).

For an observation in group \(A\), all included indicators equal 0, so its fitted value is \(\beta_0^{(A)}\). For an observation in any other group \(g\), its fitted value is \(\beta_0^{(A)}+\beta_g^{(A)}\). It is useful to write \(m_g\) for the fitted mean of group \(g\). Thus \(m_A=\beta_0^{(A)}\) and \(m_g=\beta_0^{(A)}+\beta_g^{(A)}\) for \(g\ne A\).

To state the conversion compactly, define \(\beta_A^{(A)}=0\) by convention. This zero is not an estimated coefficient: it records that group \(A\) is the reference. Then every group mean under this coding can be written as \(m_g=\beta_0^{(A)}+\beta_g^{(A)}\).

Changing the Reference from A to B

Now change the reference group from \(A\) to a different group \(B\). The new intercept must equal the fitted mean for \(B\), because \(B\) is now the reference. Every new indicator coefficient must be the fitted mean for that indicator's group minus the fitted mean for \(B\). These requirements give the complete coefficient conversion.

Result: When the reference changes from \(A\) to \(B\), with \(A\ne B\), the transformed coefficients are
$$ \begin{aligned} \beta_0^{(B)}&=\beta_0^{(A)}+\beta_B^{(A)},\\ \beta_A^{(B)}&=-\beta_B^{(A)},\\ \beta_g^{(B)}&=\beta_g^{(A)}-\beta_B^{(A)} \quad\text{for }g\ne A,B. \end{aligned} $$
The reference coefficient is \(\beta_B^{(B)}=0\) by convention.

The first line says that the new intercept is the old intercept plus the old coefficient for \(B\), which is the old fitted mean for \(B\). The second line expresses the old reference \(A\)'s mean relative to \(B\). The third line expresses each remaining group's mean relative to \(B\). In particular, these are differences between fitted means; they are not new group means on their own.

Why the Transformation Preserves Every Fitted Mean

The conversion follows by equating each group's fitted mean before and after recoding. For the new reference \(B\), the old model gives \(m_B=\beta_0^{(A)}+\beta_B^{(A)}\). Since the new intercept must equal \(m_B\), this gives the first line of the result. For the old reference \(A\), its mean is \(\beta_0^{(A)}\). Subtracting the new intercept gives its new coefficient:

$$ \beta_A^{(B)} =m_A-m_B =\beta_0^{(A)}-\bigl(\beta_0^{(A)}+\beta_B^{(A)}\bigr) =-\beta_B^{(A)}. $$

For any group \(g\) other than \(A\) and \(B\), its old fitted mean is \(\beta_0^{(A)}+\beta_g^{(A)}\). Subtracting the new reference mean gives

$$ \begin{aligned} \beta_g^{(B)} &=m_g-m_B\\ &=\bigl(\beta_0^{(A)}+\beta_g^{(A)}\bigr) -\bigl(\beta_0^{(A)}+\beta_B^{(A)}\bigr)\\ &=\beta_g^{(A)}-\beta_B^{(A)}. \end{aligned} $$

These expressions also verify the fitted means directly. Under the new coding, group \(A\) has fitted mean \(\beta_0^{(B)}+\beta_A^{(B)}=m_B+(m_A-m_B)=m_A\). Group \(B\), now the reference, has fitted mean \(\beta_0^{(B)}=m_B\). Any other group \(g\) has fitted mean \(\beta_0^{(B)}+\beta_g^{(B)}=m_B+(m_g-m_B)=m_g\). Each category therefore retains exactly its previous fitted mean.

For ordinary least squares, this is more than an identity for a set of coefficients. Provided all \(K\) groups are represented in the data, either reference coding can describe any set of \(K\) group means: the intercept specifies the reference mean, and the \(K-1\) coefficients specify differences from it. The coding change is reversible, so the possible sets of fitted values are the same. Ordinary least squares minimizes the sum of squared residuals over those fitted values; minimizing over the same possibilities gives the same fitted values and residual sum of squares. No assumption of normally distributed errors is needed for this algebraic invariance. The usual regression assumptions matter for inference, but are not needed to show that recoding preserves predictions.

Worked Example: Changing the Reference Among Three Groups

Suppose hypothetical quiz scores, measured in points, are observed in groups A, B, and C. The observed scores are A: 10, 12, and 14; B: 15 and 17; C: 9 and 12. Use a model with one mean per group and first use A as the reference. All three groups have observations, so their sample means can be estimated.

The group means are

$$ m_A=\frac{10+12+14}{3}=12,\qquad m_B=\frac{15+17}{2}=16,\qquad m_C=\frac{9+12}{2}=10.5. $$

With A as the reference, the intercept is 12 points. The coefficient for B is \(16-12=4\) points, and the coefficient for C is \(10.5-12=-1.5\) points. Thus the fitted means are 12 for A, \(12+4=16\) for B, and \(12-1.5=10.5\) for C.

Now use B as the reference. The old coefficient for B is 4, so the new intercept is \(12+4=16\) points. The new coefficient for A is \(-4\) points, and the new coefficient for C is \(-1.5-4=-5.5\) points. Checking all categories gives A: \(16-4=12\); B: \(16\); C: \(16-5.5=10.5\). Each fitted mean is unchanged. The C coefficient changed from \(-1.5\) to \(-5.5\) because it now describes the difference between C and B, rather than C and A.

Worked Example: A Four-Group Conversion

Suppose a hypothetical service records waiting time in minutes for people in four locations. The observed times are North: 12, 14, and 16; South: 18 and 20; East: 11 and 13; West: 21, 23, and 25. Use North as the original reference, then recode with East as the reference. The group means are calculated from all observations in each location, and every location is represented.

The fitted means are North \((12+14+16)/3=14\) minutes, South \((18+20)/2=19\) minutes, East \((11+13)/2=12\) minutes, and West \((21+23+25)/3=23\) minutes. Under North-reference coding, the intercept is 14; the coefficients are South \(19-14=5\), East \(12-14=-2\), and West \(23-14=9\), all in minutes.

East is the new reference, and its old coefficient is \(-2\). Therefore the new intercept is \(14+(-2)=12\) minutes. The new North coefficient is \(-(-2)=2\) minutes. The new South coefficient is \(5-(-2)=7\) minutes, and the new West coefficient is \(9-(-2)=11\) minutes.

The new coding predicts North: \(12+2=14\) minutes; South: \(12+7=19\) minutes; East: \(12\) minutes; West: \(12+11=23\) minutes. These equal the original fitted means. A positive new North coefficient is sensible: North's mean waiting time is 2 minutes higher than East's.

Worked Example: Checking the Signs with a Different Reference

Suppose a hypothetical model for a laboratory measurement, in milligrams per litre, is currently coded with C as its reference. Its fitted intercept is 42, with coefficients A: \(-6\), B: \(3\), and D: \(8\). These values describe fitted differences from C. Change the reference to D.

The fitted means under the original coding are C: 42 mg/L; A: \(42-6=36\) mg/L; B: \(42+3=45\) mg/L; and D: \(42+8=50\) mg/L. The old coefficient for the new reference D is 8, so the new intercept is \(42+8=50\) mg/L.

The new coefficient for the old reference C is \(-8\) mg/L. For A, subtract the old D coefficient from the old A coefficient: \(-6-8=-14\) mg/L. For B, calculate \(3-8=-5\) mg/L. Under D-reference coding, the fitted means are D: 50 mg/L; C: \(50-8=42\) mg/L; A: \(50-14=36\) mg/L; and B: \(50-5=45\) mg/L. The negative coefficients mean those groups have lower fitted means than D. The signs change according to the reference, while the fitted means agree exactly.

What Recoding Does and Does Not Change

The new coefficients answer different comparison questions, but the same model is being described. If the data, outcome, included groups, and fitting method remain fixed, changing only the reference group does not change the fitted value for any observation, its residual, or the residual sum of squares. The coefficient values and their direct interpretations do change because the comparisons are now made against a different group.

This invariance depends on changing only the coding of the same categorical predictor model. It does not say that predictions remain unchanged when a group is removed, a predictor is added, or a different model is fit. It also requires that the groups to be compared are represented in the data for their group means to be estimated from observed outcomes. A category with no observations does not have an empirically estimated group mean in this model.

Common Mistake

A common mistake is to see the coefficient for C change from \(-1.5\) to \(-5.5\) points in the three-group example and conclude that C's fitted score decreased by 4 points. That reasoning treats a difference-from-reference coefficient as though it were the group's fitted mean.

The correction is to reconstruct the mean using the intercept and the relevant coefficient. With A as reference, C's mean is \(12+(-1.5)=10.5\) points. With B as reference, it is \(16+(-5.5)=10.5\) points. The coefficient changed because its comparison group changed; C's fitted mean did not.

Key takeaway: When the reference changes from \(A\) to \(B\), add the old B coefficient to the old intercept, and subtract that old B coefficient from every other nonreference coefficient. The resulting parameters describe the same fitted mean for every group.

Check Your Understanding

Use the conversion rule and distinguish each coefficient from its group's fitted mean.

  1. With A as the reference, the intercept is 20, the coefficient for B is 3, and the coefficient for C is \(-2\). What are the intercept and coefficients when B is the reference?
  2. For the model in question 1, calculate the fitted mean for each group under both codings.
  3. With C as the reference, the intercept is 50, the coefficient for A is 4, and the coefficient for B is \(-6\). Recode the model with A as the reference.
  4. In a model with all groups represented, explain why changing the reference group does not change the ordinary least squares fitted values.
  5. A student says, “The coefficient for a group changed, so that group's fitted mean must have changed.” Identify the error and state what should be compared instead.