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
  • Python – Data visualization
  • Pandas
  • Seaborn
  • Matplotlib
  • Plotly
  • Altair
  • Bokeh
  • Pygal
  • Exploratory Data Analysis
  • Power BI
  • Tableau
  • Data Analysis with Python
  • Python Interview Questions
  • Machine Learning
  • Deep Learning
  • Natural Language Processing
  • Data Science
  • R Programming
Open In App
Next Article:
Plotting ROC curve in R Programming
Next article icon

Plotting ROC curve in R Programming

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss what is ROC curve, the Importance of ROC Curves in Model Evaluation, and how to plot the ROC curve in R Programming Language.

Introduction to ROC Curve in R

In binary classification problems, evaluating the performance of a model is crucial. One common method to assess a model's classification performance is through the Receiver Operating Characteristic (ROC) curve. The ROC curve in R is a graphical representation that explains the diagnostic ability of a binary classifier system as its discrimination threshold is varied. It plots the True Positive Rate (Sensitivity) against the False Positive Rate (1 - Specificity) at various threshold settings.

  • True Positive Rate (TPR): The proportion of actual positives correctly identified by the model (also known as sensitivity or recall).
  • False Positive Rate (FPR): The proportion of actual negatives incorrectly identified as positives by the model.
  • Area Under the Curve (AUC): The area under the ROC curve, quantifies the overall ability of the model to discriminate between positive and negative classes. A perfect model has an AUC of 1, while a random guess has an AUC of 0.5.

Importance of ROC Curves in Model Evaluation

The ROC curve in R helps in understanding how well the model performs across different thresholds. It provides a visual understanding of the trade-off between true positives and false positives. The ROC curve is particularly helpful when:

  • Imbalanced dataset where one class dominates over the other.
  • Compare the performance of multiple classification models.
  • Interested in how the classifier performs over a range of thresholds.

To plot an ROC curve in R, you will need some commonly used libraries, such as pROC and ROCR. Here's how to install them:

library(pROC) library(ROCR)

1: Plotting ROC Curve Using pROC

The pROC package makes it simple to compute and visualize ROC curves. Let's start with a basic example using a simulated dataset.

  • Generate or load data: Assume we have a binary outcome (0 or 1) and corresponding predicted probabilities from a logistic regression model.
  • Use roc() function to calculate the ROC curve.
  • Plot the ROC curve and compute the Area Under the Curve (AUC).
R
# Simulated data set.seed(123) actual <- sample(c(0, 1), 100, replace = TRUE)  # Actual binary outcomes predicted_probs <- runif(100)  # Predicted probabilities from the model  # Load pROC package library(pROC)  # Calculate ROC curve roc_curve <- roc(actual, predicted_probs)  # Plot ROC curve plot(roc_curve, col = "blue", main = "ROC Curve", print.auc = TRUE)  # Add additional information to the plot abline(a = 0, b = 1, lty = 2, col = "red")  # Diagonal line for random guess 

Output:

gh
Plotting ROC curve in R Programming

In this example, we used roc() to calculate the ROC curve and plot() to visualize it. The diagonal red line indicates a random classifier. The ROC curve is plotted, and the AUC value is displayed on the plot.

2: Plotting ROC Curve Using ROCR

The ROCR package offers flexibility in terms of plotting and evaluating the ROC curve with more customizable options.

  • Use the prediction() function to generate the ROC curve data.
  • Calculate performance metrics like TPR and FPR.
  • Plot the ROC curve.
R
# Load ROCR package library(ROCR)  # Create prediction object pred <- prediction(predicted_probs, actual)  # Calculate performance (True Positive Rate and False Positive Rate) perf <- performance(pred, "tpr", "fpr")  # Plot the ROC curve plot(perf, col = "darkgreen", lwd = 2, main = "ROC Curve with ROCR")  # Add a diagonal reference line abline(a = 0, b = 1, col = "red", lty = 2) 

Output:

gh
Plotting ROC curve in R Programming

This code plots the ROC curve using ROCR and adds a diagonal reference line representing random guessing.

  • High AUC value (close to 1): This indicates that the model has a high ability to distinguish between positive and negative classes.
  • Low AUC value (close to 0.5): This suggests the model performs no better than random guessing.
  • ROC curve closer to the top-left corner: A good-performing model that has high sensitivity and specificity.

Conclusion

The ROC curve in R is an essential tool for evaluating the performance of binary classification models. In R, packages like pROC and ROCR offer efficient ways to compute and visualize ROC curves. By understanding the trade-off between true positives and false positives, as well as calculating the Area Under the Curve (AUC), you can make informed decisions on the performance of your models.


Next Article
Plotting ROC curve in R Programming

T

therebootedcoder
Improve
Article Tags :
  • R Language
  • Data Visualization
  • R-plots
  • R-Charts
  • R-Graphs
  • R-Packages
  • R-Data Visualization

Similar Reads

    Graph Plotting in R Programming
    When it comes to interpreting the world and the enormous amount of data it is producing on a daily basis, Data Visualization becomes the most desirable way. Rather than screening huge Excel sheets, it is always better to visualize that data through charts and graphs, to gain meaningful insights.  R
    6 min read
    How to Code in R programming?
    R is a powerful programming language and environment for statistical computing and graphics. Whether you're a data scientist, statistician, researcher, or enthusiast, learning R programming opens up a world of possibilities for data analysis, visualization, and modeling. This comprehensive guide aim
    4 min read
    Curve Fitting in R
    In this article, we will discuss how to fit a curve to a dataframe in the R Programming language. Curve fitting is one of the basic functions of statistical analysis. It helps us in determining the trends and data and helps us in the prediction of unknown data based on a regression model/function.
    4 min read
    Plotting Graphs using Two Dimensional List in R Programming
    List is a type of an object in R programming. Lists can contain heterogeneous elements like strings, numeric, matrices, or even lists. A list is a generic vector containing other objects. Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nest
    2 min read
    R Programming Language - Introduction
    R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti
    4 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