0% found this document useful (0 votes)
17 views2 pages

ROC Curve and AUC in R Tutorial

The document discusses generating a ROC curve and calculating AUC to evaluate a naive Bayes classifier. It provides code in R to load training and test data, build the naive Bayes model, make predictions on the test data, plot the ROC curve and compute the AUC for the naive Bayes classifier built on bank customer data.

Uploaded by

Low Jia Hui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

ROC Curve and AUC in R Tutorial

The document discusses generating a ROC curve and calculating AUC to evaluate a naive Bayes classifier. It provides code in R to load training and test data, build the naive Bayes model, make predictions on the test data, plot the ROC curve and compute the AUC for the naive Bayes classifier built on bank customer data.

Uploaded by

Low Jia Hui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 8

DSA1101
Introduction to Data Science
October 26, 2018

Exercise 1. Receiver Operating Characteristic (ROC) Curve in R


In last week’s lecture, we introduced the ROC curve which is a common
tool to evaluate classifiers in terms of trade-off between True Positive Rate
(TPR) and False Positive Rate (FPR) when the classification threshold varies.
We have also studied area under the curve (AUC), which is calculated by
measuring the area under the ROC curve. Higher AUC scores mean the
classifier performs better. In this week’s tutorial, we will illustrate how AUC
can be computed with the R package ‘ROCR’.
Consider the dataset ‘[Link]’ we discussed in the lectures. For
this exercise, we will predict the binary outcome subscribed using the naı̈ve
Bayes classifier, and generate the ROC curve of the naı̈ve Bayes classifier built
on a training set of 2,000 instances and tested on a testing set of 100 instances.
All the datasets for this exercise are posted under the folder ‘Tutorial 8’ in
IVLE.

(a) Load the training and testing bank sample datasets.

1 # training set
2 banktrain <- read . table ( " bank - sample . csv " , header = TRUE , sep = " ," )
3 # drop a few columns
4 drops <- c ( " balance " , " day " , " campaign " , " pdays " , " previous " , " month "
)
5 banktrain <- banktrain [ , ! ( names ( banktrain ) % in % drops ) ]
6 # testing set
7 banktest <- read . table ( " bank - sample - test . csv " , header = TRUE , sep = " ," )
8 banktest <- banktest [ , ! ( names ( banktest ) % in % drops ) ]

1
(b) Build the naı̈ve Bayes classifier based on the training dataset, and per-
form prediction for the test dataset.

1 library ( e1071 )
2
3 # build the naive Bayes classifier
4 nb _ model <- naiveBayes ( subscribed ~ . ,
5 data = banktrain )
6 # perform on the testing set
7 nb _ prediction <- predict ( nb _ model ,
8 # remove column " subscribed "
9 banktest [ , - ncol ( banktest ) ] ,
10 type = ’ raw ’)

(c) Plot the ROC curve for the naı̈ve Bayes classifier.

1 library ( ROCR )
2
3 score <- nb _ prediction [ , c ( " yes " ) ]
4
5 actual _ class <- banktest $ subscribed == ’ yes ’
6 pred <- prediction ( score , actual _ class )
7
8 perf <- performance ( pred , " tpr " , " fpr " )
9 plot ( perf , lwd =2 , xlab = " False Positive Rate ( FPR ) " ,
10 ylab = " True Positive Rate ( TPR ) " )
11 abline ( a =0 , b =1 , col = " gray50 " , lty =3)

(d) Compute AUC for the naı̈ve Bayes classifier.

1 auc <- performance ( pred , " auc " )


2 auc <- unlist ( slot ( auc , " y . values " ) )
3 auc

Common questions

Powered by AI

When selecting the test and training datasets, considerations should include ensuring that the datasets are representative of the whole population and are sufficiently large to capture the variability of the data. The training set should be large enough to allow the model to learn adequately, while the test set must be independent and used solely for evaluation purposes. Stratified sampling might be necessary to maintain class distribution similarity across both datasets to prevent biased performance metrics.

To plot a ROC curve in R, the following steps are used: first, predictions from the classifier are obtained; then, actual class labels need to be defined. Using the 'ROCR' package, create a 'prediction' object with the predicted scores and actual class labels. Next, use the 'performance' function to calculate TPR and FPR, and finally plot these values using the 'plot' function. An additional line (abline) is drawn for reference with 'a=0, b=1'.

Interpreting ROC curves and AUC values can be challenging due to several factors: the ROC curve's shape can vary significantly with different threshold levels; the AUC value, while useful, may not capture nuances such as class imbalance or the cost of false positives versus false negatives in specific contexts. ROC curves can also sometimes suggest a misleading sense of classifier quality if viewed in isolation without context of the underlying data distribution or model assumptions.

The AUC is a measure of how well a classifier can distinguish between classes. A higher AUC indicates better performance of the classifier, as it represents a higher chance that the classifier will correctly rank a randomly chosen positive instance higher than a randomly chosen negative one.

Overfitting in the modeling process could be identified if the model performs exceptionally well on the training dataset but poorly on the testing dataset, indicating it has learned the noise or non-generalizable patterns of the training data. This can also be observed if the AUC significantly drops when moving from training data to testing data evaluations. Regularization techniques or pruning could be used to handle overfitting, besides adjusting feature selection.

The Naïve Bayes classifier was built using the 'e1071' library in R. The classifier was trained on a dataset using the formula 'subscribed~.' and predicted on a test dataset. The ROC curve and AUC were then evaluated using the 'ROCR' package, with the ROC curve plotted to show the performance of the classifier through visualization of TPR vs. FPR.

Computing the AUC from an ROC curve involves using a prediction object that contains the true and predicted values. The 'performance' function in the 'ROCR' package in R computes the TPR and FPR at various threshold levels. The AUC is then calculated by integrating these values to find the area under the ROC curve, representing the probability that the classifier ranks a randomly chosen positive instance higher than a negative one. This is extracted using the 'slot' function and represents a single scalar value indicating classifier performance.

Dropping certain columns before building the Naïve Bayes classifier is crucial for several reasons: it prevents overfitting by reducing noise from irrelevant features, it simplifies the model, and it can improve computational efficiency. In the tutorial, columns that were deemed unnecessary for prediction such as 'balance', 'day', 'campaign', etc., were dropped to focus on the relevant predictors for the 'subscribed' outcome.

Performance of the Naïve Bayes classifier is tested by first building the classifier on a training dataset. The model's predictive performance is then evaluated on a separate testing set, where the predictions are made using the classifier's learned parameters. In this exercise, the predictions for the test set were generated using the 'predict' function and evaluated on their ability to distinguish between the 'yes' and 'no' classes.

The ROC curve is primarily used to evaluate classifiers by analyzing the trade-off between the True Positive Rate (TPR) and the False Positive Rate (FPR) as the classification threshold varies. This helps to understand the performance of the classifier at different thresholds.

You might also like