Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In 2008 a US college pulled together a table of its faculty: for each of 397 professors, the table recorded rank, academic discipline, years since the PhD, years of service, sex, and the nine-month academic salary. The table was assembled for a specific reason, to monitor whether men and women were paid differently. That is a question with real stakes, and it is also a question that a single number cannot answer well. The average male salary in this dataset is about $115,090 and the average female salary is about $101,002, a raw gap of roughly $14,088. Is that gap the whole story?

Almost certainly not, because salary depends on more than one thing. It depends heavily on rank: full professors out-earn assistant professors by a wide margin, as Figure 1 shows. It depends on discipline and on years of service. If the mix of ranks, disciplines, and experience differs between the two groups, then a raw average difference blends the effect of sex with the effects of everything else. To say anything careful, we need a model that can hold those other variables fixed. But rank, discipline, and sex are not numbers. They are categories, and so far every predictor in this book has been a measured quantity like lot size or years. This chapter is about putting categories into a regression.

A boxplot with jittered points of academic salary in thousands of dollars for three faculty ranks, Assistant, Associate, and Full. Salaries climb from a tight cluster around 80 thousand for Assistant professors, to about 95 thousand for Associate, to a wide spread centered near 127 thousand for Full professors. Red diamonds mark the group means at each rank.

Figure 1:Nine-month academic salary rises steeply with rank, from a mean near 81k for assistant professors to about 127k for full professors. A predictor with three named categories like this one cannot go into a regression as a single number; this chapter shows how to encode it.

By the end you will be able to turn a categorical predictor into indicator variables and read each coefficient. You will combine a category with a continuous predictor to fit parallel or separate lines, test which fit the data supports, and model curvature with polynomials the right way. And you will see that the one-way analysis of variance you may remember from intro stats is just a regression on indicators. The salary data carries us through the categorical part; the classic cars stopping-distance data carries us through the polynomial part.

11.1 From categories to numbers: indicator coding

Intuition

A regression equation multiplies each predictor by a number, so a predictor has to be a number. “Rank” is not: its values are the words Assistant, Associate, and Full. You might be tempted to code them 1, 2, 3 and feed that in. Resist. Coding rank as 1, 2, 3 forces a specific and probably false claim on the data. It says the salary step from Assistant to Associate equals the step from Associate to Full, because both are a move of one unit on your invented scale. The categories have an order, but nothing says the gaps are equal, and a nominal predictor like discipline or sex has no order at all.

The honest way to carry a category into a regression is with indicator variables (Definition 11.1), also called dummy variables: 0-or-1 columns, each answering a yes-or-no question (“is this professor a full professor?”). Indicators let each category have its own level, free of any assumption about spacing. The only subtlety is that you do not use one indicator per category. You use one fewer, and let the left-out category set the baseline. Figure 2 shows the idea for the three ranks: one group becomes the baseline, and the coefficients measure how far each other group sits above it.

Three group means for salary by rank plotted as dots, Assistant at 80.8 thousand, Associate at 93.9, and Full at 126.8. A dashed horizontal line sits at the Assistant mean and is labeled b-zero equals 80.8. Red arrows point up from that baseline to the Associate dot, labeled b-one equals plus 13.1, and to the Full dot, labeled b-two equals plus 46.0.

Figure 2:Reference-cell coding for rank. The intercept is the reference group’s mean (Assistant, 80.8k), and each coefficient is the vertical gap from that baseline up to another group’s mean: +13.1k to Associate, +46.0k to Full.

Formula

Suppose a categorical predictor (a factor, Definition 11.2) has kk levels. Pick one level as the reference (or baseline). For each of the other k1k-1 levels, define an indicator variable that is 1 when a case is in that level and 0 otherwise. For the three ranks, taking Assistant as the reference, define

X1={1if Associate0otherwise,X2={1if Full0otherwise.X_1 = \begin{cases} 1 & \text{if Associate} \\ 0 & \text{otherwise} \end{cases}, \qquad X_2 = \begin{cases} 1 & \text{if Full} \\ 0 & \text{otherwise} \end{cases}.

Figure 3 shows this coding at work on the first few professors in the data: the single column of rank words becomes two 0-or-1 columns, and the assistant professor’s row is just a pair of zeros.

A table with a left column labeled rank in words, listing Full, Full, Assistant, Full, Full, Associate for six professors, and an arrow labeled encode pointing to two indicator columns on the right, the Associate indicator X-one and the Full indicator X-two. Each Full row shows 0 and 1, each Associate row shows 1 and 0, and the Assistant row shows 0 and 0, marked with a red note reading Assistant equals 0 0, both zeros are its code.

Figure 3:Reference-cell coding in action. One column of category words becomes k1k-1 indicator columns of zeros and ones. A full professor is coded (0, 1) and an associate (1, 0); the assistant professor gets no column of its own, and its pair of zeros is the code for the reference level.

The model with these two indicators is

Yi=β0+β1Xi1+β2Xi2+εi.Y_i = \beta_0 + \beta_1 X_{i1} + \beta_2 X_{i2} + \varepsilon_i .

In words: the mean salary is a baseline β0\beta_0, plus an extra β1\beta_1 if the professor is an associate, plus an extra β2\beta_2 if the professor is full. Read the mean response one level at a time by plugging in the indicators:

E{YAssistant}=β0,E{YAssociate}=β0+β1,E{YFull}=β0+β2.E\{Y \mid \text{Assistant}\} = \beta_0, \qquad E\{Y \mid \text{Associate}\} = \beta_0 + \beta_1, \qquad E\{Y \mid \text{Full}\} = \beta_0 + \beta_2 .

So β0\beta_0 is the mean of the reference group, β1\beta_1 is the Associate-minus-Assistant difference in means, and β2\beta_2 is the Full-minus-Assistant difference. The coefficients are contrasts against the baseline, nothing more.

This is reference-cell coding (Definition 11.3; R and Python call it treatment coding). It is the default in both languages, and the one this book uses throughout. Chapter 13 will code the predictors of a logistic regression exactly the same way (11.1 From categories to numbers: indicator coding is the pattern it reuses).

Derivation (why k1k-1 indicators, not kk)

The reason is easier to see than to say. Figure 4 lays out the design matrix with the intercept column and all three rank indicators. In every row exactly one indicator is a 1, so adding the three indicator columns together rebuilds the intercept column of ones. One column is then a copy of the others combined, which is the linear dependence that sinks the fit.

A table with an intercept column of all ones on the left, and three indicator columns on the right, one for Assistant, Associate, and Full. In each of the six rows exactly one of the three indicators is 1 and the other two are 0. A red bracket under the three indicator columns is labeled add these three columns arrow a column of all ones, with an equals sign pointing back to the intercept column. A caption above reads X-Asst plus X-Assoc plus X-Full equals one, redundant with the intercept.

Figure 4:Keeping all kk indicators alongside the intercept is redundant. Because every professor holds exactly one rank, the three indicator columns add up row by row to the intercept column of ones. That linear dependence makes XX\mathbf{X}'\mathbf{X} singular, so we drop one indicator and let its level become the baseline.

Proof. Suppose you stubbornly include all kk indicators, X1,,XkX_1, \dots, X_k, one per level, alongside the intercept column of ones. Every case belongs to exactly one level, so exactly one of its indicators is 1 and the rest are 0. Adding the indicator columns across all levels therefore reconstructs the all-ones column:

X1+X2++Xk=1.X_1 + X_2 + \cdots + X_k = \mathbf{1} .

That is a linear dependence among the columns of the design matrix X\mathbf{X}. From 7.1 The model and least squares in matrix form, the least-squares estimate b=(XX)1XY\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} needs XX\mathbf{X}'\mathbf{X} to be invertible, which requires the columns of X\mathbf{X} to be linearly independent (full column rank). With the intercept plus all kk indicators, they are not: the coefficients are not uniquely determined, because you could add any constant to β0\beta_0 and subtract it from every level’s effect and get the identical fitted values. Drop one indicator (equivalently, drop the intercept) and the dependence is gone. Reference-cell coding drops the reference level’s indicator, keeps the intercept, and gives a full-rank design with k1k-1 indicators. \blacksquare

This “one fewer than the number of levels” fact has a name you will meet again: a factor with kk levels contributes k1k-1 degrees of freedom to the model.

R

R stores categories as factors and builds the indicators for you. Read the data and look at the categorical columns.

salaries <- read.csv("data/salaries.csv")
dim(salaries)
table(salaries$rank)
table(salaries$discipline)
table(salaries$sex)
[1] 397   6

AssocProf  AsstProf      Prof
       64        67       266

  A   B
181 216

Female   Male
    39    358

By default R orders factor levels alphabetically and makes the first one the reference. For rank that would make AssocProf the baseline, which is awkward to read. We reorder so that Assistant is the reference, matching Figure 2.

You can see the indicators themselves in the model’s design matrix. The first column is the intercept (all ones), and the next two are the Associate and Full indicators.

head(model.matrix(fit_rank), 4)
  (Intercept) rankAssocProf rankProf
1           1             0        1
2           1             0        1
3           1             0        0
4           1             0        1
print(fit_rank.model.exog[:4])
[[1. 0. 1.]
 [1. 0. 1.]
 [1. 0. 0.]
 [1. 0. 1.]]

Rows 1, 2, and 4 are full professors (Full indicator 1); row 3 is an assistant professor (both indicators 0). No row ever has both indicators equal to 1, because no professor holds two ranks at once.

Coefficients that are really differences of means are easier to trust once you have moved the means yourself and watched the coefficients answer.

Three sliders set each rank’s mean salary, and the readouts show the intercept and indicator coefficients a regression on rank would report.

11.2 One categorical and one continuous predictor

Intuition

A category by itself just sorts cases into groups. The interesting models mix a category with a measured predictor. Salary, for instance, depends both on discipline (a category, theoretical A or applied B) and on years of service (a number). We now have two knobs, and there are three increasingly flexible ways to turn them, drawn schematically in Figure 6.

Three side-by-side panels each showing a response Y against a continuous predictor X for two groups A and B, drawn as a solid line and a dashed line. Left panel, common line: the two lines lie exactly on top of each other. Middle panel, parallel lines: the two lines have the same slope but the dashed group-B line is shifted up. Right panel, separate slopes: the two lines start close together but the dashed line rises more steeply and they fan apart.

Figure 6:The three models for one continuous and one categorical predictor. Common line ignores the group; parallel lines give each group its own intercept but a shared slope; separate slopes give each group its own slope as well, which is what an interaction term does.

The simplest model ignores the category and fits one line to everybody. The next model, the subject of this section, gives each discipline its own intercept but makes them share a single slope: two parallel lines. Reading it is easy: experience is worth the same number of dollars per year in both disciplines, and one discipline simply sits a fixed amount above the other at every experience level. This parallel-lines model has a traditional name, analysis of covariance or ANCOVA (Definition 11.5), from the days when the continuous predictor was called a covariate and the category was the treatment. The third model, separate slopes, is the next section.

Formula

Let XX be years of service and let DD be the discipline-B indicator (D=1D = 1 for applied, D=0D = 0 for theoretical). The parallel-lines model is

Yi=β0+β1Xi+β2Di+εi.Y_i = \beta_0 + \beta_1 X_i + \beta_2 D_i + \varepsilon_i .

In words: salary is a baseline, plus a per-year effect of service that both disciplines share, plus a fixed bump for being in discipline B.

Split it by discipline to see the two lines:

Theoretical (D=0):E{Y}=β0+β1X,Applied (D=1):E{Y}=(β0+β2)+β1X.\text{Theoretical } (D=0): \quad E\{Y\} = \beta_0 + \beta_1 X, \\ \text{Applied } (D=1): \quad E\{Y\} = (\beta_0 + \beta_2) + \beta_1 X .

In words: both lines have slope β1\beta_1; the applied line is shifted up by β2\beta_2. The two lines never cross, because they are parallel. The reading of β2\beta_2 is a genuine “holding years of service fixed” comparison: at any given experience, applied faculty are paid β2\beta_2 more on average, exactly the kind of adjusted comparison the chapter opened by asking for.

R

Scatterplot of salary in thousands against years of service, with points colored by discipline, blue for theoretical A and orange for applied B. Two straight fitted lines with the same slope are drawn, the orange applied line sitting a constant distance above the blue theoretical line across the whole range of service.

Figure 7:The parallel-lines model. Both disciplines share one slope for years of service, and the applied line sits about 13.2k above the theoretical line at every experience level. Parallel lines are the visual signature of a model with no interaction.

11.3 Interactions: letting the slopes differ

Intuition

The parallel model makes a strong assumption: a year of service is worth the same in both disciplines. Maybe it is not. Maybe applied faculty, whose skills track a hot outside job market, gain more per year than theoretical faculty. If so, the two lines should have different slopes, fanning apart as service grows rather than staying a fixed distance apart. Letting the slopes differ is what an interaction (Definition 11.6) does. An interaction between a category and a continuous predictor says the effect of the continuous predictor depends on the category.

You build it by adding one more column: the product of the continuous predictor and the indicator. That product is zero for the reference group (so its line is unchanged) and equals the continuous predictor for the other group (so that group gets an extra slope term).

Formula

With XX = years of service and DD = discipline-B indicator, the interaction model is

Yi=β0+β1Xi+β2Di+β3(XiDi)+εi.Y_i = \beta_0 + \beta_1 X_i + \beta_2 D_i + \beta_3 (X_i D_i) + \varepsilon_i .

In words: take the parallel-lines model and add one product term, which lets discipline B have its own slope instead of sharing the baseline slope.

Split by discipline again:

Theoretical (D=0):E{Y}=β0+β1X,Applied (D=1):E{Y}=(β0+β2)+(β1+β3)X.\text{Theoretical } (D=0): \quad E\{Y\} = \beta_0 + \beta_1 X, \\ \text{Applied } (D=1): \quad E\{Y\} = (\beta_0 + \beta_2) + (\beta_1 + \beta_3) X .

In words: the theoretical line has intercept β0\beta_0 and slope β1\beta_1; the applied line has intercept β0+β2\beta_0 + \beta_2 and slope β1+β3\beta_1 + \beta_3. So β2\beta_2 is the gap between the two intercepts (the difference at X=0X = 0), and β3\beta_3 is the gap between the two slopes (the extra dollars per year that applied faculty gain over theoretical faculty). If β3=0\beta_3 = 0 the slopes are equal and we are back to parallel lines, which is why testing β3=0\beta_3 = 0 is the test for “do the slopes differ.”

The wrong reading, first

Here is where careful reading matters, because the interaction model sets a trap. Look at the fitted coefficients (Example 11.3 below computes them): the discipline coefficient β2\beta_2 comes out to about $857 with a large standard error and a p-value of 0.86, nowhere near significant. A hurried reader concludes: “discipline does not matter for salary.” That reading is wrong, and the mistake is worth dwelling on.

In the interaction model, β2\beta_2 is not “the effect of discipline.” It is only the gap between the disciplines at X=0X = 0, that is, for a professor with zero years of service. At zero service the two lines nearly meet, so of course that particular gap is small and uncertain. But the lines are not parallel; the applied line climbs faster. Out where the actual faculty live, at 20 or 30 years of service, the disciplines are far apart. Figure 8 makes the point: β2\beta_2 measures the gap at the far-left edge of the plot, which says nothing about the gap in the body of the data.

Two fitted salary lines against years of service, blue for theoretical A and dashed orange for applied B. At X equals zero on the far left the two lines nearly touch, and a small red double arrow labels the gap there as b-two equals 0.9k, not significant. At 40 years of service a tall green double arrow shows a much larger gap of 28.7k between the lines, computed as 0.9 plus 0.695 times 40.

Figure 8:The trap in reading an interaction model. The discipline coefficient b-two is only the vertical gap at X = 0 (red arrow, tiny and not significant). Because the slopes differ, the gap out in the data is large (green arrow, 28.7k at 40 years). A small, non-significant b-two does not mean the groups are alike.

The correct statement is: the interaction coefficient β3\beta_3 is significant, so the slopes differ, and the discipline gap grows with service. The gap at a specific service level XX is β2+β3X\beta_2 + \beta_3 X, not β2\beta_2 alone. Whenever a model contains an interaction, the coefficient on a main effect is a conditional statement about the point where the other predictor is zero, never a blanket “effect.”

R

Scatterplot of salary in thousands against years of service, points colored by discipline, blue for theoretical A and orange for applied B. Two fitted lines with different slopes start close together at low service and fan apart as service increases, the orange applied line climbing more steeply than the blue theoretical line.

Figure 9:The separate-slopes model. The applied line is steeper, so the two disciplines start close and fan apart with experience. The distance between them at any service level is the interaction coefficient times that service, plus the small intercept gap.

Testing whether the slopes really differ

Eyeballing a fanned pair of lines is not proof. The interaction added exactly one term to the parallel model, so we can ask whether that one term earns its place with the general linear test of 8.4 The general linear test: fit the reduced (parallel) model and the full (interaction) model, and compare their error sums of squares. Here the reduced model is fit_par from Example 11.2 and the full model is fit_int.

anova(fit_par, fit_int)
Analysis of Variance Table

Model 1: salary ~ yrs.service + discipline
Model 2: salary ~ yrs.service * discipline
  Res.Df        RSS Df  Sum of Sq      F   Pr(>F)
1    394 3.0594e+11
2    393 2.9807e+11  1 7864744229 10.369 0.001388 **
from statsmodels.stats.anova import anova_lm
print(anova_lm(fit_par, fit_int))
   df_resid           ssr  df_diff       ss_diff          F    Pr(>F)
0     394.0  3.059377e+11      0.0           NaN        NaN       NaN
1     393.0  2.980729e+11      1.0  7.864744e+09  10.369424  0.001388

The FF statistic is 10.37 on 1 and 393 degrees of freedom, with a p-value of 0.0014. That p-value equals the p-value of the interaction coefficient’s tt test above, and indeed t2=3.222=10.37=Ft^2 = 3.22^2 = 10.37 = F, the same equivalence between the tt and FF tests you saw for a single slope in 3.7 The F test and its equivalence to the t test. Adding the interaction significantly reduces the error sum of squares, so the data support separate slopes over parallel lines.

The trap in reading β2\beta_2 is much harder to fall for after you have watched the gap grow under your own finger.

Move the interaction coefficient b3 and the year you read the gap at, and compare the gap at zero years with the gap out where the faculty actually are.

Adjustment and the salary-equity question

We opened with a raw male-minus-female gap of about $14,088. Now we can ask what happens to that gap when the model holds rank, discipline, and years of service fixed. This is the statistical heart of an equity analysis: a raw difference can shrink, vanish, or grow once you account for other measured variables, and honest reporting shows the estimate before and after adjustment.

A dot-and-interval plot of the estimated male minus female salary difference in thousands of dollars. The unadjusted estimate, from salary regressed on sex alone, sits at 14.1k with a 95 percent interval clearly above zero. The adjusted estimate, from the model that also includes rank, discipline, and service, sits at 4.8k with a wider interval that crosses the dashed zero line.

Figure 11:The estimated sex difference before and after adjustment. Controlling for rank, discipline, and years of service moves the estimate from 14.1k (interval above zero) to 4.8k (interval crossing zero). The adjusted estimate is an association among otherwise-similar faculty, not a causal effect.

11.4 Polynomial regression and centering

Intuition

Categories are one way a straight line can fail to fit; curvature is another. Stopping distance does not grow in a straight line with speed. Physics says braking distance rises with the square of speed, because kinetic energy does, and the classic cars data (50 cars from the 1920s) shows exactly the upward bend, drawn in Figure 12. A straight line undershoots at the high speeds where the curve turns up. These are the same cars data whose residual funnel drove the square-root transformation in 10.3 Box-Cox: letting the data choose the power; there we reshaped the response, and here we leave it alone and let the mean function bend instead.

Scatterplot of stopping distance in feet against speed in miles per hour for fifty cars, with two fitted curves. A blue straight line rises steadily, and a red quadratic curve bends gently upward, sitting slightly higher than the line at the highest speeds and slightly higher at the very lowest speeds. The scatter of points widens as speed increases.

Figure 12:Stopping distance against speed with a straight line (blue) and a quadratic curve (red). The quadratic bends upward to match the physics that distance grows faster than linearly with speed, though for these data the improvement over the line is modest.

We can bend a regression line by adding the square of the predictor as a second term. A polynomial regression (Definition 11.7) keeps the model linear in its coefficients (so all the least-squares machinery still applies) while making the fitted curve a parabola, or a cubic, or higher. The one practical wrinkle is that a predictor and its square are usually strongly related to each other, which makes the raw coefficients unstable and hard to read. The fix is centering (Definition 11.8): subtract the mean of the predictor before squaring.

Formula

The second-order (quadratic) polynomial model in a predictor XX is

Yi=β0+β1Xi+β2Xi2+εi.Y_i = \beta_0 + \beta_1 X_i + \beta_2 X_i^2 + \varepsilon_i .

In words: the straight-line model with one extra column, the square of the predictor, which lets the fitted curve bend instead of staying straight.

The centered version replaces XX by x=XXˉx = X - \bar X:

Yi=β0+β1xi+β2xi2+εi,xi=XiXˉ.Y_i = \beta_0^{\ast} + \beta_1^{\ast} x_i + \beta_2^{\ast} x_i^2 + \varepsilon_i, \qquad x_i = X_i - \bar X .

In words: measure the predictor as a deviation from its average before squaring. Centering does not change the shape of the fitted curve or any fitted value. It only shifts what the linear and intercept terms mean, and it sharply reduces the correlation between the first-order and second-order columns. After centering, β0\beta_0^{\ast} is the fitted mean at the average speed and β1\beta_1^{\ast} is the slope of the curve at the average speed, both readable numbers.

Why centering helps

The raw predictor speed runs from 4 to 25, so speed and speed^2 both increase together across that whole range; their correlation in the cars data is 0.98. Two columns that move almost in lockstep carry nearly the same information, and the regression struggles to divide credit between them: the individual coefficients get large standard errors and can even flip sign. Subtracting the mean recenters the predictor on zero, so its square becomes small in the middle and large at both ends, a U-shape that no longer tracks the linear term. Figure 13 shows the correlation collapsing from 0.98 to about -0.10 after centering.

Two side-by-side scatterplots. Left, raw speed on the horizontal axis against speed squared on the vertical axis, the points falling almost perfectly along an upward curve, labeled r equals 0.98. Right, centered speed against centered speed squared, the points forming a U shape scattered around a nearly flat trend, labeled r equals minus 0.10.

Figure 13:Centering breaks the near-perfect correlation between a predictor and its square. On the raw scale speed and speed-squared move together (r = 0.98); after subtracting the mean, the centered predictor and its square are nearly uncorrelated (r = -0.10), which stabilizes the polynomial’s coefficients.

R

Claiming that centering changes the coefficients but not the curve is one thing; sliding the centering point yourself and seeing the curve hold still is another.

The quadratic is refitted for every centering point c, so you can watch SE(b1) and the correlation between the linear and squared columns move while the curve and its prediction stay put.

The hierarchy principle and choosing the degree

Two rules keep polynomial models sane.

First, the hierarchy principle (Definition 11.9): if a model includes a higher-order term, keep all the lower-order terms below it, even if they look non-significant. A model with X2X^2 but no XX forces the parabola’s vertex to sit at X=0X = 0. That is an arbitrary constraint the data never asked for, and it is not even preserved if you shift the predictor’s origin. Keep XX whenever you keep X2X^2. The same logic says keep both main effects whenever you keep their interaction, which is why the interaction models in section 11.3 always carried both yrs.service and discipline.

Second, do not chase degree. Each extra power bends the curve more and will always fit the sample a little better, but high-degree polynomials wiggle wildly between and beyond the data points. Figure 15 fits degrees 1, 2, and 5 to the cars data: the quadratic tracks the gentle bend, while the degree-5 curve starts chasing individual points and swings at the ends, the classic face of overfitting. Test whether the added curvature is real rather than reading individual high-order coefficients.

Scatterplot of stopping distance against speed with three fitted polynomial curves. The degree-1 blue line and degree-2 red quadratic both rise smoothly through the cloud of points. The degree-5 violet dashed curve wiggles, dipping and rising to chase individual points, and swings sharply upward at the highest speeds.

Figure 15:Fitting polynomials of degree 1, 2, and 5 to the same fifty points. The quadratic captures the real upward bend; the degree-5 curve overfits, wiggling between points and swinging at the ends where data is sparse. More flexibility is not more truth.

We test the quadratic the same way we tested the interaction, with the general linear test: is the reduced (linear) model beaten by the full (quadratic) model?

anova(fit_lin, fit_quad)
predict(fit_quad, newdata = data.frame(speed = 20))
predict(fit_quad_c, newdata = data.frame(speed_c = 20 - mean(cars$speed)))
Analysis of Variance Table

Model 1: dist ~ speed
Model 2: dist ~ speed + I(speed^2)
  Res.Df   RSS Df Sum of Sq     F Pr(>F)
1     48 11354
2     47 10825  1    528.81 2.296 0.1364
       1
60.71961
       1
60.71961
print(anova_lm(fit_lin, fit_quad))
print(fit_quad.predict(pd.DataFrame({"speed": [20]})))
xc = 20 - cars["speed"].mean()
print(fit_quad_c.predict(pd.DataFrame({"speed_c": [xc]})))
   df_resid           ssr  df_diff     ss_diff         F    Pr(>F)
0      48.0  11353.521051      0.0         NaN       NaN       NaN
1      47.0  10824.715908      1.0  528.805143  2.296027  0.136402
0    60.719611
dtype: float64
0    60.719611
dtype: float64

Two things to read here. The FF for the quadratic term is 2.30 with a p-value of 0.14: for these 50 cars the upward bend is suggestive but not decisive, and a straight line is defensible. The honest report is that the curvature is in the expected direction but not established at conventional significance, exactly the kind of judgment call this course asks you to make and state rather than hide. Second, both the raw and centered models predict the identical stopping distance of 60.72 feet at 20 mph, confirming that centering is a change of parametrization, not a change of model.

Overfitting is easier to believe when you make it happen yourself, one degree at a time.

One slider sets the polynomial degree fitted to the fifty cars, and the readouts track SSE, R-squared, the general linear test against the straight line, and the prediction five miles an hour past the data.

A brief look at piecewise regression

Polynomials bend a single smooth curve across the whole range. Sometimes a relationship instead changes slope at a particular point: a dose that helps up to a threshold and hurts after, a tax rate that kicks in at an income level. For these you can fit piecewise linear regression (Definition 11.10), two or more straight segments joined at chosen points called knots. The trick is one extra column: for a knot at cc, add the “hinge” predictor (Xc)+(X - c)_+, which equals XcX - c when X>cX > c and 0 otherwise. Its coefficient is the change in slope at the knot, and because the hinge is continuous the two segments meet without a jump. Figure 17 shows a two-segment fit with a knot at X=5X = 5. This is the simplest member of the spline family; the course does not pursue splines further, but the idea, add hinge columns to let the slope change at knots, is a natural extension of everything in this chapter.

Scatterplot of synthetic data rising with X, with a fitted curve made of two straight segments that meet at a vertical red dashed line marking a knot at X equals 5. The first segment rises gently and the second segment, after the knot, rises more steeply. The two segments join continuously at the knot.

Figure 17:Piecewise linear regression with a knot at X = 5. Adding one hinge column lets the slope change at the knot while the two line segments stay joined. This is the simplest spline, and it uses the same add-a-column idea as indicators and polynomials.

11.5 Analysis of variance as regression

Intuition

If you took intro statistics you likely met one-way analysis of variance (ANOVA, Definition 11.11), the FF test for whether several group means are equal, and you may have met it as a separate technique with its own formulas. It is not separate. One-way ANOVA is exactly the regression of the response on the group factor, coded with indicators, which is the model of section 11.1. Seeing this collapses two things you learned as one and lets every tool of regression, diagnostics, intervals, the general linear test, work on group-comparison problems.

The key fact is that when the only predictor is a factor, the fitted value for every case is just its own group’s sample mean. So the regression “curve” is a set of flat levels, one per group, as Figure 18 shows for the three ranks. Nothing else could minimize the squared error within each group, because the mean is the least-squares constant.

Salary in thousands for the three faculty ranks, shown as faint jittered points in three vertical clusters, with a thick red horizontal bar at each cluster marking the group mean, at about 81, 94, and 127 thousand. A dashed horizontal line marks the grand mean near 114 thousand. The fitted value for every point is its own group's mean.

Figure 18:One-way ANOVA is regression on rank indicators. The fitted value for each professor is simply that rank’s group mean (red bars), so the model is a step function across the groups. The regression F test for the rank factor is identical to the one-way ANOVA F test.

Formula

The one-way ANOVA model is usually written

Yij=μ+τi+εij,Y_{ij} = \mu + \tau_i + \varepsilon_{ij},

for observation jj in group ii, where μ\mu is a baseline and τi\tau_i is group ii’s effect. With reference-cell coding this is precisely Y=β0+β1X1++βk1Xk1+εY = \beta_0 + \beta_1 X_1 + \cdots + \beta_{k-1} X_{k-1} + \varepsilon from section 11.1: μ\mu is the reference group’s mean β0\beta_0, and each τi\tau_i is a coefficient β\beta, the difference of a group’s mean from the reference. The ANOVA null hypothesis that all group means are equal,

H0:τ1=τ2==τk=0,H_0: \tau_1 = \tau_2 = \cdots = \tau_k = 0,

is the regression hypothesis that all the indicator coefficients are zero, H0:β1==βk1=0H_0: \beta_1 = \cdots = \beta_{k-1} = 0: the factor explains nothing. That is tested by the model’s overall FF statistic.

R

This bridge pays off in both directions. Everything you know about ANOVA (that a significant FF says the means differ somewhere, not which pair) transfers to reading a factor’s overall test in a regression. And everything regression offers (residual diagnostics, confidence intervals for specific contrasts, adding a continuous covariate to get the ANCOVA of section 11.2) is now available for problems you might have thought needed a separate ANOVA toolkit. Two levels of the factor make it the two-sample tt test; more levels make it one-way ANOVA; add a covariate and it is ANCOVA. All one model.

11.6 Chapter summary

You can now bring categories and curves into a regression. You encode a kk-level factor with k1k-1 reference-cell indicators, and you can say why one indicator must be dropped: including all kk plus the intercept makes the design rank-deficient (Theorem 11.4). You read each indicator coefficient as a difference of group means and the intercept as the reference-group mean. You combine a category with a continuous predictor three ways: a common line, parallel lines (the ANCOVA model), and separate slopes (an interaction), and you test between the parallel and separate models with the general linear test of 8.4 The general linear test. You know the trap that a main-effect coefficient in an interaction model is only the effect at the other predictor’s zero. You fit a polynomial, center it so its coefficients are stable and interpretable, apply the hierarchy principle, and resist over-fitting the degree. And you see one-way ANOVA as nothing but regression on indicators, with the same FF test (Theorem 11.12).

Key results at a glance.

ResultStatement or formulaValid when
Reference-cell model (Def 11.3)Y=β0+j=1k1βjXj+εY = \beta_0 + \sum_{j=1}^{k-1}\beta_j X_j + \varepsilonone kk-level factor, one level chosen as reference
Indicator coefficientβj=E{Ylevel j}E{Yref}\beta_j = E\{Y\mid\text{level }j\} - E\{Y\mid\text{ref}\}reference-cell coding
k1k-1 indicators (Theorem 11.4)X1++Xk=1X_1+\cdots+X_k=\mathbf{1} forces rank deficiency; drop oneintercept kept in the model
Parallel lines / ANCOVA (Def 11.5)Y=β0+β1X+β2D+εY = \beta_0 + \beta_1 X + \beta_2 D + \varepsilonone continuous, one categorical, equal slopes
Separate slopes / interaction (Def 11.6)Y=β0+β1X+β2D+β3XD+εY = \beta_0 + \beta_1 X + \beta_2 D + \beta_3 X D + \varepsilonslopes allowed to differ by group
Between-group gap (interaction)gap(X)=β2+β3X\text{gap}(X) = \beta_2 + \beta_3 Xequals β2\beta_2 only at X=0X = 0
Test of equal slopesgeneral linear test, full vs reduced (t2=Ft^2 = F)nested parallel vs interaction models
Quadratic model (Def 11.7)Y=β0+β1X+β2X2+εY = \beta_0 + \beta_1 X + \beta_2 X^2 + \varepsiloncurvature; still linear in coefficients
Centering (Def 11.8)x=XXˉx = X - \bar X; curvature β2\beta_2 unchangedto cut linear-square correlation, stabilize coefficients
Hierarchy principle (Def 11.9)keep XX with X2X^2; keep main effects with interactionany higher-order term present
One-way ANOVA = regression (Theorem 11.12)F=MSR/MSEF = \mathrm{MSR}/\mathrm{MSE} equals the ANOVA FFa single factor is the only predictor

Key terms. indicator (dummy) variable, factor, level, reference-cell coding, effects coding, cell-means coding, analysis of covariance (ANCOVA), parallel lines, interaction, main effect, polynomial regression, centering, hierarchy principle, knot, piecewise linear regression, spline, one-way analysis of variance (ANOVA).

You should now be able to.

Where this fits. In the workflow spine of The modeling workflow, this chapter is mostly FIT: it widens the kinds of predictors a linear model can hold, from measured quantities to categories, curves, and their combinations, without leaving the least-squares framework of 7.1 The model and least squares in matrix form. It leans on the general linear test of 8.4 The general linear test to decide between nested models, which is the CHECK-inside-FIT loop the course uses whenever a model gains a term. It also serves USE: interpreting an interaction, or an adjusted coefficient, is the communication step where most real mistakes happen, which is why the chapter spent so long on wrong readings. Looking ahead, 11.1 From categories to numbers: indicator coding is the exact machinery Chapter 13 uses to put factors into a logistic regression, and the centering and hierarchy habits carry into Chapter 12, where correlated predictors (including a predictor and its own square) drive the study of multicollinearity.

11.7 Frequently asked questions

Q1. Which category should I make the reference? Any of them; the fit is identical, only the coefficients’ meaning changes. Pick the reference that makes the comparisons you care about easiest to read: a control group, the largest group, or a natural baseline like “assistant professor.” If a coefficient’s sign feels backwards, you may just want to relevel so the other category is the reference.

Q2. Do I ever include an indicator for every level? Only if you drop the intercept. The intercept column plus all kk indicators are linearly dependent, so software will either drop one automatically or (with the intercept removed) switch to cell-means coding, where each coefficient is a group mean. Never include the intercept and all kk indicators; the model is not identifiable.

Q3. My interaction is significant but one main effect is not. Do I drop the main effect? No. The hierarchy principle says keep both main effects whenever you keep their interaction. A non-significant main effect in an interaction model usually just means the groups are similar at the point where the other predictor is zero, which is rarely an interesting place and often outside the data. Dropping it distorts the model.

Q4. Why not just fit separate regressions to each group instead of one interaction model? Fitting each group separately gives the same fitted lines as the full interaction model, so for prediction it is fine. But the single interaction model lets you test whether the slopes differ (one coefficient, one p-value) and pools the groups to estimate a common error variance, which separate fits cannot do. When the question is “are these groups different,” one model with an interaction is the cleaner tool.

Q5. Does centering change my predictions or my R2R^2? No. Centering is a change of parametrization: it shifts the meaning of the intercept and linear coefficient but leaves every fitted value, residual, R2R^2, and FF statistic untouched. Example 11.5 shows the raw and centered quadratics predicting the identical 60.72 feet at 20 mph. Center for interpretation and numerical stability, not to change the fit.

Q6. How high a polynomial degree should I go? Rarely past two or three, and only with a test to back it up. Each degree fits the sample better but generalizes worse, wiggling between points (Figure 15). Use the general linear test to check whether the added term reduces error significantly, prefer the lowest degree that captures the visible shape, and never extrapolate a high-degree polynomial beyond the data, where it can shoot off to absurd values.

Q7. The adjusted sex coefficient is not significant. Does that prove pay is fair? No. It says that in this sample and this model, once rank, discipline, and service are held fixed, the remaining male-minus-female difference is not distinguishable from zero, with a wide interval driven partly by there being only 39 women. It does not establish equity, and it depends on the choice of adjustment variables; if rank itself is affected by sex, adjusting for it can hide part of the difference. Report the numbers and the assumptions, and let readers judge.

11.8 Practice problems

  1. (A) Explain why coding the three ranks as a single numeric column 1, 2, 3 imposes an assumption the data reject, and say what that assumption is in one sentence.

  2. (A) In the model salary ~ rank with Assistant as reference, state in words what the intercept and each of the two coefficients mean.

  3. (A) A factor has 5 levels. How many indicator variables does reference-cell coding use, and how many degrees of freedom does the factor contribute? Explain.

  4. (A) Describe the difference between the parallel-lines model and the separate-slopes model in terms of what the two fitted lines look like.

  5. (A) In an interaction model salary ~ yrs.service * discipline, a classmate says “the discipline coefficient is not significant, so discipline does not affect salary.” Correct this statement.

  6. (A) Explain the hierarchy principle in one or two sentences, and give an example of a model that violates it.

  7. (A) Why does centering a predictor before squaring leave every fitted value unchanged while changing the linear coefficient’s value and meaning?

  8. (A) State the connection between one-way ANOVA and regression, and say what the ANOVA between-groups and within-groups sums of squares correspond to in the regression.

  9. (B) Prove that including an intercept plus all kk indicators of a kk-level factor makes the design matrix rank-deficient (Theorem 11.4), by exhibiting the linear dependence among its columns. Explain why this makes the least-squares coefficients non-unique.

  10. (B) In reference-cell coding, prove that the intercept equals the reference group’s sample mean and each coefficient equals a difference of group sample means, for a one-factor model. Use the fact that the least-squares fitted value in each group is that group’s sample mean.

  11. (B) For the parallel-lines model Y=β0+β1X+β2D+εY = \beta_0 + \beta_1 X + \beta_2 D + \varepsilon, write the two fitted lines for D=0D = 0 and D=1D = 1, and prove their vertical distance is β2\beta_2 at every XX.

  12. (B) For the interaction model Y=β0+β1X+β2D+β3XD+εY = \beta_0 + \beta_1 X + \beta_2 D + \beta_3 XD + \varepsilon, derive the fitted gap between the two groups as a function of XX, and show it equals β2\beta_2 only at X=0X = 0.

  13. (B) Show algebraically that the tt test of the interaction coefficient (t=3.22t = 3.22) and the general linear test of parallel-versus-separate slopes (F=10.37F = 10.37) satisfy t2=Ft^2 = F, and explain why this must hold for a single added term (cite 3.7 The F test and its equivalence to the t test).

  14. (B) Let x=XXˉx = X - \bar X. Starting from Y=β0+β1X+β2X2Y = \beta_0 + \beta_1 X + \beta_2 X^2, substitute X=x+XˉX = x + \bar X and collect terms to express β0,β1,β2\beta_0^{\ast}, \beta_1^{\ast}, \beta_2^{\ast} of the centered model in terms of β0,β1,β2\beta_0, \beta_1, \beta_2 and Xˉ\bar X. Confirm β2=β2\beta_2^{\ast} = \beta_2 (the curvature is unchanged).

  15. (B) Using the result of problem 14, explain precisely why the raw and centered quadratics give identical fitted values and identical residuals, so R2R^2 is unchanged.

  16. (B) Prove that in a one-factor regression the least-squares fitted value for each group is the group’s sample mean, by minimizing (Yijmi)2\sum (Y_{ij} - m_i)^2 over the group constants mim_i.

  17. (B) A model with a three-level factor has coefficients b0,b1,b2b_0, b_1, b_2 under reference-cell coding. Derive what the intercept would become under cell-means coding (all three indicators, no intercept), and confirm the fitted values are unchanged.

  18. (B) In the ANCOVA model, show that the estimated slope β1\beta_1 is a pooled within-group slope, and explain in words why pooling assumes the true slopes are equal across groups (the assumption the interaction test checks).

  19. (C) Fit salary ~ discipline and confirm from the coefficients that the intercept is the mean salary in discipline A and the slope is the B-minus-A difference. Report both means.

  20. (C) Fit salary ~ rank with Full professor (Prof) as the reference instead of Assistant. Report the new intercept and coefficients, and verify the fitted group means are identical to the chapter’s.

  21. (C) Fit the parallel model salary ~ yrs.service + sex and the interaction model salary ~ yrs.service * sex. Report the interaction coefficient and its p-value, run the general linear test between the two models, and state whether the slopes differ for men and women.

  22. (C) Reproduce Example 11.4’s adjustment: fit salary ~ sex and salary ~ rank + discipline + yrs.service + sex, report the sex coefficient in each, and write two sentences interpreting the change, being careful not to claim a causal effect.

  23. (C) Fit the raw quadratic dist ~ speed + I(speed^2) and the centered quadratic on cars. Report the correlation between the linear and squared columns in each case, and the standard error of the linear coefficient in each case, and explain the difference.

  24. (C) Fit polynomials of degree 1, 2, and 3 to the cars data. Use the general linear test to decide whether degree 2 improves on degree 1 and whether degree 3 improves on degree 2. State which degree you would report and why.

  25. (C) Using the interaction model salary ~ yrs.service * discipline, predict the salary of a theoretical (A) and an applied (B) professor at 0, 15, and 30 years of service. Tabulate the six predictions and the A-versus-B gap at each service level, and comment on how the gap changes.

  26. (C) Fit dist ~ speed on cars and add the hinge predictor (speed15)+(\text{speed} - 15)_+ to fit a two-segment piecewise model with a knot at 15 mph. Report the two segment slopes (before and after the knot) and say whether the slope increases after the knot.

  27. (C) Verify the ANOVA-regression bridge (Theorem 11.12) numerically: fit salary ~ rank, extract the overall FF from the model summary and the FF from anova() (R) or anova_lm (Python), and confirm they are identical. Report both.

  28. (C) Make a residuals-versus-fitted plot for the linear dist ~ speed fit on cars. Describe any pattern you see (curvature, widening spread) and say which remedy from this chapter or an earlier one each pattern would suggest.

11.9 Exam practice

These five questions match the style of the course exams: each one asks you to explain in full sentences, not just to compute. Read any output given, say which numbers you used, and write your answer the way you would on the exam. Then open the model answer and compare. A correct number with no reasoning earns little credit here, so practice saying why.

EP 11.1 (interpret output in context, then evaluate a claim). A colleague fits salary on years since the PhD, sex, and their interaction, using female as the reference level for sex.

fit <- lm(salary ~ yrs.since.phd * sex, data = salaries)
round(coef(summary(fit)), 4)
                        Estimate Std. Error t value Pr(>|t|)
(Intercept)           73840.7607  8696.7132  8.4907   0.0000
yrs.since.phd          1644.8825   454.6163  3.6182   0.0003
sexMale               20209.6135  9179.2131  2.2017   0.0283
yrs.since.phd:sexMale  -727.9822   468.0468 -1.5554   0.1207

The colleague reads the table and concludes: “The sexMale coefficient is about $20,200 and it is significant, so men are paid about $20,000 more than women.” Evaluate this claim. In your answer, say what each of the four coefficients means in context, and state what the sex gap actually is at a realistic level of experience.

EP 11.2 (explain why). In Example 11.5 the raw quadratic dist ~ speed + I(speed^2) gives the speed coefficient a standard error of about 2.03 (so t=0.45t = 0.45), while the centered quadratic gives the speed_c coefficient a standard error of about 0.41 (so t=9.69t = 9.69). The curvature coefficient, the fitted curve, and R2R^2 are identical in the two fits. Explain why centering shrinks the standard error of the linear coefficient so dramatically, and explain why this is not evidence that the centered model “fits better.”

EP 11.3 (what would change if). Example 11.1 fit salary ~ rank with Assistant as the reference level and reported an intercept of $80,776, a rankAssocProf coefficient of $13,100, and a rankProf coefficient of $45,996. A colleague refits the identical model but sets Full professor (Prof) as the reference instead. Describe what would change in the coefficient table and what would stay exactly the same, and explain why. Give the numerical values the new table would show.

EP 11.4 (interpret output in context, then evaluate a claim). For the cars data a student fits the linear model and the quadratic model and compares them.

fit_lin  <- lm(dist ~ speed, data = cars)
fit_quad <- lm(dist ~ speed + I(speed^2), data = cars)
round(coef(summary(fit_quad)), 4)
anova(fit_lin, fit_quad)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   2.4701    14.8172  0.1667   0.8683
speed         0.9133     2.0342  0.4490   0.6555
I(speed^2)    0.1000     0.0660  1.5153   0.1364

Analysis of Variance Table

Model 1: dist ~ speed
Model 2: dist ~ speed + I(speed^2)
  Res.Df   RSS Df Sum of Sq     F Pr(>F)
1     48 11354
2     47 10825  1    528.81 2.296 0.1364

The student concludes: “The squared-speed coefficient is positive, which matches the physics that braking distance grows faster than linearly, so we should report the quadratic model.” Evaluate this conclusion, using the numbers in the output.

EP 11.5 (what would change if). Example 11.4 estimated the male-minus-female salary difference, adjusting for rank, discipline, and years of service, and found sexMale =$4,771= \$4,771 with p=0.22p = 0.22. Suppose you drop rank from the adjustment and refit salary ~ discipline + yrs.service + sex.

fit_norank <- lm(salary ~ discipline + yrs.service + sex, data = salaries)
round(coef(summary(fit_norank)), 2)
            Estimate Std. Error t value Pr(>|t|)
(Intercept) 84361.06    4941.37   17.07     0.00
disciplineB 13033.85    2840.35    4.59     0.00
yrs.service   832.15     110.22    7.55     0.00
sexMale      8423.34    4744.54    1.78     0.08

Explain why the sex coefficient changed when rank left the model, and explain why deciding whether to include rank is a question about the world rather than a question the regression can settle.

Chapter game