// Tutorial //

The predict() function in R programming

Published on August 3, 2022
Default avatar
By Prajwal CN
Developer and author at DigitalOcean.
The predict() function in R programming

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

The predict() function in R is used to predict the values based on the input data. All the modeling aspects in the R program will make use of the predict() function in its own way, but note that the functionality of the predict() function remains the same irrespective of the case.


Syntax of predict() function in R

predict(): The predict() function in R is used to predict the values based on the input data.

predict(object,newdata,interval)

Where:

  • Object = The class inheriting from the “lm”
  • newdata = Input data to predict the values
  • Interval = Type of interval calculation

An example of the predict() function

We need data to predict the values. Hence, we are going to import the data from the inbuilt dataset in R - “Cars”.

The simple reason for this is, it is a simple dataset having only 2 attributes in it.

df<-datasets::cars
     speed dist
1      4    2
2      4   10
3      7    4
4      7   22
5      8   16
6      9   10
7     10   18
8     10   26
9     10   34
10    11   17

We have two columns such as ‘speed’ and ‘dist’. We have the first 10 values in it. All we need to do is to predict future values using this data. Interesting right? Let’s roll!

First, we need to compute a linear model for this data frame. Executing the below code will fetch you the linear model results.

#Creates a linear model
my_linear_model <- lm(dist~speed,data = df)

#Prints the model results 
my_linear_model
Call:
lm(formula = dist ~ speed, data = df)

Coefficients:
(Intercept)        speed  
    -17.579        3.932  

The linear model has returned the speed of the cars as per our input data behavior. Now, we got the model and everything is ready to get values predicted.

Let’s see how it works!

#Creating a data frame
variable_speed<-data.frame(speed=c(11,11,12,12,12,12,13,13,13,13))

#fiting the linear model
liner_model<-lm(dist~speed,data = df)

#predicts the future values
predict(liner_model,newdata = variable_speed)
     1         2         3         4         5        
 25.67740  25.67740  29.60981  29.60981  29.60981
      6         7         8       9        10 
29.60981  33.54222  33.54222  33.54222  33.54222 

Well, we have successfully predicted the future distance values based on the previous data and with the help of the linear model.

Now, we have to check the “confidence” level in our predicted values to see how accurate is our prediction.

Confidence in the Predicted Values

The confidence interval in the predict function will help us to gauge the uncertainty in the predictions.

#Input data
variable_speed <-data.frame(speed=c(11,11,12,12,12,12,13,13,13,13))

#Fits the model
liner_model<-lm(dist~speed,data = df)

#Predits the values with confidence interval 
predict(liner_model,newdata = variable_speed,interval = 'confidence')
      fit      lwr      upr
1  25.67740 19.96453 31.39028
2  25.67740 19.96453 31.39028
3  29.60981 24.39514 34.82448
4  29.60981 24.39514 34.82448
5  29.60981 24.39514 34.82448
6  29.60981 24.39514 34.82448
7  33.54222 28.73134 38.35310
8  33.54222 28.73134 38.35310
9  33.54222 28.73134 38.35310
10 33.54222 28.73134 38.35310

You can see the confidence interval in our predicted values in the above output.

The output clearly says that the cars which are traveling at a speed of 11-13 mph have chances to travel the distance in the range of 19.9 to 31.3 miles.

Simple right? This is how the predict() function in R language works. I hope now you understand the predict() function in R.

Wrapping Up

The predict() function is used to predict the values based on the previous data behaviors and thus by fitting that data to the model.

You can also use the confidence intervals to check the accuracy of our predictions.

That’s all for now. Happy Predicting!

More read: R documentation


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

What is the meaning of attribute Fit in the above result set?

- Learner

    speed prediction calculation details please for the new learners.that table shows just the numbers . Thank You

    - Learner

      “The output clearly says that the cars which are traveling at a speed of 11-13 mph have chances to travel the distance in the range of 19.9 to 31.3 miles.” No, it really doesn’t.

      - None ya