简体   繁体   English

一张图中的多条 ROC 曲线 ROCR

[英]Multiple ROC curves in one plot ROCR

Is it possible to plot the roc curve for diffrent classifiers in the same plot using the ROCR package?是否可以使用 ROCR 包在同一图中绘制不同分类器的 roc 曲线? I've tried:我试过了:

>plot(perf.neuralNet, colorize=TRUE)
>lines(perf.randomForest)

But I get:但我得到:

Error en as.double(y) : cannot coerce type 'S4' to vector of type 'double'错误 en as.double(y):无法将类型“S4”强制转换为“double”类型的向量

Thank you!谢谢!

The problem with your lines -approach is that there is no generic S4 lines function for an object of class performance defined in the ROCR package.您的lines -approach 的问题在于,对于ROCR包中定义的类performance对象,没有通用的S4线函数。 But you can use the generic plot function as you did with an additional add = TRUE argument.但是您可以像使用附加add = TRUE参数一样使用通用绘图函数。 For example this is partly from the example page of ?plot.performance :例如,这部分来自?plot.performance的示例页面:

library(ROCR)
data(ROCR.simple)
pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels )
pred2 <- prediction(abs(ROCR.simple$predictions + 
                        rnorm(length(ROCR.simple$predictions), 0, 0.1)), 
        ROCR.simple$labels)
perf <- performance( pred, "tpr", "fpr" )
perf2 <- performance(pred2, "tpr", "fpr")
plot( perf, colorize = TRUE)
plot(perf2, add = TRUE, colorize = TRUE)

OR, you can store all your predictions in a matrix and do all the subsequent steps in one:或者,您可以将所有预测存储在一个矩阵中,然后将所有后续步骤合二为一:

preds <- cbind(p1 = ROCR.simple$predictions, 
                p2 = abs(ROCR.simple$predictions + 
                rnorm(length(ROCR.simple$predictions), 0, 0.1)))

pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels, 
                nrow = length(ROCR.simple$labels), ncol = 2) )

perf.mat <- performance(pred.mat, "tpr", "fpr")
plot(perf.mat, colorize = TRUE)

Btw, if you for some reason really wanted to use lines to plot consecutive ROC curves you would have to do sth.顺便说一句,如果您出于某种原因真的想使用lines来绘制连续的 ROC 曲线,您将不得不这样做。 like this:像这样:

plot(perf) 
lines(perf2@x.values[[1]], perf2@y.values[[1]], col = 2)

Echoing @adibender, and adding a comment: the example doesn't cover how to set separate colors for each individual curve using the second (plot all at once) approach.回应@adibender,并添加评论:该示例不包括如何使用第二种(一次全部绘制)方法为每条曲线设置单独的颜色。 In this case, pass col as a list:在这种情况下,将 col 作为列表传递:

library(ROCR)
data(ROCR.hiv)
x   <- prediction(ROCR.hiv$hiv.nn$predictions, ROCR.hiv$hiv.nn$labels)
ROC <- performance(x, "tpr", "fpr")
plot(ROC, col = as.list(1:10))

R has functions for draw several plots in one window. R 具有在一个窗口中绘制多个图的功能。 And if package doesn't support several plots in one window,you can solve problem with standard instruments of R. Other way: Example of several ROCs Article with this script: An example of ROC curves plotting with ROCR如果包不支持在一个窗口中绘制多个图,您可以使用 R 的标准仪器解决问题。 其他方式:几个 ROC 的示例带有此脚本的文章: 使用 ROCR 绘制 ROC 曲线的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM