使用 pROC R 绘制 ROC 曲线

Plot ROC curve with pROC R

我使用 randomForest 构建了一个文本分类器,因此为了对其进行评估,我尝试使用 pROC pâckage 创建 ROC 曲线。

这里是代码:

ndsi.forest <- randomForest(tf.idf[train.index, ], as.factor(train$Note.Reco[train.index]), ntree = 100)

#predict with test data
ndsi.pred <-predict(ndsi.forest, newdata = tf.idf[test.index, ], response  = 'class')
pred <- data.frame(ndsi.pred)
result <- data.frame(id = Data_clean$id[test.index], sentiment = pred[ , ])

##"ROC curve"
multiclass.roc(result$sentiment, test$Note.Reco)

我想知道这是否是一种创建情节的方法?使用 pROC 包的 ROC 图?

我尝试使用此代码:

roc(test$Note.Reco, result$sentiment, levels = c(1,2,3,4,5,6,7,8,9,10))

但是我得到这个错误:

Error in roc.default(test$Note.Reco, result$sentiment, levels = c(1, 2,  : 
  'levels' argument must have length 2

提前致谢

据我了解,你有一个多类响应变量(对应于 10 个不同的组)。

ROC - 曲线是为两组分类定义的,因此 multiclass 所做的是计算“一组与其余组的分类”。 multiclass.roc 函数不允许您表示曲线,但了解它的作用,您可以:

1) 考虑拥有多少组的 roc 曲线。即,分类的 ROC - 曲线:

  • 第 1 组与非第 1 组
  • 第 2 组与非第 2 组
  • 。 . .
  • 第 10 组与非第 10 组

您可以使用 roc 函数来做到这一点。您唯一需要做的就是重新定义响应向量,其中属于第 i 组的个人为 1,其余个人为 0。用不同的名称保存每个 roc 对象。

2) 要表示所有曲线,只需对每条曲线使用 plot 函数,将 plot(..., add=T) 添加到除第一条以外的所有曲线。