Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute AUC with ROCR package

I have fitted a SVM model and created the ROC curve with ROCR package. How can I compute the Area Under the Curve (AUC)?

set.seed(1)
tune.out=tune(svm ,Negative~.-Positive, data=trainSparse, kernel ="radial",ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4) ))
summary(tune.out)
best=tune.out$best.model

##prediction on the test set
ypred = predict(best,testSparse, type = "class")
table(testSparse$Negative,ypred)

###Roc curve
yhat.opt = predict(best,testSparse,decision.values = TRUE)
fitted.opt = attributes(yhat.opt)$decision.values
rocplot(fitted.opt,testSparse ["Negative"], main = "Test Data")## 
like image 631
mac gionny Avatar asked Jan 07 '17 16:01

mac gionny


People also ask

How are AUC values calculated manually?

You can divide the space into 2 parts: a triangle and a trapezium. The triangle will have area TPR*FRP/2 , the trapezium (1-FPR)*(1+TPR)/2 = 1/2 - FPR/2 + TPR/2 - TPR*FPR/2 . The total area is 1/2 - FPR/2 + TPR/2 . This is how you can get it, having just 2 points.

What package is AUC R?

AUC: Area Under the ROC Curve This function is a wrapper for functions from the ROCR package.

How do you get AUC from ROC curve in R?

The roc() function takes the actual and predicted value as an argument and returns a ROC curve object as 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.


1 Answers

Start with the prediction Method from the ROCR Package.

pred_ROCR <- prediction(df$probabilities, df$target)

to get the ROC in a plot:

roc_ROCR <- performance(pred_ROCR, measure = "tpr", x.measure = "fpr")
plot(roc_ROCR, main = "ROC curve", colorize = T)
abline(a = 0, b = 1)

and get the AUC Value:

  auc_ROCR <- performance(pred_ROCR, measure = "auc")
  auc_ROCR <- [email protected][[1]]
like image 63
Dan Avatar answered Oct 02 '22 19:10

Dan