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:

  1. Assign values to be predicted to newdata:
    > newdata <- data.frame(Sqft=c(800, 900, 1000))
    
  2. Compute the prediction result of the given data:
    > predict(lmfit ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.