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.

We return to the Dwaine Studios cities of 6. Matrix algebra for regression. The company runs a chain of portrait studios in mid-sized cities, the kind of place families visit for graduation and holiday photos, and it wants a rule for guessing how much a new studio will sell before it signs a lease. Two facts about a city are easy to look up in advance: the number of young people (population aged 16 or younger, in thousands) and the per-capita disposable income (in thousands of dollars). The question is whether those two numbers together predict annual studio sales well enough to guide the expansion.

In Chapter 6 you already assembled this fit by hand: you built the design matrix, formed XX\mathbf{X}'\mathbf{X} and XY\mathbf{X}'\mathbf{Y}, inverted the small matrix, and solved for the coefficients b\mathbf{b}. What that arithmetic could not tell you is why the recipe b=(XX)1XY\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} is the right one, or how far to trust the numbers it returns. This chapter proves why, and turns the fit into inference. Figure 1 shows the raw picture: sales climb with each predictor on its own, but neither plot alone tells you what the two predictors do together, and that “together” is the whole point of multiple regression.

Two side-by-side scatterplots for the 21 Dwaine Studios cities. One panel plots sales in thousands of dollars against disposable income in thousand dollars; the other plots the same sales against target population in thousands. Each panel has an upward-sloping fitted line, and both clouds of points trend clearly upward, with sales ranging from roughly 140 to 290 thousand dollars.

Figure 1:Dwaine Studios sales rise with both the young-population count and disposable income when each is viewed alone. Multiple regression asks a harder question: how much does each predictor add once the other is already in the model?

Simple linear regression, from Chapters 2 and 3, handles one predictor. Real questions almost always come with several. We could try to rebuild the whole theory predictor by predictor, but that path gets ugly fast: two predictors, then three, each with its own normal equations and variance formulas. Instead we do what 6. Matrix algebra for regression set up. We write the model once, in matrix form, so that one predictor or ten predictors look identical on the page. Every result from Chapter 2, the least-squares estimates, their unbiasedness, their variances, the Gauss-Markov optimality, the n2n-2 divisor, reappears here in a single clean form that works for any number of predictors at once. This chapter is where regression stops being a collection of special cases and becomes one general method.

This is the most abstract chapter in the course, and worth a word on how to read it. The geometry in 7.2 The geometry of least squares is the idea to hold onto: once you picture fitting as dropping a perpendicular from the data onto the space of possible fits, every proof that follows reduces to one of two facts about the hat matrix. Hold that picture and the algebra becomes bookkeeping.

7.1 The model and least squares in matrix form

Intuition

In Chapter 2 a single predictor gave one slope and one intercept. Add a second predictor and you have two slopes plus an intercept; the mean response is now a tilted plane instead of a line, as Figure 2 shows for Dwaine. With more predictors it is a flat sheet in a space you cannot picture. Writing separate symbols for every coefficient and separate sums for every normal equation would bury the idea under bookkeeping. The fix is to stack the observations into vectors and matrices and let one line of matrix algebra carry all the bookkeeping for us.

A three-dimensional scatterplot of the 21 Dwaine cities. The floor axes are target population in thousands and disposable income in thousand dollars; the vertical axis is sales in thousands of dollars. A translucent blue plane tilts upward across the box, and each dark data point is joined to the plane by a short dashed gray vertical stem, the residual. Points sit above and below the plane with no systematic gap.

Figure 2:With one predictor the fit is a line; with two it is a tilted plane. Each city’s sales is a dark dot, the plane is the model’s guess, and each dashed stem is a residual. Multiple regression finds the plane whose stems are smallest in total squared length.

The systematic part of each observation is a weighted sum of that city’s predictor values, the same weights (β0,β1,\beta_0, \beta_1, \dots) reused for every city. Stacking the cities vertically turns “the same weights applied to every row” into a single matrix-times-vector product. Figure 3 shows the four objects and their shapes.

A schematic of the equation Y equals X beta plus epsilon drawn as four labeled boxes. A tall dark n-by-1 box of responses Y1 through Yn equals a wide blue n-by-p design matrix X whose first column is all ones and whose other columns hold predictor values, times a gray p-by-1 box of coefficients beta-0 through beta-p-minus-1, plus a gray n-by-1 box of errors epsilon-1 through epsilon-n.

Figure 3:The general linear model as stacked arrays. The design matrix X carries a leading column of ones so that the intercept beta-0 is handled exactly like any other coefficient.

Formula

The general linear model (Definition 7.1) collects all nn observations into one matrix equation.

In words: each observation’s response equals a weighted sum of that observation’s predictor values plus a random error, and the same weights are reused for every row. The four objects are:

The error assumptions of Chapter 2 become two compact statements about the whole vector at once:

E{ε}=0,Cov{ε}=σ2I.E\{\boldsymbol{\varepsilon}\} = \mathbf{0}, \qquad \operatorname{Cov}\{\boldsymbol{\varepsilon}\} = \sigma^2 \mathbf{I} .

In words: every error averages to zero, and the covariance matrix is σ2\sigma^2 times the identity. That one symbol packs both assumptions at once: each error has the same variance σ2\sigma^2 (down the diagonal), and distinct errors are uncorrelated (off the diagonal). Because X\mathbf{X} and β\boldsymbol{\beta} are fixed, E{Y}=XβE\{\mathbf{Y}\} = \mathbf{X}\boldsymbol{\beta} and Cov{Y}=σ2I\operatorname{Cov}\{\mathbf{Y}\} = \sigma^2 \mathbf{I} as well. We assume throughout that X\mathbf{X} has full column rank pp: no predictor is a copy of another, or an exact linear combination of the others. That is exactly what makes XX\mathbf{X}'\mathbf{X} invertible (6.4 The inverse and the normal equations).

To fit the model we reuse the least-squares idea from 2.2 Least squares from first principles: pick the coefficient vector b\mathbf{b} that makes the fitted values closest to the data in total squared error. Writing a candidate vector as b\mathbf{b}, the sum of squared errors is

Q(b)=i=1n(Yixib)2=(YXb)(YXb),Q(\mathbf{b}) = \sum_{i=1}^n (Y_i - \mathbf{x}_i'\mathbf{b})^2 = (\mathbf{Y} - \mathbf{X}\mathbf{b})'(\mathbf{Y} - \mathbf{X}\mathbf{b}) ,

where xi\mathbf{x}_i' is row ii of X\mathbf{X}. In words: QQ is the squared length of the residual vector, the same quantity Chapter 2 minimized, now written as one dot product. The minimizer is the least-squares estimator (Theorem 7.3)

b=(XX)1XY.\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} .

In words: gather the cross-products of the predictors (XX\mathbf{X}'\mathbf{X}) and of the predictors with the response (XY\mathbf{X}'\mathbf{Y}), and multiply by the inverse of the first. We derive this next, then check it on Dwaine.

Derivation by calculus

Proof. Expand QQ using the transpose rules of 6.2 Transpose and matrix multiplication. Since bXY\mathbf{b}'\mathbf{X}'\mathbf{Y} is a scalar, it equals its own transpose YXb\mathbf{Y}'\mathbf{X}\mathbf{b}, so the two middle terms combine:

Q(b)=YY2bXY+bXXb.Q(\mathbf{b}) = \mathbf{Y}'\mathbf{Y} - 2\,\mathbf{b}'\mathbf{X}'\mathbf{Y} + \mathbf{b}'\mathbf{X}'\mathbf{X}\mathbf{b} .

We minimize by taking the gradient with respect to b\mathbf{b} and setting it to zero. The gradient is just the vector of partial derivatives, one per entry of b\mathbf{b}, and setting it to zero finds the bottom of the bowl. Two vector-derivative rules do the work: for a constant vector a\mathbf{a}, b(ab)=a\nabla_{\mathbf{b}}(\mathbf{a}'\mathbf{b}) = \mathbf{a}; and for a symmetric matrix M\mathbf{M}, b(bMb)=2Mb\nabla_{\mathbf{b}}(\mathbf{b}'\mathbf{M}\mathbf{b}) = 2\mathbf{M}\mathbf{b}. These are the vector twins of the scalar rules ddb(ab)=a\frac{d}{db}(ab) = a and ddb(mb2)=2mb\frac{d}{db}(mb^2) = 2mb you already know. The matrix XX\mathbf{X}'\mathbf{X} is symmetric, so

bQ=2XY+2XXb.\nabla_{\mathbf{b}} Q = -2\,\mathbf{X}'\mathbf{Y} + 2\,\mathbf{X}'\mathbf{X}\mathbf{b} .

Setting the gradient to zero and dividing by 2 gives the normal equations (Definition 7.4)

XXb=XY.\mathbf{X}'\mathbf{X}\mathbf{b} = \mathbf{X}'\mathbf{Y} .

Because X\mathbf{X} has full column rank, XX\mathbf{X}'\mathbf{X} is invertible (6.4 The inverse and the normal equations), so we may multiply on the left by (XX)1(\mathbf{X}'\mathbf{X})^{-1}:

b=(XX)1XY.\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} .

This stationary point is the global minimum, not a saddle or maximum, because the matrix of second derivatives of QQ is 2XX2\mathbf{X}'\mathbf{X}, which is positive definite; a quadratic with a positive-definite Hessian is strictly convex and has a single lowest point. \blacksquare

These are the same normal equations as Chapter 2, now stated once for any number of predictors. In 2.2 Least squares from first principles the two scalar normal equations were ei=0\sum e_i = 0 and Xiei=0\sum X_i e_i = 0; the single matrix equation XXb=XY\mathbf{X}'\mathbf{X}\mathbf{b} = \mathbf{X}'\mathbf{Y} contains both of those and one more for each extra predictor, as we will see in 7.7 Simple regression as a special case.

R

Chapter 6 built XX\mathbf{X}'\mathbf{X} and XY\mathbf{X}'\mathbf{Y} for Dwaine by hand. Here we let the computer form them and solve.

dwaine <- read.csv("data/dwaine.csv")
head(dwaine, 3)
X <- cbind(1, dwaine$targtpop, dwaine$dispoinc)
Y <- dwaine$sales
dim(X)
  targtpop dispoinc sales
1     68.5     16.7 174.4
2     45.2     16.8 164.4
3     91.3     18.2 244.2
[1] 21  3

7.2 The geometry of least squares

Intuition

The calculus derivation finds the answer but hides why it is the answer. There is a picture that makes least squares obvious, and it is worth carrying for the rest of the course. Think of the response Y\mathbf{Y} as a single arrow (a point) in nn-dimensional space, one axis per observation. As the coefficient vector b\mathbf{b} ranges over all possible values, the fitted vector Y^=Xb\hat{\mathbf{Y}} = \mathbf{X}\mathbf{b} traces out a flat subspace: every linear combination of the columns of X\mathbf{X}. This subspace is the column space of X\mathbf{X} (Definition 7.5), and it holds every prediction the model can possibly make.

The data point Y\mathbf{Y} almost never sits exactly in that subspace, because real data has noise. So the fitting problem is: which point in the column space is closest to Y\mathbf{Y}? The closest point on a flat surface to an outside point is the foot of the perpendicular dropped from the point to the surface. That foot is Y^\hat{\mathbf{Y}}, and the perpendicular segment is the residual vector e=YY^\mathbf{e} = \mathbf{Y} - \hat{\mathbf{Y}}. Figure 4 draws exactly this for the smallest case that fits on a page, three observations and two columns.

A three-dimensional diagram. A tilted translucent blue plane is the column space of X, spanned by two gray arrows labeled columns of X. A black arrow labeled Y points from the origin up out of the plane. A blue arrow labeled Y-hat equals HY lies in the plane. A gold dashed segment labeled e equals Y minus Y-hat connects the tip of Y-hat up to the tip of Y and meets the plane at a right angle, marked by a small gold square.

Figure 4:Least squares as projection. The fitted vector Y-hat is the point of the column space closest to Y, reached by dropping a perpendicular; the residual e is that perpendicular, meeting the column space at a right angle.

Formula

The geometric condition is that the residual is orthogonal to every column of X\mathbf{X}:

Xe=0,where e=YXb.\mathbf{X}'\mathbf{e} = \mathbf{0}, \qquad \text{where } \mathbf{e} = \mathbf{Y} - \mathbf{X}\mathbf{b} .

In words: each column of X\mathbf{X}, dotted with the residual vector, gives zero, so the residual points in a direction the model cannot represent. Expanding X(YXb)=0\mathbf{X}'(\mathbf{Y} - \mathbf{X}\mathbf{b}) = \mathbf{0} gives back the normal equations XXb=XY\mathbf{X}'\mathbf{X}\mathbf{b} = \mathbf{X}'\mathbf{Y}, so the geometry and the calculus lead to the identical solution.

Derivation by projection

Derivation (least squares as orthogonal projection). Let b\mathbf{b} solve the normal equations, so Xe=0\mathbf{X}'\mathbf{e} = \mathbf{0} with e=YXb\mathbf{e} = \mathbf{Y} - \mathbf{X}\mathbf{b}. Take any other candidate coefficient vector b\mathbf{b}^{\ast}, with fitted vector Xb\mathbf{X}\mathbf{b}^{\ast}. Split the gap from Y\mathbf{Y} to that candidate by routing through Y^=Xb\hat{\mathbf{Y}} = \mathbf{X}\mathbf{b}:

YXb=(YXb)e+X(bb)g.\mathbf{Y} - \mathbf{X}\mathbf{b}^{\ast} = \underbrace{(\mathbf{Y} - \mathbf{X}\mathbf{b})}_{\mathbf{e}} + \underbrace{\mathbf{X}(\mathbf{b} - \mathbf{b}^{\ast})}_{\mathbf{g}} .

The second piece g=X(bb)\mathbf{g} = \mathbf{X}(\mathbf{b} - \mathbf{b}^{\ast}) lives in the column space. Its squared length adds without a cross term, because the cross term vanishes by orthogonality:

eg=eX(bb)=(Xe)(bb)=0(bb)=0.\mathbf{e}'\mathbf{g} = \mathbf{e}'\mathbf{X}(\mathbf{b} - \mathbf{b}^{\ast}) = (\mathbf{X}'\mathbf{e})'(\mathbf{b} - \mathbf{b}^{\ast}) = \mathbf{0}'(\mathbf{b} - \mathbf{b}^{\ast}) = 0 .

Therefore, by the Pythagorean theorem in nn dimensions,

YXb2=e2+g2    e2,\|\mathbf{Y} - \mathbf{X}\mathbf{b}^{\ast}\|^2 = \|\mathbf{e}\|^2 + \|\mathbf{g}\|^2 \; \ge \; \|\mathbf{e}\|^2 ,

with equality only when g2=0\|\mathbf{g}\|^2 = 0, that is Xb=Xb\mathbf{X}\mathbf{b}^{\ast} = \mathbf{X}\mathbf{b}. Since X\mathbf{X} has full column rank, this forces b=b\mathbf{b}^{\ast} = \mathbf{b}. So the normal-equations solution gives the unique smallest sum of squared errors, and Y^\hat{\mathbf{Y}} is the closest point of the column space to Y\mathbf{Y}. \blacksquare

This is the same algebraic identity as the Chapter 2 proof in 2.2 Least squares from first principles, now read as geometry: the cross term vanishes there because ei=0\sum e_i = 0 and Xiei=0\sum X_i e_i = 0, which is precisely Xe=0\mathbf{X}'\mathbf{e} = \mathbf{0} written out. The projection view (in 6.5 Idempotent and projection matrices) is the one to remember, because diagnostics in Chapter 9 are all about which observations sit in awkward corners of the column space.

R and Python

A scatterplot of the 21 Dwaine residuals in thousands of dollars on the vertical axis against fitted sales on the horizontal axis, with a dashed horizontal line at zero. The points scatter above and below zero with no funnel shape and no bend.

Figure 5:The Dwaine residuals against fitted values form a flat, patternless band around zero, the picture that supports the constant-variance and linearity assumptions. Chapter 9 turns this habit into a full diagnostic toolkit.

Orthogonality is easy to state and easy to doubt, so the simulation below hands you the data and invites you to break it.

Drag any of the seven cities anywhere you like. The slope and SSE respond, but both entries of X’e, and the Pythagoras gap SST minus SSR minus SSE, stay at zero.

7.3 The hat matrix

Intuition

Section 7.2 pictured the fit as a projection of Y\mathbf{Y} onto the column space. That projection is carried out by a single matrix, and giving it a name pays off for the rest of the chapter. The fitted vector is a fixed linear function of the data: plug the formula for b\mathbf{b} into Y^=Xb\hat{\mathbf{Y}} = \mathbf{X}\mathbf{b} and you get Y^=X(XX)1XY\hat{\mathbf{Y}} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y}. The matrix sitting in front of Y\mathbf{Y} turns the raw responses into fitted values, so it earns the name hat matrix (Definition 7.6): it puts the hat on Y\mathbf{Y}. It is the single most useful matrix in the rest of the course. Its diagonal entries are the leverages that flag unusual predictor values (Chapter 9), and its algebra makes the variance results below fall out in a line.

Formula

The hat matrix and the fitted and residual vectors are

H=X(XX)1X,Y^=HY,e=(IH)Y.\mathbf{H} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}', \qquad \hat{\mathbf{Y}} = \mathbf{H}\mathbf{Y}, \qquad \mathbf{e} = (\mathbf{I} - \mathbf{H})\mathbf{Y} .

Figure 7 shows the Toluca hat matrix as a grid of weights; the diagonal entries, outlined, are the leverages hiih_{ii}.

A 25-by-25 heatmap of the Toluca hat matrix, with red for positive entries and blue for negative, most entries near zero. The 25 diagonal cells are outlined in gold, marking the leverage values h-i-i, which are the largest entries in each row.

Figure 7:The Toluca hat matrix H turns responses into fitted values. Reading across row i gives the weights that build the i-th fitted value; the outlined gold diagonal entries are the leverage values that Chapter 9 uses to find unusual observations.

Derivation (properties of H)

Proof. Write A=(XX)1\mathbf{A} = (\mathbf{X}'\mathbf{X})^{-1}, which is symmetric because XX\mathbf{X}'\mathbf{X} is (the inverse of a symmetric matrix is symmetric, by 6.4 The inverse and the normal equations).

Symmetric. Using (BCD)=DCB(\mathbf{BCD})' = \mathbf{D}'\mathbf{C}'\mathbf{B}' from 6.2 Transpose and matrix multiplication,

H=(XAX)=XAX=XAX=H.\mathbf{H}' = \big(\mathbf{X}\mathbf{A}\mathbf{X}'\big)' = \mathbf{X}\mathbf{A}'\mathbf{X}' = \mathbf{X}\mathbf{A}\mathbf{X}' = \mathbf{H} .

Idempotent. Projecting twice equals projecting once. The middle XX\mathbf{X}'\mathbf{X} cancels its inverse:

HH=XAXXAX=XAX=H.\mathbf{H}\mathbf{H} = \mathbf{X}\mathbf{A}\,\mathbf{X}'\mathbf{X}\,\mathbf{A}\mathbf{X}' = \mathbf{X}\mathbf{A}\mathbf{X}' = \mathbf{H} .

Figure 8 shows why: once H\mathbf{H} has landed a vector in the column space, projecting again cannot move it.

Trace pp. The trace of a matrix is the sum of its diagonal entries. Use the cyclic property of the trace, tr(BC)=tr(CB)\operatorname{tr}(\mathbf{B}\mathbf{C}) = \operatorname{tr}(\mathbf{C}\mathbf{B}) (a standard identity: both sides equal i,jBijCji\sum_{i,j} B_{ij} C_{ji}), to slide X\mathbf{X} around:

tr(H)=tr ⁣(XAX)=tr ⁣(AXX)=tr ⁣((XX)1(XX))=tr(Ip)=p.\operatorname{tr}(\mathbf{H}) = \operatorname{tr}\!\big(\mathbf{X}\mathbf{A}\mathbf{X}'\big) = \operatorname{tr}\!\big(\mathbf{A}\mathbf{X}'\mathbf{X}\big) = \operatorname{tr}\!\big((\mathbf{X}'\mathbf{X})^{-1}(\mathbf{X}'\mathbf{X})\big) = \operatorname{tr}(\mathbf{I}_p) = p .

Consequently IH\mathbf{I} - \mathbf{H} is also symmetric and idempotent, with tr(IH)=np\operatorname{tr}(\mathbf{I} - \mathbf{H}) = n - p. That trace is the degrees of freedom for error, and it is the reason the divisor in the variance estimate is npn - p, proved in 7.5 Estimating the error variance. \blacksquare

A two-dimensional diagram. A blue line through the origin is labeled the column space. A black arrow y points up and to the right; a blue arrow Hy lies along the line; a gold dashed segment e connects the tip of Hy to the tip of y, meeting the line at a right angle. A small annotation notes that projecting Hy a second time does not move it.

Figure 8:Why H is idempotent. Projecting the vector y onto the column space lands it at Hy inside that space; projecting a second time leaves it exactly where it is, so HH equals H.

R and Python

The claim that H\mathbf{H} depends only on the predictors sounds like a technicality until you try to move it with the response, which is what the next simulation asks you to do.

Drag a city straight up and the leverages do not move at all; drag it sideways and they jump immediately, while the trace of H stays pinned at p.

7.4 The sampling distribution of b

Intuition

The vector b\mathbf{b} came from one sample of 21 cities. A different 21 cities would give a slightly different b\mathbf{b}, so b\mathbf{b} is random, with a mean and a covariance across the samples we could have drawn. Two questions repeat from Chapter 2, now for a whole vector: is b\mathbf{b} right on average, and how much does it vary? The answers are clean because b\mathbf{b} is a fixed matrix times Y\mathbf{Y}, and 6.7 Random vectors, expectation, and covariance matrices tells us exactly how means and covariances pass through a linear map.

Formula

The least-squares vector is unbiased with a covariance matrix built from (XX)1(\mathbf{X}'\mathbf{X})^{-1}:

E{b}=β,Cov{b}=σ2(XX)1.E\{\mathbf{b}\} = \boldsymbol{\beta}, \qquad \operatorname{Cov}\{\mathbf{b}\} = \sigma^2 (\mathbf{X}'\mathbf{X})^{-1} .

In words: on average b\mathbf{b} hits the true coefficient vector, and its covariance matrix is σ2\sigma^2 times the inverse cross-product matrix. The diagonal entries are the variances of the individual coefficients; the off-diagonal entries are the covariances between them, which is why two coefficients can be correlated (the tilted cloud in Figure 10). Replacing σ2\sigma^2 by its estimate MSE\mathrm{MSE} gives the estimated covariance matrix s2{b}=MSE(XX)1s^2\{\mathbf{b}\} = \mathrm{MSE}\,(\mathbf{X}'\mathbf{X})^{-1}, whose diagonal square roots are the standard errors software prints.

Derivation (mean and covariance of b)

Proof. Write b=AY\mathbf{b} = \mathbf{A}\mathbf{Y} with the fixed matrix A=(XX)1X\mathbf{A} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'. For a fixed matrix times a random vector, 6.7 Random vectors, expectation, and covariance matrices gives E{AY}=AE{Y}E\{\mathbf{A}\mathbf{Y}\} = \mathbf{A}\,E\{\mathbf{Y}\} and Cov{AY}=ACov{Y}A\operatorname{Cov}\{\mathbf{A}\mathbf{Y}\} = \mathbf{A}\operatorname{Cov}\{\mathbf{Y}\}\mathbf{A}'.

Mean. Since E{Y}=XβE\{\mathbf{Y}\} = \mathbf{X}\boldsymbol{\beta},

E{b}=(XX)1XXβ=(XX)1(XX)β=β.E\{\mathbf{b}\} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\,\mathbf{X}\boldsymbol{\beta} = (\mathbf{X}'\mathbf{X})^{-1}(\mathbf{X}'\mathbf{X})\boldsymbol{\beta} = \boldsymbol{\beta} .

Covariance. Since Cov{Y}=σ2I\operatorname{Cov}\{\mathbf{Y}\} = \sigma^2\mathbf{I},

Cov{b}=A(σ2I)A=σ2(XX)1XX(XX)1=σ2(XX)1,\operatorname{Cov}\{\mathbf{b}\} = \mathbf{A}\,(\sigma^2\mathbf{I})\,\mathbf{A}' = \sigma^2 (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\,\mathbf{X}(\mathbf{X}'\mathbf{X})^{-1} = \sigma^2 (\mathbf{X}'\mathbf{X})^{-1} ,

where the middle XX\mathbf{X}'\mathbf{X} cancels one inverse and the symmetry of (XX)1(\mathbf{X}'\mathbf{X})^{-1} handles the transpose. \blacksquare

If we add the normality assumption εN(0,σ2I)\boldsymbol{\varepsilon} \sim N(\mathbf{0}, \sigma^2\mathbf{I}), then Y\mathbf{Y} is multivariate normal, and a fixed linear map of a multivariate normal is again multivariate normal (6.8 The multivariate normal, in brief). So

bN ⁣(β, σ2(XX)1).\mathbf{b} \sim N\!\big(\boldsymbol{\beta},\ \sigma^2 (\mathbf{X}'\mathbf{X})^{-1}\big) .

In words: under normal errors each coefficient is normally distributed around its true value, and this is what powers the tt tests and confidence intervals of Chapter 8. Figure 10 confirms the covariance by simulation: refitting many datasets scatters the two Dwaine slopes into a tilted ellipse that matches σ2(XX)1\sigma^2 (\mathbf{X}'\mathbf{X})^{-1}.

A scatterplot of 4000 simulated pairs of slope estimates, the target-population slope b1 on the horizontal axis and the disposable-income slope b2 on the vertical axis. The cloud is an elongated tilted ellipse sloping downward from upper left to lower right, centered near b1 equals 1.45 and b2 equals 9.4. A blue curve traces the theoretical 95 percent ellipse and matches the cloud.

Figure 10:Simulating 4000 refits scatters the two Dwaine slope estimates into a tilted ellipse. The downward tilt is a negative covariance: the two slope estimates trade off, because the predictors are correlated. The blue theoretical ellipse from sigma-squared times the inverse cross-product matrix matches the simulated cloud.

R and Python

The covariance formula is a prediction about data you have not collected, so the simulation below collects it for you a thousand times and checks.

Each refit uses the same 21 cities with fresh errors. Press Draw 1000 and the spread of the b1 pile matches the standard error that the matrix formula predicted in advance.

7.5 Estimating the error variance

Intuition

The coefficients describe the plane; the model still has one more unknown, the error variance σ2\sigma^2, which sets how far the data scatter off the plane. As in Chapter 2 we estimate it from the residuals, but now we must find the right divisor for a model with pp parameters instead of 2. The answer is npn - p, and the reason is the trace we computed in 7.3 The hat matrix: fitting pp coefficients uses up pp directions of the data, leaving npn - p directions for the residual to live in. Figure 12 keeps the bookkeeping in view: the nn directions of the data split into a fit part and a residual part, and we average the squared residual over its own npn - p directions, not all nn.

A horizontal bar of total length n equals 21 representing the directions of the data. A short blue segment of length p equals 3 on the left is labeled spent on the fit, the column space. A long gold segment of length n minus p equals 18 on the right is labeled left for the residual, the error space. A double arrow beneath the whole bar marks the total of 21 directions of data.

Figure 12:The n directions of the data split into p directions spent fitting the plane and n minus p left for the residual. Dividing SSE by n minus p, not by n, averages over exactly the directions the residual is free to fill, which is what makes MSE unbiased.

Formula

The error sum of squares and the estimator of σ2\sigma^2 are

SSE=ee=Y(IH)Y,s2=MSE=SSEnp,s=MSE.\mathrm{SSE} = \mathbf{e}'\mathbf{e} = \mathbf{Y}'(\mathbf{I} - \mathbf{H})\mathbf{Y}, \qquad s^2 = \mathrm{MSE} = \frac{\mathrm{SSE}}{n - p}, \qquad s = \sqrt{\mathrm{MSE}} .

Derivation (why the divisor is n - p)

We need one fact about the expected value of a quadratic form. It is provable at our level, so we prove it, then apply it.

Lemma (expectation of a quadratic form). For any random vector Y\mathbf{Y} with mean μ\boldsymbol{\mu} and covariance Σ\boldsymbol{\Sigma}, and any fixed symmetric matrix A\mathbf{A},

E{YAY}=tr(AΣ)+μAμ.E\{\mathbf{Y}'\mathbf{A}\mathbf{Y}\} = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \boldsymbol{\mu}'\mathbf{A}\boldsymbol{\mu} .

Proof. The scalar YAY\mathbf{Y}'\mathbf{A}\mathbf{Y} equals its own trace, and the trace is cyclic, so YAY=tr(AYY)\mathbf{Y}'\mathbf{A}\mathbf{Y} = \operatorname{tr}(\mathbf{A}\mathbf{Y}\mathbf{Y}'). Taking expectations and using E{YY}=Σ+μμE\{\mathbf{Y}\mathbf{Y}'\} = \boldsymbol{\Sigma} + \boldsymbol{\mu}\boldsymbol{\mu}' (6.7 Random vectors, expectation, and covariance matrices),

E{YAY}=tr ⁣(A(Σ+μμ))=tr(AΣ)+tr(Aμμ)=tr(AΣ)+μAμ,E\{\mathbf{Y}'\mathbf{A}\mathbf{Y}\} = \operatorname{tr}\!\big(\mathbf{A}(\boldsymbol{\Sigma} + \boldsymbol{\mu}\boldsymbol{\mu}')\big) = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \operatorname{tr}(\mathbf{A}\boldsymbol{\mu}\boldsymbol{\mu}') = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \boldsymbol{\mu}'\mathbf{A}\boldsymbol{\mu} ,

since tr(Aμμ)=μAμ\operatorname{tr}(\mathbf{A}\boldsymbol{\mu}\boldsymbol{\mu}') = \boldsymbol{\mu}'\mathbf{A}\boldsymbol{\mu} is a scalar. \blacksquare

Proof of Theorem 7.9. Apply the lemma with A=IH\mathbf{A} = \mathbf{I} - \mathbf{H}, μ=Xβ\boldsymbol{\mu} = \mathbf{X}\boldsymbol{\beta}, and Σ=σ2I\boldsymbol{\Sigma} = \sigma^2\mathbf{I}. The two pieces are:

Trace piece. tr ⁣((IH)σ2I)=σ2tr(IH)=σ2(np)\operatorname{tr}\!\big((\mathbf{I} - \mathbf{H})\sigma^2\mathbf{I}\big) = \sigma^2 \operatorname{tr}(\mathbf{I} - \mathbf{H}) = \sigma^2 (n - p), using the trace from 7.3 The hat matrix.

Mean piece. The mean term vanishes, because (IH)X=XHX=XX=0(\mathbf{I} - \mathbf{H})\mathbf{X} = \mathbf{X} - \mathbf{H}\mathbf{X} = \mathbf{X} - \mathbf{X} = \mathbf{0} (the hat matrix leaves the columns of X\mathbf{X} unchanged, since they already lie in the column space). Therefore

μ(IH)μ=βX(IH)Xβ=β0β=0.\boldsymbol{\mu}'(\mathbf{I} - \mathbf{H})\boldsymbol{\mu} = \boldsymbol{\beta}'\mathbf{X}'(\mathbf{I} - \mathbf{H})\mathbf{X}\boldsymbol{\beta} = \boldsymbol{\beta}'\mathbf{0}\,\boldsymbol{\beta} = 0 .

Combining, E{SSE}=σ2(np)+0=(np)σ2E\{\mathrm{SSE}\} = \sigma^2(n - p) + 0 = (n - p)\sigma^2, so

E{MSE}=E{SSE}np=σ2.E\{\mathrm{MSE}\} = \frac{E\{\mathrm{SSE}\}}{n - p} = \sigma^2 .

The divisor npn - p, and no other, makes MSE\mathrm{MSE} unbiased. \blacksquare

This is the Chapter 2 result E{MSE}=σ2E\{\mathrm{MSE}\} = \sigma^2 generalized: there p=2p = 2 gave the divisor n2n - 2; here any model divides by nn minus the number of parameters. Figure 13 shows what goes wrong with the naive divisor nn: dividing by nn instead of npn - p biases the estimate low, and the gap is largest when pp is a big fraction of nn.

Two overlaid histograms of 5000 simulated estimates of sigma-squared for the Dwaine model. The blue histogram, dividing SSE by n minus p, is centered on the true value near 121. The gold histogram, dividing by n, is shifted left and centered near 104, biased low. A gray dashed vertical line marks the true sigma-squared at 121.

Figure 13:Dividing SSE by n - p (blue) centers the variance estimate on the truth; dividing by n (gold) pulls it systematically low. The bias is the price of estimating p coefficients before measuring the residuals.

R and Python

We can also read off how well the plane fits overall. The total variation SSTO=(YiYˉ)2\mathrm{SSTO} = \sum (Y_i - \bar{Y})^2 splits into the part the model explains and the part it does not, and R2=1SSE/SSTOR^2 = 1 - \mathrm{SSE}/\mathrm{SSTO} reports the explained fraction, exactly as in 3.6 The analysis of variance approach but now for many predictors.

SSTO <- sum((Y - mean(Y))^2)
SSR <- SSTO - SSE
c(SSTO = round(SSTO, 2), SSR = round(SSR, 2), SSE = round(SSE, 2),
  R2 = round(1 - SSE / SSTO, 4))
      SSTO        SSR        SSE         R2
26196.2100 24015.2800  2180.9300     0.9167
SSTO = np.sum((Y - Y.mean()) ** 2)
SSR = SSTO - SSE
print(round(SSTO, 2), round(SSR, 2), round(SSE, 2), round(1 - SSE / SSTO, 4))
26196.21 24015.28 2180.93 0.9167

The two predictors together explain about 91.7%91.7\% of the variation in Dwaine sales. Chapter 8 unpacks this number, warns about how it can only rise when predictors are added, and builds the general linear test on top of the SSE\mathrm{SSE} we just computed (8.3 Extra sums of squares).

7.6 The Gauss-Markov theorem

Intuition

We chose least squares because squared error is a natural penalty, and Chapter 2 rewarded that choice: in simple regression, no other linear unbiased estimator beats b1b_1. The same reward holds for the full vector, and the matrix proof is barely longer than the scalar one. The claim is that any competitor built as a fixed linear combination of the responses, and required to be right on average, must have a covariance matrix at least as large as b\mathbf{b}'s. No normality is needed; the mean-zero, constant-variance, uncorrelated error assumptions are enough. Figure 14 shows the payoff on Toluca: least squares and a competing unbiased estimator are both centered on the truth, but least squares scatters far less.

Two overlaid histograms of 5000 simulated Toluca slope estimates. The blue histogram, least squares, is tall and narrow and centered on the true slope 3.5702. The gold histogram, a two-point competitor using only the lightest and heaviest lots, is short and wide, also centered near 3.5702 but far more spread out. A gray dashed line marks the true slope.

Figure 14:Both the least-squares slope (blue, narrow) and a two-point competitor (gold, wide) are unbiased, centered on the true slope. Least squares has the smaller spread, exactly what the Gauss-Markov theorem guarantees.

Formula

Derivation (b is BLUE)

Proof. Let b=CY\mathbf{b}^{\ast} = \mathbf{C}\mathbf{Y} be any linear estimator with a fixed p×np \times n matrix C\mathbf{C}. Its mean is E{CY}=CXβE\{\mathbf{C}\mathbf{Y}\} = \mathbf{C}\mathbf{X}\boldsymbol{\beta}. For b\mathbf{b}^{\ast} to be unbiased for β\boldsymbol{\beta} for every possible β\boldsymbol{\beta}, we need

CX=Ip.\mathbf{C}\mathbf{X} = \mathbf{I}_p .

Write C=(XX)1X+D\mathbf{C} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' + \mathbf{D}, defining D\mathbf{D} as the difference from the least-squares matrix. The unbiasedness condition CX=I\mathbf{C}\mathbf{X} = \mathbf{I} becomes

I=[(XX)1X+D]X=I+DX,soDX=0.\mathbf{I} = \big[(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' + \mathbf{D}\big]\mathbf{X} = \mathbf{I} + \mathbf{D}\mathbf{X}, \qquad \text{so} \qquad \mathbf{D}\mathbf{X} = \mathbf{0} .

Now compute the covariance. Since Cov{Y}=σ2I\operatorname{Cov}\{\mathbf{Y}\} = \sigma^2\mathbf{I}, Cov{b}=σ2CC\operatorname{Cov}\{\mathbf{b}^{\ast}\} = \sigma^2\mathbf{C}\mathbf{C}'. Expand CC\mathbf{C}\mathbf{C}' with C=(XX)1X+D\mathbf{C} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' + \mathbf{D}. The two cross terms vanish because DX=0\mathbf{D}\mathbf{X} = \mathbf{0} (and its transpose XD=0\mathbf{X}'\mathbf{D}' = \mathbf{0}):

(XX)1XD=(XX)1(DX)=0.(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{D}' = (\mathbf{X}'\mathbf{X})^{-1}(\mathbf{D}\mathbf{X})' = \mathbf{0} .

What survives is

CC=(XX)1XX(XX)1+DD=(XX)1+DD.\mathbf{C}\mathbf{C}' = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{X}(\mathbf{X}'\mathbf{X})^{-1} + \mathbf{D}\mathbf{D}' = (\mathbf{X}'\mathbf{X})^{-1} + \mathbf{D}\mathbf{D}' .

Therefore

Cov{b}=σ2(XX)1+σ2DD=Cov{b}+σ2DD.\operatorname{Cov}\{\mathbf{b}^{\ast}\} = \sigma^2 (\mathbf{X}'\mathbf{X})^{-1} + \sigma^2\mathbf{D}\mathbf{D}' = \operatorname{Cov}\{\mathbf{b}\} + \sigma^2\mathbf{D}\mathbf{D}' .

For any fixed c\mathbf{c}, the variance of the scalar cb\mathbf{c}'\mathbf{b}^{\ast} is

Var{cb}=cCov{b}c+σ2cDDc=Var{cb}+σ2Dc2.\operatorname{Var}\{\mathbf{c}'\mathbf{b}^{\ast}\} = \mathbf{c}'\operatorname{Cov}\{\mathbf{b}\}\mathbf{c} + \sigma^2 \mathbf{c}'\mathbf{D}\mathbf{D}'\mathbf{c} = \operatorname{Var}\{\mathbf{c}'\mathbf{b}\} + \sigma^2\|\mathbf{D}'\mathbf{c}\|^2 .

The added term σ2Dc20\sigma^2\|\mathbf{D}'\mathbf{c}\|^2 \ge 0 is a squared length, so every linear unbiased competitor has variance at least that of cb\mathbf{c}'\mathbf{b}, with equality only when Dc=0\mathbf{D}'\mathbf{c} = \mathbf{0}; taking c\mathbf{c} to range over the coordinate vectors forces D=0\mathbf{D} = \mathbf{0}, that is, the competitor is b\mathbf{b} itself. Least squares is BLUE. \blacksquare

Setting X=[1,x]\mathbf{X} = [\mathbf{1}, \mathbf{x}] and c=(0,1)\mathbf{c} = (0, 1)' recovers the Chapter 2 statement that b1b_1 is BLUE (2.5 Sampling behavior and the Gauss-Markov theorem); the matrix proof did every coefficient, and every linear combination of coefficients, at once.

7.7 Simple regression as a special case

Intuition

Everything in Chapters 2 and 3 should drop out of the matrix formulas when there is one predictor, and it does. Seeing it happen once turns the general formulas from abstractions into trusted tools: the same machine that fit Dwaine’s plane fits Toluca’s line, and returns the exact numbers you already know. Figure 15 is the whole idea in one picture: one formula, two jobs.

A flow diagram. A central gray box holds the one matrix formula b equals X-prime-X inverse times X-prime-Y. Two arrows point down from it. The left blue box, labeled p equals 2 Toluca line, shows the design matrix with a ones column and one predictor collapsing to the Chapter 2 scalar formulas b1 equals Sxy over Sxx and b0 equals Ybar minus b1 Xbar. The right gold box, labeled p equals 3 Dwaine plane, shows the design matrix with two predictors and the fitted coefficients minus 68.86, 1.4546, 9.3655.

Figure 15:One matrix formula does two jobs. Set p equals 2 and it collapses into the exact Chapter 2 scalar formulas for the Toluca line; set p equals 3 and it fits the Dwaine plane. The general model does not replace simple regression, it contains it.

Derivation (matrix formulas reduce to Chapter 2)

Derivation (the p=2p = 2 case). With one predictor, the design matrix is X=[1,x]\mathbf{X} = [\mathbf{1}, \mathbf{x}], so

XX=(nXiXiXi2),XY=(YiXiYi).\mathbf{X}'\mathbf{X} = \begin{pmatrix} n & \sum X_i \\ \sum X_i & \sum X_i^2 \end{pmatrix}, \qquad \mathbf{X}'\mathbf{Y} = \begin{pmatrix} \sum Y_i \\ \sum X_i Y_i \end{pmatrix} .

where Sxx=(XiXˉ)2S_{xx} = \sum (X_i - \bar{X})^2 and Sxy=(XiXˉ)(YiYˉ)S_{xy} = \sum (X_i - \bar{X})(Y_i - \bar{Y}) are the Chapter 2 sums of squares and cross-products, and Xˉ,Yˉ\bar{X}, \bar{Y} are the sample means. The inverse of a 2×22 \times 2 matrix divides by its determinant det(XX)=nXi2(Xi)2=nSxx\det(\mathbf{X}'\mathbf{X}) = n\sum X_i^2 - (\sum X_i)^2 = n\,S_{xx} (6.4 The inverse and the normal equations),

(XX)1=1nSxx(Xi2XiXin).(\mathbf{X}'\mathbf{X})^{-1} = \frac{1}{n\,S_{xx}}\begin{pmatrix} \sum X_i^2 & -\sum X_i \\ -\sum X_i & n \end{pmatrix} .

Multiplying by XY\mathbf{X}'\mathbf{Y} and simplifying (each entry collapses using Xi=nXˉ\sum X_i = n\bar{X} and the definitions of Sxx,SxyS_{xx}, S_{xy}) gives

b=(XX)1XY=(Yˉb1XˉSxy/Sxx),\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} = \begin{pmatrix} \bar{Y} - b_1\bar{X} \\ S_{xy}/S_{xx} \end{pmatrix} ,

which are exactly the Chapter 2 estimators b0=Yˉb1Xˉb_0 = \bar{Y} - b_1\bar{X} and b1=Sxy/Sxxb_1 = S_{xy}/S_{xx}. The covariance matrix reduces the same way: the bottom-right entry of σ2(XX)1\sigma^2(\mathbf{X}'\mathbf{X})^{-1} is σ2n/(nSxx)=σ2/Sxx\sigma^2 n / (n S_{xx}) = \sigma^2/S_{xx}, matching Var{b1}\operatorname{Var}\{b_1\}, and the top-left entry is σ2Xi2/(nSxx)=σ2(1/n+Xˉ2/Sxx)\sigma^2 \sum X_i^2/(n S_{xx}) = \sigma^2(1/n + \bar{X}^2/S_{xx}), matching Var{b0}\operatorname{Var}\{b_0\} from 2.5 Sampling behavior and the Gauss-Markov theorem. \blacksquare

Example 7.5 already confirmed this numerically: the matrix pipeline returned Toluca’s b0=62.3659b_0 = 62.3659, b1=3.5702b_1 = 3.5702, and standard errors 26.1774 and 0.3470, the same figures Chapter 2 produced from the scalar formulas. The general linear model does not replace simple regression; it contains it.

7.8 Chapter summary

This chapter turned Chapter 6’s matrix arithmetic into a full multiple regression. One equation, Y=Xβ+ε\mathbf{Y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}, carries any number of predictors. Least squares projects Y\mathbf{Y} onto the column space of X\mathbf{X}, giving b=(XX)1XY\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} by calculus and by geometry alike. The hat matrix H\mathbf{H} packages that projection, and its two properties, being a projection (symmetric and idempotent) with trace pp, drive the sampling distribution of b\mathbf{b}, the npn - p divisor in MSE\mathrm{MSE}, and the Gauss-Markov optimality. Every Chapter 2 formula returns as the p=2p = 2 special case.

Key results at a glance.

ResultStatement or formulaValid when
Least-squares estimator (Theorem 7.3)b=(XX)1XY\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} minimizes Q(b)Q(\mathbf{b})X\mathbf{X} full column rank
Normal equations (Definition 7.4)XXb=XY\mathbf{X}'\mathbf{X}\mathbf{b} = \mathbf{X}'\mathbf{Y}, equivalently Xe=0\mathbf{X}'\mathbf{e} = \mathbf{0}any least-squares fit
Hat matrix properties (Theorem 7.7)H\mathbf{H} symmetric, idempotent, trH=p\operatorname{tr}\mathbf{H} = p, tr(IH)=np\operatorname{tr}(\mathbf{I}-\mathbf{H}) = n-pX\mathbf{X} full column rank
Mean and covariance of b\mathbf{b} (Theorem 7.8)E{b}=βE\{\mathbf{b}\} = \boldsymbol{\beta}, Cov{b}=σ2(XX)1\operatorname{Cov}\{\mathbf{b}\} = \sigma^2(\mathbf{X}'\mathbf{X})^{-1}E{ε}=0E\{\boldsymbol{\varepsilon}\}=\mathbf{0}, Cov{ε}=σ2I\operatorname{Cov}\{\boldsymbol{\varepsilon}\}=\sigma^2\mathbf{I}
Sampling distribution (Theorem 7.8)bN(β,σ2(XX)1)\mathbf{b} \sim N(\boldsymbol{\beta}, \sigma^2(\mathbf{X}'\mathbf{X})^{-1})errors also normal
Unbiasedness of MSE (Theorem 7.9)E{SSE}=(np)σ2E\{\mathrm{SSE}\} = (n-p)\sigma^2, so E{MSE}=σ2E\{\mathrm{MSE}\} = \sigma^2Cov{ε}=σ2I\operatorname{Cov}\{\boldsymbol{\varepsilon}\}=\sigma^2\mathbf{I}
Gauss-Markov (Theorem 7.10)b\mathbf{b} is the best linear unbiased estimator of β\boldsymbol{\beta}E{ε}=0E\{\boldsymbol{\varepsilon}\}=\mathbf{0}, Cov{ε}=σ2I\operatorname{Cov}\{\boldsymbol{\varepsilon}\}=\sigma^2\mathbf{I}; no normality needed
Coefficient of determinationR2=1SSE/SSTOR^2 = 1 - \mathrm{SSE}/\mathrm{SSTO} (Dwaine 0.9167)descriptive, any fit

Key terms. general linear model, design matrix, normal equations, least-squares estimator, column space, hat matrix, idempotent matrix, full column rank, error degrees of freedom, best linear unbiased estimator (BLUE).

You should now be able to.

Where this fits. This chapter is the general engine for the FIT stage of the course workflow (The modeling workflow): it estimates the coefficients and the error variance for a model of any size, replacing the predictor-by-predictor algebra of Chapters 2 and 3 with one formula that a computer can run for ten predictors as easily as one. It also arms the CHECK stage, because the hat matrix H\mathbf{H} is the object Chapter 9 (9. Model diagnostics) turns into the leverage values and influence measures, and the USE stage, because Cov{b}\operatorname{Cov}\{\mathbf{b}\} is what Chapter 8 turns into tests and intervals. Chapter 8 takes the Dwaine fit built here, interprets each coefficient with care, and develops the extra-sums-of-squares and general-linear-test machinery (8.3 Extra sums of squares) that decides which predictors a model needs.

7.9 Frequently asked questions

Q1. Why does the design matrix need a column of ones? The column of ones lets the intercept β0\beta_0 be handled like any other coefficient: its “predictor value” is 1 for every observation, so β01\beta_0 \cdot 1 is the same baseline for every row. Without it the fitted plane would be forced through the origin. The first normal equation, coming from that column, is exactly ei=0\sum e_i = 0.

Q2. What does full column rank mean, and what breaks without it? Full column rank means no predictor column is a linear combination of the others (including the ones column). If one is, say a predictor recorded in both meters and feet, then XX\mathbf{X}'\mathbf{X} is singular and (XX)1(\mathbf{X}'\mathbf{X})^{-1} does not exist, so there is no unique b\mathbf{b}: infinitely many coefficient vectors fit equally well. Software then drops a column or returns an error. Near-violations (highly correlated predictors) are multicollinearity, the subject of Chapter 12.

Q3. Is the hat matrix worth computing for real data? As a full n×nn \times n matrix, usually not; software gets fitted values and residuals more cheaply. But its diagonal entries hiih_{ii}, the leverage values, are computed and used constantly in diagnostics, and its algebra (symmetric, idempotent, trace pp) is what makes the theory work. Think of H\mathbf{H} as a proof tool and a source of the leverages, not a thing you print.

Q4. Why is the divisor npn - p and not n1n - 1 or nn? Each parameter you estimate uses one degree of freedom before the residuals are measured. A sample variance estimates one quantity (the mean) and divides by n1n - 1; a regression with pp parameters divides by npn - p. The trace derivation in 7.5 Estimating the error variance shows E{SSE}=(np)σ2E\{\mathrm{SSE}\} = (n - p)\sigma^2 exactly, so npn - p is the unique divisor making MSE\mathrm{MSE} unbiased. Dividing by nn (the maximum-likelihood choice) is biased low.

Q5. Do I need normal errors for any of this? No, for most of it. The estimator b\mathbf{b}, its unbiasedness, its covariance σ2(XX)1\sigma^2(\mathbf{X}'\mathbf{X})^{-1}, the unbiasedness of MSE\mathrm{MSE}, and the Gauss-Markov optimality all hold with only mean-zero, constant-variance, uncorrelated errors. Normality is an extra assumption, used to get the exact multivariate normal sampling distribution of b\mathbf{b} and the tt and FF tests of Chapter 8.

Q6. The Dwaine coefficients from the matrix formula and from lm matched exactly. Is that always guaranteed? Yes, up to tiny floating-point differences. Both compute the same (XX)1XY(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y}; lm and statsmodels use a more numerically stable routine (a QR or SVD decomposition) instead of forming the inverse explicitly, which matters only for badly conditioned designs. For well-behaved data like these, every printed digit agrees.

Q7. Why are the two Dwaine slope estimates negatively correlated in Figure 10? Because the two predictors are themselves positively correlated across cities. When a sample happens to make the population slope come out high, the fit compensates by making the income slope come out low to avoid double-counting the shared signal. That trade-off is exactly the off-diagonal entry of σ2(XX)1\sigma^2(\mathbf{X}'\mathbf{X})^{-1}, and it is why you cannot read one coefficient’s uncertainty without the whole covariance matrix.

7.10 Practice problems

  1. (A) Write the Dwaine model in the form Y=Xβ+ε\mathbf{Y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}, giving the dimensions of each of the four objects and describing the first column of X\mathbf{X}.

  2. (A) Explain in one sentence each what XX\mathbf{X}'\mathbf{X} and XY\mathbf{X}'\mathbf{Y} represent, and why the normal equations use them.

  3. (A) State the two error assumptions E{ε}=0E\{\boldsymbol{\varepsilon}\} = \mathbf{0} and Cov{ε}=σ2I\operatorname{Cov}\{\boldsymbol{\varepsilon}\} = \sigma^2\mathbf{I} in plain words, saying what the diagonal and the off-diagonal of σ2I\sigma^2\mathbf{I} each encode.

  4. (A) In the projection picture, what is the column space of X\mathbf{X}, where does Y^\hat{\mathbf{Y}} sit, and in what direction does the residual point? Answer without formulas.

  5. (A) Give the three properties of the hat matrix proved in this chapter and say what each one means geometrically.

  6. (A) Why is the divisor in MSE\mathrm{MSE} equal to npn - p? Answer by naming what the number pp counts and what npn - p counts.

  7. (A) A colleague says “with normality the least-squares estimator is BLUE; without it, it is not.” Correct the statement, naming exactly which assumptions the Gauss-Markov theorem uses.

  8. (A) Explain why XX\mathbf{X}\mathbf{X}' (an n×nn \times n matrix) cannot replace XX\mathbf{X}'\mathbf{X} in the normal equations.

  9. (B) Starting from Q(b)=(YXb)(YXb)Q(\mathbf{b}) = (\mathbf{Y} - \mathbf{X}\mathbf{b})'(\mathbf{Y} - \mathbf{X}\mathbf{b}), expand it, take the gradient, and derive the normal equations and b=(XX)1XY\mathbf{b} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} (Theorem 7.3). Justify why the stationary point is a minimum.

  10. (B) Prove the projection identity YXb2=e2+X(bb)2\|\mathbf{Y} - \mathbf{X}\mathbf{b}^{\ast}\|^2 = \|\mathbf{e}\|^2 + \|\mathbf{X}(\mathbf{b} - \mathbf{b}^{\ast})\|^2 for any b\mathbf{b}^{\ast}, showing the cross term vanishes, and conclude that b\mathbf{b} uniquely minimizes SSE.

  11. (B) Prove that H=X(XX)1X\mathbf{H} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' is symmetric and idempotent (Theorem 7.7). Then prove IH\mathbf{I} - \mathbf{H} is symmetric and idempotent as well.

  12. (B) Prove tr(H)=p\operatorname{tr}(\mathbf{H}) = p using the cyclic property of the trace, and deduce tr(IH)=np\operatorname{tr}(\mathbf{I} - \mathbf{H}) = n - p.

  13. (B) Show HX=X\mathbf{H}\mathbf{X} = \mathbf{X} and use it to prove (IH)X=0(\mathbf{I} - \mathbf{H})\mathbf{X} = \mathbf{0}. Explain in words why the hat matrix leaves the columns of X\mathbf{X} unchanged.

  14. (B) Derive E{b}=βE\{\mathbf{b}\} = \boldsymbol{\beta} and Cov{b}=σ2(XX)1\operatorname{Cov}\{\mathbf{b}\} = \sigma^2(\mathbf{X}'\mathbf{X})^{-1} (Theorem 7.8), naming the random-vector facts you use at each step. Then, adding the assumption εN(0,σ2I)\boldsymbol{\varepsilon} \sim N(\mathbf{0}, \sigma^2\mathbf{I}), state the full sampling distribution of b\mathbf{b} and explain why the mean and covariance did not need normality but this distributional statement does.

  15. (B) Prove the quadratic-form lemma E{YAY}=tr(AΣ)+μAμE\{\mathbf{Y}'\mathbf{A}\mathbf{Y}\} = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \boldsymbol{\mu}'\mathbf{A}\boldsymbol{\mu}, then use it to show E{SSE}=(np)σ2E\{\mathrm{SSE}\} = (n - p)\sigma^2 (Theorem 7.9).

  16. (B) State and prove the Gauss-Markov theorem in matrix form (Theorem 7.10). Where in the proof does the assumption Cov{ε}=σ2I\operatorname{Cov}\{\boldsymbol{\varepsilon}\} = \sigma^2\mathbf{I} enter, and where does full column rank enter?

  17. (B) For the simple-regression design X=[1,x]\mathbf{X} = [\mathbf{1}, \mathbf{x}], compute XX\mathbf{X}'\mathbf{X}, its determinant, and (XX)1(\mathbf{X}'\mathbf{X})^{-1}, and derive b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=Yˉb1Xˉb_0 = \bar{Y} - b_1\bar{X} from the matrix formula.

  18. (B) Using the reduction in problem 17, derive Var{b1}=σ2/Sxx\operatorname{Var}\{b_1\} = \sigma^2/S_{xx} and Var{b0}=σ2(1/n+Xˉ2/Sxx)\operatorname{Var}\{b_0\} = \sigma^2(1/n + \bar{X}^2/S_{xx}) from σ2(XX)1\sigma^2(\mathbf{X}'\mathbf{X})^{-1}.

  19. (B) Show that Cov{e,Y^}=0\operatorname{Cov}\{\mathbf{e}, \hat{\mathbf{Y}}\} = \mathbf{0} under the model, using Y^=HY\hat{\mathbf{Y}} = \mathbf{H}\mathbf{Y}, e=(IH)Y\mathbf{e} = (\mathbf{I} - \mathbf{H})\mathbf{Y}, and H(IH)=0\mathbf{H}(\mathbf{I} - \mathbf{H}) = \mathbf{0}. Why does this matter for diagnostics?

  20. (B) Prove that adding a predictor (a new column to X\mathbf{X}) cannot increase SSE\mathrm{SSE}, using the projection argument that enlarging the column space can only move Y^\hat{\mathbf{Y}} closer to Y\mathbf{Y}.

  21. (C) Read dwaine.csv, build X\mathbf{X} and Y\mathbf{Y}, and compute b\mathbf{b} from (XX)1XY(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\mathbf{Y} in R or Python. Confirm your answer matches lm / smf.ols.

  22. (C) For the Dwaine fit, compute the residual vector and verify numerically that Xe=0\mathbf{X}'\mathbf{e} = \mathbf{0} (all three entries) and that Y^i=Yi\sum \hat{Y}_i = \sum Y_i.

  23. (C) Build the Dwaine hat matrix and verify it is symmetric and idempotent and has trace 3. Then report the five largest diagonal entries hiih_{ii} and say what a large hiih_{ii} indicates about that city’s predictors.

  24. (C) Compute SSE\mathrm{SSE}, MSE\mathrm{MSE}, and ss for Dwaine two ways: from the residual vector, and from summary / .summary2(). Then compute SSE/n\mathrm{SSE}/n and report the percentage by which it understates MSE\mathrm{MSE}.

  25. (C) Compute the estimated covariance matrix MSE(XX)1\mathrm{MSE}\,(\mathbf{X}'\mathbf{X})^{-1} for Dwaine, report the three standard errors, and confirm they match the built-in summary. Report the correlation between the two slope estimates from the off-diagonal entry.

  26. (C) Refit Toluca through the matrix formulas and confirm b0=62.3659b_0 = 62.3659, b1=3.5702b_1 = 3.5702, MSE=2383.72\mathrm{MSE} = 2383.72, and the two standard errors, matching Chapter 2 to every digit.

  27. (C) Write a simulation (seed 4210) that draws 2000 datasets from the fitted Dwaine model, refits each, and reports the mean of the 2000 estimates of each coefficient (checking unbiasedness) and the standard deviation of the target-population slope (checking Cov{b}\operatorname{Cov}\{\mathbf{b}\}).

  28. (C) Regress Dwaine sales on targtpop alone, then on targtpop and dispoinc together, and confirm numerically that adding dispoinc lowers SSE\mathrm{SSE}. Relate the drop to the projection argument in problem 20.

7.11 Exam practice

EP 7.1 (interpret output in context). A colleague fits the Dwaine model in R and reads off the coefficient table.

            Estimate Std. Error t value Pr(>|t|)
(Intercept) -68.8571    60.0170 -1.1473   0.2663
targtpop      1.4546     0.2118  6.8682   0.0000
dispoinc      9.3655     4.0640  2.3045   0.0333

Interpret the targtpop coefficient and its tt value in the context of studio sales, being explicit about what is held fixed. Then evaluate the colleague’s conclusion: “the intercept has p=0.27p = 0.27, so it is not significantly different from zero, which means the model is misspecified and we should drop the intercept.” Say whether the conclusion is right and why.

EP 7.2 (explain why, and what would change if). Explain in full sentences why the divisor in MSE=SSE/(np)\mathrm{MSE} = \mathrm{SSE}/(n - p) is npn - p and not nn. Then answer a “what would change if”: suppose an analyst kept adding predictors to the Dwaine data until the model had p=21p = 21 parameters, one for every observation. Describe what happens to SSE\mathrm{SSE}, to the divisor, and to MSE\mathrm{MSE}, and explain why a fit with SSE=0\mathrm{SSE} = 0 is a warning rather than a success.

EP 7.3 (a student claims X, evaluate). A student writes: “Adding a predictor can never increase SSE\mathrm{SSE}, and it always raises R2R^2. So the best strategy is to keep adding predictors until R2R^2 is as close to 1 as possible.” Using the projection picture of least squares, evaluate the two factual claims and the strategy they support.

EP 7.4 (interpret computed output in context). The leverage values hiih_{ii} are the diagonal entries of the hat matrix H=X(XX)1X\mathbf{H} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' for the Dwaine fit. The six largest, and a check on their sum, are below.

row 20  targtpop=  82.7  dispoinc= 19.1  h_ii=0.2788
row 13  targtpop=  88.4  dispoinc= 17.4  h_ii=0.2390
row 15  targtpop=  52.5  dispoinc= 17.8  h_ii=0.2095
row  3  targtpop=  91.3  dispoinc= 18.2  h_ii=0.1737
row  5  targtpop=  46.9  dispoinc= 17.3  h_ii=0.1620
row 19  targtpop=  89.6  dispoinc= 18.1  h_ii=0.1611
sum(h_ii) = 3.0000     mean(targtpop)=62.02  mean(dispoinc)=17.14

Explain what a large hiih_{ii} says about a city, and interpret why row 20 has the largest leverage value even though its target population (82.7) is not the largest in the data. Then explain why the leverage values sum to exactly 3, and use the rule-of-thumb cutoff 2p/n=0.2862p/n = 0.286 to say whether any Dwaine city should be called a high-leverage point.

EP 7.5 (what would change if). An analyst re-expresses target population in hundreds of people instead of thousands, multiplying every targtpop value by 10, and refits. The two coefficient tables are below.

original                              rescaled
             Estimate Std. Error       Estimate Std. Error
(Intercept)  -68.8571    60.0170       -68.8571    60.0170
targtpop       1.4546     0.2118        0.1455     0.0212
dispoinc       9.3655     4.0640        9.3655     4.0640
original  MSE=121.16  R2=0.9167     rescaled  MSE=121.16  R2=0.9167

Explain, in full sentences and with reference to the model algebra, why the target- population coefficient and its standard error both divide by 10 while its tt value, the fitted values, MSE\mathrm{MSE}, and R2R^2 are all unchanged. What does this tell you about reading the size of a coefficient as a measure of a predictor’s importance?

7.12 Chapter game