使用caret中的lift绘制两个不同算法的ROC曲线

我有如下两个模型:

library(mlbench)data(Sonar)library(caret)set.seed(998)my_data <- SonarfitControl <-  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 <- SonarfitControl <-  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 = 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    y1    xgbTree 0.9000756 0.952 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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注