Tutorials › Regression Analysis (Undergraduate) › A Worked Sum-Coding Example With Unequal Group Sizes

Coding Categorical Predictors · Tutorial 309 of 991

A Worked Sum-Coding Example With Unequal Group Sizes

See how sum coding represents three group means and why its intercept is not generally the pooled mean when group sizes differ.

Beginner 9 min read

What You'll Learn

  • Set up a sum-coded regression model for three groups.
  • Calculate the fitted mean for each group from the model coefficients.
  • Recover the intercept and sum-coded coefficients from the three group means.
  • Check how unequal group sizes affect the pooled sample mean but not the unweighted average of group means.
  • Verify the fitted means and distinguish algebraic fitting from assumptions used for inference.

Why Unequal Group Sizes Make This Example Useful

With sum coding, the intercept represents the unweighted average of the group means. It is tempting to mistake this for the overall sample mean, especially when each group contributes a different number of observations. These quantities answer different questions: the unweighted average gives each group equal weight, while the pooled sample mean gives each observation equal weight.

This tutorial works through the calculation for three groups. The central checks are to reconstruct all three fitted means from the coefficients and to calculate the pooled mean separately using the group sizes.

The Three-Group Sum-Coded Model

Call the groups A, B, and C, and use the following sum-coding convention: A is coded \((1,0)\), B is coded \((0,1)\), and C is coded \((-1,-1)\). Let \(x_{1j}\) and \(x_{2j}\) be the two coding values for group \(j\). A regression model with an intercept and these two coded predictors is

$$ Y_{ij}=\beta_0+\beta_1x_{1j}+\beta_2x_{2j}+\varepsilon_{ij}. $$

Here \(Y_{ij}\) is the outcome for observation \(i\) in group \(j\); \(\beta_0,\beta_1,\beta_2\) are model coefficients; and \(\varepsilon_{ij}\) is the error. The fitted value for a group is the model prediction for that group. Write these three fitted group means as \(\hat{\mu}_A,\hat{\mu}_B,\hat{\mu}_C\).

For the algebraic calculations below, each group must have at least one observation. In a model with only a categorical group predictor, least squares chooses the fitted value for each group to minimize the sum of squared differences between observations and their group’s fitted value. Thus the fitted value for each group is its sample mean. This equality does not require normally distributed errors. Interpreting group means as estimates of population means requires a suitable sampling or study design; conventional inference additionally relies on assumptions such as independent errors and, for standard pooled-variance methods, equal error variances.

Definition: With the stated three-group sum coding, \(\beta_0\) is the unweighted average of the three fitted group means. The coefficients \(\beta_1\) and \(\beta_2\) describe the deviations of groups A and B from that average. Group C’s deviation is determined by the negative sum of those two coefficients.

Translate the Coefficients Into Fitted Means

Substitute each group’s code values into the model. The A code keeps \(\beta_1\), the B code keeps \(\beta_2\), and the C code subtracts both coefficients:

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

Adding the three fitted means cancels both deviation coefficients:

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

Therefore the intercept is the unweighted average of those means. Solving the same equations for the coefficients gives

$$ \beta_0=\frac{\hat{\mu}_A+\hat{\mu}_B+\hat{\mu}_C}{3},\qquad \beta_1=\hat{\mu}_A-\beta_0,\qquad \beta_2=\hat{\mu}_B-\beta_0. $$

The third group’s deviation is \(\hat{\mu}_C-\beta_0=-\beta_1-\beta_2\). This is the “sum” in sum coding: the three deviations from the intercept add to zero. It does not say that the group sizes are equal, or that the three sample means are equal.

Worked Example: Reading Fitted Means From Coefficients

Suppose a hypothetical analysis uses the stated coding and gives \(\beta_0=10\), \(\beta_1=-2\), and \(\beta_2=4\). Find the fitted mean for each group and check the equal-weight interpretation of the intercept.

For group A, the code is \((1,0)\), so

$$ \hat{\mu}_A=10+(-2)=8. $$

For group B, the code is \((0,1)\), giving

$$ \hat{\mu}_B=10+4=14. $$

For group C, the code is \((-1,-1)\), so

$$ \hat{\mu}_C=10-(-2)-4=8. $$

The average of the three fitted means is \((8+14+8)/3=10\), matching the intercept. Group A and C are each 2 outcome units below the intercept, while group B is 4 units above it. These are model-based group means in the outcome’s units; they are not automatically causal effects.

Worked Example With Unequal Group Sizes

Now consider hypothetical observations from three groups. The values below are in outcome units. Group A has one observation, group B has two, and group C has five. The group sizes are deliberately unequal.

GroupObserved outcomesSample sizeSample mean
A616
B10, 14212
C14, 16, 18, 20, 22518

Worked Example: Calculating Coefficients and the Pooled Mean

Find the sum-coded coefficients, verify the fitted group means, and compare the intercept with the pooled sample mean.

Step 1: Calculate the group means. The means are 6 for A, \((10+14)/2=12\) for B, and \((14+16+18+20+22)/5=90/5=18\) for C. With a group-only model and all three groups represented, least squares fits these group sample means.

Step 2: Calculate the intercept. Sum coding uses the unweighted average of the three group means:

$$ \hat{\beta}_0=\frac{6+12+18}{3}=\frac{36}{3}=12. $$

Step 3: Calculate the two coded coefficients. Group A’s deviation from 12 is \(-6\), and group B’s deviation is 0:

$$ \hat{\beta}_1=6-12=-6,\qquad \hat{\beta}_2=12-12=0. $$

Group C’s implied deviation is \(-\hat{\beta}_1-\hat{\beta}_2=-(-6)-0=6\). Its fitted mean is therefore \(12+6=18\).

Step 4: Reconstruct all three fitted means. Using the group codes gives

$$ \hat{\mu}_A=12-6=6,\qquad \hat{\mu}_B=12+0=12,\qquad \hat{\mu}_C=12-(-6)-0=18. $$

These match the group sample means, as required for this group-only least-squares model.

Step 5: Calculate the pooled sample mean separately. There are \(1+2+5=8\) observations. Weight each group mean by its sample size:

$$ \bar{x}_{\text{outcome}} =\frac{1(6)+2(12)+5(18)}{1+2+5} =\frac{6+24+90}{8} =\frac{120}{8} =15. $$

Thus the sum-coded intercept is 12 outcome units, whereas the pooled sample mean is 15 outcome units. Group C has the largest sample size and the largest mean, so weighting observations equally gives more influence to C than the unweighted average of group means does. Neither calculation is an arithmetic error: they are different summaries with different weights.

Why the Least-Squares Fits Are the Group Means

The equality between a fitted group value and its sample mean can be justified directly. For any one group with observations \(y_1,\ldots,y_m\), let \(c\) be the fitted value assigned to that group. Its contribution to the residual sum of squares is

$$ S(c)=\sum_{i=1}^{m}(y_i-c)^2. $$

To find the minimizing value, differentiate with respect to \(c\) and set the derivative to zero:

$$ \frac{dS(c)}{dc}=-2\sum_{i=1}^{m}(y_i-c)=0 \quad\Longrightarrow\quad \sum_{i=1}^{m}y_i-mc=0 \quad\Longrightarrow\quad c=\frac{\sum_{i=1}^{m}y_i}{m}=\bar{y}. $$

The second derivative is \(d^2S(c)/dc^2=2m>0\), so this value minimizes the sum of squares. In a three-group model with two coded predictors and an intercept, each group has its own fitted value, and the coefficient formulas above can represent any three group means. Consequently, minimizing the total sum of squares fits each group’s sample mean. Unequal group sizes change how many squared residuals each group contributes, but they do not change this group-by-group minimizing value.

Worked Example: A Second Unequal-Size Check

Suppose hypothetical groups A, B, and C have sample sizes 4, 1, and 3, with sample means 20, 26, and 14 outcome units. Calculate the sum-coded coefficients and pooled mean.

The unweighted average of the means gives the intercept:

$$ \hat{\beta}_0=\frac{20+26+14}{3}=\frac{60}{3}=20. $$

The A and B deviations give the coefficients:

$$ \hat{\beta}_1=20-20=0,\qquad \hat{\beta}_2=26-20=6. $$

The implied C deviation is \(-0-6=-6\). The fitted means are therefore \(20+0=20\) for A, \(20+6=26\) for B, and \(20-0-6=14\) for C.

The pooled mean gives each observation equal weight:

$$ \bar{x}_{\text{outcome}} =\frac{4(20)+1(26)+3(14)}{4+1+3} =\frac{80+26+42}{8} =\frac{148}{8} =18.5. $$

The intercept remains 20 because it averages the three group means equally. The pooled mean is 18.5 because the lower-mean group C has three observations and the higher-mean group B has only one.

Common Mistake: Calling the Intercept the Overall Sample Mean

A common incorrect argument is: “The intercept is the average outcome, so it must equal the mean of all observations.” That reasoning overlooks what is being averaged. With this sum-coding convention, the intercept averages the three group means with equal weight, regardless of group sizes. The pooled mean instead weights each group mean by its sample size.

Key takeaway: For three groups under the stated sum coding, calculate \(\hat{\beta}_0\) as \((\bar{y}_A+\bar{y}_B+\bar{y}_C)/3\). Calculate the pooled sample mean separately as \((n_A\bar{y}_A+n_B\bar{y}_B+n_C\bar{y}_C)/(n_A+n_B+n_C)\). These are generally different when group sizes differ.

Keep the distinction tied to the question. If the aim is to report a typical group while giving each category equal importance, the unweighted average of group means may be relevant. If the aim is to describe the average outcome among all observed individuals, use the pooled sample mean. A coding choice changes coefficient meanings and values, but it does not change the observed group means or fitted predictions when the model represents the same three group means.

Check Your Understanding

Try each question by writing down the group codes and showing the relevant arithmetic.

  1. Under the coding used here, what is the fitted mean for group C if \(\beta_0=15\), \(\beta_1=2\), and \(\beta_2=-3\)?
  2. Three groups have means 4, 10, and 16. Calculate the sum-coded intercept and the two coefficients.
  3. For group means 6, 12, and 18 with sample sizes 1, 2, and 5, calculate both the unweighted average of means and the pooled sample mean. Why are they different?
  4. If all three group means equal 9 but the group sizes differ, what is the sum-coded intercept? What is the pooled sample mean?
  5. State the condition on group representation needed to estimate all three group means in this model, and explain why least squares fits a group’s sample mean.