Tutorials › Regression Analysis (Undergraduate) › Sum Coding for Categorical Predictors

Coding Categorical Predictors · Tutorial 307 of 991

Sum Coding for Categorical Predictors

Build sum-to-zero codes for a three-category factor and use the model equations to see how its coefficients differ from reference-cell indicator coefficients.

Beginner 9 min read

What You'll Learn

  • Construct two code columns for a factor with three categories so each column sums to zero across the categories.
  • Write the regression equation for a three-category predictor under sum coding.
  • Recover all three fitted category means from the intercept and two sum-coded coefficients.
  • Relate sum-coded coefficients to category deviations from the unweighted average of the three fitted means.
  • Compare sum coding with reference-cell indicator coding and translate between their coefficients.
  • Identify why changing the coding changes coefficient values but not fitted category means.

Why Use Sum Coding?

A categorical predictor with three levels can be represented by two numerical columns in a regression model. Reference-cell indicator coding makes one category the comparison baseline: the intercept gives that category’s fitted mean, and the other coefficients give differences from it. Sum coding uses a different parameterization: its code values balance to zero across the three categories, so the intercept and coefficients relate to the category means in a different way.

The point is not to change the fitted values. For a given set of three category means, reference-cell coding and sum coding can represent the same means. What changes is how those means are expressed using the model coefficients.

Constructing Two Sum-to-Zero Codes

Call the categories A, B, and C. Assign two codes to each category using the following convention. The code in each column sums to zero over the three categories.

Category\(x_1\)\(x_2\)
A10
B01
C-1-1

For \(x_1\), the three category codes add to \(1+0+(-1)=0\). For \(x_2\), they add to \(0+1+(-1)=0\). These are codes for the categories, not values that vary with the outcome.

Definition: In this tutorial, sum coding for a three-category factor assigns two numerical code columns whose values sum to zero across the three categories. We use the specific assignments A = \((1,0)\), B = \((0,1)\), and C = \((-1,-1)\). A regression model with an intercept and these two columns can represent three category means.

To state the model, let \(Y_i\) be the outcome for observation \(i\), and let \(x_{1i}\) and \(x_{2i}\) be the codes for that observation’s category. The one-factor regression model is

$$ Y_i=\beta_0+\beta_1x_{1i}+\beta_2x_{2i}+\varepsilon_i. $$

Here \(\beta_0,\beta_1,\beta_2\) are the regression coefficients and \(\varepsilon_i\) is the error for observation \(i\). For the usual least-squares model, assume errors have mean zero and are independent with a common variance. The category coding determines the fitted means; those assumptions concern the errors and are relevant to statistical inference about the model.

Worked Example: Checking the Three-Category Codes

Suppose a hypothetical predictor has categories A, B, and C. Assign the sum codes and check that both columns satisfy the sum-to-zero condition.

Step 1: Assign each category’s pair. Under the convention in this tutorial, A receives \((1,0)\), B receives \((0,1)\), and C receives \((-1,-1)\).

Step 2: Add the first column. Its total across categories is \(1+0-1=0\).

Step 3: Add the second column. Its total across categories is \(0+1-1=0\).

Thus the assignments meet the sum-to-zero condition. Each observation is assigned the pair for its category; observations in the same category receive the same pair.

From the Codes to Category Means

The fitted value for a category is obtained by substituting that category’s two codes into the model. Denote the fitted means for A, B, and C by \(\hat{Y}_A,\hat{Y}_B,\hat{Y}_C\). The model gives

$$ \begin{aligned} \hat{Y}_A&=\beta_0+\beta_1,\\ \hat{Y}_B&=\beta_0+\beta_2,\\ \hat{Y}_C&=\beta_0-\beta_1-\beta_2. \end{aligned} $$

These equations show how two coefficients and an intercept encode three category means. Adding the three fitted means gives

$$ \hat{Y}_A+\hat{Y}_B+\hat{Y}_C =(\beta_0+\beta_1)+(\beta_0+\beta_2)+(\beta_0-\beta_1-\beta_2) =3\beta_0. $$

The two code-coefficient terms cancel. Dividing by three proves that the intercept is the unweighted average of the three fitted category means:

$$ \beta_0=\frac{\hat{Y}_A+\hat{Y}_B+\hat{Y}_C}{3}. $$

Solving the category equations for the coefficients gives another useful description:

$$ \beta_1=\hat{Y}_A-\beta_0,\qquad \beta_2=\hat{Y}_B-\beta_0,\qquad -\beta_1-\beta_2=\hat{Y}_C-\beta_0. $$

So the first two coefficients are the deviations of A and B from the unweighted average of the three fitted means. The deviation for C is determined by the other two: the three deviations sum to zero. These are algebraic descriptions of the coefficients under this coding. The next tutorial develops their interpretation further.

Worked Example: Coding Three Given Category Means

Suppose the fitted outcome means for a hypothetical three-category predictor are 12 units for A, 18 units for B, and 24 units for C. Find the coefficients under the specified sum coding, then verify all three fitted means.

Step 1: Find the unweighted average.

$$ \beta_0=\frac{12+18+24}{3}=\frac{54}{3}=18\text{ units}. $$

Step 2: Find the first two coefficients. For A, the code is \((1,0)\), so \(\beta_1=12-18=-6\) units. For B, the code is \((0,1)\), so \(\beta_2=18-18=0\) units.

Step 3: Check the category predictions.

$$ \begin{aligned} \hat{Y}_A&=18+(-6)=12\text{ units},\\ \hat{Y}_B&=18+0=18\text{ units},\\ \hat{Y}_C&=18-(-6)-0=24\text{ units}. \end{aligned} $$

The equations reproduce all three given means. In particular, C’s fitted mean uses both negative codes, which is why its prediction is \(\beta_0-\beta_1-\beta_2\).

Contrast With Reference-Cell Indicator Coefficients

With reference-cell indicator coding and A chosen as the reference, the model uses an indicator for B and an indicator for C. Each indicator equals 1 for its named category and 0 otherwise. The corresponding model can be written

$$ \hat{Y}=\alpha+\delta_B I_B+\delta_C I_C. $$

In this model, the fitted means are \(\hat{Y}_A=\alpha\), \(\hat{Y}_B=\alpha+\delta_B\), and \(\hat{Y}_C=\alpha+\delta_C\). Thus the intercept is A’s fitted mean, while \(\delta_B\) and \(\delta_C\) are differences from A. Under sum coding, the fitted means instead follow the three equations above: the intercept is their unweighted average, and the two coefficients are deviations for A and B from that average.

QuantitySum codingA-reference indicator coding
InterceptUnweighted average of the three fitted category meansFitted mean for A
First coefficientA’s fitted mean minus the unweighted averageNot applicable as a category-specific indicator coefficient
Second coefficientB’s fitted mean minus the unweighted averageNot applicable as a category-specific indicator coefficient
Category-comparison coefficientsC’s deviation is \(-\beta_1-\beta_2\)\(\delta_B=\hat{Y}_B-\hat{Y}_A\), \(\delta_C=\hat{Y}_C-\hat{Y}_A\)

The coefficient values are connected by the category means. In terms of sum-coded coefficients, \(\alpha=\beta_0+\beta_1\), \(\delta_B=\beta_2-\beta_1\), and \(\delta_C=-2\beta_1-\beta_2\). These follow by expressing each reference-coded quantity using \(\hat{Y}_A=\beta_0+\beta_1\), \(\hat{Y}_B=\beta_0+\beta_2\), and \(\hat{Y}_C=\beta_0-\beta_1-\beta_2\).

Worked Example: Translating Between the Two Codings

Use the fitted means 12, 18, and 24 units for A, B, and C. Find the A-reference indicator coefficients and compare them with the sum-coded coefficients.

Sum coding. The unweighted average is 18 units, so \(\beta_0=18\), \(\beta_1=12-18=-6\), and \(\beta_2=18-18=0\).

Reference-cell coding. Since A is the reference, \(\alpha=\hat{Y}_A=12\) units. The B coefficient is the B-minus-A difference, \(\delta_B=18-12=6\) units. The C coefficient is \(\delta_C=24-12=12\) units. Substitution verifies the means: \(12\) for A, \(12+6=18\) for B, and \(12+12=24\) for C.

The two parameterizations assign different values to their intercepts and coefficients: for example, the sum-coded intercept is 18 units, whereas the A-reference intercept is 12 units. Both describe the same three fitted means.

Why the Choice of Coding Matters

Coding determines the meanings attached to individual coefficients, so a coefficient should not be interpreted without checking the code assignments and the model equation. The fitted values are the predictions for each category; changing from sum coding to reference coding changes how those predictions are represented, not the predictions themselves.

For an ordinary least-squares model with a single categorical predictor, the fitted mean for each category is its observed sample mean. Sum coding expresses these three fitted means through an intercept and two coefficients. It does not make the category sample sizes equal, and the unweighted average in the intercept formula is an average over the three category means, not an average over all observations.

Key takeaway: With A = \((1,0)\), B = \((0,1)\), and C = \((-1,-1)\), the fitted means are \(\beta_0+\beta_1\), \(\beta_0+\beta_2\), and \(\beta_0-\beta_1-\beta_2\). Their unweighted average is \(\beta_0\). Reference-cell coding instead makes the intercept the reference category’s fitted mean and its indicator coefficients differences from that category.

Common Mistake

A common mistake is to treat a sum-coded coefficient as the difference between the category named by its code and the category with the omitted reference indicator. For example, \(\beta_1\) is not the A-minus-C difference under the coding used here. The equations show that A-minus-C is \((\beta_0+\beta_1)-(\beta_0-\beta_1-\beta_2)=2\beta_1+\beta_2\). The correction is to substitute the codes for both categories into the model, obtain both fitted means, and then subtract in the requested order.

Check Your Understanding

Use the coding A = \((1,0)\), B = \((0,1)\), and C = \((-1,-1)\).

  1. Show that the assigned values in each code column sum to zero across the three categories.
  2. Write the fitted-mean equation for category C in terms of \(\beta_0,\beta_1,\beta_2\).
  3. If the fitted means are 10, 13, and 19 units for A, B, and C, find \(\beta_0,\beta_1,\beta_2\).
  4. With A as the reference category, what are the indicator-model intercept and the coefficients for B and C for those same fitted means?
  5. Explain why changing from sum coding to reference-cell coding does not, by itself, change the three fitted category means.