O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To generate ROC charts, follow these steps:

  1. Load the ROCR package:
> library(ROCR) 
  1. Read the data file and take a look:
> dat <- read.csv("roc-example-1.csv") 
> head(dat) 
 
       prob class 
1 0.9917340     1 
2 0.9768288     1 
3 0.9763148     1 
4 0.9601505     1 
5 0.9351574     1 
6 0.9335989     1 
  1. Create the prediction object:
> pred <- prediction(dat$prob, dat$class) 
  1. Create the performance object:
> perf <- performance(pred, "tpr", "fpr") 
  1. Plot the chart:
> plot(perf) 
> lines( par()$usr[1:2], par()$usr[3:4] ) 

The following output is obtained:

  1. Find the cutoff values for various true positive rates. Extract the relevant data from the perf object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required