一个plot ROCR中的多个ROC曲线。

时间:2023-02-09 17:45:59

Is it possible to plot the roc curve for diffrent classifiers in the same plot using the ROCR package? I've tried:

是否可以用ROCR包在同一块区域绘制不同分类器的roc曲线?我试过了:

>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'

Thank you!

谢谢你!

3 个解决方案

#1


31  

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. But you can use the generic plot function as you did with an additional add = TRUE argument. For example this is partly from the example page of ?plot.performance:

您的lines方法的问题是,在ROCR包中定义的类性能对象没有通用的S4行函数。但是你可以使用一般的plot函数,就像你用一个额外的add = TRUE参数那样。例如,这部分是来自于一个例子页面的?

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. like this:

顺便说一下,如果你真的想用直线来画连续的ROC曲线,你必须这样做:

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

#2


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. In this case, pass col as a list:

响应@adibender,并添加一条注释:该示例不包括如何使用第二种方法为每个单独的曲线设置不同的颜色。在这种情况下,将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))

#3


1  

R has functions for draw several plots in one window. 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具有在一个窗口中绘制多个图的函数。如果包不支持一个窗口中的多个小块,那么您可以用其他方法来解决问题:用这个脚本编写几个ROCs文章的例子:ROC曲线与ROCR绘图的例子。

#1


31  

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. But you can use the generic plot function as you did with an additional add = TRUE argument. For example this is partly from the example page of ?plot.performance:

您的lines方法的问题是,在ROCR包中定义的类性能对象没有通用的S4行函数。但是你可以使用一般的plot函数,就像你用一个额外的add = TRUE参数那样。例如,这部分是来自于一个例子页面的?

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. like this:

顺便说一下,如果你真的想用直线来画连续的ROC曲线,你必须这样做:

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

#2


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. In this case, pass col as a list:

响应@adibender,并添加一条注释:该示例不包括如何使用第二种方法为每个单独的曲线设置不同的颜色。在这种情况下,将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))

#3


1  

R has functions for draw several plots in one window. 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具有在一个窗口中绘制多个图的函数。如果包不支持一个窗口中的多个小块,那么您可以用其他方法来解决问题:用这个脚本编写几个ROCs文章的例子:ROC曲线与ROCR绘图的例子。