简体   繁体   English

使用 ROCR 在一个独特的 plot 中循环到 plot 多条 ROC 曲线

[英]Loop to plot multiple ROC curves in one unique plot using ROCR

I am using ROCR package to generate ROC curves.我正在使用 ROCR package 生成 ROC 曲线。 I already have a loop to generate multiple ROC plots from multiple files.我已经有一个循环来从多个文件生成多个 ROC 图。 I have 30 files.我有 30 个文件。 But I would like to combine all 30 ROC curves in one plot (different colors if possible).但我想将所有 30 条 ROC 曲线组合在一个 plot 中(如果可能,使用不同的 colors)。 I know that there are few posts about it, but I would like to use my own loop and modify it.我知道关于它的帖子很少,但我想使用我自己的循环并对其进行修改。 Any suggestions?有什么建议么?

My script:我的脚本:

library(ROCR)
labels <- read.table(file="c:/data1/input")
files <- list.files(path="c:/data2/", pattern="*.txt")
for(i in files){
  predictions <- read.table(paste("c:/data2/",i,sep=""))
  pred <- prediction(predictions, labels)
  perf <- performance(pred,"tpr","fpr")
  pdf(paste0("c:/data2/", i,"ROC.pdf"))
  plot(perf)
  dev.off()
  }

When you call plot() on a prediction object, it generates a new plot.当您在预测 object 上调用plot()时,它会生成一个新的 plot。 One way is to use lines() and to call the values directly.一种方法是使用lines()并直接调用这些值。 First I generate 30 models, since you did not provide your data:首先,我生成 30 个模型,因为您没有提供数据:

library(gbm)
library(mlbench)
library(colorspace)
data(DNA)
Pal = qualitative_hcl(30)
dat = DNA[,-c(1:2)]
dat$Class = as.numeric(dat$Class == "ei")
idx = split(sample(nrow(dat)),1:nrow(dat) %% 30)

mdl = lapply(1:30,function(i){
  mod = gbm(Class~.,data=dat[idx[[i]],])
  predictions = predict(mod,dat[-idx[[i]],],n.trees=10,type="response")
  labels = dat[-idx[[i]],"Class"]
  list(labels=labels,predictions=predictions)
})

names(mdl) = paste("model",1:30)

Now we start a empty plot, iterate through the 30 models, essentially the part you need is lines(...):现在我们开始一个空的plot,遍历30个模型,本质上你需要的部分是lines(...):

plot(NULL,xlim=c(0,1),ylim=c(0,1),
xlab="False positive rate",ylab="True positive rate")

for(i in 1:30){
  
  predictions = mdl[[i]]$predictions
  labels = mdl[[i]]$labels
  pred = prediction(predictions, labels)
  perf <- performance(pred,"tpr","fpr")
  
  lines(perf@x.values[[1]],perf@y.values[[1]],col=Pal[i])
  
  }

legend("bottomright",fill=Pal,names(mdl),ncol=5,cex=0.7)

在此处输入图像描述

For your example trying something like this:对于您的示例,尝试这样的事情:

files <- list.files(path="c:/data2/", pattern="*.txt")
mdl = lapply(files,read.table)
names(mdl) = gubs(".txt","",files)

plot(NULL,xlim=c(0,1),ylim=c(0,1),
xlab="False positive rate",ylab="True positive rate")

for(i in 1:30){
  
  pred = prediction(mdl[[i]], labels)
  perf <- performance(pred,"tpr","fpr")
  lines(perf@x.values[[1]],perf@y.values[[1]],col=Pal[i])
  
  }

legend("bottomright",fill=Pal,names(mdl),ncol=5,cex=0.7)

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

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