Hexa Scholar

OLS as a projection: the geometry of regression

A brass plumb bob on a thin gold thread, its tip touching a navy board.
A plumb bob hanging down to a board.

The question

The data y = (1, 3, 2) is one arrow in three dimensions. Which point of the plane of possible fits does least squares pick?

Move a fit across that plane; the page measures your residual.

  1. 1 SeeMove a fit across the plane.
  2. 2 TryPredict how columns and data move R².
  3. 3 ProveBuild the proof, line by line.
  4. 4 ApplyProblems to check yourself.
Before you start · 3 quick checks on a dot product, the length of a vector and the angle between two vectors, about 3 min

Two ideas before you start

2 ideas

A line gives a fitted vector

Figure: the points (0, 1), (1, 3) and (2, 2), a dashed line and the vertical misses from the points to the line. It needs JavaScript.

Line:

a1 + bx = (2, 2, 2)

e = y − (a1 + bx) = (−1, 1, 0)

The heights of a line y = a + bx at x = 0, 1 and 2 stack into one vector, a1 + bx. The vertical misses stack into the residual vector e. Every line gives one point a1 + bx of the plane spanned by 1 and x.

Exact definition

Data and columns. Take \(n\) observations \(y = (y_1, \dots, y_n)\), a vector in \(\mathbb{R}^n\): each observation is one coordinate. Let \(\mathbf{1} = (1, \dots, 1)\), and let \(X\) be a matrix with \(n\) rows whose columns are linearly independent. For the line of this card, the columns are \(\mathbf{1}\) and \(x = (x_1, \dots, x_n)\), and the coefficient vector is \((a, b)\).

Fitted vector and residual. A coefficient vector \(\beta\) gives the fitted vector \(X\beta\), a combination of the columns, and the residual \(e = y - X\beta\). The set of all \(X\beta\) is the column space of \(X\). The squared length \(\lvert e\rvert^2 = e_1^2 + \dots + e_n^2\) is the sum of the squared misses.

Least squares. The ordinary least-squares (OLS) coefficients \(\hat{\beta}\) make \(\lvert y - X\beta\rvert^2\) smallest. They give the fitted vector \(\hat{y} = X\hat{\beta}\) and the residual \(\hat{e} = y - \hat{y}\).

The mean is a projection onto 1

Figure: a vector y with two entries, the dashed line along 1 = (1, 1), the point ȳ1 on that line and the segment from ȳ1 to y, orthogonal to the line. It needs JavaScript.

y:

(3, 1) = (2, 2) + (1, −1)

(y − ȳ1) · 1 = 1 − 1 = 0

Replace every observation by the mean ȳ and you get ȳ1, a point on the line along 1. The rest, y − ȳ1, is orthogonal to that line (its dot product with 1 is 0), because the deviations from the mean add up to 0.

Exact definition

Centering. With \(\bar{y} = (y_1 + \dots + y_n)/n\), the centered data \(y - \bar{y}\mathbf{1}\) has entries \(y_i - \bar{y}\), and \((y - \bar{y}\mathbf{1}) \cdot \mathbf{1} = \sum_i (y_i - \bar{y}) = 0\). For every number \(c\), \(\lvert y - c\mathbf{1}\rvert^2 = \lvert y - \bar{y}\mathbf{1}\rvert^2 + n(\bar{y} - c)^2\), so \(\bar{y}\mathbf{1}\) is the point of the line along \(\mathbf{1}\) closest to \(y\): the projection of \(y\) onto \(\mathbf{1}\).

Sums of squares and \(R^2\). For a fit whose columns include \(\mathbf{1}\): the total sum of squares \(\operatorname{SST} = \lvert y - \bar{y}\mathbf{1}\rvert^2\), the regression sum of squares \(\operatorname{SSR} = \lvert\hat{y} - \bar{y}\mathbf{1}\rvert^2\) and the residual sum of squares \(\operatorname{SSE} = \lvert\hat{e}\rvert^2\). When \(\operatorname{SST} \gt 0\), the coefficient of determination is \(R^2 = 1 - \operatorname{SSE}/\operatorname{SST}\).

Fine print: the fact this page uses, and names

The closest point. From the lesson on projection onto a subspace: among all points \(X\beta\) of the column space, \(\hat{y} = X\hat{\beta}\) is the closest to \(y\) exactly when \(\hat{e} = y - \hat{y}\) is orthogonal to every column of \(X\), that is \(X^\mathsf{T}\hat{e} = 0\). That point exists and is unique, and with independent columns so is \(\hat{\beta}\). Why the orthogonal point is the closest: if \(X^\mathsf{T}\hat{e} = 0\), then for any coefficient vector \(\beta\), \(y - X\beta = \hat{e} + X(\hat{\beta} - \beta)\), the two pieces are orthogonal, and so \(\lvert y - X\beta\rvert^2 = \lvert\hat{e}\rvert^2 + \lvert X(\hat{\beta} - \beta)\rvert^2\).

Names. The equations \(X^\mathsf{T}X\hat{\beta} = X^\mathsf{T}y\) are the normal equations. For a line, \(\hat{\beta} = (\hat{a}, \hat{b})\) is the least-squares line of the lesson on least squares. The data here are fixed numbers: nothing on this page assumes how they arose.

1 See

Two pictures of one fit

How the game works

Each observation is one axis. The data are the points (0, 1), (1, 3) and (2, 2), so y = (1, 3, 2) is the arrow to the point with coordinates 1, 3 and 2, and the columns are 1 = (1, 1, 1) and x = (0, 1, 2). The tiles show your fitted vector and the squared length of your residual, |e|² = e₁² + e₂² + e₃², which is the sum of the squared misses in the lower picture.

Check my fit locks the sliders, draws the least-squares fit dashed, and measures the angles between your residual and the columns. A fit counts as close when every angle between e and a column of the model is within 3° of 90°. Round 2 drops the column 1, so the model is y = bx. After round 2 a table compares your fits with the best ones. Start over clears everything.

2 Try

Columns, slopes and R²

Two predictions. The explorer opens once the second one is answered.

Change one observation

3 Prove

Why the sums of squares add up

Theorem

Assumptions. \(n\) observations \(y = (y_1, \dots, y_n)\) and a matrix \(X\) with \(n\) rows whose columns are linearly independent and include \(\mathbf{1} = (1, \dots, 1)\). \(\hat{\beta}\) is the least-squares coefficient vector, \(\hat{y} = X\hat{\beta}\) has entries \(\hat{y}_i\), \(\hat{e} = y - \hat{y}\) has entries \(\hat{e}_i\), and \(\bar{y}\) is the mean of the \(y_i\). The fit is neither exact nor flat: \(\hat{e} \ne 0\) and \(\hat{y} \ne \bar{y}\mathbf{1}\). With the sums of squares of the Start tab,

\[\begin{aligned} \operatorname{SST} &= \lvert y - \bar{y}\mathbf{1}\rvert^2, \\ \operatorname{SSR} &= \lvert\hat{y} - \bar{y}\mathbf{1}\rvert^2, \\ \operatorname{SSE} &= \lvert\hat{e}\rvert^2, \end{aligned}\]

and \(R^2 = 1 - \operatorname{SSE}/\operatorname{SST}\).

Conclusion. The residuals add up to 0 and the fitted values have mean \(\bar{y}\); \(\operatorname{SST} = \operatorname{SSR} + \operatorname{SSE}\); and \(R^2 = \operatorname{SSR}/\operatorname{SST} = \cos^2\theta\), where \(\theta\) is the angle between \(y - \bar{y}\mathbf{1}\) and \(\hat{y} - \bar{y}\mathbf{1}\).

Facts the proof uses.

  • the fact of the fine print on the Start tab: \(\hat{e}\) is orthogonal to every column of \(X\), and so to every combination of the columns;
  • the dot product is symmetric and linear in each vector, and \(\lvert v\rvert^2 = v \cdot v\);
  • for nonzero vectors \(u\) and \(v\), the angle \(\theta\) between them has \(\cos\theta = u \cdot v/(\lvert u\rvert\,\lvert v\rvert)\).

Each option is one step about the data and the fit of the theorem. Pick the one that is true for every data set and every matrix \(X\) the theorem covers.

What else follows
  • Without \(\mathbf{1}\) in the column space.
  • One regressor: \(R^2 = r^2\). With the columns \(\mathbf{1}\) and \(x\), \(\hat{y} = \hat{a}\mathbf{1} + \hat{b}x\) and \(\hat{a} = \bar{y} - \hat{b}\bar{x}\) from the lesson on least squares, so \(\hat{y} - \bar{y}\mathbf{1} = \hat{b}(x - \bar{x}\mathbf{1})\). So \(\theta\) is the angle between \(y - \bar{y}\mathbf{1}\) and \(x - \bar{x}\mathbf{1}\) when \(\hat{b} \gt 0\), and 180° minus that angle when \(\hat{b} \lt 0\). The cosine of the angle between the two centered vectors is the correlation \(r\) of the lesson on covariance and correlation, so \(R^2 = r^2\).
  • Adding a column. The column space grows, the shortest residual can only get shorter, and \(\operatorname{SST}\) depends on \(y\) alone. So \(R^2\) never falls when a column is added, and with \(n\) independent columns \(R^2 = 1\) for every \(y\) that is not constant.
  • Edge cases. If \(\hat{y} = \bar{y}\mathbf{1}\), lines 1 to 3 still hold with \(\operatorname{SSR} = 0\), so \(R^2 = 0\) and \(\theta\) is not defined. If \(\hat{e} = 0\) and \(\operatorname{SST} \gt 0\), then \(\operatorname{SSE} = 0\) and \(R^2 = 1\).
  • Matrix form. \(\hat{y} = Hy\) with \(H = X(X^\mathsf{T}X)^{-1}X^\mathsf{T}\), the projection matrix of the lesson on projection onto a subspace, and \(\hat{e} = (I - H)y\).
4 Apply

Problems

Each problem has hints and a full solution.

Key point To read a regression as a projection: the residual is orthogonal to every column; with an intercept the residuals add up to 0 and \(\operatorname{SST} = \operatorname{SSR} + \operatorname{SSE}\); \(R^2 = \cos^2\theta\), and with one regressor \(R^2 = r^2\).
Level Intermediate Prerequisites least squares (the lesson on least squares), projection onto a subspace (refreshed in the fine print), dot products, lengths and angles (refreshed in Before you start) Time ~35 min

Further reading