簡體   English   中英

使用 for 循環繪制多條 ROC 曲線

[英]Plot multiple ROC curves using a for loop

我需要在一個圖上繪制許多不同的 ROC 曲線。 為了避免手動創建每個 ROC 曲線,我創建了一個 for 循環來自動執行此過程。 但是,出於某種原因,代碼僅輸出一條曲線,用於名稱列表中的最后一個模型。 誰能幫我弄清楚為什么它不起作用? 請參閱下面的可重現示例:

library(pROC)
library(tidyverse)

dat_tst_2 <- data.frame(result = sample(letters[1:2], 100, replace = T))

preds_1 <- data.frame(x = runif(100),
                      y = runif(100))
preds_2 <- data.frame(x = runif(100),
                      y = runif(100))

names_preds <- c("preds_1", "preds_2")

output <- list()

for (j in 1:length(names_preds)) {
     for (i in names_preds) {
          roc_model <- roc(response = dat_tst_2$result, 
                           predictor = eval(as.name(i))[,2],
                           levels = c("a", "b"),
                           plot = F) 
          output[[j]] <- roc_model
     }   
}

ggroc(output)

首先確保使用 str(output) 輸出有多個項目。 然后嘗試將輸出中的每個項目傳遞給 ggroc:

 lappy( output, function (out) { png()
                  print (ggroc(out))
                   dev.off()               }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM