gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:58:19 27 4
gpt4 key购买 nike

我有如下两个模型:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com