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 prediction intervals and confidence intervals. In the following recipe, we introduce how to predict unknown values under these two measurements.
Getting ready
One needs to have completed the previous recipe by fitting the house rental data into a regression model and have the fitted model assigned to variable lmfit
.
How to do it…
Perform the following steps to predict values with linear regression:
- Assign values to be predicted to
newdata
:> newdata <- data.frame(Sqft=c(800, 900, 1000))
- Compute the prediction result of the given data:
> predict(lmfit ,newdata) 1 2 3 34092.60 37926.04 41759.47
- On the other hand, you can obtain the coefficient and intercept of the fitted model:
> lmfit$coefficients[1] (Intercept) 3425.133 > lmfit$coefficients[2] Sqft...