【发布时间】:2021-02-28 03:31:03
【问题描述】:
我正在尝试获取我在测试数据集上获得的模型的ROC曲线。
但得到一个错误:
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.
【问题讨论】:
-
这能回答你的问题吗? ROC function error "Predictor must be numeric or ordered." 正如现在所说的那样,这个问题在我看来很像一个完全重复的问题。
标签: r r-caret roc proc-r-package