MLR: Properties of Least Squares Estimators

STAT 306 - Module 09

Learning objectives touched on in this lecture

  1. Define the mean and covariance matrix of a vector.
  2. Show that in multiple regression, the least squares estimators are unbiased.
  3. Derive the covariance matrix of the least squares estimators in multiple regression, giving the result in terms of the design matrix \(\mathbf{X}\) and the unknown variance \(\sigma^{2}\).
  4. Explain how to estimate the covariance matrix of the least squares estimators in multiple regression.
  5. Under the assumption of normal errors in the model, compute confidence intervals for coefficients in a linear model.
  6. Under the assumption of normal errors in the model, compute a confidence interval for the expected value of the response in a linear model at a given set of covariate values.

Review: Matrix formulation of MLR

  • Suppose we have \(p\) covariates, with data of the form \((x_{i1},x_{i2},...,x_{ip},y_i)\), \(i=1,...,n\). Our linear model is \[ \mathbf{Y} = \mathbf{X}\pmb{\beta} + \pmb{\varepsilon} \] where

\[ \mathbf{Y} = \begin{bmatrix} Y_1 \\ Y_2 \\ \vdots \\ Y_n \end{bmatrix} \;, \qquad \mathbf{X} = \begin{bmatrix} 1 & x_{11} & x_{12} & \cdots & x_{1p} \\ 1 & x_{21} & x_{22} & \cdots & x_{2p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & x_{n2} & \cdots & x_{np} \end{bmatrix} \;, \\ \pmb{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \\ \vdots \\ \beta_p \end{bmatrix} \;, \qquad \pmb{\varepsilon} = \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_n \end{bmatrix} \;, \]

with \(\pmb{\varepsilon}\sim N_n(\mathbf{0}, \sigma^2\mathbf{I}_n)\), i.e., the errors \(\varepsilon_i\) are i.i.d. \(N(0,\sigma^2)\).

Today’s lecture

  • In today’s lecture, we will cover:
    • Properties of the least squares estimator
    • Confidence intervals and hypothesis tests for parameters
    • Confidence and prediction intervals for the response

Review: Random Vectors

  • A random vector is a vector whose components are random variables.
  • The mean of a random vector is the vector of means of its components.

\[ \mathbb{E}[\mathbf{Y}] = \begin{bmatrix} \mathbb{E}[Y_1] \\ \mathbb{E}[Y_2] \\ \vdots \\ \mathbb{E}[Y_n] \end{bmatrix}\; \]

  • The covariance matrix of a random vector is the matrix of covariances between its components.

\[ \mathrm{Cov}(\mathbf{Y}) = \begin{bmatrix} \mathrm{Var}(Y_1) & \mathrm{Cov}(Y_1,Y_2) & \cdots & \mathrm{Cov}(Y_1,Y_n) \\ \mathrm{Cov}(Y_2,Y_1) & \mathrm{Var}(Y_2) & \cdots & \mathrm{Cov}(Y_2,Y_n) \\ \vdots & \vdots & \ddots & \vdots \\ \mathrm{Cov}(Y_n,Y_1) & \mathrm{Cov}(Y_n,Y_2) & \cdots & \mathrm{Var}(Y_n) \end{bmatrix} \;. \]

Properties of Covariance Matrices: \(\Sigma\)

  • A covariance matrix is always symmetric, i.e., \(\mathbf{\Sigma}=\mathbf{\Sigma}^\top\).

  • A covariance matrix is always positive semi-definite, i.e., for any vector \(\mathbf{a}\), \(\mathbf{a}^\top \mathbf{\Sigma} \mathbf{a} \geq 0\).

  • If the components of a random vector are independent, then its covariance matrix is diagonal.

  • If \(\mathbf{A}\) and \(\mathbf{B}\) are constant matrices and \(\mathbf{X}\), \(\mathbf{Y}\) are random vectors, then \[ \mathrm{Cov}(\mathbf{A}\mathbf{X}, \mathbf{B}\mathbf{Y}) = \mathbf{A}\text{Cov}(\mathbf{X}, \mathbf{Y})\mathbf{B}^\top \]

  • In particular, taking \(\mathbf{X}=\mathbf{Y}\) and \(\mathbf{B}=\mathbf{A}\), \[ \mathrm{Cov}(\mathbf{A}\mathbf{Y}) = \mathbf{A}\mathbf{\Sigma}_\mathbf{Y}\mathbf{A}^\top \]

  • If \(\mathbf{a}\) is a constant vector of the same dimension as \(\mathbf{Y}\), then \[ \mathrm{Cov}(\mathbf{a}+\mathbf{Y}) = \mathrm{Cov}(\mathbf{Y}) = \mathbf{\Sigma}_{\mathbf{Y}} \;. \]

Error vector

\[ \mathbb{E}[\pmb{\varepsilon}] = \begin{bmatrix} \mathbb{E}[\varepsilon_1] \\ \mathbb{E}[\varepsilon_2] \\ \vdots \\ \mathbb{E}[\varepsilon_n] \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ \vdots \\ 0 \end{bmatrix} = \mathbf{0}_n \; \]

\[ \text{Cov}(\pmb{\varepsilon}) = \mathbf{\Sigma}_{\pmb{\varepsilon}} = \begin{bmatrix} \mathrm{Var}(\varepsilon_1) & \mathrm{Cov}(\varepsilon_1,\varepsilon_2) & \cdots & \mathrm{Cov}(\varepsilon_1,\varepsilon_n) \\ \mathrm{Cov}(\varepsilon_2,\varepsilon_1) & \mathrm{Var}(\varepsilon_2) & \cdots & \mathrm{Cov}(\varepsilon_2,\varepsilon_n) \\ \vdots & \vdots & \ddots & \vdots \\ \mathrm{Cov}(\varepsilon_n,\varepsilon_1) & \mathrm{Cov}(\varepsilon_n,\varepsilon_2) & \cdots & \mathrm{Var}(\varepsilon_n) \end{bmatrix} = \sigma^2\mathbf{I}_n \;. \]

Activity 1: Properties of the LSE (10 min)

  • To carry out any inference on the parameters \(\pmb{\beta}\), we need to know the sampling distribution of the least squares estimator \(\widehat{\pmb{\beta}}\).

    1. Compute the mean vector of \(\widehat{\pmb{\beta}}\);
    2. Compute the covariance matrix of the least squares estimator \(\widehat{\pmb{\beta}}\).
    3. What is the distribution of \(\widehat{\pmb{\beta}}\)?

iClicker 1

What is the mean vector of the least squares estimator \(\widehat{\pmb{\beta}}\) in a model with \(p\) covariates?


  1. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \mathbf{0}_p\)
  2. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \mathbf{0}_{p+1}\)
  3. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \pmb{\beta}\)
  1. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\pmb{\beta}\)
  2. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\pmb{\beta}\mathbf{X^\top}\left(\mathbf{X^\top X}\right)^{-1}\)

iClicker 1: Solution

What is the mean vector of the least squares estimator \(\widehat{\pmb{\beta}}\) in a model with \(p\) covariates?


  1. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \mathbf{0}_p\)
  2. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \mathbf{0}_{p+1}\)
  3. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \pmb{\beta}\)
  1. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\pmb{\beta}\)
  2. \(\mathbb{E}[\widehat{\pmb{\beta}}] = \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\pmb{\beta}\mathbf{X^\top}\left(\mathbf{X^\top X}\right)^{-1}\)

Expected value of \(\widehat{\pmb{\beta}}\)

\[ \begin{align} \mathbf{E}[\widehat{\pmb{\beta}}] &= \mathbf{E}\left[(\mathbf{X^\top X})^{-1}\mathbf{X^\top}\mathbf{Y}\right] \\ &= (\mathbf{X^\top X})^{-1}\mathbf{X^\top}\mathbf{E}\left[\mathbf{Y}\right] \\ &= (\mathbf{X^\top X})^{-1}\mathbf{X^\top}(\mathbf{X}\pmb{\beta}) \\ &= (\mathbf{X^\top X})^{-1}\mathbf{X^\top}\mathbf{X}\pmb{\beta} \\ &= \pmb{\beta} \;. \end{align} \]

iClicker 2

Which of the following is the covariance matrix of the least squares estimator \(\widehat{\pmb{\beta}}\) in a model with \(p\) covariates?


  1. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2 \mathbf{I}_{p+1}\)
  2. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2 \mathbf{X^\top X}\)
  3. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2\left(\mathbf{X X^\top}\right)^{-1}\)
  1. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma\left(\mathbf{X^\top X}\right)^{-1}\)
  2. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2\left(\mathbf{X^\top X}\right)^{-1}\)

iClicker 2: Solution

Which of the following is the covariance matrix of the least squares estimator \(\widehat{\pmb{\beta}}\) in a model with \(p\) covariates?


  1. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2 \mathbf{I}_{p+1}\)
  2. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2 \mathbf{X^\top X}\)
  3. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2\left(\mathbf{X X^\top}\right)^{-1}\)
  1. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma\left(\mathbf{X^\top X}\right)^{-1}\)
  2. \(\text{Cov}(\widehat{\pmb{\beta}}) = \sigma^2\left(\mathbf{X^\top X}\right)^{-1}\)

Covariance matrix of \(\widehat{\pmb{\beta}}\)

\[ \begin{align} \text{Cov}(\widehat{\pmb{\beta}}) &= \text{Cov}\left(\left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top Y}\right)\\ &=\left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top} \mathbf{Cov}(\mathbf{Y})\left[\left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top} \right]^\top\\ &= \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top} \sigma^2\mathbf{I}_n \mathbf{X}\left(\mathbf{X^\top X}\right)^{-1}\\ &= \sigma^2\left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\mathbf{X}\left(\mathbf{X^\top X}\right)^{-1}\\ &= \sigma^2\left(\mathbf{X^\top X}\right)^{-1} \;. \end{align} \]

  • Remember: given \(p\) covariates, there are \(p+1\) regression coefficients \(\beta_0,\beta_1,\dotsc,\beta_p\).
    • The covariance matrix of \(\widehat{\pmb{\beta}}\) is then a \((p+1)\times (p+1)\) matrix of the form

\[ \text{Cov}(\widehat{\pmb{\beta}}) = \begin{bmatrix} \mathrm{Var}(\widehat{\beta}_0) & \mathrm{Cov}(\widehat{\beta}_0,\widehat{\beta}_1) & \cdots & \cdots & \mathrm{Cov}(\widehat{\beta}_0,\widehat{\beta}_p) \\ \mathrm{Cov}(\widehat{\beta}_1,\widehat{\beta}_0) & \mathrm{Var}(\widehat{\beta}_1) & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \mathrm{Var}(\widehat{\beta}_{p-1}) & \mathrm{Cov}(\widehat{\beta}_{p-1},\widehat{\beta}_p) \\ \mathrm{Cov}(\widehat{\beta}_p,\widehat{\beta}_0) & \cdots & \cdots & \mathrm{Cov}(\widehat{\beta}_p,\widehat{\beta}_{p-1}) & \mathrm{Var}(\widehat{\beta}_p) \end{bmatrix} \;. \]

  • The diagonal entries are the variances of the individual estimators \(\widehat{\beta}_j\), \(j=0,1,\dotsc,p\).

Sampling distribution of \(\widehat{\pmb{\beta}}\)

  • \(\widehat{\pmb{\beta}} = \left(\mathbf{X^\top X}\right)^{-1}\mathbf{X^\top}\mathbf{Y}\) is a linear transformation of \(\mathbf{Y}\sim N_n(\mathbf{X}\pmb{\beta}, \sigma^2\mathbf{I}_n)\), so it is also (multivariate) normal.

  • Therefore, \[ \widehat{\pmb{\beta}} \sim N_{p+1}\left(\pmb{\beta},\; \sigma^2\left(\mathbf{X^\top X}\right)^{-1}\right) \;. \]

  • In particular, \(\widehat{\beta}_j \sim N\left(\beta_j,\; \sigma^2\left[\left(\mathbf{X^\top X}\right)^{-1}\right]_{jj}\right)\), \(j=0,1,\dotsc,p\).

The unknown parameter \(\sigma^2\)

  • We have \(\text{Cov}(\widehat{\pmb{\beta}})=\mathbf{\Sigma}_{\widehat{\pmb{\beta}}} = \sigma^2(\mathbf{X^\top X})^{-1}\), which depends on the unknown parameter \(\sigma^2\).

  • To estimate this covariance matrix, we estimate \(\sigma^2\) with

\[ \hat\sigma^2 = \frac{RSS}{n-(p+1)} = \frac{\mathbf{e^\top e}}{n-(p+1)}\;. \]

  • \(\hat\sigma^2 = \frac{RSS}{n-(p+1)}\) is frequently referred to as residual mean square and commonly denoted as \(MSE\).

  • Hence, \(\widehat{\text{Cov}}(\widehat{\pmb{\beta}}) = \widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}} = \widehat{\sigma}^2(\mathbf{X^\top X})^{-1}\)

  • Why divide by \(n-(p+1)\)? Same idea as the \(n-2\) in SLR: the residuals must satisfy \(p+1\) constraints (\(\mathbf{X^\top e} = \mathbf{0}\), the normal equations), so only \(n-(p+1)\) of them are “free”.

  • In Module 10, we will prove that \(\hat\sigma^2\) is unbiased for \(\sigma^2\) and see why replacing \(\sigma^2\) by \(\hat\sigma^2\) leads to a \(t\) distribution.

iClicker 3

Suppose we fit a model with \(p=3\) covariates and obtain the estimated covariance matrix of \(\widehat{\pmb{\beta}}\) below:

\[ \widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}} = \begin{bmatrix} 4 & 2 & 2 & 0 \\ 2 & 5 & 1 & -2 \\ 2 & 1 & 6 & 1 \\ 0 & -2 & 1 & 6 \end{bmatrix} \]

Based on this matrix, what is \(\widehat{\text{Cov}}(\widehat{\beta}_1,\widehat{\beta}_3)\)?


  1. \(5\)
  2. \(-2\)
  3. \(2\)
  1. \(1\)
  2. \(6\)

iClicker 3: Solution

  • Remember the first row is related to \(\beta_0\), so the counting of the rows and columns starts from \(0\) (counting from \(1\) would give \(2\), option (C)). \[ \widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}} = \begin{bmatrix} 4 & 2 & 2 & 0 \\ 2 & 5 & 1 & \boxed{-2} \\ 2 & 1 & 6 & 1 \\ 0 & \boxed{-2} & 1 & 6 \end{bmatrix} \]

  1. \(5\)
  2. \(-2\)
  3. \(2\)
  1. \(1\)
  2. \(6\)

Confidence intervals and hypothesis tests for \(\beta_j\)

  • Now, we can create confidence intervals and hypothesis tests for individual parameters \(\beta_j\), \(j=0,1,\dotsc,p\), based on \[ \frac{\widehat{\beta}_j - \beta_j}{\sqrt{\left(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\right)_{jj}}} \sim t_{n-(p+1)} \;, \]

  • Therefore, a \(100(1-\alpha)\%\) confidence interval for \(\beta_j\) is

\[ \left[\widehat{\beta}_j - t_{n-(p+1),\alpha/2} \cdot \sqrt{\left(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\right)_{jj}}, \widehat{\beta}_j + t_{n-(p+1),\alpha/2} \cdot \sqrt{\left(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\right)_{jj}}\right] \]

  • To test \(H_0: \beta_j = \beta_{j,0}\) vs \(H_1: \beta_j \neq \beta_{j,0}\) we can use the test statistic \[ \frac{\widehat{\beta}_j - \beta_{j,0}}{\sqrt{\left(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\right)_{jj}}} \sim t_{n-(p+1)} \qquad (\text{under } H_0) \]

Activity 2

  • Back to penguins! Let’s fit a linear model with response body_mass_g and covariates flipper_length_mm, bill_length_mm, and bill_depth_mm.

  • I have already computed the least squares estimates for you \(\widehat{\pmb{\beta}}\)

  • Manually compute the following quantities:

    1. The estimated covariance matrix \(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\).
    2. The 95% confidence interval for \(\beta_{\text{bill_depth_mm}}\).

Tip

  • res: recall the residual vector in matrix form is \(\mathbf{e} = \mathbf{y} - \mathbf{X}\widehat{\pmb{\beta}}\).
  • Sigma_beta: recall \(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}} = \widehat{\sigma}^2(\mathbf{X^\top X})^{-1}\), and \(\widehat{\sigma}^2 = \dfrac{\mathbf{e^\top e}}{n-(p+1)}\). You’ll need nrow(X) and ncol(X) to get \(n\) and \(p+1\).
  • CI_bill_depth: a \(100(1-\alpha)\%\) CI for \(\beta_j\) is \(\widehat{\beta}_j \pm t_{n-(p+1),\alpha/2}\sqrt{\left(\widehat{\mathbf{\Sigma}}_{\widehat{\pmb{\beta}}}\right)_{jj}}\). Check the order of the columns in X to figure out which entry of beta_hat (and which diagonal entry of Sigma_beta) corresponds to bill_depth_mm.

Solution:

#################
# Design Matrix #
#################
X <- cbind(1, flipper_length_mm, bill_length_mm, bill_depth_mm)

############
# Response #
############
y <- body_mass_g

###########################
# Least Squares Estimates #
###########################
beta_hat <- solve(t(X)%*%X,t(X)%*%y)

#############
# Residuals #
#############
res <- y - X %*% beta_hat

########################################
# Sample Covariance Matrix of beta-hat #
########################################
Sigma_beta <- solve(t(X)%*%X)*sum(res**2/(nrow(X)-ncol(X)))

#############################
# CI for beta_bill_depth_mm #
#############################
(CI_bill_depth <- beta_hat[4] + c(-1,1)*qt(0.975, nrow(X)-ncol(X))*sqrt(Sigma_beta[4,4]) )
  • Compare your results with the output of R:



iClicker 4

  • We fit the model
    body_mass_g ~ flipper_length_mm + bill_length_mm + bill_depth_mm

  • We obtain \(\text{CI}\left(\beta_{\text{bill_depth_mm}}, 0.95\right) = (-9.36,\, 45.03)\).

  • If we test \[H_0: \beta_{\text{bill_depth_mm}} = 0\quad \text{ vs } \quad H_1: \beta_{\text{bill_depth_mm}} \neq 0\] at \(\alpha=0.05\), would we reject or not reject \(H_0\)?


  1. reject \(H_0\), because zero is in the interval
  2. reject \(H_0\), because zero is not in the interval
  3. don’t reject \(H_0\), because zero is in the interval
  4. don’t reject \(H_0\), because zero is not in the interval

iClicker 4: Solution

  • We fit the model
    body_mass_g ~ flipper_length_mm + bill_length_mm + bill_depth_mm

  • We obtain \(\text{CI}\left(\beta_{\text{bill_depth_mm}}, 0.95\right) = (-9.36,\, 45.03)\).

  • If we test \[H_0: \beta_{\text{bill_depth_mm}} = 0\quad \text{ vs } \quad H_1: \beta_{\text{bill_depth_mm}} \neq 0\] at \(\alpha=0.05\), would we reject or not reject \(H_0\)?


  1. reject \(H_0\), because zero is in the interval
  2. reject \(H_0\), because zero is not in the interval
  3. don’t reject \(H_0\), because zero is in the interval
  4. don’t reject \(H_0\), because zero is not in the interval

Activity 3: About the Expected Response

  • Let \(\mathbf{x}_{new}=\begin{bmatrix} 1 & x_{new,1} & x_{new,2} & \cdots & x_{new,p} \end{bmatrix}^\top\).

  • The expected response at \(\mathbf{x}_{new}\) is \[ \mathbb{E}[Y_{new}] = \mathbf{x}_{new}^\top \pmb{\beta} \;. \]

  • An estimator of \(\mathbb{E}[Y_{new}]\) is \[ \widehat{\mathbb{E}}[Y_{new}] = \mathbf{x}_{new}^\top \widehat{\pmb{\beta}} \;. \]

  • What is the sampling distribution of \(\widehat{\mathbb{E}}[Y_{new}]\)?

Expected value of \(\widehat{\mathbb{E}}[Y_{new}]\)

\[ \begin{align} \mathbb{E}[\widehat{\mathbb{E}}[Y_{new}]] &= \mathbb{E}[\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}] \\ &= \mathbf{x}_{new}^\top \mathbb{E}[\widehat{\pmb{\beta}}] \\ &= \mathbf{x}_{new}^\top \pmb{\beta} \\ &= \mathbb{E}[Y_{new}] \;. \end{align} \]

  • Therefore, \(\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}\) is an unbiased estimator of \(\mathbb{E}[Y_{new}]\).

Covariance of \(\widehat{\mathbb{E}}[Y_{new}]\)

\[ \begin{align} \text{Var}[\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}] &= \mathbf{x}_{new}^\top \text{Cov}[\widehat{\pmb{\beta}}] \mathbf{x}_{new} \\ &= \mathbf{x}_{new}^\top \sigma^2(\mathbf{X^\top X})^{-1} \mathbf{x}_{new} \\ &= \sigma^2 \mathbf{x}_{new}^\top (\mathbf{X^\top X})^{-1} \mathbf{x}_{new} \;. \end{align} \]

CI for \(\widehat{\mathbb{E}}[Y_{new}]\)

  • Therefore, \(\widehat{\mathbb{E}}[Y_{new}] \sim N(\mathbf{x}_{new}^\top \pmb{\beta}, \text{Var}[\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}])\).
  • A \(100(1-\alpha)\%\) confidence interval for \(\mathbb{E}[Y_{new}]\) is

\[ CI(\mathbb{E}[Y_{new}], 1-\alpha) = \mathbf{x}_{new}^\top \widehat{\pmb{\beta}} \pm t_{n-(p+1),\alpha/2} \times \sqrt{\widehat{\sigma}^2 \mathbf{x}_{new}^\top (\mathbf{X^\top X})^{-1} \mathbf{x}_{new}} \]

Activity 4

  • Compute the \(95\%\) confidence interval for the expected body mass of a penguin with flipper length \(200 \text{ mm}\), bill length \(40 \text{ mm}\), and bill depth \(18 \text{ mm}\).

Tip

  • x_new is already built for you: its entries follow the same order as the columns of X (intercept first, then the covariates in the order used to build X).
  • Recall the CI for \(\mathbb{E}[Y_{new}]\) is \(\mathbf{x}_{new}^\top\widehat{\pmb{\beta}} \pm t_{n-(p+1),\alpha/2}\sqrt{\widehat{\sigma}^2\, \mathbf{x}_{new}^\top(\mathbf{X^\top X})^{-1}\mathbf{x}_{new}}\). Everything you need (beta_hat, X, res) is already available from the setup chunk.
  • The degrees of freedom are \(n-(p+1)\), i.e. nrow(X) - ncol(X).

Solution:

x_new <- matrix(c(1, 200, 40, 18), ncol = 1)
df <- nrow(X) - ncol(X)
(CI_expected_body_mass <- c(
  t(x_new) %*% beta_hat - qt(0.975, df)*sqrt(t(x_new) %*% solve(t(X) %*% X) %*% x_new * sum(res**2)/(df)),
  t(x_new) %*% beta_hat + qt(0.975, df)*sqrt(t(x_new) %*% solve(t(X) %*% X) %*% x_new * sum(res**2)/(df))
))
  • You can compare with the output of R:

Activity 5: Prediction interval

  • Now derive the prediction interval for a new observation \(Y_{new}\) at \(\mathbf{x}_{new}\).

  • The prediction error is \[ \widehat{Y}_{new}-Y_{new} = \mathbf{x}_{new}^\top \widehat{\pmb{\beta}} - Y_{new} \;. \]

iClicker 5

What is \(\text{Var}\left[\widehat{Y}_{new}-Y_{new}\right]\), the variance of the prediction error for a new observation at \(\mathbf{x}_{new}\)?


  1. \(\sigma^2\left(\mathbf{X^\top X}\right)^{-1}\)
  2. \(\sigma^2\,\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\)
  3. \(\sigma^2\left(1+\left(\mathbf{X^\top X}\right)^{-1}\right)\)
  1. \(\sigma^2\left(1+\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\right)\)
  2. \(\sigma^2\left(2+\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\right)\)

iClicker 5: Solution

What is \(\text{Var}\left[\widehat{Y}_{new}-Y_{new}\right]\), the variance of the prediction error for a new observation at \(\mathbf{x}_{new}\)?


  1. \(\sigma^2\left(\mathbf{X^\top X}\right)^{-1}\)
  2. \(\sigma^2\,\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\)
  3. \(\sigma^2\left(1+\left(\mathbf{X^\top X}\right)^{-1}\right)\)
  1. \(\sigma^2\left(1+\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\right)\)
  2. \(\sigma^2\left(2+\mathbf{x}_{new}^\top\left(\mathbf{X^\top X}\right)^{-1}\mathbf{x}_{new}\right)\)

Prediction interval

  • The prediction error is \[ \widehat{Y}_{new}-Y_{new} = \mathbf{x}_{new}^\top \widehat{\pmb{\beta}} - Y_{new} \;. \]

  • \(Y_{new}\) is a new observation, so it is independent of \(\widehat{\pmb{\beta}}\) (which only depends on the training data). Hence \(\text{Cov}\left(\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}, Y_{new}\right) = 0\).

  • The variance of the prediction error is

\[ \begin{align} \text{Var}[\widehat{Y}_{new}-Y_{new}] &= \text{Var}[\mathbf{x}_{new}^\top \widehat{\pmb{\beta}} - Y_{new}] \\ &= \text{Var}\left(\mathbf{x}_{new}^\top \widehat{\pmb{\beta}}\right) + Var[Y_{new}] \qquad (\text{by independence}) \\ &= \mathbf{x}_{new}^\top \text{Cov}\left(\widehat{\pmb{\beta}}\right)\mathbf{x}_{new} + Var[Y_{new}] \\ &= \sigma^2 \mathbf{x}_{new}^\top (\mathbf{X^\top X})^{-1} \mathbf{x}_{new} + Var[Y_{new}] \\ &= \sigma^2 \mathbf{x}_{new}^\top (\mathbf{X^\top X})^{-1} \mathbf{x}_{new} + \sigma^2 \;. \end{align} \]

Prediction interval: Computation

  • A \(100(1-\alpha)\%\) prediction interval for a new observation \(Y_{new}\) at \(\mathbf{x}_{new}\) is

\[ PI(Y_{new}|\mathbf{x}_{new}, 1-\alpha) = \mathbf{x}_{new}^\top \widehat{\pmb{\beta}} \pm t_{n-(p+1),\alpha/2} \times \sqrt{\widehat{\sigma}^2 \left(1 + \mathbf{x}_{new}^\top (\mathbf{X^\top X})^{-1} \mathbf{x}_{new}\right)} \]

Activity 6

  • Compute the 95% prediction interval for the body mass of a new penguin with flipper length 200 mm, bill length 40 mm, and bill depth 18 mm.

Tip

  • x_new is built the same way as in Activity 4: a column vector matching the column order of X (intercept first).
  • Recall the PI for a new observation \(Y_{new}\) is \(\mathbf{x}_{new}^\top\widehat{\pmb{\beta}} \pm t_{n-(p+1),\alpha/2}\sqrt{\widehat{\sigma}^2\left(1 + \mathbf{x}_{new}^\top(\mathbf{X^\top X})^{-1}\mathbf{x}_{new}\right)}\) — compare to the CI formula from Activity 4: the extra “\(+1\)” inside the square root accounts for the variability of the new observation itself around its mean.

Solution:

x_new <- matrix(c(1, 200, 40, 18), ncol = 1)
df <- nrow(X) - ncol(X)
(prediction_interval_body_mass <- c(
  t(x_new) %*% beta_hat - qt(0.975, df)*sqrt((t(x_new) %*% solve(t(X) %*% X) %*% x_new + 1)* sum(res**2)/(df)),
  t(x_new) %*% beta_hat + qt(0.975, df)*sqrt((t(x_new) %*% solve(t(X) %*% X) %*% x_new + 1)* sum(res**2)/(df))
))
  • You can compare with the output of R: