【问题标题】:Error in roc.default Predictor must be numeric or orderedroc.default 预测器中的错误必须是数字或有序的
【发布时间】: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

几个月前,我在此链接上的其他人发布了一个类似的示例:

Error in table(data, reference, dnn = dnn, ...) : all arguments must have the same length when run confusionMatrix with caret, in R

但是,为了重现性,我以“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.

【问题讨论】:

标签: r r-caret roc proc-r-package


【解决方案1】:

第二个参数应该是预测的概率,所以如果你看这个例子:

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

【讨论】:

  • 我总是遇到不同解决结果的问题。它工作得很好。谢谢。
  • 是的,你帮了我很多。谢谢!!
猜你喜欢
  • 2019-09-09
  • 2015-02-12
  • 2021-07-06
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 2018-01-21
相关资源
最近更新 更多