Why Use Helmert Coding?
When a categorical predictor has an order of interest, a regression model can express comparisons that follow that order. With three categories, one useful sequence is to compare the second category with the first, then compare the third category with the average of the first two. These are not comparisons with a single reference category in both cases: the comparison group changes as we move through the order.
Helmert coding represents those sequential comparisons with numerical predictor codes. The particular scaling matters: here the codes are chosen so that each regression coefficient equals its intended difference in fitted means, rather than a multiple of that difference. The categories are still treated as categories; their labels are not used as numerical scores.
The Coding and Its Coefficients
Call the categories 1, 2, and 3, in their chosen order. Define the two coded predictors \(h_1\) and \(h_2\) as follows. The category labels determine which row to use; they are not numerical values entered directly into the regression.
| Category | Intercept column | \(h_1\) | \(h_2\) |
|---|---|---|---|
| 1 | 1 | \(-1/2\) | \(-1/3\) |
| 2 | 1 | \(1/2\) | \(-1/3\) |
| 3 | 1 | 0 | \(2/3\) |
For an outcome \(Y\), the model for an observation in one of these categories is
Here \(\beta_0\) is the intercept, \(\beta_1\) is the coefficient of \(h_1\), and \(\beta_2\) is the coefficient of \(h_2\). The fitted mean for a category is the model’s predicted outcome for that category, with the error term set to zero. Substituting the codes gives:
These equations show how each coefficient contributes to the fitted means. In particular, the first two categories receive equal contributions from \(\beta_2\), so their average is \(\beta_0-\beta_2/3\). Category 3 has fitted mean \(\beta_0+2\beta_2/3\); subtracting the average for categories 1 and 2 leaves \(\beta_2\). Similarly, subtracting the fitted mean for category 1 from that for category 2 leaves \(\beta_1\).
Recovering the Fitted Means
The coding works in both directions. Once the regression coefficients are known, substitute them into the three equations to obtain the category means. Conversely, the coefficients can be calculated from the three fitted means. The following short derivation verifies both the comparisons and the intercept interpretation.
\(\widehat{\mu}_2-\widehat{\mu}_1=(\beta_0+\beta_1/2-\beta_2/3)-(\beta_0-\beta_1/2-\beta_2/3)=\beta_1.\)
\((\widehat{\mu}_1+\widehat{\mu}_2)/2=\beta_0-\beta_2/3\). Therefore \(\widehat{\mu}_3-(\widehat{\mu}_1+\widehat{\mu}_2)/2=(\beta_0+2\beta_2/3)-(\beta_0-\beta_2/3)=\beta_2.\)
\((\widehat{\mu}_1+\widehat{\mu}_2+\widehat{\mu}_3)/3=(3\beta_0)/3=\beta_0\), because the \(h_1\) and \(h_2\) contributions each sum to zero across the three categories.
Thus the reverse formulas are:
The intercept is the unweighted average of the three category means. It is not necessarily the average outcome across all observations: if the categories have different numbers of observations, an observation-weighted average gives more weight to categories with larger sample sizes.
Worked Examples
Worked Example: Calculate Coefficients From Unequal Group Sizes
Consider hypothetical outcome measurements from three ordered categories. The observed values are 8, 10, and 12 in category 1; 12, 14, 14, and 16 in category 2; and 18 and 22 in category 3. Find the fitted means and the three Helmert coefficients for a model with this categorical predictor.
Check the model setup. This is a one-factor model with three observed categories and three parameters: an intercept and two coded predictors. Each category has observations, so all three category means can be estimated. In this saturated categorical model, the least-squares fitted mean for each category is that category’s sample mean. The coding only changes how those fitted means are represented by coefficients.
Calculate the fitted means. The sample sizes are 3, 4, and 2. The category means are:
Calculate the coefficients. The intercept is the unweighted average of the three category means. The first contrast is category 2 minus category 1. The second is category 3 minus the average of categories 1 and 2:
The unequal sample sizes do not change the definitions of these coefficients. In particular, category 3 is compared with \((10+14)/2=12\), not with a pooled average of all observations in categories 1 and 2. That pooled average would be \((30+56)/7\approx12.29\), because category 2 has more observations; it is not the comparison defined by this coding.
Verify by recovering the means. Substitution into the fitted-mean equations gives:
The small differences before rounding disappear if the exact intercept \(44/3\) is used. The recovered values match the three category sample means.
Worked Example: Recover Means From Given Coefficients
Suppose a fitted model has \(\beta_0=30\), \(\beta_1=-6\), and \(\beta_2=9\). Recover the fitted mean for each of the three ordered categories and interpret the two comparisons.
Category 1: Use the category 1 codes \(h_1=-1/2\) and \(h_2=-1/3\):
Category 2: Use \(h_1=1/2\) and \(h_2=-1/3\):
Category 3: Use \(h_1=0\) and \(h_2=2/3\):
The first comparison is \(\widehat{\mu}_2-\widehat{\mu}_1=24-30=-6\), as specified by \(\beta_1\). The average of the first two means is \((30+24)/2=27\), so the category 3 comparison is \(36-27=9\), as specified by \(\beta_2\). The intercept checks as \((30+24+36)/3=30\).
Worked Example: Use the Coding Row for a Prediction
A hypothetical fitted model for an outcome measured in points has \(\beta_0=50\), \(\beta_1=8\), and \(\beta_2=-3\). Find the fitted outcome for an observation in category 2, and explain why its coded values are not simply the number 2.
Category 2 uses \(h_1=1/2\) and \(h_2=-1/3\). Therefore:
The category label selects the coding row; the regression uses that row’s two codes. Entering the number 2 as a single predictor would impose a different model in which moving between categories has a numerical, constant-step interpretation. Helmert coding instead represents two specifically defined comparisons among category means.
Model Conditions and What the Coding Does
For the mean model, write an observation in category \(j\) as \(Y_{ij}=\mu_j+\varepsilon_{ij}\), where \(\mu_j\) is the mean for category \(j\) and \(\varepsilon_{ij}\) is its deviation from that mean. The coding provides a one-to-one way to express the three category means using three regression parameters. With observations in all three categories, the fitted category means from least squares are the corresponding sample means.
The algebraic recovery formulas do not require equal sample sizes. Nor does interpreting the coefficients as differences in fitted means require equal variances. Independence, zero-mean errors, and (for conventional pooled-error standard errors) a common error variance are conditions relevant to standard regression inference, not to the coding identities themselves. This tutorial’s coefficient interpretations concern fitted means; they do not by themselves establish that any population difference is statistically significant.
The order of categories is part of the question being asked. If the order changes, the sequence of comparisons changes too. For example, recoding the order as 2, 1, 3 would make the first coefficient compare category 1 with category 2, and the second compare category 3 with their average. The category names alone do not determine a meaningful order; it should come from the context.
Common Mistakes
- Reading \(\beta_2\) as category 3 minus category 2. That is not its definition here. The correct comparison is category 3 minus the average of categories 1 and 2.
- Using a sample-size-weighted average for the preceding categories. The comparison is the ordinary average of their fitted means. Unequal group sizes do not change the contrast’s definition.
- Calling the intercept the overall observation-weighted mean. The intercept is the unweighted average of the three fitted category means. Those two averages can differ when category sizes differ.
- Assuming the category labels are numeric scores. The labels determine which coding row applies; the codes, not the labels, enter the regression equation.
- Forgetting the scaling of the codes. With the codes shown here, the coefficients equal the intended differences. Other Helmert scalings can make coefficients equal a constant multiple of those differences, so always check the coding table before interpreting a coefficient.
Check Your Understanding
Use the category order 1, 2, 3 and the scaled Helmert codes from this tutorial.
- If the fitted means are 6, 10, and 15, calculate \(\beta_0\), \(\beta_1\), and \(\beta_2\).
- If \(\beta_0=12\), \(\beta_1=3\), and \(\beta_2=-2\), calculate the fitted mean for each category.
- Explain why the category 3 comparison is not necessarily the difference between category 3 and category 2.
- With unequal sample sizes, should the category 3 comparison use an observation-weighted average of categories 1 and 2? Explain.
- What does the intercept represent, and how can it differ from the mean across all observations?