Using linear regression to predict unknown values
With a fitted regression model, we can apply the model to predict unknown values. For regression models, we can express the precision of prediction with a prediction interval and a confidence interval. In the following recipe, we will introduce how to predict unknown values under these two measurements.
Getting ready
You need to have completed the previous recipe by computing the linear model of the x
and y1
variables from the quartet
dataset.
How to do it...
Perform the following steps to predict values with linear regression:
- Fit a linear model with the
x
andy1
variables:
> lmfit = lm(y1~x, Quartet)
- Assign values to be predicted into
newdata
:
> newdata = data.frame(x = c(3,6,15))
- Compute the prediction result using the confidence interval with
level
set as0.95
:
> predict(lmfit, newdata, interval="confidence", level=0.95) Output: fit lwr upr 1 4.500364 2.691375 6.309352 2 6.000636...