【问题标题】:R get AUC and plot multiple ROC curves together at the same timeR获取AUC并同时绘制多条ROC曲线
【发布时间】:2016-09-11 16:19:21
【问题描述】:

我尝试了 2 种方法来绘制 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 值吗?

【问题讨论】:

    标签: r roc auc


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 那是因为我未能将 roc.curve 更改为 roc。我编辑了我的答案,它现在应该可以工作了。
    • 太棒了!非常感谢!
    猜你喜欢
    • 2019-02-27
    • 2019-04-11
    • 2017-06-09
    • 2014-04-20
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多