我正在尝试获得我在测试数据集上获得的模型的ROC曲线。
然而,get a error:
Setting levels: control = negative, case = positive
Error in roc.default(testing_data$tested, predict_rf) :
Predictor must be numeric or ordered.
我遵循了下面的答案,但没有成功。
SVM in R: "Predictor must be numeric or ordered."
Failure plotting ROC curve using pROC
几个月前,我在这个链接上的其他人的帖子中写出了一个类似的例子:
然而,我以'stupidWolf‘为例,出于可重复性的考虑,我在这里发布了它,因为我之前对他的答案有问题。然而,当我试图获得ROC曲线时,最终又遇到了另一个问题。
# choose a sample
idx = sample(nrow(iris),100)
data = iris
data$Petal.Length[sample(nrow(data),10)] = NA
data$tested = factor(ifelse(data$Species=="versicolor","positive","negative"))
data = data[,-5]
training_data = data[idx,]
testing_data= data[-idx,]
# train data
rf <- caret::train(tested ~., data = training_data,
method = "rf",
trControl = ctrlInside,
metric = "ROC",
na.action = na.exclude)
# test the model on test data
colnames(evalResult.rf)[max.col(evalResult.rf)]
testing_data = testing_data[complete.cases(testing_data),]
evalResult.rf <- predict(rf, testing_data, type = "prob")
predict_rf <- factor(colnames(evalResult.rf)[max.col(evalResult.rf)])
cm_rf_forest <- confusionMatrix(predict_rf, testing_data$tested, "positive")
# get the roc
library(pROC)
rfROCt <- pROC::roc(testing_data$tested, predict_rf)
并得到错误:
Setting levels: control = negative, case = positive
Error in roc.default(testing_data$tested, predict_rf) :
Predictor must be numeric or ordered.
发布于 2020-11-17 07:38:41
第二个参数应该是预测的概率,所以如果你看这个例子:
evalResult.rf <- predict(rf, testing_data, type = "prob")
head(evalResult.rf)
negative positive
2 0.968 0.032
8 1.000 0.000
9 0.996 0.004
13 0.990 0.010
第二列是正类的概率。
所以你可以这样使用它
pROC::roc(testing_data$tested,evalResult.rf[,2])
Setting levels: control = negative, case = positive
Setting direction: controls < cases
Call:
roc.default(response = testing_data$tested, predictor = evalResult.rf[, 2])
Data: evalResult.rf[, 2] in 24 controls (testing_data$tested negative) < 22 cases (testing_data$tested positive).
Area under the curve: 0.9924
https://stackoverflow.com/questions/64866403
复制相似问题