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

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:

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

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.

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

**Syntax:

**auc(actual, predicted)

**Parameters:

auc(actual, predicted)

`

**Output:

0.916666666666667

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

**Syntax:

**auc_roc(actual, predicted, returnDT)

**Parameters:

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.