Linear models: vector and matrix formulation

STAT 306 - Module 07

Learning objectives

  1. Express a linear model in matrix form, defining all vectors and matrices involved.
  2. Provide the dimensions of all vectors and matrices relevant to a linear model.
  3. Recall that obtaining a unique least squares estimate via the closed-form inverse formula is not possible if \(\mathbf{X}^\top\mathbf{X}\) is singular, where \(\mathbf{X}\) is the design matrix of the model.

Linear models in terms of vectors and matrices

  • So far, we’ve only considered the simple linear regression with one covariate. In practice, we often have more than one covariate of interest.

  • When we have more than one covariate, we call the model a multiple linear regression.

  • The theory underlying the simple linear model naturally generalizes to multiple linear regression.

  • However, in multiple linear regression, it’s more convenient to express our linear model in terms of vectors and matrices.

  • Before tackling multiple covariates, let’s first see how to express the familiar simple linear regression model in matrix form!

Simple Linear Model: vector and matrix formulation

  • We have formulated the simple linear model equation as: \[ Y_i = \beta_0 + \beta_1x_i + \varepsilon_i \;, \qquad i=1,\dotsc,n \;, \] with error assumptions \(\mathbb{E}[\varepsilon_i] = 0\), \(\operatorname{Var}(\varepsilon_i) = \sigma^2\), and \(\operatorname{Cov}(\varepsilon_i, \varepsilon_j) = 0\) for \(i \ne j\).

  • How can we write this in terms of vectors and matrices?

\[ \begin{align} \begin{bmatrix} Y_1 \\ Y_2 \\ \vdots \\ Y_n \end{bmatrix} &= \begin{bmatrix} \beta_0 + \beta_1x_1 + \varepsilon_1 \\ \beta_0 + \beta_1x_2 + \varepsilon_2 \\ \vdots \\ \beta_0 + \beta_1x_n + \varepsilon_n \end{bmatrix}\\ \ \\ &= \begin{bmatrix} \beta_0 + \beta_1x_1\\ \beta_0 + \beta_1x_2\\ \vdots \\ \beta_0 + \beta_1x_n\end{bmatrix} + \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_n \end{bmatrix}\\ \ \\ &=\begin{bmatrix} 1 & x_1\\ 1 & x_2\\ \vdots & \vdots \\ 1 & x_n\end{bmatrix}\begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix} + \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_n \end{bmatrix} \end{align} \]

  • Therefore, we can write the simple linear model as \[ \mathbf{Y} = \mathbf{X}\pmb{\beta} + \pmb{\varepsilon} \;, \] where:

\[ \mathbf{Y} = \begin{bmatrix} Y_1 \\ Y_2 \\ \vdots \\ Y_n \end{bmatrix}_{n \times 1} \; \mathbf{X} = \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_n \end{bmatrix}_{n \times 2} \; \pmb{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix}_{2 \times 1} \; \pmb{\varepsilon} = \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_n \end{bmatrix}_{n \times 1} \; \]

  • Note that the first column of \(\mathbf{X}\) is a column of \(1\)’s.

    • This column corresponds to the intercept parameter \(\beta_0\).
  • The matrix \(\mathbf{X}\) is sometimes referred to as the design matrix.

  • The error assumptions in vector/matrix form are: \[\mathbb{E}[\pmb{\varepsilon}] = \mathbf{0} \qquad \text{and} \qquad \operatorname{Cov}(\pmb{\varepsilon}) = \sigma^2\mathbf{I}_n \;,\] where \(\mathbf{I}_n\) is the \(n\times n\) identity matrix.

Matrix formulation of the normal equations

  • Recall the normal equations from Lecture 2 that we solved to find the least squares estimates \(\hat\beta^\top=\begin{bmatrix}\hat\beta_0 & \hat\beta_1\end{bmatrix}\):

\[ \begin{align} n\hat{\beta}_0 + \hat{\beta}_1\sum_{i=1}^nx_i &= \sum_{i=1}^ny_i \;, \\ \hat{\beta}_0\sum_{i=1}^nx_i+\hat{\beta}_1\sum_{i=1}^nx_i^2 &= \sum_{i=1}^nx_iy_i \;. \end{align} \]

iClicker 1

Which of the following matrix equations represents the normal equations?

\[\begin{align}n\hat\beta_0 + \hat\beta_1\sum_{i=1}^n x_i &= \sum_{i=1}^n Y_i \;,\\ \hat\beta_0\sum_{i=1}^n x_i + \hat\beta_1\sum_{i=1}^n x_i^2 &= \sum_{i=1}^n x_iY_i \;.\end{align}\]


  1. \(\mathbf{X}^\top \hat\beta = \mathbf{X}^\top \mathbf{y}\)
  2. \(\mathbf{X}\hat\beta = \mathbf{X}^\top \mathbf{y}\)
  3. \(\mathbf{X}\hat\beta = \mathbf{X}\mathbf{y}\)
  1. \(\mathbf{X}^\top\mathbf{X}\hat\beta = \mathbf{X}^\top \mathbf{y}\)
  2. \(\mathbf{X}\mathbf{X}^\top\hat\beta = \mathbf{X}^\top \mathbf{y}\)

iClicker 1: Solution

Which of the following matrix equations represents the normal equations?

\[\begin{align}n\hat\beta_0 + \hat\beta_1\sum_{i=1}^n x_i &= \sum_{i=1}^n Y_i \;,\\ \hat\beta_0\sum_{i=1}^n x_i + \hat\beta_1\sum_{i=1}^n x_i^2 &= \sum_{i=1}^n x_iY_i \;.\end{align}\]


  1. \(\mathbf{X}^\top \hat\beta = \mathbf{X}^\top \mathbf{y}\)
  2. \(\mathbf{X}\hat\beta = \mathbf{X}^\top \mathbf{y}\)
  3. \(\mathbf{X}\hat\beta = \mathbf{X}\mathbf{y}\)
  1. \(\mathbf{X}^\top\mathbf{X}\hat\beta = \mathbf{X}^\top \mathbf{y}\)
  2. \(\mathbf{X}\mathbf{X}^\top\hat\beta = \mathbf{X}^\top \mathbf{y}\)

The normal equations of SLR: Matrix formulation

  • The normal equations can be written as

\[ \begin{align} \begin{bmatrix} n\hat{\beta}_0 + \hat{\beta}_1\sum_{i=1}^nx_i\\ \hat{\beta}_0\sum_{i=1}^nx_i+\hat{\beta}_1\sum_{i=1}^nx_i^2 \end{bmatrix} &= \begin{bmatrix} \sum_{i=1}^n y_i \\ \sum_{i=1}^n x_iy_i \end{bmatrix} \\ \ \\ \begin{bmatrix} n & \sum_{i=1}^nx_i \\ \sum_{i=1}^nx_i & \sum_{i=1}^nx_i^2 \end{bmatrix} \begin{bmatrix} \hat\beta_0 \\ \hat\beta_1 \end{bmatrix} &= \begin{bmatrix} 1 & 1 & \ldots & 1 \\ x_1 & x_2 & \ldots & x_n \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix}\\ \ \\ \begin{bmatrix} 1 & 1 & \ldots & 1 \\ x_1 & x_2 & \ldots & x_n \end{bmatrix} \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_n \end{bmatrix} \begin{bmatrix} \hat\beta_0 \\ \hat\beta_1 \end{bmatrix} &= \begin{bmatrix} 1 & 1 & \ldots & 1 \\ x_1 & x_2 & \ldots & x_n \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix}\\ \mathbf{X}^\top\mathbf{X}\hat\beta &= \mathbf{X}^\top\mathbf{y} \;. \end{align} \]

The LSE for SLR: Matrix formulation

  • If \(\mathbf{X}^\top\mathbf{X}\) is invertible, then by the normal equations \[ \begin{align} \mathbf{X}^\top\mathbf{X}\hat\beta &= \mathbf{X}^\top\mathbf{y} \Leftrightarrow\\ (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{X}\hat\beta &= (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y} \Leftrightarrow\\ \hat\beta &= (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y} \end{align} \]

  • In other words, our least squares estimate is given by: \[\hat\beta =(\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}\]

  • Notice that this closed-form formula requires taking the inverse of \(\mathbf{X}^\top\mathbf{X}\), and so obtaining a unique estimate requires \(\mathbf{X}^\top\mathbf{X}\) to be invertible.

iClicker 2

When is \(\mathbf{X}^\top\mathbf{X}\) not invertible?

\[\mathbf{X}^\top\mathbf{X} = \begin{bmatrix}n & \sum_{i=1}^n x_i\\\sum_{i=1}^n x_i & \sum_{i=1}^n x_i^2\end{bmatrix}\]

Hint: A \(2\times 2\) matrix \(\mathbf{A}=\begin{bmatrix}a & b\\ c & d\end{bmatrix}\) has determinant \(\det(\mathbf{A}) = ad-bc\), and \(\mathbf{A}^{-1} = \dfrac{1}{\det(\mathbf{A})}\begin{bmatrix}d & -b\\ -c & a\end{bmatrix}\).


  1. \(\sum_{i=1}^n x_i^2 = 0\)
  2. \(\sum_{i=1}^n x_i = 0\)
  3. \(\sum_{i=1}^n x_i^2 - \sum_{i=1}^n x_i = 0\)
  1. \(n\sum_{i=1}^n x_i^2 + \left(\sum_{i=1}^n x_i\right)^2 = 0\)
  2. \(n\sum_{i=1}^n x_i^2 - \left(\sum_{i=1}^n x_i\right)^2 = 0\)

iClicker 2: Solution

When is \(\mathbf{X}^\top\mathbf{X}\) not invertible?

\[\mathbf{X}^\top\mathbf{X} = \begin{bmatrix}n & \sum_{i=1}^n x_i\\\sum_{i=1}^n x_i & \sum_{i=1}^n x_i^2\end{bmatrix}\]

Hint: A \(2\times 2\) matrix \(\mathbf{A}=\begin{bmatrix}a & b\\ c & d\end{bmatrix}\) has determinant \(\det(\mathbf{A}) = ad-bc\), and \(\mathbf{A}^{-1} = \dfrac{1}{\det(\mathbf{A})}\begin{bmatrix}d & -b\\ -c & a\end{bmatrix}\).


  1. \(\sum_{i=1}^n x_i^2 = 0\)
  2. \(\sum_{i=1}^n x_i = 0\)
  3. \(\sum_{i=1}^n x_i^2 - \sum_{i=1}^n x_i = 0\)
  1. \(n\sum_{i=1}^n x_i^2 + \left(\sum_{i=1}^n x_i\right)^2 = 0\)
  2. \(n\sum_{i=1}^n x_i^2 - \left(\sum_{i=1}^n x_i\right)^2 = 0\)

Conditions for Invertibility in SLR

  • The matrix \((\mathbf{X}^\top\mathbf{X})^{-1}\) does not exist when \[ \begin{align} \mathrm{det}(\mathbf{X}^\top\mathbf{X}) & = n\sum_{i=1}^nx_i^2 - \left(\sum_{i=1}^nx_i\right)^2 = 0 \\ &\Rightarrow n\sum_{i=1}^nx_i^2 - n^2\bar{x}^2 = 0 \\ &\Rightarrow \sum_{i=1}^n\left(x_i-\bar{x}\right)^2 = 0 \end{align} \]
  • In other words, \(\mathbf{X}^\top\mathbf{X}\) is not invertible when \(x_1=x_2=\dotsc=x_n\).

  • Intuitively, this makes sense: if all the covariate values in our data are the same, then it’s not possible to observe a trend between the covariate and the response. See Exercise 4.

  • For simple linear regression, this means the two columns of \(\mathbf{X}\) must be linearly independent, which requires having at least two distinct covariate values in our data (and \(n \ge 2\)).

iClicker 3

For which of the following design matrices is \(\mathbf{X}^\top\mathbf{X}\) invertible? (Assume \(n \ge 2\) and \(c \ne 0\))

\[\mathbf{X}_A = \begin{bmatrix}1 & c\\1 & 0\\\vdots & \vdots\\1 & 0\end{bmatrix}\qquad \mathbf{X}_B = \begin{bmatrix}1 & c\\1 & c\\\vdots & \vdots\\1 & c\end{bmatrix}\]

\[\mathbf{X}_C = \begin{bmatrix}1 & 0\\1 & 0\\\vdots & \vdots\\1 & 0\end{bmatrix}\qquad \mathbf{X}_D = \begin{bmatrix}1 & 1/c\\1 & 1/c\\\vdots & \vdots\\1 & 1/c\end{bmatrix}\]


  1. \(\mathbf{X}_A\)
  2. \(\mathbf{X}_B\)
  1. \(\mathbf{X}_C\)
  2. \(\mathbf{X}_D\)

iClicker 3: Solution

For which of the following design matrices is \(\mathbf{X}^\top\mathbf{X}\) invertible? (Assume \(n \ge 2\) and \(c \ne 0\))

\[\mathbf{X}_A = \begin{bmatrix}1 & c\\1 & 0\\\vdots & \vdots\\1 & 0\end{bmatrix}\qquad \mathbf{X}_B = \begin{bmatrix}1 & c\\1 & c\\\vdots & \vdots\\1 & c\end{bmatrix}\]

\[\mathbf{X}_C = \begin{bmatrix}1 & 0\\1 & 0\\\vdots & \vdots\\1 & 0\end{bmatrix}\qquad \mathbf{X}_D = \begin{bmatrix}1 & 1/c\\1 & 1/c\\\vdots & \vdots\\1 & 1/c\end{bmatrix}\]


  1. \(\mathbf{X}_A\)
  2. \(\mathbf{X}_B\)
  1. \(\mathbf{X}_C\)
  2. \(\mathbf{X}_D\)

What We Learned Today

  • Linear Model in Matrix Form: \[\mathbf{Y} = \mathbf{X}\pmb{\beta} + \pmb{\varepsilon}\]
    • \(\mathbf{Y}_{n \times 1}\): vector of responses.
    • \(\mathbf{X}_{n \times 2}\): design matrix, with a column of \(1\)’s for the intercept \(\beta_0\) and a column of covariate values \(x_i\).
    • \(\pmb{\beta}_{2 \times 1} = \begin{bmatrix} \beta_0 & \beta_1 \end{bmatrix}^\top\): vector of parameters.
    • \(\pmb{\varepsilon}_{n \times 1}\): vector of random errors with \(\mathbb{E}[\pmb{\varepsilon}] = \mathbf{0}\) and \(\operatorname{Var}(\pmb{\varepsilon}) = \sigma^2 \mathbf{I}_n\).
  • Normal Equations in Matrix Form: \[\mathbf{X}^\top\mathbf{X}\hat{\pmb{\beta}} = \mathbf{X}^\top\mathbf{y}\]
    • \(\mathbf{X}^\top\mathbf{X} = \begin{bmatrix} n & \sum_{i=1}^n x_i \\ \sum_{i=1}^n x_i & \sum_{i=1}^n x_i^2 \end{bmatrix}\) is a \(2 \times 2\) symmetric matrix.
    • \(\mathbf{X}^\top\mathbf{y} = \begin{bmatrix} \sum_{i=1}^n y_i \\ \sum_{i=1}^n x_iy_i \end{bmatrix}\) is a \(2 \times 1\) vector.
  • Least Squares Estimate:
    • When \(\mathbf{X}^\top\mathbf{X}\) is invertible, the unique least squares estimate is given by: \[\hat{\pmb{\beta}} = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}\]
  • Conditions for Invertibility in SLR:
    • \(\det(\mathbf{X}^\top\mathbf{X}) = n\sum_{i=1}^n (x_i - \bar{x})^2\).
    • \(\mathbf{X}^\top\mathbf{X}\) is singular if and only if all covariate values are identical (\(x_1 = x_2 = \dots = x_n\)).
    • A unique fit requires at least two distinct covariate values (linearly independent columns in \(\mathbf{X}\)) and \(n \ge 2\).

Looking Ahead

  • In Module 08 we will generalize this exact matrix formulation to Multiple Linear Regression (MLR) with \(p\) covariates:
    • General design matrix \(\mathbf{X}\) of dimension \(n \times (p+1)\)
    • Minimizing the residual sum of squares \(RSS(\mathbf{b}) = \mathbf{e}^\top\mathbf{e}\)
    • Estimating error variance: \(\hat{\sigma}^2 = \frac{RSS}{n - p - 1}\)
    • Model fit statistics: \(R^2\) and adjusted \(R^2\) in matrix form