R获得AUC并同时绘制多条ROC曲线

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

我尝试了两种方法来绘制 ROC 曲线并获得每条 ROC 曲线的 AUC。

方法一 - 第一种方法很简单但我不知道如何将多条 ROC 曲线绘制在一起。 我只是使用 roc.curve(hacide.test$cls, pred_rose[,2]),输出将显示 ROC 曲线并给出 AUC。

方法二 我现在可以一起绘制多条 ROC 曲线,但不能同时获得 AUC。 这是我一起绘制多条 ROC 曲线的方式:

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)

这是我获得 AUC 的方式:

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

如您所见,当我使用第二种方法时,用于获取ROC曲线和AUC的performance()方法是不同的。这里pf1、pf2的输出没有AUC值。

方法 1 更简单,但是你知道如何使用方法 1 将 ROC 曲线绘制在一起并仍然保持每个 AUC 值吗?

您可以使用 pROC 包执行此操作。在对 plot:

的调用中使用 print.auc 参数
library(pROC)
roc_rose <- plot(roc(hacide.test$cls, pred_rose[,2]), print.auc = TRUE, col = "blue")

对于第二条 ROC 曲线,您需要更改 AUC 的 y 位置并使用 add 在同一图上绘制两条曲线:

roc_rose <- plot(roc(hacide.test$cls, pred_both[,2]), print.auc = TRUE, 
                 col = "green", print.auc.y = .4, add = TRUE)