Question
(Prediction Intervals without `predict`) how to write a function named `calc_pred_int` that performs calculates prediction intervals: $$ hat{y}(x) pm t_{alpha/2, n - 2} cdot s_esqrt{1
(Prediction Intervals "without" `predict`)
how to write a function named `calc_pred_int` that performs calculates prediction intervals:
$$
\hat{y}(x) \pm t_{\alpha/2, n - 2} \cdot s_e\sqrt{1 + \frac{1}{n}+\frac{(x-\bar{x})^2}{S_{xx}}}.
$$
for the linear model
$$
Y_i = \beta_0 + \beta_1 x_i + \epsilon_i.
$$
**(a)** with this function. You may use the `predict()` function, but you may **not** supply a value for the `level` argument of `predict()`. (You can certainly use `predict()` any way you would like in order to check your work.)
The function should take three inputs:
- `model`, a model object that is the result of fitting the SLR model with `lm()`
- `newdata`, a data frame with a single observation (row)
- This data frame will need to have a variable (column) with the same name as the data used to fit `model`.
- `level`, the level (0.90, 0.95, etc) for the interval with a default value of `0.95`
The function should return a named vector with three elements:
- `estimate`, the midpoint of the interval
- `lower`, the lower bound of the interval
- `upper`, the upper bound of the interval
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started