Why Compare Residuals by Category?
A residual-versus-fitted plot or residual-versus-continuous-predictor plot can be useful, but it may combine observations from distinct groups. If one category tends to have positive residuals and another negative residuals, their patterns can cancel in an overall summary. Likewise, one group may have much more residual variability than another even when the overall spread appears moderate.
Comparing residuals across levels of a categorical predictor is a way to look for these group-specific features. For example, a model predicting appointment duration might leave consistently positive residuals for one appointment type and negative residuals for another. This is evidence that the fitted model has a systematic pattern associated with appointment type; it is not, by itself, evidence that appointment type causes the difference.
A positive residual means the observed response is above its fitted value; a negative residual means it is below. A group mean near zero does not establish that the residuals are well behaved: large positive and negative residuals can balance. It is therefore useful to inspect both center and spread, alongside a plot that shows the individual residuals.
Set Up the Comparison
Suppose a fitted regression model is being used to describe a quantitative response \(Y\). Its residuals are \(e_i=y_i-\hat{y}_i\), and each observation belongs to one of the levels of a categorical predictor \(G\). For each level \(k\), record the number of observations \(n_k\) and summarize the residuals belonging to that level. A group mean residual is
A common measure of group spread is the sample standard deviation of the residuals around that group mean:
These quantities are descriptive summaries of the fitted residuals, not estimates that automatically settle whether a model assumption is true. Small groups give imprecise-looking summaries because a few observations can change them substantially. Always show or inspect the individual points, and report group sizes so that a group with only a few observations is not mistaken for strong evidence of a stable pattern.
A useful display places residuals on the vertical axis and category levels on the horizontal axis, using separate points and, if helpful, boxplots. The zero line is a reference for residual centers. A table of \(n_k\), \(\bar{e}_k\), and \(s_{e,k}\) can make group comparisons more explicit. The plot and table answer related but different questions: summaries compress the data, while the points can reveal outliers, clusters, or other features that summaries hide.
Worked Example: Opposing Residual Centers in Two Groups
Hypothetical data. A clinic uses an intercept-only model to predict visit duration, in minutes, for six visits. The visits belong to category A or B. Suppose the observed durations are 8, 9, and 10 minutes for A, and 12, 13, and 14 minutes for B. Find the fitted value and compare the group residual centers.
With an intercept-only least-squares model, every visit receives the sample mean as its fitted value:
The residuals for A are \(8-11=-3\), \(9-11=-2\), and \(10-11=-1\) minutes. Their mean is \( \bar{e}_A=(-3-2-1)/3=-2 \) minutes. The residuals for B are \(12-11=1\), \(13-11=2\), and \(14-11=3\) minutes, so \( \bar{e}_B=(1+2+3)/3=2 \) minutes.
The overall residual mean is \((-3-2-1+1+2+3)/6=0\), but the group means are two minutes below and above zero. In this example, an overall statement that residuals are centered at zero would conceal the opposing group offsets. The residual centers suggest that the intercept-only model misses a systematic difference associated with category. They do not show that category causes longer visits.
Interpret Centers and Spreads Together
A group residual mean clearly away from zero can indicate that the fitted model systematically underpredicts or overpredicts in that group. If \(\bar{e}_k\) is positive, observations in level \(k\) are higher than their fitted values on average; if it is negative, they are lower on average. Compare both the magnitude and the direction across levels, in the response's units.
Residual spread asks a different question: do observations in one group vary around their fitted values more than observations in another? For a model that assumes a common error variance, a visibly larger residual spread in one category can be a warning that the equal-variance assumption may not describe all groups equally well. The diagnostic identifies a pattern to investigate; it does not establish its cause or amount to a formal test.
A crucial detail is whether the categorical predictor is already represented in the fitted model. If it is omitted, differing group residual centers may indicate that the model has not accounted for group-level response differences. If category indicators are included in an ordinary least-squares model with an intercept, group residual means are constrained to be zero. In that case, comparing residual spreads remains informative, while group means cannot be treated as an independent check for offsets.
To see why, write the residual sum of squares as \(SSE=\sum_i e_i^2\). For any one category \(k\), the model has a group-specific fitted level represented by its intercept and indicators. A small change in that group's fitted level changes each fitted value in the group by the same amount. At the least-squares minimum, the derivative of \(SSE\) with respect to this group level is zero:
Therefore \(\sum_{i:G_i=k}e_i=0\), and dividing by \(n_k\) gives \(\bar{e}_k=0\). This argument requires that the fitted model can vary the fitted level for each category and that the least-squares fit includes those observations. It does not imply equal spreads, normal residual distributions, or the absence of other systematic patterns.
Worked Example: Unequal Residual Spread After Including Category
Hypothetical data. A model predicts a laboratory measurement using category A or B, and includes the category in its fitted mean. In both groups the fitted value is 20 units. Suppose the observed values are 19, 20, and 21 for A, and 16, 19, 20, 21, and 24 for B. Compare the residual centers and sample standard deviations.
For A, the residuals are \(-1,0,1\) units. Their mean is \((-1+0+1)/3=0\), and their sample standard deviation is
For B, the residuals are \(-4,-1,0,1,4\) units. Their mean is \((-4-1+0+1+4)/5=0\), and
Both centers are zero, as expected when the category-specific means are fitted by least squares. But B's sample residual standard deviation is nearly three times A's. The pattern is therefore about spread, not an average group offset. It warrants attention if a common residual variance is part of the model's assumptions. Since there are only three observations in A and five in B, the displayed points matter; these small groups do not justify overconfident conclusions.
How an Overall Summary Can Conceal Group Patterns
The overall residual mean is a size-weighted combination of group residual means:
Consequently, a zero overall mean only says that the signed residuals balance across the full data set. It does not require every group mean to be zero. More generally, a single overall measure of spread combines within-group variation with differences between group centers, so it cannot tell you which category contributes to the pattern.
Worked Example: Three Groups With an Overall Mean of Zero
Hypothetical data. A model with one common fitted value predicts a response of 50 units for 16 observations in three categories. The residuals are A: \(0,1,1,2\); B: \(-2,-1,-1,0\); and C: \(-2,-1,-1,0,0,1,1,2\). Compare group centers and spreads with the overall summaries.
The category residual means are \( \bar{e}_A=(0+1+1+2)/4=1 \), \( \bar{e}_B=(-2-1-1+0)/4=-1 \), and \( \bar{e}_C=0/8=0 \), all in response units. The total residual sum is \(4-4+0=0\), so the overall mean is \(0/16=0\). The positive and negative group centers cancel exactly.
For A, the deviations from its mean of 1 are \(-1,0,0,1\), giving \( s_{e,A}=\sqrt{(1+0+0+1)/3}=\sqrt{2/3}=0.816 \) units. For B, the deviations from \(-1\) are \(-1,0,0,1\), so \(s_{e,B}=\sqrt{2/3}=0.816\) units. For C, the mean is zero and the squared residuals sum to \(4+1+1+0+0+1+1+4=12\), giving \(s_{e,C}=\sqrt{12/7}=1.309\) units.
Across all 16 residuals, the squared residuals sum to \(6+6+12=24\). Their overall sample standard deviation is \( \sqrt{24/(16-1)}=\sqrt{1.6}=1.265 \) units. This single number does not reveal that A and B have opposite centers or that C has a larger within-group spread. Grouped summaries expose both features; the overall mean alone exposes neither.
A Practical Diagnostic Routine
List its levels and note whether category indicators are already included in the fitted model. This determines whether group residual centers can show an omitted offset or are constrained by the fit.
Use a common vertical scale and mark the zero line. Show the individual points, and include group counts; a boxplot can supplement, but should not replace, the observations.
Summarize the group means or medians and a spread measure such as the sample standard deviation or interquartile range. Note patterns and possible outliers in the points.
State which category has residuals centered higher or lower, or a wider spread, and in what units. Treat the pattern as a reason to review model adequacy, not as proof of a cause.
Common Mistakes
- Wrong reasoning: “The overall residual mean is zero, so every category is well fitted.” Correction: group means can cancel. Calculate or plot the residual center within each level.
- Wrong reasoning: “The category residual means are zero, so there is no category pattern.” Correction: when category indicators are in an ordinary least-squares model, those means are zero by construction. Compare spreads and inspect the individual residuals as well.
- Wrong reasoning: “A wider residual spread proves the category causes greater variability.” Correction: a diagnostic plot describes observed model residuals. It does not identify a causal mechanism, and small groups may produce unstable summaries.
- Wrong reasoning: “The residual standard deviations are similar, so the groups are identical.” Correction: similar spreads say nothing by themselves about group centers or other patterns. Read center, spread, sample size, and individual points together.
Check Your Understanding
Try these questions using the definitions and results above.
- A model omitting a two-level categorical predictor has an overall residual mean of zero. What additional group summaries would help reveal whether residual centers differ?
- Why are residual means zero within levels of a categorical predictor represented by indicators in an ordinary least-squares model with an intercept?
- A category has residual mean near zero but a much larger sample standard deviation than other categories. What kind of model feature might this pattern prompt you to investigate?
- Why should residual plots by category display group counts as well as points or boxplots?
- Can a residual pattern across category levels establish that the categorical predictor causes differences in the response? Explain.