如何绘制不同插入符号训练模型的AUC ROC?
这是一个雷普雷克斯如何绘制不同插入符号训练模型的AUC ROC?,r,roc,R,Roc,这是一个雷普雷克斯 library(caret) library(dplyr) set.seed(88, sample.kind = "Rounding") mtcars <- mtcars %>% mutate(am = as.factor(am)) test_index <- createDataPartition(mtcars$am, times = 1, p= 0.2, list = F) train_cars <- mtcars[-test_inde
library(caret)
library(dplyr)
set.seed(88, sample.kind = "Rounding")
mtcars <- mtcars %>%
mutate(am = as.factor(am))
test_index <- createDataPartition(mtcars$am, times = 1, p= 0.2, list = F)
train_cars <- mtcars[-test_index,]
test_cars <- mtcars[test_index,]
set.seed(88, sample.kind = "Rounding")
cars_nb <- train(am ~ mpg + cyl,
data = train_cars, method = "nb",
trControl = trainControl(method = "cv", number = 10, savePredictions = "final"))
cars_glm <- train(am ~ mpg + cyl,
data = train_cars, method = "glm",
trControl = trainControl(method = "cv", number = 10, savePredictions = "final"))
库(插入符号)
图书馆(dplyr)
种子(88,sample.kind=“四舍五入”)
mtcars%
突变(am=作为因子(am))
test_index我假设您希望在测试集上显示ROC曲线,这与注释()中使用训练数据的问题不同
首先要做的是以概率的形式提取测试数据的预测(newdata=test\u cars
):
要使其更具可读性,可以添加图例:
legend("bottomright", col=c("green", "blue"), legend=c("NB", "GLM"), lty=1)
与AUC:
legend_nb <- sprintf("NB (AUC: %.2f)", auc(roc_nb))
legend_glm <- sprintf("GLM (AUC: %.2f)", auc(roc_glm))
legend("bottomright",
col=c("green", "blue"), lty=1,
legend=c(legend_nb, legend_glm))
legend\u nb谢谢你的链接!答案中我唯一不明白的是,为什么参数设置设置为2?mtry:在每次拆分时随机抽样作为候选变量的变量数()这不是只适用于随机森林模型吗?我不知道,但输出说了什么?非常感谢您的有见地的回答@Calimo,我将使用您的方法,而不是evalm函数要求输入插入符号训练结果的MLeval。有没有办法将AUC值添加到曲线中?非常感谢!我将使用你的plot.roc函数!如果这个答案解决了你的问题,请记住接受它。
library(pROC)
roc_nb <- roc(test_cars$am, predictions_nb)
roc_glm <- roc(test_cars$am, predictions_glm)
plot(roc_nb, col="green")
lines(roc_glm, col="blue")
legend("bottomright", col=c("green", "blue"), legend=c("NB", "GLM"), lty=1)
legend_nb <- sprintf("NB (AUC: %.2f)", auc(roc_nb))
legend_glm <- sprintf("GLM (AUC: %.2f)", auc(roc_glm))
legend("bottomright",
col=c("green", "blue"), lty=1,
legend=c(legend_nb, legend_glm))