【发布时间】:2020-10-30 19:28:01
【问题描述】:
我正在尝试从测试集上的插入符号获取最佳模型的 ROC 曲线。我遇到了MLeval 包,它似乎很方便(输出非常全面,使用几行代码提供所有需要的指标和图表)。一个很好的例子在这里:https://stackoverflow.com/a/59134729/12875646
我正在尝试下面的代码,并能够获得训练集所需的指标/图表,但在尝试处理测试集时不断出错。
library(caret)
library(MLeval)
data(GermanCredit)
Train <- createDataPartition(GermanCredit$Class, p=0.6, list=FALSE)
training <- GermanCredit[ Train, ]
testing <- GermanCredit[ -Train, ]
ctrl <- trainControl(method = "repeatedcv", number = 10, classProbs = TRUE, savePredictions = TRUE)
mod_fit <- train(Class ~ Age + ForeignWorker + Property.RealEstate + Housing.Own +
CreditHistory.Critical, data=training, method="glm", family="binomial",
trControl = ctrl, tuneLength = 5, metric = "ROC")
pred <- predict(mod_fit, newdata=testing)
confusionMatrix(data=pred, testing$Class)
test = evalm(mod_fit) # this gives the ROC curve for test set
test1 <- evalm(pred) # I am trying this to calculate the ROC curve for the test set (I understand this should be the final curve to report), but I keep getting this error:
evalm(pred) 中的错误:请提供数据框或 Caret 火车对象。
在软件包网站上,第一个参数可以是包含概率和观察数据的数据框。你知道如何使用插入符号准备这个数据框吗? https://www.rdocumentation.org/packages/MLeval/versions/0.1/topics/evalm
谢谢
更新:
这应该是正确的脚本,除了在一个图表上显示多个 ROC 之外,它运行良好:
library(caret)
library(MLeval)
data(GermanCredit)
Train <- createDataPartition(GermanCredit$Class, p=0.6, list=FALSE)
training <- GermanCredit[ Train, ]
testing <- GermanCredit[ -Train, ]
ctrl <- trainControl(method = "repeatedcv", number = 10, classProbs = TRUE, savePredictions = TRUE)
mod_fit <- train(Class ~ Age + ForeignWorker + Property.RealEstate + Housing.Own +
CreditHistory.Critical, data=training, method="glm", family="binomial",
trControl = ctrl, tuneLength = 5, metric = "ROC")
#pred <- predict(mod_fit, newdata=testing, type="prob")
confusionMatrix(data=pred, testing$Class)
test = evalm(mod_fit) # this gives the ROC curve for test set
m1 = data.frame(pred, testing$Class)
test1 <- evalm(m1)
#Train and eval a second model:
mod_fit2 <- train(Class ~ Age + ForeignWorker + Property.RealEstate + Housing.Own,
data=training, method="glm", family="binomial",
trControl = ctrl, tuneLength = 5, metric = "ROC")
pred2 <- predict(mod_fit2, newdata=testing, type="prob")
m2 = data.frame(pred2, testing$Class)
test2 <- evalm(m2)
# Display ROCs for both models in one graph:
compare <- evalm(list(m1, m1), gnames=c('logistic1','logistic2'))
我从这个来源得到了代码的最后一步:https://www.r-bloggers.com/how-to-easily-make-a-roc-curve-in-r/
但是它只显示一条 ROC 曲线(如果我想显示插入符号序列输出,效果很好)
【问题讨论】:
-
首先,您是否意识到您是在完整数据集(通过交叉验证)上训练的,而不仅仅是在测试集上?
mod_fit <- train([...], data=GermanCredit, [...]) -
@Calimo 感谢您指出这一点。这是一个错字 - 我修正了它。
-
在
train函数中,应该使用训练数据而不是您使用的测试数据。 -
@DJJ 感谢分享这两个资源。它们有助于更深入地理解该过程,并可用于使用 pROC 包重现结果。