Plot The ROC Curve In R: Definition and Tutorial

Today, we will learn how to plot the ROC curve in the R programming language. And it is widely used in the machine learning. But first, let’s find out what the ROC curve means. You can read it below.

What is the roc curve in R?

The ROC plot, also referred to as the ROC AUC curve, is a measurement of classification error. It assesses the effectiveness and effects of machine learning algorithms for categorization.


To the number of values the model correctly distinguished and categorized based on the labels may be examined and inferred using the ROC AUC curve.


The greater the AUC score, the better the forecast of the expected values was. The ROC curve shows the link between a model’s True Positive Rate and False Positive Rate. We will use the ROC curve’s ideas in the section that follows.

How to plot the roc curve in R?

Here, we can assess machine learning models using ROC charts. Therefore, let’s attempt to apply the ROC curve idea to the Logistic Regression model.

  • First, we will load the pROC and randomForest package. Then we create a dataset in the environment.
  • Secondly, we will sample the dataset into training and test data values.
  • Next, To evaluate the model’s performance, we created error metrics such as precision, recall, precision, F1 score, ROC plot, etc.
  • Then, we use R’s glm() function to apply logistic regression to our data collection. The model is then evaluated using the predict() function against the test data, and the error metric is computed.
  • Finally, using the roc() function and the ‘pROC’ library’s plot() function, we get the model’s roc AUC score.

Now, you can look at the code example below

# Install packages
install("pROC")
install("randomForest")

# Load packages
library(pROC)
library(randomForest)

# Make my results match yours
set.seed(600)

nSamples <- 300

# Generate 300 values from a normal distribution with mean 190, sd 32
wt <- sort(rnorm(n = nSamples, mean = 190, sd = 32))

ob <- ifelse(test = (runif(n = nSamples) < (rank(wt) / nSamples)),
    no = 0, yes = 1
)

# Change the color of the ROC line
roc(ob,
    plot = TRUE, glmFit$fitted.values,
    ylab = "True Percentage", xlab = "False Percentage",
    col = "#377eb8", lwd = 5,
    percent = TRUE, legacy.axes = TRUE
)

# Determine the area
roc(ob,
    plot = TRUE, glmFit$fitted.values,
    ylab = "True Postive Percentage", xlab = "False Positive Percentage",
    col = "#377eb8", print.auc = TRUE, lwd = 5,
    percent = TRUE, legacy.axes = TRUE
)

# The partial region
roc(ob,
    plot = TRUE, glmFit$fitted.values,
    ylab = "True Postive Percentage", xlab = "False Positive Percentage",
    col = "#377eb8", print.auc.x = 45, lwd = 5, print.auc = TRUE,
    legacy.axes = TRUE, percent = TRUE,
    auc.polygon.col = "#377eb822",
    auc.polygon = TRUE, partial.auc = c(100, 90)
)

Output

Another, you want to plot layer logistic regression and random forest ROC graphs… Check out the code below

#######################################
# Fit the data
#######################################

rocModel <- randomForest(factor(ob) ~ wt)

roc(ob, plot = TRUE, 
    rocModel$votes[, 1],
    percent = TRUE,, legacy.axes = TRUE, 
    ylab = "True Percentage", xlab = "False Percentage", 
    col = "#4daf4a", print.auc = TRUE, lwd = 5)

#######################################
# Plot random forest ROC graphs and layer logistic regression graphs
#######################################

roc(ob, percent = TRUE, glmFit$fitted.values,
    xlab = "False Percentage", col = "#377eb8",
    plot = TRUE, legacy.axes = TRUE,
    lwd = 4, print.auc = TRUE,
    ylab = "True Percentage")

plot.roc(ob, rocModel$votes[, 1],
         col = "#4daf4a", lwd = 4,  add = TRUE, print.auc = TRUE,
         percent = TRUE, print.auc.y = 40)

legend(legend = c("The Logisitic Regression", "The Random Forest"), "bottomright",
       col = c("#377eb8", "#4daf4a"), lwd = 5)

par(pty = "m")

Output

NOTE: you can click this link here if you want all the source code.

Summary

This article demonstrated how to plot the ROC curve in R. Thank you for reading this post, and please comment if you have any questions.

Have a great day!

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *