首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >roc.default Predictor中的错误必须是数字或排序

roc.default Predictor中的错误必须是数字或排序
EN

Stack Overflow用户
提问于 2020-11-17 06:05:12
回答 1查看 858关注 0票数 0

我正在尝试获得我在测试数据集上获得的模型的ROC曲线。

然而,get a error:

代码语言:javascript
运行
复制
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曲线时,最终又遇到了另一个问题。

代码语言:javascript
运行
复制
# 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)

并得到错误:

代码语言:javascript
运行
复制
Setting levels: control = negative, case = positive
Error in roc.default(testing_data$tested, predict_rf) : 
  Predictor must be numeric or ordered.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-17 07:38:41

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

代码语言:javascript
运行
复制
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

第二列是正类的概率。

所以你可以这样使用它

代码语言:javascript
运行
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64866403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档