Contents
02Least squares
We start with the method itself, before any assumption. The question is simple: given a cloud of points, which line describes it best?
2.1The best line
We start with one input, body mass index, so that the data can be drawn on a two-dimensional plane (body mass index across, progression up); more inputs change nothing but the number of columns of X. Figure 1a plots disease progression against body mass index, one dot per patient. Heavier patients tend to progress further, and we want to summarize that with a line \hat y=\beta_0+\beta_1x. For any candidate line, the vertical miss of patient i is the residual e_i=y_i-\beta_0-\beta_1x_i (the red segments). A good line makes the misses small overall. Least squares measures “small overall” by the sum of the squared misses,
and picks the \beta that makes it smallest. Squaring makes every miss count positively, punishes big misses more than small ones, and, as we will see, gives a closed-form answer. Figure 1b turns a line about the mean point and plots RSS against its slope: a parabola with a single bottom. Least squares picks the bottom.
2.2The normal equations
RSS is a quadratic function of \beta, so its minimum is where the gradient vanishes. Expand (2):
Using \nabla_\beta(\beta^\top a)=a and \nabla_\beta(\beta^\top A\beta)=2A\beta for a symmetric matrix A,
These are the normal equations (Murphy, 2022, eq. 11.8). If the columns of X are linearly independent (X has full column rank: no column is an exact linear combination of the others; this needs at least as many observations as columns, that is, as coefficients), X^\top X is invertible and
The minimum is unique because the Hessian, 2X^\top X, is positive definite: v^\top X^\top Xv=\|Xv\|^2>0 for every v\neq0 when the columns are independent. With one input, solving the two normal equations by hand gives the familiar formulas
the slope is the covariance of x and y divided by the variance of x, and the line passes through the mean point.
Worked example 1 (Body mass index and progression). For our 442 patients, (6) gives \hat y=-117.8+10.2\,x. Each extra point of body mass index goes with about 10 more points of progression a year later. The intercept, -117.8, is the prediction at body mass index 0: the point where the line meets the vertical axis of Figure 1a, drawn at 0 for that reason. No patient has a body mass index anywhere near 0, so the intercept places the line; it is not meant to be read on its own. The line explains R^2=34\% of the variance of progression: \mathrm{RSS} is 66\% of the flat line’s \mathrm{TSS} in Figure 1b.
2.3Geometry: a projection
The normal equations have a picture (Murphy, 2022, §11.2.2.2). Write them as X^\top(y-X\hat\beta)=X^\top e=0: the residual vector is perpendicular (normal, hence the name) to every column of X. Think of y as a point in n-dimensional space and of all vectors X\beta as a flat subspace through the origin. The fitted vector \hat y=X\hat\beta=X(X^\top X)^{-1}X^\top y is the foot of the perpendicular from y to that subspace, its orthogonal projection, and the residual e is the perpendicular itself.
Pause and think. Why do the residuals of a least-squares fit with an intercept always add up to zero?
Show the answer
The first column of X is all ones, and X^\top e=0 says the residuals are perpendicular to it: \sum_ie_i=0. The same equations make the residuals uncorrelated with every input, whatever the truth is, a fact that matters in Chapter 3.
So far the derivation used no assumption at all: least squares gives a line for our data, whatever the data look like. So why does linear regression need assumptions? To judge the estimate \hat\beta. Is it unbiased, that is, right on average (Chapter 3)? Does it have the least variance (Chapter 4)? How confident can we be about the true slope (Chapter 5)? Each answer needs assumptions, and each chapter adds the ones it needs.