STAT 306 - Module 05
predict().We know that \(\widehat{Y}_{\text{new}} = \widehat{\beta}_0 + \widehat{\beta}_1 x_{\text{new}}\) is the predicted value of \(Y\) when \(X=x_{\text{new}}\).
\(\widehat{Y}_{\text{new}}\) is also an estimator for the mean value of the response when \(X=x_{\text{new}}\).
\[ \begin{align} Var(\widehat{Y}_{\text{new}}) &= Var\left(\bar{Y} - \widehat{\beta}_1\bar{x} + \widehat{\beta}_1x_{\text{new}}\right) \\ &= Var\left(\bar{Y} + \widehat{\beta}_1(x_{\text{new}}-\bar{x})\right) \\ &= Var(\bar{Y}) + (x_{\text{new}}-\bar{x})^2Var(\widehat{\beta}_1) + 2(x_{\text{new}}-\bar{x})Cov(\bar{Y},\widehat{\beta}_1)\\ &= \frac{\sigma^2}{n} + \frac{(x_{\text{new}}-\bar{x})^2\sigma^2}{\sum_{i=1}^n(x_i-\bar{x})^2} \\ &= \sigma^2\left[\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}\right] \;. \end{align} \]
What is the distribution of the prediction \(\widehat{Y}_{\text{new}} = \widehat{\beta}_0 + \widehat{\beta}_1 x_{\text{new}}\)?
What is the distribution of the prediction \(\widehat{Y}_{\text{new}} = \widehat{\beta}_0 + \widehat{\beta}_1 x_{\text{new}}\)?
Using \(\widehat{\beta}_0 = \bar{Y} - \widehat{\beta}_1\bar{x}\) and \(\widehat{\beta}_1 = \frac{\sum_{i=1}^n(x_i-\bar{x})Y_i}{\sum_{i=1}^n(x_i-\bar{x})^2}\), we can write \(\widehat{Y}_{\text{new}}\) directly in terms of the \(Y_i\)s: \[ \widehat{Y}_{\text{new}} = \bar{Y} + \widehat{\beta}_1(x_{\text{new}}-\bar{x}) = \sum_{i=1}^n \underbrace{\left[\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})(x_i-\bar{x})}{\sum_{j=1}^n(x_j-\bar{x})^2}\right]}_{c_i}Y_i \;. \]
The \(c_i\) are constants, so \(\widehat{Y}_{\text{new}}\) is a linear combination of the independent normal \(Y_i\)s, and hence normally distributed.
Therefore, \[\widehat{Y}_{\text{new}} \sim N\left(\beta_0 + \beta_1 x_{\text{new}}, \sigma^2\left[\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}\right]\right) \;.\]
\[\frac{\widehat{Y}_{\text{new}} - \mathbb{E}\left[Y_{\text{new}}\right]}{\sigma\sqrt{\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}}} \sim N(0,1)\]
\[\begin{align} CI(\mathbb{E}[Y_{\text{new}}], 1-\alpha) &= \widehat{Y}_{\text{new}} \pm t^*_{n-2,1-\frac{\alpha}{2}}\times \widehat{\text{SE}}(\widehat{Y}_{\text{new}})\\ &=\widehat{Y}_{\text{new}} \pm t^*_{n-2,1-\frac{\alpha}{2}}\times \hat{\sigma}\sqrt{\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}} \;. \end{align}\]
Which of the following is the 95% confidence interval for \(\mathbb{E}[Y_{\text{new}}]\)?
Which of the following is the 95% confidence interval for \(\mathbb{E}[Y_{\text{new}}]\)?
We are \(95\%\) confident that the mean body mass of penguins with a flipper length of \(200 \text{ mm}\) is between \(4116\text{ g}\) and \(4201\text{ g}\).
Solution
\[ CI\big(\mathbb{E}\left[Y_{\text{new}}\right], 1-\alpha\big) = \widehat{Y}_{\text{new}} \pm t^*_{n-2,1-\frac{\alpha}{2}}\times \hat{\sigma}\sqrt{\frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}} \;.\]
By inspection of the confidence interval, the width increases as \(x_{\text{new}}\) moves away from \(\bar{x}\).
This implies that the farther \(x_{\text{new}}\) is from \(\bar{x}\), the less certainty we have for \(\mathbb{E}[Y_{\text{new}}]\) (or equivalently, the closer \(x_{\text{new}}\) is to \(\bar{x}\), the more certainty we have for \(\mathbb{E}[Y_{\text{new}}]\)). The confidence interval is narrowest at \(x_{\text{new}} = \bar{x}\).
The regression line \(\widehat{Y} = \widehat{\beta}_0 + \widehat{\beta}_1 x\) is an estimate of the mean of \(Y\) for a given value \(x\).
The prediction \(\widehat{Y}_{\text{new}}\), which is an estimator for the mean of \(Y_{\text{new}}\), when \(X=x_{\text{new}}\), is the best predictor of \(Y_{\text{new}}\) (in terms of MSE).
Note that in the first part of this lecture we constructed a confidence interval for the expected response; i.e., \(\mathbb{E}[Y_{\text{new}}]\) when \(x=x_{\text{new}}\).
Now, we want to predict a particular response \(Y_{\text{new}}\) at \(x=x_{\text{new}}\).
A prediction interval is an interval that likely contains the value of a new observation \(Y_{\text{new}}\) at a given value \(x=x_{\text{new}}\).
This is different from a confidence interval for the expected response \(\mathbb{E}\left[Y_{\text{new}}\right]\) when \(x=x_{\text{new}}\).
Therefore, even with known parameters, a prediction interval is given by: \[ (\beta_0 + \beta_1 x_{\text{new}}) \pm z^*_{1-\frac{\alpha}{2}} \times \sigma \] where \(z^*_{1-\frac{\alpha}{2}}\) is the \((1-\frac{\alpha}{2})\) quantile of the standard normal distribution.
Even with known parameters, we can only predict \(Y_{\text{new}}\) within an interval that accounts for this irreducible variability: \(\sigma\).
Goal: In this activity, we will work towards constructing a prediction interval:
What is \(\text{Var}\left(\widehat{Y}_{\text{new}} - Y_{\text{new}}\right)\)?
What is \(\text{Var}\left(\widehat{Y}_{\text{new}} - Y_{\text{new}}\right)\)?
As \(n\to\infty\), what happens to the variance of the prediction error, \(\text{Var}\left(\widehat{Y}_{\text{new}} - Y_{\text{new}}\right)\)?
As \(n\to\infty\), what happens to the variance of the prediction error, \(\text{Var}\left(\widehat{Y}_{\text{new}} - Y_{\text{new}}\right)\)?
As we saw before, \(\widehat{Y}_{\text{new}} = \sum_{i=1}^n c_iY_i\), where \(c_i = \frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})(x_i-\bar{x})}{\sum_{j=1}^n(x_j-\bar{x})^2}\) are constants.
Therefore, \[ \widehat{Y}_{\text{new}}-Y_{\text{new}} = c_1Y_1 + \dotsb + c_nY_n - Y_{\text{new}} \] is a linear combination of \(Y_1,\dotsc,Y_n,Y_{\text{new}}\).
By the model assumptions, \(Y_1,\dotsc,Y_n,Y_{\text{new}}\) are independent normal random variables, so \(\widehat{Y}_{\text{new}}-Y_{\text{new}}\) is normally distributed.
\[ \text{Var}\left[\widehat{Y}_{\text{new}}-Y_{\text{new}}\right] = \text{Var}\left(\widehat{Y}_{\text{new}}\right) + \text{Var}\left(Y_{\text{new}}\right) - 2\text{Cov}\left(\widehat{Y}_{\text{new}},Y_{\text{new}}\right) \]
Therefore, we have that \[\frac{\widehat{Y}_{\text{new}}-Y_{\text{new}}}{\sigma\sqrt{1 + \frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}}} \sim N(0,1) \;.\]
Similarly to what we did for \(\widehat{\beta}_1\), we can replace \(\sigma\) with \(\hat{\sigma}\) to obtain a pivotal quantity: \[\frac{\widehat{Y}_{\text{new}}-Y_{\text{new}}}{\hat{\sigma}\sqrt{1 + \frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}}} \sim t_{n-2} \;.\]
\[ \begin{align} PI(Y_{\text{new}}, 1-\alpha) &= \widehat{Y}_{\text{new}} \pm t^*_{n-2,1-\frac{\alpha}{2}}\times \widehat{\text{SE}}_{\text{pred}} \\ &= \widehat{Y}_{\text{new}} \pm t^*_{n-2,1-\frac{\alpha}{2}}\times \hat{\sigma}\sqrt{1 + \frac{1}{n} + \frac{(x_{\text{new}}-\bar{x})^2}{\sum_{i=1}^n(x_i-\bar{x})^2}} \;. \end{align} \]
What is the 95% prediction interval for \(Y_{\text{new}}\) at \(x = x_{\text{new}}\)?
What is the 95% prediction interval for \(Y_{\text{new}}\) at \(x = x_{\text{new}}\)?
model <- lm(body_mass_g ~ flipper_length_mm, data = penguins_clean):
predict function expects a data frame with the new \(x\) value and the same variable name as in the model.predict() function with the argument interval="confidence" for the confidence interval for the expected response, and interval="prediction" for the prediction interval.Compared to the 95% confidence interval for the expected response \(\mathbb{E}[Y_{\text{new}}]\), the 95% prediction interval for \(Y_{\text{new}}\) is…
Compared to the 95% confidence interval for the expected response \(\mathbb{E}[Y_{\text{new}}]\), the 95% prediction interval for \(Y_{\text{new}}\) is…
The prediction interval has an irreducible variability, meaning that no matter what the sample size is, we cannot reduce the variability more than this value. What value is this?
The prediction interval has an irreducible variability, meaning that no matter what the sample size is, we cannot reduce the variability more than this value. What value is this?
Two Inferential Goals at \(X = x_{\text{new}}\):
Standard Errors: \[ \begin{aligned} \widehat{\text{SE}}(\widehat{Y}_{\text{new}}) &= \widehat{\sigma}\sqrt{\frac{1}{n} + \frac{(x_{\text{new}} - \bar{x})^2}{\sum_{i=1}^n (x_i - \bar{x})^2}} \quad \xrightarrow{n \to \infty} 0 \\ \widehat{\text{SE}}_{\text{pred}} &= \widehat{\sigma}\sqrt{1 + \frac{1}{n} + \frac{(x_{\text{new}} - \bar{x})^2}{\sum_{i=1}^n (x_i - \bar{x})^2}} \quad \xrightarrow{n \to \infty} \sigma \end{aligned} \]
Confidence vs. Prediction Intervals:
Shape of the Bands:
Computation in R:
predict(model, newdata, interval = "confidence") for the expected response.predict(model, newdata, interval = "prediction") for a new observation.© 2025 Rodolfo Lourenzutti – Material Licensed under CC By-SA 4.0