MLtools Package in R to Calculate AUC-ROC

The mltools package contains helper functions that help majorly in exploratory data analysis. The main objective behind this function is that it provides highly optimized functions for speed and memory. In the below code implementation, we can observe how easy it is to calculate auc() using the Metrics Package in R.

Installing and Loading the Libraries

Let’s first install the package using the install.packages() function and then we will load the same using the library() function in R.

R




install.packages("mltools")
library(mltools)


Initializing the Vectors

Now let’s create two imaginary vectors with the actual target values and the predicted probabilities for the respective classes.

R




# Actual Target variable
actual <- c(0, 0, 1, 1, 1, 0, 0)
 
# Predicted probabilities for
# the respective classes
predicted <- c(.1, .3, .4, .9,
               0.76, 0.55, 0.2)


Calculating AUC-ROC

Syntax:

auc_roc(actual, predicted, returnDT)

where,

  • actual – The ground truth binary numeric vector containing 1 for the positive class and 0 for the negative class.
  • predicted – A vector containing probabilities predicted by the model of each example being 1.
  • returnDT – Returns a data.table object with False Positive Rate and True Positive Rate for plotting the ROC curve

R




auc_roc(predicted, actual)


Output:

0.916666666666667

R




auc_roc(predicted, actual, returnDT=TRUE)


Output:

TPR and FPR for the actual and predicted values



How to Calculate AUC (Area Under Curve) in R?

In this article, we will discuss how to calculate the AUC (Area under Curve) of the ROC (Receiver Operating Characteristic) curve in the R Programming Language.

Similar Reads

What is the AUC-ROC curve?

The ROC (Receiver Operating Characteristic) curve helps us to visualize the true positive rate or true negative rate of a prediction based on some model. This helps us to assess how well a regression model has fitted the data. The AUC (Area under Curve) of this ROC curve helps us to determine the specificity and sensitivity of the model. The closer the AUC value is to the 1, the better the given model fits the data....

pROC Package in r to Calculate AUC-ROC

To create the ROC (Receiver Operating Characteristic) curve object in the R Language, we use the roc() function of the pROC package library. The pROC is an R Language package to display and analyze ROC curves. The roc() function takes the actual and predicted value as an argument and returns a ROC curve object as a result. Then, to find the AUC (Area under Curve) of that curve, we use the auc() function. The auc() function takes the roc object as an argument and returns the area under the curve of that roc curve....

Metrics Package in R to Calculate AUC-ROC

...

MLtools Package in R to Calculate AUC-ROC

...