Tutorials › Regression Analysis (Undergraduate) › Recovering Group Means from Any Coding

Coding Categorical Predictors · Tutorial 306 of 991

Recovering Group Means from Any Coding

Practice translating coded regression coefficients into the fitted outcome for every category, whatever the coding scheme.

Beginner 10 min read

What You'll Learn

  • Write the fitted mean for each category using reference coding.
  • Recover category means from the intercept and coefficients under sum coding.
  • Read category means directly from cell-means coefficients.
  • Check that different parameterizations give the same fitted values.
  • Distinguish a fitted category mean from a sample-size-weighted overall mean.

Why Recovering the Means Matters

A regression table reports coefficients, but the quantities most useful for understanding a categorical predictor are often the fitted outcomes for its categories. Those fitted means are not always individual coefficients: depending on the coding, a category mean may be an intercept, an intercept plus one or more coefficients, or a coefficient from a model without an intercept.

The goal here is to make that translation explicit. We will use one categorical predictor with three categories, called A, B, and C. The same reasoning applies to any number of categories: identify the coding row for a category, substitute it into the fitted regression equation, and simplify.

Definition: A fitted mean for category g, written \(\hat{\mu}_g\), is the model’s predicted outcome for an observation in category \(g\). In a model containing only one categorical predictor, it is the predicted value for every observation in that category. It is a model-based fitted value, not necessarily the mean of a new sample.

Model and Conditions

Let \(Y_{ig}\) be the outcome for observation \(i\) in category \(g\). A one-factor categorical model can be written as

$$ Y_{ig}=\mu_g+\varepsilon_{ig}, $$

where \(\mu_g\) is the mean outcome for category \(g\), and \(\varepsilon_{ig}\) is the observation’s error. To estimate these category means by ordinary least squares, the model must represent each category, and each category considered must have observations. The coding must also be full rank: its columns must allow the category means to be uniquely represented.

For the usual linear-model interpretation, errors are assumed to have mean zero and to be independent; a common error variance is often assumed for standard error calculations. These error assumptions support statistical inference, but the arithmetic for recovering fitted means is determined by the fitted equation and the coding. Normality is not needed to substitute category codes into that equation.

The formulas below concern a model with one categorical predictor and no additional predictors or interactions. If other terms are present, the fitted outcome may also depend on their values; then the fitted category mean must be calculated at specified values of those other predictors.

Reference Coding: Start at the Reference Category

Under reference coding, one category is the reference, and the model has an intercept plus an indicator for each other category. Let A be the reference. With coefficients \(\beta_0,\beta_B,\beta_C\), the fitted equation is

$$ \hat{Y}=\hat{\beta}_0+\hat{\beta}_B I(B)+\hat{\beta}_C I(C), $$

Here \(I(B)=1\) for an observation in B and \(0\) otherwise; \(I(C)\) is defined in the same way. For A, both indicators are zero. For B, only \(I(B)\) is one; for C, only \(I(C)\) is one. Thus the category means are recovered by substitution:

$$ \hat{\mu}_A=\hat{\beta}_0,\qquad \hat{\mu}_B=\hat{\beta}_0+\hat{\beta}_B,\qquad \hat{\mu}_C=\hat{\beta}_0+\hat{\beta}_C. $$

Worked Example: Recovering Means Under Reference Coding

Suppose a hypothetical model predicts a quiz score in points, with A as the reference category. The fitted coefficients are \(\hat{\beta}_0=62\), \(\hat{\beta}_B=-5.5\), and \(\hat{\beta}_C=3.2\). Find the fitted score for each category.

For A, both indicators are zero, so \(\hat{\mu}_A=\hat{\beta}_0=62\) points.

For B, \(I(B)=1\) and \(I(C)=0\): \[ \hat{\mu}_B=62+(-5.5)=56.5\text{ points}. \]

For C, \(I(B)=0\) and \(I(C)=1\): \[ \hat{\mu}_C=62+3.2=65.2\text{ points}. \]

The intercept is the fitted score for A, while each other category’s fitted score combines the intercept with its own indicator coefficient. The negative coefficient for B yields a fitted score 5.5 points below A; it is not itself B’s fitted score.

Sum Coding: Substitute the Category’s Codes

For three categories, one common sum-coding arrangement assigns A the codes \((1,0)\), B the codes \((0,1)\), and C the codes \((-1,-1)\). With intercept \(\beta_0\) and two coded coefficients \(\beta_1,\beta_2\), the fitted equation is \(\hat{Y}=\hat{\beta}_0+\hat{\beta}_1 x_1+\hat{\beta}_2 x_2\). The category means come from substituting each category’s pair of codes.

$$ \begin{aligned} \hat{\mu}_A&=\hat{\beta}_0+\hat{\beta}_1,\\ \hat{\mu}_B&=\hat{\beta}_0+\hat{\beta}_2,\\ \hat{\mu}_C&=\hat{\beta}_0-\hat{\beta}_1-\hat{\beta}_2. \end{aligned} $$

In this parameterization, the intercept alone is generally not any one category’s fitted mean. The code values for the category must be used to recover its prediction.

Worked Example: Recovering Means Under Sum Coding

Consider a hypothetical model of a rating in points with \(\hat{\beta}_0=70\), \(\hat{\beta}_1=6\), and \(\hat{\beta}_2=-4\). Use the stated codes for A, B, and C to calculate the fitted rating for each category.

Category A has codes \((1,0)\): \[ \hat{\mu}_A=70+6(1)+(-4)(0)=76\text{ points}. \]

Category B has codes \((0,1)\): \[ \hat{\mu}_B=70+6(0)+(-4)(1)=66\text{ points}. \]

Category C has codes \((-1,-1)\): \[ \hat{\mu}_C=70+6(-1)+(-4)(-1)=70-6+4=68\text{ points}. \]

The three fitted ratings are therefore 76, 66, and 68 points. Their unweighted average is \((76+66+68)/3=70\) points, matching the intercept for this particular sum-coding arrangement. This average gives equal weight to the three category means; it is not necessarily the mean across all observations when category sizes differ.

Cell-Means Coding: Read Each Mean Directly

In cell-means coding, the model has no intercept and includes one indicator for every category:

$$ \hat{Y}=\hat{\gamma}_A I(A)+\hat{\gamma}_B I(B)+\hat{\gamma}_C I(C). $$

Exactly one of these indicators equals one for each observation. Consequently, the fitted mean for a category is its corresponding coefficient:

$$ \hat{\mu}_A=\hat{\gamma}_A,\qquad \hat{\mu}_B=\hat{\gamma}_B,\qquad \hat{\mu}_C=\hat{\gamma}_C. $$

Worked Example: Recovering Means Under Cell-Means Coding

A hypothetical model predicts clinic waiting time in minutes. It uses cell-means coding and reports \(\hat{\gamma}_A=18.4\), \(\hat{\gamma}_B=22.1\), and \(\hat{\gamma}_C=16.7\).

For a patient in A, \(I(A)=1\) and the other indicators are zero: \[ \hat{Y}=18.4(1)+22.1(0)+16.7(0)=18.4\text{ minutes}. \]

For B, only \(I(B)=1\), giving \(\hat{Y}=18.4(0)+22.1(1)+16.7(0)=22.1\) minutes. For C, only \(I(C)=1\), giving \(\hat{Y}=18.4(0)+22.1(0)+16.7(1)=16.7\) minutes. Each coefficient is a category’s fitted waiting time because this model assigns one coefficient directly to each category.

Why the Same Means Can Have Different Coefficients

The coefficients depend on the coding, but the fitted category means need not. To see how this works, start with any three fitted means \(m_A,m_B,m_C\). Under reference coding with A as the reference, the coefficients that reproduce them are \(\beta_0=m_A\), \(\beta_B=m_B-m_A\), and \(\beta_C=m_C-m_A\). Substitution gives \(m_A\), \(m_A+(m_B-m_A)=m_B\), and \(m_A+(m_C-m_A)=m_C\).

For the sum-coding arrangement above, choose \(\beta_0=(m_A+m_B+m_C)/3\), \(\beta_1=m_A-\beta_0\), and \(\beta_2=m_B-\beta_0\). The fitted values for A and B are then \(\beta_0+\beta_1=m_A\) and \(\beta_0+\beta_2=m_B\). For C,

$$ \begin{aligned} \beta_0-\beta_1-\beta_2 &=\beta_0-(m_A-\beta_0)-(m_B-\beta_0)\\ &=3\beta_0-m_A-m_B\\ &=m_C. \end{aligned} $$

Thus this sum-coded equation reproduces all three means too. In cell-means coding, set \(\gamma_A=m_A\), \(\gamma_B=m_B\), and \(\gamma_C=m_C\), which reproduces them directly. This algebra shows why coefficient values cannot be compared across coding schemes without first accounting for how each scheme defines the fitted equation.

Worked Example: Translating One Set of Means Across Codings

Suppose hypothetical fitted waiting times are 18.4 minutes for A, 22.1 minutes for B, and 16.7 minutes for C. Recover coefficients under each of the three parameterizations.

Reference coding with A as the reference: the intercept is 18.4. The B coefficient is \(22.1-18.4=3.7\), and the C coefficient is \(16.7-18.4=-1.7\). Checking, A is \(18.4\), B is \(18.4+3.7=22.1\), and C is \(18.4-1.7=16.7\) minutes.

Sum coding: the intercept is the unweighted mean of the three fitted means: \[ \hat{\beta}_0=(18.4+22.1+16.7)/3=57.2/3=19.0667\text{ minutes}. \] The first coefficient is \(18.4-19.0667=-0.6667\), and the second is \(22.1-19.0667=3.0333\). The recovered values are A: \(19.0667-0.6667=18.4\); B: \(19.0667+3.0333=22.1\); and C: \(19.0667-(-0.6667)-3.0333=16.7\) minutes, allowing for rounding.

Cell-means coding: the coefficients are 18.4, 22.1, and 16.7 minutes for A, B, and C. Each is read directly as its category’s fitted mean. The coefficient lists differ, but each fitted equation gives the same three predictions.

A Common Pitfall

A frequent mistake is to read the intercept as the fitted mean for every category—or to assume it always represents the reference category. That is correct for the reference-coded equation shown here, but not for the sum-coded equation, where the category’s coded terms must be added, or cell-means coding, which has no intercept. Another error is to treat an indicator coefficient as a category mean when it is actually a difference from the reference mean.

A reliable check is to write the fitted equation and the category’s indicator values before doing arithmetic. If the resulting predictions do not match the intended means, check the coding values, the signs, and whether an intercept is present. Also keep the averaging target clear: the average of three category means weights categories equally, while an overall observation-level mean weights categories according to their sample sizes.

Key takeaway: To recover a category’s fitted mean, substitute its coding values into the full fitted equation. Reference coding combines the intercept with the relevant indicator coefficient, sum coding combines the intercept with the category’s coded terms, and cell-means coding gives the mean directly as a coefficient.

Check Your Understanding

Use the fitted equation and the stated coding to answer each question.

  1. Under reference coding, \(\hat{\beta}_0=40\) and the coefficient for category B is 7. What is the fitted mean for B, and what does the coefficient 7 represent?
  2. With sum codes A \((1,0)\), B \((0,1)\), and C \((-1,-1)\), the intercept is 12 and the coded coefficients are 2 and 3. Calculate all three fitted means.
  3. Under cell-means coding, a coefficient for category C is \(-1.5\). What is the fitted mean for C, and what additional terms must be included for that prediction?
  4. Why can two regression outputs report different coefficients yet give identical fitted means for each category?
  5. If three fitted category means are averaged equally, does that necessarily equal the overall mean across all observations when the category sizes are unequal? Explain.