Tutorials › Regression Analysis (Undergraduate) › Comparing Coding Schemes in a Three-Group Example

Coding Categorical Predictors · Tutorial 319 of 991

Comparing Coding Schemes in a Three-Group Example

Work through one three-group dataset to interpret coefficients under three coding schemes and verify that all three give the same fitted values.

Beginner 9 min read

What You'll Learn

  • Set up a three-group regression model and identify its fitted group means.
  • Interpret the intercept and group coefficients under reference coding.
  • Interpret the intercept and contrasts under a specified sum-coding convention.
  • Read group means directly from a cell-means model.
  • Verify algebraically and numerically that recoding preserves fitted means.
  • Avoid confusing the sum-coded intercept with the sample-size-weighted overall mean.

One Set of Group Means, Several Coefficient Stories

A categorical predictor with three groups can be represented in a regression equation in more than one way. The representation changes the coefficients and their interpretation, but it need not change the model’s fitted outcome for any group. Comparing the coding schemes on one dataset makes both facts concrete: coefficients depend on the coding, while fitted group means can remain identical.

We will use reference coding, sum coding, and cell-means coding for the same three groups. We will specify the contrast convention for each, calculate the coefficients, and check each group’s fitted mean. The arithmetic is descriptive: no hypothesis tests or confidence intervals are needed to compare the coding schemes.

Dataset and Model

Suppose, hypothetically, that an outcome is measured in points for individuals in groups A, B, and C. The observations are:

GroupObserved outcomes (points)Group sizeSample mean (points)
A9, 11210
B13, 14, 15314
C18, 20, 22, 20, 20520

For a one-way group-means model, write the observation for individual \(i\) in group \(j\) as \(Y_{ij}=\mu_j+\epsilon_{ij}\), where \(\mu_j\) is that group’s mean outcome and \(\epsilon_{ij}\) is the individual’s deviation from the group mean. The usual linear-model assumptions are that the errors are independent, have mean zero, and have a common variance. Normality is additionally used for some exact small-sample inferences, but is not needed for the coefficient and fitted-mean calculations here.

Definition: A fitted group mean is the model’s predicted outcome for an individual whose categorical predictor is in that group. In this example, the three fitted means are the predictions for groups A, B, and C, measured in points.

The observed group means are \(10\), \(14\), and \(20\) points. We will use them to estimate the group means under all three coding schemes. The group sizes are unequal, which makes it especially useful to distinguish an unweighted average of group means from the overall sample mean.

Worked Example: Find the Three Group Means

Calculate each group’s sample mean from the hypothetical observations.

For group A, the outcome total is \(9+11=20\), so the mean is \(20/2=10\) points.

For group B, the total is \(13+14+15=42\), so the mean is \(42/3=14\) points.

For group C, the total is \(18+20+22+20+20=100\), so the mean is \(100/5=20\) points. Thus the group means to be represented are \(10\), \(14\), and \(20\) points.

Reference Coding: Differences from a Baseline Group

Take group A as the reference category. Define \(I_B=1\) for a group B observation and \(0\) otherwise; define \(I_C\) in the same way for group C. The reference-coded model is

$$ \widehat{Y}=\beta_0+\beta_B I_B+\beta_C I_C. $$

For group A, both indicators equal zero, so its fitted mean is \(\beta_0\). For group B, \(I_B=1\) and \(I_C=0\), giving fitted mean \(\beta_0+\beta_B\). For group C, the fitted mean is \(\beta_0+\beta_C\). Therefore the coefficients are set by these three relationships:

$$ \beta_0=10,\qquad \beta_0+\beta_B=14,\qquad \beta_0+\beta_C=20. $$

Solving gives \(\beta_0=10\), \(\beta_B=14-10=4\), and \(\beta_C=20-10=10\), all in points. The intercept is the fitted mean for group A. The coefficient for B is the difference between B’s mean and A’s mean, and the coefficient for C is the difference between C’s mean and A’s mean.

Worked Example: Predict with Reference Coding

Using the coefficients just calculated, find the fitted outcome for one individual from each group.

For A, the indicators are \(I_B=0\) and \(I_C=0\): \(10+4(0)+10(0)=10\) points.

For B, the indicators are \(I_B=1\) and \(I_C=0\): \(10+4(1)+10(0)=14\) points.

For C, the indicators are \(I_B=0\) and \(I_C=1\): \(10+4(0)+10(1)=20\) points.

The baseline choice determines which group mean is the intercept and which differences appear as coefficients. Here the coefficients compare B and C with A; they are not the group means for B and C themselves.

Sum Coding: Deviations from an Unweighted Group Average

For sum coding, specify the contrast values rather than relying on the coding name alone. In this example, assign group A the pair \((1,0)\), group B the pair \((0,1)\), and group C the pair \((-1,-1)\). Let these two coded values be \(z_1\) and \(z_2\). The model is

$$ \widehat{Y}=\beta_0+\beta_1z_1+\beta_2z_2. $$

Under this convention, the fitted means are \(\beta_0+\beta_1\) for A, \(\beta_0+\beta_2\) for B, and \(\beta_0-\beta_1-\beta_2\) for C. The three group means sum to \(3\beta_0\), because the contrast contributions cancel:

$$ (\beta_0+\beta_1)+(\beta_0+\beta_2)+(\beta_0-\beta_1-\beta_2)=3\beta_0. $$

Consequently, \(\beta_0\) is the unweighted average of the three fitted group means. In this example it is \((10+14+20)/3=44/3\), or about \(14.67\) points. The coefficient \(\beta_1\) is A’s deviation from this average, and \(\beta_2\) is B’s deviation. The deviation for C is represented by \(-\beta_1-\beta_2\).

Worked Example: Calculate the Sum-Coded Coefficients

Use the specified contrast values and the three observed group means to solve for the sum-coded coefficients.

For A, \(\beta_0+\beta_1=10\), so \(\beta_1=10-44/3=-14/3\approx-4.667\) points.

For B, \(\beta_0+\beta_2=14\), so \(\beta_2=14-44/3=-2/3\approx-0.667\) points.

For C, the model predicts \(44/3-(-14/3)-(-2/3)=44/3+16/3=60/3=20\) points. The C deviation is \(16/3\approx5.333\) points, which is also \(-\beta_1-\beta_2\). The three deviations sum to zero: \(-14/3-2/3+16/3=0\).

Thus the sum-coded intercept is \(14.67\) points, with group deviations of approximately \(-4.667\), \(-0.667\), and \(5.333\) points for A, B, and C. The coding changes the coefficient story, not the fitted group means.

Cell-Means Coding: One Coefficient per Group

In cell-means coding, use one indicator for each of the three groups and omit the separate intercept. Let \(I_A\), \(I_B\), and \(I_C\) equal 1 for an observation in the named group and 0 otherwise. The model is

$$ \widehat{Y}=\gamma_A I_A+\gamma_B I_B+\gamma_C I_C. $$

Exactly one indicator equals 1 for each observation. Therefore \(\gamma_A\), \(\gamma_B\), and \(\gamma_C\) are directly the fitted means for A, B, and C. For this dataset the estimates are \(10\), \(14\), and \(20\) points, respectively. Unlike reference coding, there is no single intercept that serves as a baseline; unlike the specified sum coding, the coefficients are not deviations from a common average.

Worked Example: Predict with Cell-Means Coding

Use \(\widehat{Y}=10I_A+14I_B+20I_C\) to predict the outcome for a group C individual. For this individual, \(I_A=0\), \(I_B=0\), and \(I_C=1\), so

$$ \widehat{Y}=10(0)+14(0)+20(1)=20\text{ points}. $$

For group A, the same equation gives \(10(1)+14(0)+20(0)=10\) points; for group B it gives \(10(0)+14(1)+20(0)=14\) points. Each coefficient is a group’s fitted mean because its indicator selects that group.

Why All Three Schemes Give the Same Fitted Means

In this example, each coding scheme can represent any set of three group means. The model equations differ, but their coefficients can be translated so that the predictions agree group by group. With A as the reference, the means are \(m_A=\beta_0\), \(m_B=\beta_0+\beta_B\), and \(m_C=\beta_0+\beta_C\). Under the specified sum coding, they are \(m_A=\beta_0+\beta_1\), \(m_B=\beta_0+\beta_2\), and \(m_C=\beta_0-\beta_1-\beta_2\). In cell-means coding, they are \(m_A=\gamma_A\), \(m_B=\gamma_B\), and \(m_C=\gamma_C\).

A short least-squares argument explains why the estimated group means equal the observed group averages, regardless of which of these full three-mean representations is used. For any one group with observations \(y_1,\ldots,y_n\), the sum of squared residuals at a proposed fitted mean \(m\) is minimized at the sample mean \(\bar{y}\). To see this, write each residual as \((y_i-\bar{y})+(\bar{y}-m)\). The cross-product term sums to zero because \(\sum_i(y_i-\bar{y})=0\). Thus:

$$ \sum_{i=1}^{n}(y_i-m)^2 = \sum_{i=1}^{n}(y_i-\bar{y})^2+n(\bar{y}-m)^2. $$

The first term does not depend on \(m\), and the second is nonnegative and is zero exactly when \(m=\bar{y}\). Since each of the three coding schemes can assign a separate fitted mean to each group, minimizing the total squared error gives the same three group averages in every scheme.

For a numerical check, the fitted means are A \(=10\), B \(=14\), and C \(=20\) points under all three schemes. They also give the same residuals: A has residuals \(-1,1\); B has \(-1,0,1\); and C has \(-2,0,2,0,0\). The residual sum of squares is \(2+2+8=12\) points squared in every scheme. Identical fitted values imply identical residuals for these same observations.

Interpreting the Intercept Carefully

The three intercept stories should not be confused:

Coding schemeIntercept or group coefficientsInterpretation in this example
Reference, A baselineIntercept \(10\); B coefficient \(4\); C coefficient \(10\)A mean; B-minus-A and C-minus-A differences, in points
Sum, A \((1,0)\), B \((0,1)\), C \((-1,-1)\)Intercept \(44/3\); coefficients \(-14/3\) and \(-2/3\)Unweighted average of the three means; A and B deviations from it
Cell meansCoefficients \(10,14,20\)Fitted means for A, B, and C, in points

Here the overall sample mean is \((20+42+100)/10=16.2\) points, not \(44/3\approx14.67\) points. The sample mean weights each group according to its size; the specified sum-coded intercept gives each group mean equal weight. Unequal group sizes make the distinction visible.

Common Mistake

  • Wrong reasoning: “The sum-coded intercept is the overall sample mean, so it must be \(16.2\) points.” This uses the sample-size-weighted average, giving groups with more observations more weight.
  • Correction: With the contrast values stated here, the sum-coded intercept is the unweighted average of the three fitted group means: \((10+14+20)/3=14.67\) points. Always check the actual coding values and model equation before interpreting an intercept.
  • Another common error: Treating every coefficient as a group mean. That is true for the cell-means coefficients here, but not for the reference-coded differences or the sum-coded deviations.
Key takeaway: Coefficients describe quantities determined by the coding convention. To compare coding schemes, translate each model into its fitted mean for every group. In this three-group example, reference, sum, and cell-means coding all give fitted means of 10, 14, and 20 points for A, B, and C.

Check Your Understanding

Use the contrast convention and dataset in this tutorial to answer the questions.

  1. Under reference coding with A as the baseline, what do the intercept and the coefficient for group C represent?
  2. Under sum coding, why is the fitted mean for C \(\beta_0-\beta_1-\beta_2\) rather than \(\beta_0+\beta_1+\beta_2\)?
  3. What is the cell-means coefficient for B, and what does it represent in points?
  4. Calculate the unweighted average of the three group means. How does it differ from the overall sample mean, and why?
  5. Why do the three coding schemes have the same fitted means and residual sum of squares for these data?