Tutorials › Regression Analysis (Undergraduate) › Adjacent-Difference Coding for Category Means

Coding Categorical Predictors · Tutorial 311 of 991

Adjacent-Difference Coding for Category Means

Use a sequence of indicator variables to make each coefficient the fitted-mean difference between one category and the next.

Beginner 10 min read

What You'll Learn

  • Define the order of categories and the baseline used for adjacent-difference coding
  • Build the cumulative-indicator columns for an ordered set of categories
  • Interpret each coefficient as the difference between neighboring fitted means
  • Reconstruct every fitted category mean from the baseline and adjacent differences
  • Recognize why this coding does not require equal spacing between categories
  • Identify a common error when reading cumulative-indicator coefficients

Why Code Neighboring Differences?

When categories have a meaningful order, a comparison between neighboring categories can be useful: for example, comparing low, medium, and high exposure, or successive levels of a service. A usual reference-category coding compares every non-reference category with one selected baseline. Adjacent-difference coding instead makes each coefficient compare one category with the next. This can make a sequence of local changes easier to read.

The coding describes the fitted mean for each category. It does not require the changes between categories to be equal, and it does not treat category labels as measured numerical scores. The categories need an order so that “next” has a clear meaning.

Definition: Suppose a categorical predictor has \(K\) ordered categories, labeled \(1,2,\ldots,K\). Let \(\mu_j\) be the fitted mean response for category \(j\). Adjacent-difference coding uses category 1 as a baseline and defines \(\delta_j=\mu_{j+1}-\mu_j\) for \(j=1,\ldots,K-1\). Thus, each \(\delta_j\) is the fitted-mean difference between two neighboring categories.

The Model and Its Coding Columns

To specify the coding, define \(z_{ij}=1\) when observation \(i\) is in category \(j+1\) or any category after it, and define \(z_{ij}=0\) otherwise. There is one such column for each adjacent pair, so there are \(K-1\) columns. The model is

$$ E(Y_i\mid \text{category }r)=\beta_0+\sum_{j=1}^{K-1}\delta_j z_{ij}, \qquad z_{ij}= \begin{cases} 1,& r\geq j+1,\\ 0,& r<j+1. \end{cases} $$

Here \(Y_i\) is the response for observation \(i\), \(\beta_0\) is the fitted mean for category 1, and \(\delta_j\) is the coefficient for the step from category \(j\) to category \(j+1\). For three categories, the coding columns are:

Category\(z_{i1}\)\(z_{i2}\)Fitted mean
100\(\beta_0\)
210\(\beta_0+\delta_1\)
311\(\beta_0+\delta_1+\delta_2\)

The columns are cumulative: once a category reaches a step, it has a value of 1 for that step and every earlier step. For instance, category 3 is past both the first and second transitions, so both indicators equal 1. The coefficient \(\delta_1\) is not the whole difference between category 1 and category 3; that overall difference is \(\delta_1+\delta_2\).

Model conditions: This is a one-factor mean model: each observation belongs to one of the \(K\) categories, the category means are represented by the equation above, and the errors have mean zero within each category. To estimate all \(K\) fitted means, every category must be represented in the data. The coefficient interpretations follow from the model’s coding; normality or equal error variance is not needed just to interpret the fitted means. Such assumptions may matter for particular inferential procedures.

Why the Coefficients Are Adjacent Differences

The coding works because category \(r\) has value 1 for exactly the first \(r-1\) step columns. Its fitted mean is therefore the baseline plus the first \(r-1\) differences. In symbols, category 1 has no steps, while each later category accumulates the differences it has passed:

$$ \mu_1=\beta_0,\qquad \mu_r=\beta_0+\sum_{j=1}^{r-1}\delta_j \quad (r=2,\ldots,K). $$

Subtract the expression for category \(r\) from the expression for category \(r+1\). All terms up to \(\delta_{r-1}\) cancel, leaving exactly the next step:

$$ \mu_{r+1}-\mu_r = \left(\beta_0+\sum_{j=1}^{r}\delta_j\right) - \left(\beta_0+\sum_{j=1}^{r-1}\delta_j\right) =\delta_r. $$

This also shows how to recover the coefficients from any set of fitted category means: set \(\beta_0=\mu_1\), then calculate each successive difference \(\delta_r=\mu_{r+1}-\mu_r\). Conversely, adding the differences back to the baseline recovers every category mean. There is no restriction that the differences have the same size or even the same sign.

Worked Example: Coding Three Ordered Categories

Suppose a hypothetical training program has three ordered levels: introductory, intermediate, and advanced. The fitted mean assessment scores are 72, 67, and 61 points, respectively. Use introductory as category 1.

The first coefficient is the difference from introductory to intermediate:

$$ \delta_1=67-72=-5\text{ points}. $$

The second coefficient is the difference from intermediate to advanced:

$$ \delta_2=61-67=-6\text{ points}. $$

Thus \(\beta_0=72\), and the model is \(\widehat{Y}=72-5z_1-6z_2\). For introductory, the row is \((0,0)\), giving \(72-5(0)-6(0)=72\) points. For intermediate, the row is \((1,0)\), giving \(72-5(1)-6(0)=67\) points. For advanced, the row is \((1,1)\), giving \(72-5(1)-6(1)=61\) points.

The negative signs indicate decreases in fitted mean score at each transition: five points from introductory to intermediate, then six more points from intermediate to advanced. The total difference from introductory to advanced is \(-5+(-6)=-11\) points, matching \(61-72=-11\).

Estimating the Category Means

In a one-factor model with one free fitted mean for each category, ordinary least squares estimates each category mean by that category’s sample mean. Adjacent-difference coding changes how those same fitted means are expressed as coefficients; it does not change the fitted mean for any category. Once the category means are estimated, the coding coefficients are their successive differences.

Worked Example: Calculating Coefficients From Unequal Group Sizes

Imagine a hypothetical set of quiz scores, in points, from three ordered practice schedules: once weekly, twice weekly, and three times weekly. The scores are 8 and 10 for once weekly; 11, 13, and 12 for twice weekly; and 9 and 10 for three times weekly. The category sizes are unequal, but each category is observed.

First calculate each fitted category mean:

$$ \bar{Y}_1=\frac{8+10}{2}=9,\qquad \bar{Y}_2=\frac{11+13+12}{3}=\frac{36}{3}=12,\qquad \bar{Y}_3=\frac{9+10}{2}=\frac{19}{2}=9.5. $$

The baseline is the first fitted mean, so \(\hat{\beta}_0=9\) points. The first adjacent difference is

$$ \hat{\delta}_1=\bar{Y}_2-\bar{Y}_1=12-9=3\text{ points}, $$

and the second is

$$ \hat{\delta}_2=\bar{Y}_3-\bar{Y}_2=9.5-12=-2.5\text{ points}. $$

The fitted equation is \(\widehat{Y}=9+3z_1-2.5z_2\). It gives \(9\) points for the once-weekly group, \(9+3=12\) points for the twice-weekly group, and \(9+3-2.5=9.5\) points for the three-times-weekly group. The unequal sample sizes do not alter the interpretation of the coefficients. They can affect the precision of the estimated means, but each category still has its own fitted mean in this model.

Reconstructing Means From a Coefficient Table

A fitted coefficient table may report a baseline and adjacent changes without listing the category means directly. To find the mean for a category, start at the baseline and add every step coefficient up to that category. A positive coefficient raises the fitted mean at that transition; a negative coefficient lowers it. The sum of several coefficients describes the total fitted-mean difference across those transitions.

Worked Example: Recovering Four Fitted Means

Suppose a hypothetical model for average task-completion time, measured in minutes, uses four ordered categories. Its adjacent-difference coding gives \(\hat{\beta}_0=18\), \(\hat{\delta}_1=4\), \(\hat{\delta}_2=-3\), and \(\hat{\delta}_3=2.5\). Reconstruct the fitted mean for each category.

Category 1 is the baseline: \(\hat{\mu}_1=18\) minutes. Category 2 adds the first difference:

$$ \hat{\mu}_2=18+4=22\text{ minutes}. $$

Category 3 adds both the first and second differences:

$$ \hat{\mu}_3=18+4-3=19\text{ minutes}. $$

Category 4 adds all three differences:

$$ \hat{\mu}_4=18+4-3+2.5=21.5\text{ minutes}. $$

The coefficients have direct neighboring interpretations: category 2 is 4 minutes higher than category 1; category 3 is 3 minutes lower than category 2; and category 4 is 2.5 minutes higher than category 3. Checking the last step confirms the reconstruction: \(21.5-19=2.5\) minutes.

What This Coding Does—and Does Not—Say

Adjacent-difference coding is useful when the scientific or practical question concerns transitions between neighboring ordered groups. Its coefficients retain the response’s units, so a coefficient can be described directly as a difference in fitted response units. It also gives a compact way to reconstruct all category means without treating the category labels as numerical measurements.

The coding itself does not impose a smooth trend, a constant increase, or a constant decrease. Each transition has its own coefficient. Nor does a positive or negative coefficient establish that the category change caused the response to change; the coefficient describes a modeled difference in fitted means. A causal interpretation requires an appropriate study design and additional justification.

Common Mistakes

  • Reading a coefficient as a comparison with the baseline. The coefficient \(\delta_j\) compares category \(j+1\) with category \(j\), not category \(j+1\) with category 1. To compare with the baseline, add all intervening differences.
  • Using a one-step indicator for only one category. In this coding, the column for step \(j\) equals 1 for category \(j+1\) and every later category. A later category accumulates the earlier steps too.
  • Assuming every adjacent difference must be equal. No such condition is part of the coding. The values of \(\delta_1,\ldots,\delta_{K-1}\) can differ in size and sign.
  • Reversing the subtraction order. With the convention here, \(\delta_j=\mu_{j+1}-\mu_j\). Reversing the order changes the sign and therefore changes the interpretation.
Key takeaway: Adjacent-difference coding uses a baseline mean and one coefficient for each transition to the next category. The coefficient is the next fitted mean minus the current fitted mean, and cumulative addition reconstructs every category mean.

Check Your Understanding

Try these questions using the category order given in each question.

  1. For four categories, how many adjacent-difference coefficients are needed, and what does \(\delta_2\) compare?
  2. Three fitted means are 20, 25, and 21 units. Find the baseline and both adjacent-difference coefficients.
  3. If a three-category model has \(\hat{\beta}_0=10\), \(\hat{\delta}_1=-2\), and \(\hat{\delta}_2=5\), what are the fitted means for categories 1, 2, and 3?
  4. Why is the category 3 row \((1,1)\), rather than \((0,1)\), in the three-category cumulative-indicator coding?
  5. Does adjacent-difference coding require the fitted means to rise or fall by the same amount at every step? Explain.