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

1

8

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?

Lewallen answered 15/5, 2016 at 22:32 Comment(0)
Z
13

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)
Zamudio answered 16/5, 2016 at 6:49 Comment(2)
That's because I failed to change roc.curve to roc. I edited my answer and it should work now.Zamudio
Awesome! Thank you very much!Lewallen

© 2022 - 2024 — McMap. All rights reserved.