Tutorial

How To Use the predict() Function in R Programming

Updated on September 30, 2022
author

Prajwal CN

How To Use the predict() Function in R Programming

Introduction

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 their own way, but note that the functionality of the predict() function remains the same irrespective of the case.

In this article, you will explore how to use the predict() function in R.

Prerequisites

To complete this tutorial, you will need:

Syntax of the predict() function in R

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

predict(object, newdata, interval)
  • object: The class inheriting from the linear model
  • newdata: Input data to predict the values
  • interval: Type of interval calculation

An example of the predict() function

We will need data to predict the values. For the purpose of this example, we can import the built-in dataset in R - “Cars”.

df <- datasets::cars

This will assign a data frame a collection of speed and distance (dist) values:

     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

Next, we will use predict() to determine future values using this data.

First, we need to compute a linear model for this data frame:

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

# Prints the model results
my_linear_model

Executing this code will calculate the linear model results:

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 that we have a model, we can apply predict().

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

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

# Predicts the future values
predict(linear_model, newdata = variable_speed)

This code generates the following output:

       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 our prediction is.

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
linear_model <- lm(dist~speed, data = df)

# Predicts the values with confidence interval
predict(linear_model, newdata = variable_speed, interval = 'confidence')

This code generates the following output:

      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.

From this output, we can predict that the cars which are traveling at a speed of 11-13 mph have a likelihood to travel a distance in the range of 19.9 to 31.3 miles.

Conclusion

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.

References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Prajwal CN

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 1, 2022

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

- Learner

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 11, 2021

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

    - Learner

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 20, 2021

      “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

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.