Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R get AUC and plot multiple ROC curves together at the same time

Tags:

r

roc

auc

I have tried 2 methods to plot ROC curve and get AUC for each ROC curve.

Method 1 - The first method is simple but I don't know how to plot multiple ROC curves together. I am simply using roc.curve(hacide.test$cls, pred_rose[,2]), the output will show the ROC curve and gives me the AUC.

Method 2 I can plot multiple ROC curves together now, but cannot get AUC at the same time. This is the way I plot multiple ROC curves together:

library(ROCR)
pd1 <- prediction(pred_rose[,2], hacide.test$cls)
pf1 <- performance(pd1, "tpr","fpr")

pd2 <- prediction(pred_both[,2], hacide.test$cls)
pf2 <- performance(pd2, "tpr","fpr")

plot(pf1, colorize = TRUE)
plot(pf2, add = TRUE, colorize = TRUE)

This is the way I get AUC:

pf <- performance(pd3, "auc")
pf     # y.values is the AUC

As you can see, when I am using this second method, the performance() method used for getting ROC curve and AUC is different. The output of pf1, pf2 here has no AUC values.

Method 1 is simpler, but do you know how can I use method 1 to plot ROC curves together and still keep each AUC values?

like image 297
Cherry Wu Avatar asked May 15 '16 22:05

Cherry Wu


People also ask

How to plot multiple ROC curves in ROC?

You can use the add = TRUE argument the plot function to plot multiple ROC curves. library (pROC) a=rbinom (100, 1, 0.25) b=runif (100) c=rnorm (100) fit1=glm (a~b+c, family='binomial') fit2=glm (a~c, family='binomial') Predict on the same data you trained the model with (or hold some out to test on if you want)

How to get the AUC of a ROC curve?

You can get the AUC of the ROC curve by roc1$auc, and can add it either using the text () function in base R plotting, or perhaps just toss it in the legend. I don't know how to quantify confidence intervals...or if that is even a thing you can do with ROC curves. Someone else will have to fill in the details on that one. Sorry.

How to use plot_ROC_curve two times without specifying Ax parameter?

So if we use plot_roc_curve two times without the specifying ax parameter it will plot two graphs. So here we store the first gragh in the figure variable and access its axis and provide to the next plot_roc_curve function, so that the plot appear of the axes of the first graph only.

What is the use of plot_ROC_curve function?

Basically plot_roc_curve function plot the roc_curve for the classifier. So if we use plot_roc_curve two times without the specifying ax parameter it will plot two graphs.


1 Answers

You can do this with the pROC package. Use the print.auc argument in the call to plot:

library(pROC)
roc_rose <- plot(roc(hacide.test$cls, pred_rose[,2]), print.auc = TRUE, col = "blue")

And for the second ROC curve you need to change the y position of the AUC and use add the plot the two curves on the same plot:

roc_rose <- plot(roc(hacide.test$cls, pred_both[,2]), print.auc = TRUE, 
                 col = "green", print.auc.y = .4, add = TRUE)
like image 157
Calimo Avatar answered Sep 23 '22 02:09

Calimo