Tutorials › Linear Algebra › Reading Residuals Through Projection Geometry

Applications in Networks, Economics and Data · Tutorial 950 of 1000

Reading Residuals Through Projection Geometry

Connect least-squares residuals to orthogonal projection, calculate them from design matrices, and interpret patterns that may signal an inadequate fit.

Intermediate 11 min read

What You'll Learn

  • Define fitted vectors and residuals using a design matrix and its column space.
  • Explain why a least-squares residual is orthogonal to every column of the design matrix.
  • Compute projection matrices and residual vectors for small data sets.
  • Check residual orthogonality using matrix products.
  • Recognize residual patterns that suggest a linear model may omit structure.
  • Distinguish a diagnostic clue from proof that a particular model is correct or incorrect.

Residuals as the Part a Model Cannot Fit

A design matrix describes the directions in which a linear model can move its fitted data vector. The observed data vector may not lie in any of those directions’ linear combinations. Least squares chooses the fitted vector in the design matrix’s column space that is closest to the data, and the residual records the remaining difference. This geometric viewpoint explains a basic diagnostic fact: at a least-squares fit, the residual is perpendicular to every column of the design matrix.

Let \(\mathbf{A}\in\mathbb{R}^{m\times p}\) be a design matrix and \(\mathbf{y}\in\mathbb{R}^m\) the observed response vector. A coefficient vector \(\mathbf{x}\in\mathbb{R}^p\) produces fitted vector \(\mathbf{A}\mathbf{x}\). The set of all possible fitted vectors is \(\operatorname{Col}\mathbf{A}\), the span of the columns of \(\mathbf{A}\). The residual associated with \(\mathbf{x}\) is \(\mathbf{e}=\mathbf{y}-\mathbf{A}\mathbf{x}\). Least squares chooses coefficients to minimize the squared length \(\|\mathbf{e}\|^2\).

Definition: A least-squares solution for \(\mathbf{A}\mathbf{x}\approx\mathbf{y}\) is a vector \(\widehat{\mathbf{x}}\in\mathbb{R}^p\) that minimizes \(\|\mathbf{y}-\mathbf{A}\mathbf{x}\|^2\) over all \(\mathbf{x}\in\mathbb{R}^p\). Its fitted vector is \(\widehat{\mathbf{y}}=\mathbf{A}\widehat{\mathbf{x}}\), and its residual vector is \(\mathbf{e}=\mathbf{y}-\widehat{\mathbf{y}}\). The vectors \(\mathbf{y}\), \(\widehat{\mathbf{y}}\), and \(\mathbf{e}\) lie in \(\mathbb{R}^m\); the coefficient vector lies in \(\mathbb{R}^p\).

The distinction between data space and coefficient space matters: a residual has one entry for each observation, while a coefficient vector has one entry for each design-matrix column. Geometrically, \(\widehat{\mathbf{y}}\) is the orthogonal projection of \(\mathbf{y}\) onto \(\operatorname{Col}\mathbf{A}\), and \(\mathbf{e}\) is the component perpendicular to that space.

Why the Residual Is Orthogonal to the Model

Theorem (Least-squares orthogonality): Let \(\mathbf{A}\in\mathbb{R}^{m\times p}\), \(\mathbf{y}\in\mathbb{R}^m\), and let \(\widehat{\mathbf{x}}\) be a least-squares solution. Then the residual \(\mathbf{e}=\mathbf{y}-\mathbf{A}\widehat{\mathbf{x}}\) satisfies \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\). Conversely, if \(\mathbf{x}\in\mathbb{R}^p\) satisfies \(\mathbf{A}^{\top}(\mathbf{y}-\mathbf{A}\mathbf{x})=\mathbf{0}\), then \(\mathbf{x}\) is a least-squares solution.

Proof. Write \(\mathbf{e}=\mathbf{y}-\mathbf{A}\widehat{\mathbf{x}}\). If \(\widehat{\mathbf{x}}\) is a least-squares solution, then changing it by \(t\mathbf{q}\), for any \(\mathbf{q}\in\mathbb{R}^p\) and \(t\in\mathbb{R}\), cannot reduce the squared residual length. Expanding gives

$$ \|\mathbf{y}-\mathbf{A}(\widehat{\mathbf{x}}+t\mathbf{q})\|^2 =\|\mathbf{e}-t\mathbf{A}\mathbf{q}\|^2 =\|\mathbf{e}\|^2-2t\,\mathbf{q}^{\top}\mathbf{A}^{\top}\mathbf{e} +t^2\|\mathbf{A}\mathbf{q}\|^2. $$

If \(\mathbf{A}^{\top}\mathbf{e}\ne\mathbf{0}\), choose \(\mathbf{q}=\mathbf{A}^{\top}\mathbf{e}\). Then \(\mathbf{q}^{\top}\mathbf{A}^{\top}\mathbf{e}=\|\mathbf{A}^{\top}\mathbf{e}\|^2>0\), and \(\mathbf{A}\mathbf{q}\ne\mathbf{0}\): otherwise \(\mathbf{q}^{\top}\mathbf{A}^{\top}\mathbf{e}=(\mathbf{A}\mathbf{q})^{\top}\mathbf{e}=0\), a contradiction. For any positive \(t\) small enough that \(t<2\|\mathbf{A}^{\top}\mathbf{e}\|^2/\|\mathbf{A}\mathbf{q}\|^2\), the expansion is less than \(\|\mathbf{e}\|^2\), contradicting minimality. Therefore \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\).

Conversely, suppose \(\mathbf{e}=\mathbf{y}-\mathbf{A}\mathbf{x}\) and \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\). For any other coefficient vector \(\mathbf{x}+\mathbf{h}\), its residual is \(\mathbf{e}-\mathbf{A}\mathbf{h}\), and

$$ \|\mathbf{e}-\mathbf{A}\mathbf{h}\|^2 =\|\mathbf{e}\|^2-2\mathbf{h}^{\top}\mathbf{A}^{\top}\mathbf{e} +\|\mathbf{A}\mathbf{h}\|^2 =\|\mathbf{e}\|^2+\|\mathbf{A}\mathbf{h}\|^2 \ge \|\mathbf{e}\|^2. $$

Thus no coefficient change gives a smaller residual length, so \(\mathbf{x}\) is a least-squares solution. \(\square\)

The equation \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\) is also called the normal equation for the residual. Its \(j\)th entry is \(\mathbf{a}_j^{\top}\mathbf{e}\), where \(\mathbf{a}_j\) is column \(j\) of \(\mathbf{A}\). Thus the residual is perpendicular to each column and, by linearity, to every vector in \(\operatorname{Col}\mathbf{A}\).

Projection Matrix When the Columns Are Independent

If the columns of \(\mathbf{A}\) are linearly independent, then \(\mathbf{A}^{\top}\mathbf{A}\) is invertible. Indeed, for every nonzero \(\mathbf{z}\in\mathbb{R}^p\), \(\mathbf{z}^{\top}\mathbf{A}^{\top}\mathbf{A}\mathbf{z}=\|\mathbf{A}\mathbf{z}\|^2>0\), since independence means \(\mathbf{A}\mathbf{z}\ne\mathbf{0}\). The normal equation \(\mathbf{A}^{\top}(\mathbf{y}-\mathbf{A}\widehat{\mathbf{x}})=\mathbf{0}\) then gives an explicit formula.

Theorem: If \(\mathbf{A}\in\mathbb{R}^{m\times p}\) has linearly independent columns, then the least-squares solution is unique and is \(\widehat{\mathbf{x}}=(\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\mathbf{y}\). The fitted vector is \(\widehat{\mathbf{y}}=\mathbf{P}\mathbf{y}\), where \(\mathbf{P}=\mathbf{A}(\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\).

Proof. The least-squares orthogonality theorem says that a solution must satisfy \(\mathbf{A}^{\top}\mathbf{A}\widehat{\mathbf{x}}=\mathbf{A}^{\top}\mathbf{y}\). Since \(\mathbf{A}^{\top}\mathbf{A}\) is invertible, this equation has exactly the solution displayed. The converse part of the orthogonality theorem confirms that this solution minimizes the residual length. Multiplying the coefficient formula by \(\mathbf{A}\) gives the stated fitted vector. \(\square\)

The matrix \(\mathbf{P}\) is the projection onto \(\operatorname{Col}\mathbf{A}\). Its defining geometric features can be checked directly: \(\mathbf{P}\mathbf{y}\) is in the column space because it is \(\mathbf{A}\) times a coefficient vector, and

$$ \mathbf{A}^{\top}(\mathbf{y}-\mathbf{P}\mathbf{y}) =\mathbf{A}^{\top}\mathbf{y} -\mathbf{A}^{\top}\mathbf{A}(\mathbf{A}^{\top}\mathbf{A})^{-1} \mathbf{A}^{\top}\mathbf{y} =\mathbf{0}. $$

So \(\mathbf{y}-\mathbf{P}\mathbf{y}\) is perpendicular to the entire column space. If the columns are dependent, the coefficient vector need not be unique, but the fitted vector closest to \(\mathbf{y}\) is still unique: two different closest fitted vectors would both have residuals perpendicular to the column space, and their difference would be both in the column space and perpendicular to it. A vector \(\mathbf{z}\) with both properties has \(\mathbf{z}^{\top}\mathbf{z}=0\), hence \(\mathbf{z}=\mathbf{0}\). The formula using \((\mathbf{A}^{\top}\mathbf{A})^{-1}\) specifically requires independent columns.

Worked Residual Calculations

Worked Example: Residuals from a Linear Trend

Fit an intercept-and-slope model to observations \((0,1)\), \((1,2)\), and \((2,2)\). Then compute and check the residuals.

The design matrix and response vector are

$$ \mathbf{A}=\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix}, \qquad \mathbf{y}=\begin{bmatrix}1\\2\\2\end{bmatrix}. $$

Calculate the two sides of the normal equation:

$$ \mathbf{A}^{\top}\mathbf{A} =\begin{bmatrix}3&3\\3&5\end{bmatrix}, \qquad \mathbf{A}^{\top}\mathbf{y} =\begin{bmatrix}5\\6\end{bmatrix}. $$

Writing the coefficients as intercept \(a\) and slope \(b\), solve \(3a+3b=5\) and \(3a+5b=6\). Subtracting the first equation from the second gives \(2b=1\), so \(b=\tfrac12\). Then \(3a+\tfrac32=5\), so \(a=\tfrac76\). Therefore

$$ \widehat{\mathbf{y}}=\mathbf{A}\begin{bmatrix}7/6\\1/2\end{bmatrix} =\begin{bmatrix}7/6\\5/3\\13/6\end{bmatrix}, \qquad \mathbf{e}=\mathbf{y}-\widehat{\mathbf{y}} =\begin{bmatrix}-1/6\\1/3\\-1/6\end{bmatrix}. $$

Check: \(\mathbf{A}^{\top}\mathbf{e}=\begin{bmatrix}(-1/6)+(1/3)+(-1/6)\\0(-1/6)+1(1/3)+2(-1/6)\end{bmatrix}=\begin{bmatrix}0\\0\end{bmatrix}\). The residual is orthogonal to both the intercept column and the predictor column. Its squared length is \(1/36+1/9+1/36=1/6\).

Worked Example: Residuals for Two Groups

Suppose four responses are \(2,4,5,5\), with the first two observations in one group and the last two in another. Use one indicator column per group to fit a separate level for each group.

Then

$$ \mathbf{A}=\begin{bmatrix}1&0\\1&0\\0&1\\0&1\end{bmatrix}, \qquad \mathbf{y}=\begin{bmatrix}2\\4\\5\\5\end{bmatrix}. $$

The normal equation uses \(\mathbf{A}^{\top}\mathbf{A}=\begin{bmatrix}2&0\\0&2\end{bmatrix}\) and \(\mathbf{A}^{\top}\mathbf{y}=\begin{bmatrix}6\\10\end{bmatrix}\). Hence the fitted group levels are \(\widehat{\mathbf{x}}=\begin{bmatrix}3\\5\end{bmatrix}\), and

$$ \widehat{\mathbf{y}}=\begin{bmatrix}3\\3\\5\\5\end{bmatrix}, \qquad \mathbf{e}=\begin{bmatrix}-1\\1\\0\\0\end{bmatrix}. $$

Check: multiplying by the transpose gives \(\mathbf{A}^{\top}\mathbf{e}=\begin{bmatrix}-1+1\\0+0\end{bmatrix}=\begin{bmatrix}0\\0\end{bmatrix}\). Each group’s residuals sum to zero, as required by orthogonality to that group’s indicator column. The squared residual length is \(1+1=2\).

Residual Patterns as Diagnostic Clues

Orthogonality is a constraint on the residual vector as a whole; it does not say that each residual is zero. Nor does it say that residuals must look patternless when plotted against an input variable. A model may satisfy \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\) and still leave a systematic pattern in the order of observations or against a predictor that is not adequately represented by the columns of \(\mathbf{A}\).

Worked Example: A Pattern Left by a Linear Fit

Fit an intercept-and-slope model to \(x=0,1,2,3\) and responses \(y=0,1,4,9\). Compute the residuals and interpret their signs across the predictor values.

The design matrix has columns of ones and the predictor values. The normal-equation quantities are

$$ \mathbf{A}=\begin{bmatrix}1&0\\1&1\\1&2\\1&3\end{bmatrix}, \quad \mathbf{A}^{\top}\mathbf{A}=\begin{bmatrix}4&6\\6&14\end{bmatrix}, \quad \mathbf{A}^{\top}\mathbf{y}=\begin{bmatrix}14\\32\end{bmatrix}. $$

The equations are \(4a+6b=14\) and \(6a+14b=32\). Multiply the first by \(3\) and the second by \(2\): \(12a+18b=42\) and \(12a+28b=64\). Subtracting gives \(10b=22\), so \(b=\tfrac{11}{5}\). Substitution into \(4a+6b=14\) gives \(4a+\tfrac{66}{5}=14\), so \(a=\tfrac{1}{10}\). Thus

$$ \widehat{\mathbf{y}} =\begin{bmatrix}1/10\\23/10\\45/10\\67/10\end{bmatrix}, \qquad \mathbf{e} =\begin{bmatrix}-1/10\\-13/10\\-5/10\\23/10\end{bmatrix}. $$

Check: the residual entries sum to \((-1-13-5+23)/10=0\). Their predictor-weighted sum is \((0(-1)+1(-13)+2(-5)+3(23))/10=(-13-10+69)/10=46/10\), not zero. This signals an arithmetic error, so check the slope calculation: from the equations, multiplying \(4a+6b=14\) by \(3\) gives \(12a+18b=42\), while multiplying \(6a+14b=32\) by \(2\) gives \(12a+28b=64\); subtraction gives \(10b=22\). But \(\mathbf{A}^{\top}\mathbf{y}\) has second entry \(0(0)+1(1)+2(4)+3(9)=36\), not \(32\).

Using the corrected right-hand side, the equations are \(4a+6b=14\) and \(6a+14b=36\). Subtracting \(12a+18b=42\) from \(12a+28b=72\) gives \(10b=30\), so \(b=3\), and then \(4a+18=14\) gives \(a=-1\). Therefore

$$ \widehat{\mathbf{y}}=\begin{bmatrix}-1\\2\\5\\8\end{bmatrix}, \qquad \mathbf{e}=\begin{bmatrix}1\\-1\\-1\\1\end{bmatrix}. $$

Check: the residual entries sum to \(1-1-1+1=0\), and the predictor-weighted sum is \(0(1)+1(-1)+2(-1)+3(1)=0\). The residual signs go positive, negative, negative, positive as \(x\) increases. This structured pattern suggests that a straight-line fit may be missing curvature. It is a diagnostic clue, not by itself a proof of which alternative model should be used.

Common Pitfall: Orthogonal to the Columns, Not to the Data

A common mistake is to conclude that the residual must be perpendicular to the observed response vector \(\mathbf{y}\). Least-squares orthogonality says \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\), so it concerns the design columns, not every vector in \(\mathbb{R}^m\). In the first worked example, the residual dotted with the response is

$$ \mathbf{y}^{\top}\mathbf{e} =1(-1/6)+2(1/3)+2(-1/6) =-1/6+2/3-1/3 =1/6\ne0. $$

Claiming this dot product must be zero would be incorrect. The correct checks are the dot products with each design column: they are \(0\) and \(0\) in that example. If the design matrix includes an intercept column of ones, orthogonality to that column does imply that the residual entries sum to zero. Without that column, a zero residual sum is not guaranteed.

Residual plots are useful because they can reveal structure that the fitted columns do not capture. A visible trend, repeated curvature, or a grouping pattern can motivate checking whether the design matrix represents the data adequately. But residuals are constrained by the columns used to fit them, and a pattern-free plot does not prove that every modeling assumption is satisfied. Interpret the geometry first, then treat patterns as evidence to investigate rather than automatic verdicts.

Check Your Understanding

Use the orthogonality condition and the design-matrix geometry to answer the following.

  1. For a least-squares fit with design matrix \(\mathbf{A}\), what does the equation \(\mathbf{A}^{\top}\mathbf{e}=\mathbf{0}\) say about the residual?
  2. If a design matrix contains a column of ones, what equation must the residual entries satisfy? Explain why.
  3. For the response vector \(\begin{bmatrix}1\\2\\2\end{bmatrix}\) and design matrix from the linear-trend example, verify that the residual is not required to be perpendicular to the response.
  4. Can a residual vector be nonzero even when the least-squares solution is correct? Explain using the meaning of least squares.
  5. What might a repeated positive-negative pattern in residuals against a predictor suggest, and why is it not a conclusive diagnosis by itself?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon