R Predict Function: How To Use predict() Function In R?

Predict() function in R

In this tutorial, you will learn how to use the predict() function in R. The predict function in R can help you predict the value based on your data. Let’s follow this article to learn more about it with the explanation and examples below.

Predict() function in R

What does predict() do in R?

The predict function in R can help you predict the value based on your data. You can use the predict() function to predict the value of each attribute of your model, each attribute uses this function in its way, but the functionality of the predict() function maintains the same in every case. 

Syntax:

predict(object, newdata, interval)

Parameters:

  • object: The model object that the predictions are desired.
  • newdata: The name of the data frame that predicts value.
  • interval: The type of interval to make.

After learning the usage and syntax of the predict() function, you will learn how to use this function in R in the next title.

How to use the predict() function in R

Create a data frame

First, you have to have the data to predict the values. The same as machine learning, the computer needs the data to learn, and then it can predict the values based on this data. For convenience, I will get the data in the ‘datasets‘ package built-in R. 

Look at the example below:

# Create a data frame that is obtained from the 'datasets' package
cars_data <- datasets::cars
cars_data

Output:

speed dist
...
10    11   17
11    11   28
12    12   14
13    12   20
14    12   24
15    12   28
…

This data frame has two attributes which are the speed and distance of the cars.

Build the linear regression

After creating the data frame, you have to build the linear regression based on this data frame. 

Look at the example below:

# Create linear regression
 linear_model <- lm(dist~speed, data = cars_data)
 linear_model

Output:

Call:
lm(formula = dist ~ speed, data = cars_data)

Coefficients:
(Intercept)        speed  
    -17.579        3.932  

Use the predict() function to predict values

In this example, you predict the distance based on the speed. So then, you will create the model to use the predict() function and fit it with the previous linear expression. 

Look at the example below:

# Create a data frame that has speed values to predict distance values.
data_test <- data.frame(speed = c (6, 7, 47, 25, 14, 12, 16, 20))

predict(linear_model, newdata = data_test)

Output

1          2          3          4          5          6 
  6.015358   9.947766 167.244117  80.731124  37.474628  29.609810 
         7          8 
 45.339445  61.069080 

Confidence in the predicted values

The confidence interval can help you predict the values in an uncertain range around the mean predictions.

Look at the example below:

predict(linear_model, newdata = data_test, interval = 'confidence')

Output

  fit        lwr       upr
1   6.015358  -2.973341  15.00406
2   9.947766   1.678977  18.21656
3 167.244117 140.484322 194.00391
4  80.731124  71.596083  89.86617
5  37.474628  32.947783  42.00147
6  29.609810  24.395138  34.82448
7  45.339445  40.937676  49.74121
8  61.069080  55.247285  66.89088

Prediction interval

The confidence interval can help you predict the values in an uncertain range around a single value.

Look at the example below:

predict(linear_model, newdata = data_test, interval = 'prediction')

Output

 fit        lwr       upr
1   6.015358 -26.187314  38.21803
2   9.947766 -22.061423  41.95696
3 167.244117 126.350328 208.13791
4  80.731124  48.487298 112.97495
5  37.474628   6.222305  68.72695
6  29.609810  -1.749529  60.96915
7  45.339445  14.104995  76.57390
8  61.069080  29.603089  92.53507

Summary

You have learned the predict() function in R. By this function, you can predict the value based on your data. If you have any questions about this function, feel free to leave your comments below, and we will answer your questions. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *