![]() |
VOOZH | about |
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.
predict(object, newdata, type = "response", se.fit = FALSE, ...)
Here is how we can use predict() in different contexts:
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
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
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: