What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Predict() Function in R | With Working Examples

  • Oct 10, 2023
  • 6 Minutes Read
Predict() Function in R | With Working Examples

When it comes to statistical analysis and data modeling in R, the predict function plays a pivotal role. Whether you are a seasoned data scientist or just starting with R, understanding how to use `predict` is crucial. In this article, we will take a deep dive into the world of the `predict` function in R, exploring its purpose, usage, and nuances.

What is the `predict` Function in R?

In R, the predict function is a versatile tool that allows you to make predictions based on statistical models, most commonly linear regression models created using lm. This function is used to estimate or forecast values based on the fitted model and a set of new data points or observations. Essentially, it helps you apply your statistical model to real-world situations by making predictions.

How to Use `predict` in R?

The basic syntax for using the predict function in R is as follows:

predict(object, newdata, ...)

 

Here's what each parameter means:

  • object: This is the model object you want to use for prediction. It should be a fitted statistical model, such as the result of lm().
  • newdata: This parameter represents the new data for which you want to make predictions. It should be a data frame or list containing the predictor variables (features) you used when fitting the model.
  • ...: Additional arguments that can be passed to control various aspects of prediction, such as confidence intervals.

Example: Predicting with `lm` in R

Let's illustrate how to use `predict` with a simple example of linear regression in R:

# Create a simple data frame
data <- data.frame(x = 1:10, y = 2 * (1:10) + rnorm(10))

# Fit a linear regression model
model <- lm(y ~ x, data = data)

# Create new data for prediction
new_data <- data.frame(x = 11:15)

# Use predict to make predictions
predictions <- predict(model, newdata = new_data)

# Print the predictions
print(predictions)

 

In this example, we first create a simple dataset data and then fit a linear regression model (lm) to it. We then create a new dataset new_data for which we want to make predictions using the predict function.

What Does `predict()` Return in R?

The predict function returns a vector of predicted values for the new data points provided in newdata. Each element of this vector corresponds to the prediction for a specific observation in newdata.

In the example above, predictions would be a vector containing the predicted values for new_data.

What is the Difference Between predict and fitted in R?

While both predict and fitted functions in R are used for making predictions, they serve slightly different purposes.

  • predict: This function takes a model object and new data as input and returns predictions for the new data. It is used to make out-of-sample predictions or forecasts.
  • fitted: This function takes a model object as input and returns the predicted values for the data points that were used to fit the model (i.e., the training data). It provides in-sample predictions.

In essence, predict is used for predicting new, unseen data, while fitted provides predictions for the data used to build the model.

Confidence Intervals with predict in R

One of the powerful features of the predict function is its ability to calculate confidence intervals for predictions. Confidence intervals provide a range of values within which the true value is likely to fall. In R, you can obtain confidence intervals using the interval argument within the predict function.

Here's an example:

# Predict with confidence intervals
predictions_with_ci <- predict(model, newdata = new_data, interval = "confidence", level = 0.95)

# Print the predictions with confidence intervals
print(predictions_with_ci)

 

In this code, we added interval = "confidence" and level = 0.95 to the predict function. This instructs R to calculate a 95% confidence interval for each prediction. The result will include the lower and upper bounds of the confidence interval for each predicted value.

To provide an output for predictions_with_ci using made-up data, let's assume we have a linear regression model that predicts housing prices based on the number of bedrooms, and we want to predict the price of a house with three bedrooms, including a 95% confidence interval. Here's what the output might look like:

  fit       lwr      upr
1  $250,000  $230,000  $270,000

 

In this output:

  • fit: This is the point estimate or prediction. It suggests that the predicted price of a house with three bedrooms is $250,000.
  • lwr: This is the lower bound of the 95% confidence interval. It indicates that we are 95% confident that the true price of the house falls above $230,000.
  • upr: This is the upper bound of the 95% confidence interval. It means that we are 95% confident that the true price of the house falls below $270,000.

So, based on our dummy output, we can say that we predict the price of the house to be around $250,000, with a 95% confidence interval ranging from $230,000 to $270,000. This interval provides a range within which we are reasonably confident the true price lies.

Conclusion

The predict function in R is an invaluable tool for making predictions based on statistical models. It allows data scientists and analysts to extend the utility of their models to real-world scenarios. By understanding its usage, syntax, and the additional options it offers, you can harness the full power of this function in your data analysis projects. Whether you are using linear regression, logistic regression, or any other modeling technique, predict is your go-to function for generating meaningful predictions.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abhisek Ganguly
Passionate machine learning enthusiast with a deep love for computer science, dedicated to pushing the boundaries of AI through academic research and sharing knowledge through teaching.