为什么ggplot 中的geom_roc 与plot.roc 的ROC 差异如此之大?
我想我已经被派到这里接受培训了。
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])
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)))
Im doing something really wrong here but I can't figure out what it is.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
geom_roc
会忽略因子水平的顺序。请注意,无论您以哪种方式分配levels = c('R', 'M')
,您都会收到警告:这意味着您将获得“反预测”的 ROC(即与模型实际做出的预测相反)。因此它是实际 ROC 的镜像。
您需要将预测显式转换为 1 和 0 的数字列:
The order of your factor levels is ignored by
geom_roc
. Notice that whichever way round your assign yourlevels = c('R', 'M')
, you get the warning: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: