簡體   English   中英

使用 ROCR 在一個獨特的 plot 中循環到 plot 多條 ROC 曲線

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

我正在使用 ROCR package 生成 ROC 曲線。 我已經有一個循環來從多個文件生成多個 ROC 圖。 我有 30 個文件。 但我想將所有 30 條 ROC 曲線組合在一個 plot 中(如果可能,使用不同的 colors)。 我知道關於它的帖子很少,但我想使用我自己的循環並對其進行修改。 有什么建議么?

我的腳本:

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()
  }

當您在預測 object 上調用plot()時,它會生成一個新的 plot。 一種方法是使用lines()並直接調用這些值。 首先,我生成 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)

現在我們開始一個空的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)

在此處輸入圖像描述

對於您的示例,嘗試這樣的事情:

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