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

Last Updated : 7 Aug, 2025

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.

Syntax:

roc_object <- roc( response, prediction )

Parameters:

  • response: determines the vector that contains the actual data.
  • prediction: determines the vector that contains the data predicted by our model.

Implementation of AUC

We are calculating the AUC from a ROC curve to evaluate the performance of a logistic regression model using the pROC package.

1. Installing and Loading Required Packages

We are installing and loading the pROC package to enable ROC curve creation and AUC calculation.

  • install.packages: Installs the required package from CRAN.
  • library: Loads the installed package into the current R session.
  • pROC: A package used for creating and analyzing ROC curves in R.
R
install.packages("pROC")
library(pROC)

2. Creating Training and Test Datasets

We are initializing a sample dataset to train the logistic regression model and evaluate it on test data.

  • data.frame: Used to create a tabular dataset from vectors.
  • x, y, z: Variables used as predictors and target for the model.
R
df_train <- data.frame(x = c(1, 2, 3, 4, 5),
                       y = c(1, 5, 8, 15, 26),
                       z = c(0, 1, 1, 0, 0))

df_test <- data.frame(x = c(6, 7, 8),
                      y = c(38, 45, 72),
                      z = c(0, 1, 0))

3. Training Model and Calculating AUC

We are training a logistic regression model, making predictions, generating a ROC curve and calculating the AUC value.

  • glm: Fits a generalized linear model, here used for logistic regression.
  • predict: Generates predicted values based on the model.
  • type: Specifies the prediction type as probabilities.
  • roc: Creates a ROC curve object from actual and predicted values.
  • auc: Calculates the Area Under Curve from the ROC object.
R
model <- glm(z ~ x + y, data = df_train)

prediction <- predict(model, df_test, type = "response")

roc_object <- roc(df_test$z, prediction)

cat("Area under the curve:", auc(roc_object))

Output:

Area under the curve: 0.5

Implementation of AUC-ROC using Metrics Package in R

We use the Metrics package in R to compute the AUC-ROC score easily. This package supports a wide range of metrics suitable for regression, classification and time-series models.

1. Installing and Loading Required Packages

We are installing and loading the Metrics package which contains the auc() function for calculating the Area Under the ROC Curve.

  • install.packages: Installs the specified package from CRAN.
  • library: Loads the installed package into the R environment.
R
install.packages("Metrics")
library(Metrics)

2. Initializing Actual and Predicted Vectors

We are creating numeric vectors for actual class labels and predicted probabilities of the positive class.

  • actual: Vector containing the true binary class labels (0 or 1).
  • predicted: Vector containing predicted probabilities for class 1.
R
actual <- c(0, 0, 1, 1, 1, 0, 0)
predicted <- c(0.1, 0.3, 0.4, 0.9, 0.76, 0.55, 0.2)

3. Calculating AUC-ROC Score

We are using the auc() function from the Metrics package to compute the AUC score based on the actual and predicted values.

  • auc: Calculates the area under the ROC curve using actual and predicted values.

Syntax:

auc(actual, predicted)

Parameters:

  • 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.
R
auc(actual, predicted)

Output:

0.916666666666667

Implementation of AUC-ROC using MLtools Package in R

We use the mltools package in R to calculate the AUC-ROC score and retrieve the ROC curve values. This package is highly optimized for memory and speed and is often used for exploratory data analysis tasks.

1. Installing and Loading Required Packages

We are installing and loading the mltools package, which includes the auc_roc() function to compute AUC and ROC-related statistics.

  • install.packages: Used to install external R packages from CRAN.
  • library: Loads the specified package into the active R session.
R
install.packages("mltools")
library(mltools)

2. Initializing Actual and Predicted Vectors

We are defining two vectors- one for actual binary target values and the other for predicted class probabilities.

  • actual: A numeric vector of true binary values representing class labels.
  • predicted: A numeric vector of predicted probabilities for the positive class.
R
actual <- c(0, 0, 1, 1, 1, 0, 0)
predicted <- c(0.1, 0.3, 0.4, 0.9, 0.76, 0.55, 0.2)

3. Calculating AUC-ROC Score and Retrieving ROC Curve

We are using auc_roc() from the mltools package to calculate the AUC score and optionally retrieve ROC curve data.

  • auc_roc: Computes the AUC score and optionally returns the ROC curve points.
  • returnDT: A parameter that returns the result as a data table when set to TRUE.

Syntax:

auc_roc(actual, predicted, returnDT)

Parameters:

  • 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:

table
Output

The output of auc_roc(predicted, actual) gives the AUC score, which measures the classifier's ability to distinguish between classes. The second output with returnDT = TRUE returns a data table containing ROC curve details like true positive rate (TPR), false positive rate (FPR) and thresholds.

Comment

Explore