使用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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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