【问题标题】:R: Plot multiple different coloured ROC curves using ROCRR:使用 ROCR 绘制多条不同颜色的 ROC 曲线
【发布时间】:2017-06-09 12:57:50
【问题描述】:

以下代码取自@adibender 对“一个绘图 ROCR 中的多个 ROC 曲线”的回答。部分代码来自 ?plot.performance。

library(ROCR)
data(ROCR.simple)
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)

我想用 r 包 ROCR 在一个图中说明几条 ROC 曲线,就像上面的代码一样。但是,我希望 ROC 曲线具有不同的颜色。如何将不同的颜色应用于不同的曲线?提前致谢。

【问题讨论】:

    标签: r plot machine-learning data-mining roc


    【解决方案1】:

    你可以使用 add=TRUE 的 plot

    pred <- prediction( predicted1, response )
    pred2 <- prediction(predicted2, response)
    perf <- performance( pred, "tpr", "fpr" )
    perf2 <- performance(pred2, "tpr", "fpr")
    plot( perf, colorize = FALSE,  type="l",col="blue")
    abline(a=0,b=1)
    plot(perf2,colorize = FALSE, add = TRUE,  type="l", lty=3,col="black")
    

    【讨论】:

      【解决方案2】:

      试试这个(你可以用ROCR来做):

      library(ROCR)
      data(ROCR.simple)
      preds <- cbind(p1 = ROCR.simple$predictions, 
                     p2 = abs(ROCR.simple$predictions + 
                                rnorm(length(ROCR.simple$predictions), 0, 0.1)))
      n <- 2 # you have n models
      colors <- c('red', 'blue') # 2 colors
      for (i in 1:n) {
         plot(performance(prediction(preds[,i],ROCR.simple$labels),"tpr","fpr"), 
                                                 add=(i!=1),col=colors[i],lwd=2)
      }
      

      【讨论】:

        【解决方案3】:

        ROCR 对象的 plot 函数似乎不提供此选项。因此,您必须将对象拆开并手动完成。

        例如,使用ggplot2

        library(ggplot2)
        df <- data.frame(Curve=as.factor(rep(c(1,2), each=length(perf.mat@x.values[[1]]))), 
                         FalsePositive=c(perf.mat@x.values[[1]],perf.mat@x.values[[2]]),
                         TruePositive=c(perf.mat@y.values[[1]],perf.mat@y.values[[2]]))
        plt <- ggplot(df, aes(x=FalsePositive, y=TruePositive, color=Curve)) + geom_line()
        print(plt)
        

        【讨论】:

          猜你喜欢
          • 2019-03-16
          • 2019-02-27
          • 2012-12-14
          • 1970-01-01
          • 2012-07-13
          • 2016-09-11
          • 1970-01-01
          • 2019-09-16
          • 2015-04-11
          相关资源
          最近更新 更多