gpt4 book ai didi

使用 Caret 包的测试集的 ROC 曲线

转载 作者:行者123 更新时间:2023-12-04 12:37:40 25 4
gpt4 key购买 nike

我正在尝试从测试集上的插入符号获取最佳模型的 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:

Error in evalm(pred) : Data frame or Caret train object required please.

在包网站上,第一个参数可以是一个包含概率和观测数据的数据框。你知道如何使用插入符号准备这个数据框吗? 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 曲线(如果我想显示插入符序列输出,效果很好)

最佳答案

你可以使用下面的代码

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

enter image description here

如果你想把“Group1”的名字改成类似GLM的名字,你可以使用下面的代码

test1 <- evalm(data.frame(pred, testing$Class, Group = "GLM"))

enter image description here

关于使用 Caret 包的测试集的 ROC 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62827779/

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