【问题标题】:ROC curve for the testing set using Caret package使用 Caret 包的测试集的 ROC 曲线
【发布时间】: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 &lt;- train([...], data=GermanCredit, [...])
  • 函数predict 输出一个factor 类的对象,所以没有evalm 所期望的。也就是说,我没有在包中找到解决方案,但我没有仔细查看。您可以找到更多资源herehere。第二个资源可能并不花哨,但可以完成工作。
  • @Calimo 感谢您指出这一点。这是一个错字 - 我修正了它。
  • train 函数中,应该使用训练数据而不是您使用的测试数据。
  • @DJJ 感谢分享这两个资源。它们有助于更深入地理解该过程,并可用于使用 pROC 包重现结果。

标签: r r-caret roc


【解决方案1】:

您可以使用以下代码

library(MLeval)
pred <- predict(mod_fit, newdata=testing, type="prob")
test1 <- evalm(data.frame(pred, testing$Class))

【讨论】:

  • 这行得通。谢谢你的帮助。过去 4 天一直在努力解决这个问题!
  • 如何将“Group1”的名称更改为其他名称?
猜你喜欢
  • 2018-04-03
  • 2015-08-29
  • 2012-07-13
  • 2016-05-26
  • 2013-04-30
  • 2016-07-03
  • 2019-01-25
  • 2013-01-26
  • 2018-05-08
相关资源
最近更新 更多