Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Predict() function in R
Next article icon

Predict() function in R

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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().

R
# Sample data set.seed(123) d <- data.frame(   x = rnorm(100, mean = 10, sd = 5),   y = rnorm(100, mean = 50, sd = 10) ) # Fit the linear model model <- lm(y ~ x, data = d)  n_d <- data.frame(x = c(11, 12, 13))  predictions <- predict(model, newdata = n_d) print(predictions) 

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.

R
set.seed(123) d_logit <- data.frame(   age = rnorm(100, mean = 30, sd = 5),   bought = sample(0:1, 100, replace = TRUE) ) # Fit a logistic regression model model_logit <- glm(bought ~ age, data = d_logit, family = binomial)  n_d_logit <- data.frame(age = c(28, 32, 35))  pre_logit <- predict(model_logit, newdata = n_d_logit, type = "response") print(pre_logit) 

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.

R
install.packages("rpart") library(rpart)  data_iris <- iris # Fit a decision tree model model_tree <- rpart(Species ~ Sepal.Length + Sepal.Width, data = data_iris)  new_data_tree <- data.frame(Sepal.Length = c(5.1, 6.3, 7.2), Sepal.Width = c(3.5, 3.3, 3.1))  predictions_tree <- predict(model_tree, newdata = new_data_tree, type = "class") print(predictions_tree) 

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:

  • R Programming Language – Introduction
  • Data visualization with R and ggplot2
  • Random Forest Approach for Classification in R Programming
  • dplyr Package in R Programming

Next Article
Predict() function in R

A

algorhythm
Improve
Article Tags :
  • Geeks Premier League
  • R Machine Learning
  • AI-ML-DS
  • Geeks Premier League 2023

Similar Reads

    parse() Function in R
    The parse() function in R programming language is used to return the parsed but unevaluated expression of a given expression in an expression, a “list” of calls. Also, this function converts an R object of the character class to an R object of the expression class. Syntax: parse(file = "",  n = NULL
    2 min read
    map() Function in R
    In R Programming Language the Map function is a very useful function used for element-wise operations across vectors or lists. This article will help show how to use it with multiple code examples. Map Function in RThe Map function in R belongs to the family of apply functions, designed to make oper
    3 min read
    as.numeric() Function in R
    The as.numeric() function in R is a crucial tool for data manipulation, allowing users to convert data into numeric form, which is essential for performing mathematical operations and statistical analysis. Overview of the as.numeric() FunctionThe as. numeric() function is part of R's base package an
    3 min read
    which() Function in R
    which() function in R Programming Language is used to return the position of the specified values in the logical vector. Syntax: which(x, arr.ind, useNames) Parameters: This function accepts some parameters which are illustrated below: X: This is the specified input logical vectorArr.ind: This param
    3 min read
    Slice() Function In R
    The slice() function in R is a very useful function to manipulate and subset data frames. it allows you to pick individual rows or a range of rows from a dataset with simple syntax This function is part of the dplyr package, which is essential for data manipulation.Syntaxslice(.data, ..., n = NULL,
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences