Tutorials › Linear Algebra › Multiple Linear Regression As A Matrix Problem

Least Squares and QR Factorization · Tutorial 695 of 1000

Multiple Linear Regression As A Matrix Problem

Build and solve multiple linear regression models as least-squares matrix problems, with or without a unique coefficient vector.

Intermediate 10 min read

What You'll Learn

  • Arrange observations and several predictors as rows of a design matrix.
  • Include an intercept by placing a column of ones in the design matrix.
  • Form the normal equations from the response vector and design matrix.
  • Solve a multiple-regression example using exact arithmetic and check the result.
  • Recognize why dependent predictor columns can produce multiple coefficient vectors.
  • Identify the effect of accidentally omitting an intercept.

From Several Predictors to One Matrix Equation

In a multiple linear regression model, each observation has a response and measurements of several predictors. The model assigns one coefficient to each predictor, along with a separate intercept. Writing the observations together turns the problem into a single matrix equation: the design matrix records the predictor values, the coefficient vector records the unknowns, and the response vector records the data to be fitted.

Suppose there are \(n\) observations and \(p\) predictors. For observation \(i\), write its response as \(y_i\), and its predictor values as \(x_{i1},\dots,x_{ip}\). The coefficient vector has \(p+1\) entries: one intercept coefficient and one coefficient for each predictor. All vectors in this tutorial are columns in \(\mathbb{R}^n\) or \(\mathbb{R}^{p+1}\), as appropriate. The intercept is represented by adding a first column of ones to the design matrix.

Definition: The design matrix for \(n\) observations and \(p\) predictors, when the model includes an intercept, is the matrix \(\mathbf{X}\in\mathbb{R}^{n\times(p+1)}\) whose \(i\)th row is \(\begin{bmatrix}1&x_{i1}&\cdots&x_{ip}\end{bmatrix}\). The response vector is \(\mathbf{y}=(y_1,\dots,y_n)^{\top}\), and the coefficient vector is \(\mathbf{b}=(b_0,b_1,\dots,b_p)^{\top}\). The fitted response vector is \(\mathbf{X}\mathbf{b}\).

The matrix equation \(\mathbf{X}\mathbf{b}=\mathbf{y}\) asks whether one choice of coefficients fits every observation exactly. Usually there are more observations than coefficients, and no exact solution exists. Least squares instead chooses \(\mathbf{b}\) to minimize the squared error \(\|\mathbf{y}-\mathbf{X}\mathbf{b}\|^2\). The vector \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{b}\) is the residual vector; here it is used to express the fitting condition, rather than to interpret the individual coefficients.

Worked Example: Constructing a Design Matrix

Suppose three observations have predictor values \((x,z)=(1,0),(2,1),(3,0)\), and responses \(4,7,8\). Use a model with an intercept and both predictors.

Each row begins with \(1\), followed by that observation’s \(x\)- and \(z\)-values. The response vector lists the responses in the same order as the rows:

$$ \mathbf{X}= \begin{bmatrix} 1&1&0\\ 1&2&1\\ 1&3&0 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix} 4\\7\\8 \end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix} b_0\\b_1\\b_2 \end{bmatrix}. $$

The three columns represent the intercept, predictor \(x\), and predictor \(z\), respectively. For example, the second row of \(\mathbf{X}\mathbf{b}\) is \(b_0+2b_1+b_2\), the model’s fitted response for the observation with \(x=2\) and \(z=1\). The matrix has three rows and three columns, while \(\mathbf{y}\) has three entries, so \(\mathbf{X}\mathbf{b}\) and \(\mathbf{y}\) have matching dimensions.

Check: Multiplication gives \(\mathbf{X}\mathbf{b}=(b_0+b_1,\ b_0+2b_1+b_2,\ b_0+3b_1)^{\top}\), which contains the model expression for each observation in its original order.

The Normal Equations

For any candidate coefficient vector \(\mathbf{b}\), the residual is \(\mathbf{y}-\mathbf{X}\mathbf{b}\). A least-squares solution is a vector that minimizes the squared norm of this residual. The normal equations give a system of equations that every such minimizer satisfies—and, in fact, characterizes the minimizers.

Theorem (Normal equations): Let \(\mathbf{X}\in\mathbb{R}^{n\times q}\) and \(\mathbf{y}\in\mathbb{R}^n\). A vector \(\mathbf{b}\in\mathbb{R}^q\) minimizes \(\|\mathbf{y}-\mathbf{X}\mathbf{b}\|^2\) if and only if it satisfies \(\mathbf{X}^{\top}\mathbf{X}\mathbf{b}=\mathbf{X}^{\top}\mathbf{y}\).

Proof. Put \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{b}\). First suppose \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), which is equivalent to the stated normal equations. For any other candidate \(\mathbf{c}\), let \(\mathbf{d}=\mathbf{c}-\mathbf{b}\). Then \(\mathbf{y}-\mathbf{X}\mathbf{c}=\mathbf{r}-\mathbf{X}\mathbf{d}\), and expansion of the squared norm gives

$$ \|\mathbf{r}-\mathbf{X}\mathbf{d}\|^2 = \|\mathbf{r}\|^2-2\mathbf{r}^{\top}\mathbf{X}\mathbf{d} +\|\mathbf{X}\mathbf{d}\|^2 = \|\mathbf{r}\|^2+\|\mathbf{X}\mathbf{d}\|^2 \geq \|\mathbf{r}\|^2. $$

The middle equality holds because \(\mathbf{r}^{\top}\mathbf{X}\mathbf{d}=(\mathbf{X}^{\top}\mathbf{r})^{\top}\mathbf{d}=0\). Thus \(\mathbf{b}\) minimizes the squared residual norm. Conversely, suppose \(\mathbf{b}\) is a minimizer. For any \(\mathbf{h}\in\mathbb{R}^q\), define \(a=\mathbf{r}^{\top}\mathbf{X}\mathbf{h}\). If \(\mathbf{X}\mathbf{h}=\mathbf{0}\), then \(a=0\). If \(\mathbf{X}\mathbf{h}\ne\mathbf{0}\) and \(a\ne0\), choose \(t=a/\|\mathbf{X}\mathbf{h}\|^2\). The candidate \(\mathbf{b}+t\mathbf{h}\) has squared residual norm

$$ \|\mathbf{r}-t\mathbf{X}\mathbf{h}\|^2 = \|\mathbf{r}\|^2-2ta+t^2\|\mathbf{X}\mathbf{h}\|^2 = \|\mathbf{r}\|^2-\frac{a^2}{\|\mathbf{X}\mathbf{h}\|^2} < \|\mathbf{r}\|^2. $$

This contradicts minimality, so \(a=0\) for every \(\mathbf{h}\). Taking \(\mathbf{h}\) to be each standard basis vector of \(\mathbb{R}^q\) shows that every entry of \(\mathbf{X}^{\top}\mathbf{r}\) is zero. Hence \(\mathbf{X}^{\top}(\mathbf{y}-\mathbf{X}\mathbf{b})=\mathbf{0}\), which rearranges to the normal equations. \(\square\)

The matrix \(\mathbf{X}^{\top}\mathbf{X}\) is \(q\times q\), and \(\mathbf{X}^{\top}\mathbf{y}\) is a column with \(q\) entries, so the equation is dimensionally consistent. In a regression with an intercept, the first column of \(\mathbf{X}\) consists of ones. The first normal equation therefore involves the sum of the residual entries; the other equations involve their products with the corresponding predictor columns.

Solving a Multiple-Regression Example

Worked Example: Two Predictors and an Intercept

For four observations, let the predictor pairs \((x,z)\) be \((-1,1),(0,-1),(1,-1),(2,1)\), with responses \(-3,5,1,5\). Find a least-squares coefficient vector for the model \(b_0+b_1x+b_2z\).

Arrange the observations by rows, retaining the intercept column and the same order in \(\mathbf{y}\):

$$ \mathbf{X}= \begin{bmatrix} 1&-1&1\\ 1&0&-1\\ 1&1&-1\\ 1&2&1 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix} -3\\5\\1\\5 \end{bmatrix}. $$

Compute the entries of \(\mathbf{X}^{\top}\mathbf{X}\) as dot products of the columns. The column sums are \(4,2,0\); the sums of squares for the \(x\)- and \(z\)-columns are \(1+0+1+4=6\) and \(1+1+1+1=4\); and their dot product is \((-1)(1)+0(-1)+1(-1)+2(1)=0\). Hence

$$ \mathbf{X}^{\top}\mathbf{X} = \begin{bmatrix} 4&2&0\\ 2&6&0\\ 0&0&4 \end{bmatrix}. $$

Next, take dot products of each column with \(\mathbf{y}\). They are \(-3+5+1+5=8\), \((-1)(-3)+0(5)+1(1)+2(5)=14\), and \(1(-3)+(-1)(5)+(-1)(1)+1(5)=-4\). Thus

$$ \mathbf{X}^{\top}\mathbf{y} = \begin{bmatrix} 8\\14\\-4 \end{bmatrix}, \qquad \begin{bmatrix} 4&2&0\\ 2&6&0\\ 0&0&4 \end{bmatrix} \begin{bmatrix} b_0\\b_1\\b_2 \end{bmatrix} = \begin{bmatrix} 8\\14\\-4 \end{bmatrix}. $$

The equations are \(4b_0+2b_1=8\), \(2b_0+6b_1=14\), and \(4b_2=-4\). The third gives \(b_2=-1\). Subtracting half the first equation from the second gives \(5b_1=10\), so \(b_1=2\). Substitution in the first gives \(4b_0+4=8\), so \(b_0=1\).

Check: For \(\mathbf{b}=(1,2,-1)^{\top}\), multiplication gives \(\mathbf{X}\mathbf{b}=(-2,2,4,4)^{\top}\). Therefore \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{b}=(-1,3,-3,1)^{\top}\). Its dot products with the three columns of \(\mathbf{X}\) are \(-1+3-3+1=0\), \(1+0-3+2=0\), and \(-1-3+3+1=0\). Thus \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), verifying the normal equations and the least-squares condition.

When the Coefficients Are Not Unique

Solving the normal equations does not always produce one coefficient vector. If a predictor column is a linear combination of other columns, different coefficient vectors can produce the same fitted vector. The relevant condition is linear independence of all columns of the design matrix, including the intercept column.

Theorem (Uniqueness criterion): For a real matrix \(\mathbf{X}\) and response vector \(\mathbf{y}\), a least-squares coefficient vector is unique if and only if the columns of \(\mathbf{X}\) are linearly independent.

Proof. A least-squares solution exists for every \(\mathbf{X}\) and \(\mathbf{y}\), by the existence result for least-squares problems. Suppose first that the columns of \(\mathbf{X}\) are linearly independent, and let \(\mathbf{b}\) and \(\mathbf{c}\) both be least-squares solutions. By the normal equations, \(\mathbf{X}^{\top}\mathbf{X}\mathbf{b}=\mathbf{X}^{\top}\mathbf{y}=\mathbf{X}^{\top}\mathbf{X}\mathbf{c}\). Set \(\mathbf{d}=\mathbf{c}-\mathbf{b}\); then \(\mathbf{X}^{\top}\mathbf{X}\mathbf{d}=\mathbf{0}\). Multiplying on the left by \(\mathbf{d}^{\top}\) gives \(\|\mathbf{X}\mathbf{d}\|^2=0\), so \(\mathbf{X}\mathbf{d}=\mathbf{0}\). Independence of the columns means the only solution of this homogeneous equation is \(\mathbf{d}=\mathbf{0}\). Therefore \(\mathbf{c}=\mathbf{b}\). Conversely, if the columns are dependent, there is a nonzero vector \(\mathbf{d}\) with \(\mathbf{X}\mathbf{d}=\mathbf{0}\). For any least-squares solution \(\mathbf{b}\), the vector \(\mathbf{b}+\mathbf{d}\) gives \(\mathbf{X}(\mathbf{b}+\mathbf{d})=\mathbf{X}\mathbf{b}\), so it gives the same residual and is also a minimizer. Since \(\mathbf{d}\ne\mathbf{0}\), it differs from \(\mathbf{b}\), and the solution is not unique. \(\square\)

Worked Example: Dependent Predictor Columns

Suppose three observations have \(x\)-values \(0,1,2\), \(z\)-values \(1,3,5\), and responses \(2,5,8\). Include an intercept and find all coefficient vectors satisfying the normal equations.

The design matrix and response are

$$ \mathbf{X}= \begin{bmatrix} 1&0&1\\ 1&1&3\\ 1&2&5 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix} 2\\5\\8 \end{bmatrix}. $$

Here the \(z\)-column equals the intercept column plus twice the \(x\)-column: \((1,3,5)^{\top}=(1,1,1)^{\top}+2(0,1,2)^{\top}\). Calculate the normal-equation matrices using column dot products:

$$ \mathbf{X}^{\top}\mathbf{X} = \begin{bmatrix} 3&3&9\\ 3&5&13\\ 9&13&35 \end{bmatrix}, \qquad \mathbf{X}^{\top}\mathbf{y} = \begin{bmatrix} 15\\21\\57 \end{bmatrix}. $$

The augmented matrix for the normal equations reduces as follows. First apply \(R_2\to R_2-R_1\) and \(R_3\to R_3-3R_1\); then apply \(R_3\to R_3-2R_2\):

$$ \begin{bmatrix} 3&3&9&15\\ 3&5&13&21\\ 9&13&35&57 \end{bmatrix} \sim \begin{bmatrix} 3&3&9&15\\ 0&2&4&6\\ 0&4&8&12 \end{bmatrix} \sim \begin{bmatrix} 3&3&9&15\\ 0&2&4&6\\ 0&0&0&0 \end{bmatrix}. $$

Let \(b_2=t\), where \(t\in\mathbb{R}\) is free. The second row gives \(2b_1+4t=6\), so \(b_1=3-2t\). The first row gives \(3b_0+3b_1+9t=15\), and substitution yields \(3b_0+9+3t=15\), so \(b_0=2-t\). All solutions are therefore \(\mathbf{b}=(2-t,3-2t,t)^{\top}\).

Check: For any \(t\), the fitted values are \(2-t+t=2\), \((2-t)+(3-2t)+3t=5\), and \((2-t)+2(3-2t)+5t=8\). Thus \(\mathbf{X}\mathbf{b}=\mathbf{y}\) for every \(t\), and each member of the family is an exact fit and hence a least-squares solution.

Common Mistake: Leaving Out the Intercept

A model with an intercept must include a column of ones. Omitting it changes the set of vectors that the model can fit; the result is not simply a shorter way to write the same model. For the four-observation example above, suppose the intercept column is mistakenly removed. The resulting two-column matrix has columns \(x\) and \(z\), so its normal equations use \(\begin{bmatrix}6&0\\0&4\end{bmatrix}\) and right-hand side \(\begin{bmatrix}14\\-4\end{bmatrix}\). They give the coefficients \((7/3,-1)^{\top}\), not \((2,-1)^{\top}\).

This calculation is internally consistent for a model forced to have zero intercept, but it does not solve the stated model that includes an intercept. For example, the resulting fitted vector is \((-10/3,1,10/3,11/3)^{\top}\), whose residual is \((1/3,4,-7/3,4/3)^{\top}\). The residual entries sum to \(10/3\ne0\), so the residual is not orthogonal to the omitted column of ones. The correction is to restore the intercept column before forming \(\mathbf{X}^{\top}\mathbf{X}\) and \(\mathbf{X}^{\top}\mathbf{y}\).

In practice, the matrix setup determines the model being fitted: rows must keep the observations aligned, columns must represent the intended predictors, and the intercept must be included when required. Once the design matrix and response vector are assembled, the normal equations provide a systematic route to a least-squares solution. If the columns are independent, that solution is unique; if they are dependent, the fitted vector may still be determined even though its coefficient representation is not.

Check Your Understanding

Use the design-matrix and normal-equation ideas to answer these questions.

  1. Write the design matrix, including an intercept, for three observations with predictor pairs \((2,1),(0,3),(-1,2)\).
  2. For \(\mathbf{X}\in\mathbb{R}^{n\times q}\) and \(\mathbf{y}\in\mathbb{R}^n\), state the normal equations and the dimensions of both sides.
  3. For the four-observation example, verify directly that \(\mathbf{X}^{\top}\mathbf{X}(1,2,-1)^{\top}=\mathbf{X}^{\top}\mathbf{y}\).
  4. In the dependent-column example, identify a nonzero vector \(\mathbf{d}\) such that \(\mathbf{X}\mathbf{d}=\mathbf{0}\).
  5. Explain why deleting the column of ones changes a regression model that includes an intercept.

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