r - 使用插入符号中的提升从两种不同的算法绘制 ROC 曲线

标签 r machine-learning ggplot2 r-caret

我有如下两个模型:

library(mlbench)
data(Sonar)

library(caret)
set.seed(998)

my_data <- Sonar

fitControl <-
  trainControl(
    method = "boot632",
    number = 10,
    classProbs = T,
    savePredictions = "final",
    summaryFunction = twoClassSummary
  )


modelxgb <- train(
  Class ~ .,
  data = my_data,
  method = "xgbTree",
  trControl = fitControl,
  metric = "ROC"
)

library(mlbench)
data(Sonar)

library(caret)
set.seed(998)

my_data <- Sonar

fitControl <-
  trainControl(
    method = "boot632",
    number = 10,
    classProbs = T,
    savePredictions = "final",
    summaryFunction = twoClassSummary
  )


modelsvm <- train(
  Class ~ .,
  data = my_data,
  method = "svmLinear2",
  trControl = fitControl,
  metric = "ROC"
)

我想在一个 ggplot 上绘制两个模型的 ROC 曲线。

我正在执行以下操作来生成曲线的点:

for_lift_xgb = data.frame(Class = modelxgb$pred$obs,  xgbTree = modelxgb$pred$R)

for_lift_svm = data.frame(Class = modelsvm$pred$obs,  svmLinear2 = modelsvm$pred$R)

lift_obj_xgb = lift(Class ~ xgbTree, data = for_lift_xgb, class = "R")
lift_obj_svm = lift(Class ~ svmLinear2, data = for_lift_svm, class = "R")

将这两条曲线绘制在一个图上并用不同颜色绘制的最简单方法是什么。我还想在图中注释各个 AUC 值。

最佳答案

构建模型后,您可以将预测组合到一个数据框中:

for_lift = data.frame(Class = modelxgb$pred$obs,
                      xgbTree = modelxgb$pred$R,
                      svmLinear2 = modelsvm$pred$R)

使用它通过以下方式构建电梯对象:

lift = lift(Class ~ xgbTree + svmLinear2, data = for_lift, class = "R")

并用ggplot作图:

library(ggplot)

ggplot(lift$data)+
  geom_line(aes(1-Sp , Sn, color = liftModelVar))+
  scale_color_discrete(guide = guide_legend(title = "method"))

enter image description here

您可以通过这种方式组合和比较多个模型。

要将 auc 添加到绘图中,您可以创建一个包含模型名称、相应的 auc 和绘图坐标的数据框:

auc_ano <- data.frame(model = c("xgbTree","svmLinear2"),
                      auc = c(pROC::roc(response = for_lift$Class,
                                        predictor = for_lift$xgbTree,
                                        levels=c("M", "R"))$auc,
                              pROC::roc(response = for_lift$Class,
                                        predictor = for_lift$svmLinear2,
                                        levels=c("M", "R"))$auc),
                      y = c(0.95, 0.9))
auc_ano
#output
       model       auc    y
1    xgbTree 0.9000756 0.95
2 svmLinear2 0.5041086 0.90

并将其传递给 geom_text:

ggplot(lift$data)+
  geom_line(aes(1-Sp , Sn, color = liftModelVar))+
  scale_color_discrete(guide = guide_legend(title = "method"))+
  geom_text(data = auc_ano, aes(label = round(auc, 4), color = model, y = y), x = 0.1)

enter image description here

关于r - 使用插入符号中的提升从两种不同的算法绘制 ROC 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48647285/

相关文章:

r - 使用 native 管道将预测与变异结合使用

debugging - 列出在 R 中设置了调试标志的函数

R split apply 与 dplyr 结合使用 - 如何保持切片产生的 NA

r - 无监督字符串聚类

machine-learning - 什么是交叉熵?

python - Keras 比 TensorFlow 慢得不合理

r - 使用 ggplot 和 gganiminate 制作动画条形图

r - 使用 R 为 Leaflet 中的图层控制框添加标题

r - 为生存图指定自定义时间点

r - 带工具提示的 Ggplotly 在使用 geom_rect() 时出现问题