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.
To complete this tutorial, you will need:
predict()
function in RThe predict()
function in R is used to predict the values based on the input data.
We will need data to predict the values. For the purpose of this example, we can import the built-in dataset in R - “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:
Executing this code will calculate the linear model results:
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()
.
This code generates the following output:
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.
The confidence interval in the predict function will help us to gauge the uncertainty in the predictions.
This code generates the following output:
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.
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.
“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
speed prediction calculation details please for the new learners.that table shows just the numbers . Thank You
- Learner
What is the meaning of attribute Fit in the above result set?
- Learner