为什么ggplot 中的geom_roc 与plot.roc 的ROC 差异如此之大?

发布于 2025-01-14 23:02:13 字数 1251 浏览 4 评论 0原文

我想我已经被派到这里接受培训了。

library(caret)
library(mlbench)
library(plotROC)
library(pROC)

data(Sonar)
ctrl <- trainControl(method="cv", 
                     summaryFunction=twoClassSummary, 
                     classProbs=T,
                     savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar, 
               method="rf", preProc=c("center", "scale"), 
               trControl=ctrl)
    
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2

我想绘制 ROC。

plot.roc(rfFit$pred$obs[selectedIndices],
         rfFit$pred$M[selectedIndices])

输入图片这里的描述

然而,当我尝试 ggplot2 方法时,它给了我完全不同的东西。

g <- ggplot(rfFit$pred[selectedIndices, ], aes(m=M, d=factor(obs, levels = c("R", "M")))) + 
  geom_roc(n.cuts=0) + 
  coord_equal() +
  style_roc()

g + annotate("text", x=0.75, y=0.25, label=paste("AUC =", round((calc_auc(g))$AUC, 4)))

输入图片这里的描述

我在这里做了一些非常错误的事情,但我不知道它是什么。 谢谢。

I supposed I have a training sent here.

library(caret)
library(mlbench)
library(plotROC)
library(pROC)

data(Sonar)
ctrl <- trainControl(method="cv", 
                     summaryFunction=twoClassSummary, 
                     classProbs=T,
                     savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar, 
               method="rf", preProc=c("center", "scale"), 
               trControl=ctrl)
    
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2

and I would like to plot the ROC.

plot.roc(rfFit$pred$obs[selectedIndices],
         rfFit$pred$M[selectedIndices])

enter image description here

however when I tried a ggplot2 approach it gives me something completely different.

g <- ggplot(rfFit$pred[selectedIndices, ], aes(m=M, d=factor(obs, levels = c("R", "M")))) + 
  geom_roc(n.cuts=0) + 
  coord_equal() +
  style_roc()

g + annotate("text", x=0.75, y=0.25, label=paste("AUC =", round((calc_auc(g))$AUC, 4)))

enter image description here

Im doing something really wrong here but I can't figure out what it is.
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-01-21 23:02:13

geom_roc 会忽略因子水平的顺序。请注意,无论您以哪种方式分配 levels = c('R', 'M'),您都会收到警告:

#> Warning message:
#> In verify_d(data$d) : D not labeled 0/1, assuming M = 0 and R = 1!

这意味着您将获得“反预测”的 ROC(即与模型实际做出的预测相反)。因此它是实际 ROC 的镜像。

您需要将预测显式转换为 1 和 0 的数字列:

g <- ggplot(rfFit$pred[selectedIndices, ], 
       aes(m=M, d= as.numeric(factor(obs, levels = c("R", "M"))) - 1)) + 
  geom_roc(n.cuts=0) + 
  coord_equal() +
  style_roc()

g + annotate("text", x=0.75, y=0.25, 
           label=paste("AUC =", round((calc_auc(g))$AUC, 4)))

在此处输入图像描述

The order of your factor levels is ignored by geom_roc. Notice that whichever way round your assign your levels = c('R', 'M'), you get the warning:

#> Warning message:
#> In verify_d(data$d) : D not labeled 0/1, assuming M = 0 and R = 1!

This means you are getting the ROC of an 'anti-prediction' (i.e. the opposite of the prediction your model actually makes). Hence it is a mirror image of the actual ROC.

You need to explicitly convert the predictions to a numeric column of 1s and 0s:

g <- ggplot(rfFit$pred[selectedIndices, ], 
       aes(m=M, d= as.numeric(factor(obs, levels = c("R", "M"))) - 1)) + 
  geom_roc(n.cuts=0) + 
  coord_equal() +
  style_roc()

g + annotate("text", x=0.75, y=0.25, 
           label=paste("AUC =", round((calc_auc(g))$AUC, 4)))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文