VOOZH about

URL: https://www.geeksforgeeks.org/r-machine-learning/predict-function-in-r/

⇱ Predict() function in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Predict() function in R

Last Updated : 23 Jul, 2025

The predict() function in R is a tool used for making predictions from models By using predict(), we can generate predictions based on the fitted model and new input data. The predict() function in R is used to make predictions based on the model object we create. It can predict both the response variable (i.e., the dependent variable) for a new set of input variables or provide model diagnostics such as residuals, confidence intervals or fitted values.

Syntax

predict(object, newdata, type = "response", se.fit = FALSE, ...)

  • object: The model object (e.g., the result of a lm(), glm() or rpart() function).
  • newdata: The new data for which predictions are required.
  • type: Specifies the type of prediction to return (e.g., response, link, etc.).
  • se.fit: If TRUE, standard errors of the predicted values will be returned.

Here is how we can use predict() in different contexts:

Example 1: Predicting with a Linear Model

Let's start by fitting a simple linear regression model using the lm() function and then making predictions using predict().

In this case, the predict() function will return the predicted values of y for the new x values (11, 12 and 13).

Output:

1 2 3
48.86703 48.76208 48.65714

Example 2: Predicting with a Logistic Regression Model

let's use logistic regression with the glm() function and make predictions using the predict() function.

Here, the type = "response" argument tells predict() to return probabilities for the bought variable

Output:

1 2 3
0.4470355 0.5169052 0.5690249

Example 3: Predicting Class Labels with a Decision Tree

In this example, we will use the rpart() function to create a decision tree and predict class labels for new data.

Here, the type = "class" argument returns the predicted class labels (setosa, versicolor or virginica).

Output:

1 2 3
setosa virginica virginica
Levels: setosa versicolor virginica

Related Article:

Comment

Explore