簡體   English   中英

並行比較多個 AUC (R)

[英]Comparing multiple AUCs in parallel (R)

我在 r 中使用 pROC 包來計算和比較多個測試的 AUC,以查看哪個測試具有區分患者和對照的最佳能力。 但是,我有大量的測試,並且基本上想要對每個測試的 AUC 與每個其他測試進行一系列成對比較,然后對多重比較進行校正。 這是我使用我的代碼得到的(下面是模擬和可復制數據集的示例):

#load pROC
library(pROC)

#generate df with random numbers
set.seed(123)
df <- data.frame(disease_status = rbinom(n=100, size=1, prob=0.20),
                 test1 = rnorm(100, mean=15, sd=4),
                 test2 = rnorm(100, mean=30, sd=2),
                 test3 = rnorm(100, mean=50, sd=3))

#create roc object for test1, test2, test3
roc.out_test1<-roc(df$disease_status, df$test1, plot=TRUE, smooth = FALSE)
roc.out_test2<-roc(df$disease_status, df$test2, plot=TRUE, smooth = FALSE)
roc.out_test3<-roc(df$disease_status, df$test3, plot=TRUE, smooth = FALSE)

#compare the AUC of test1 and test 2
roc.test(roc.out_test1, roc.out_test2, reuse.auc=TRUE, method="delong", na.rm=TRUE)

#DeLong's test for two correlated ROC curves
#data:  roc.out_test1 and roc.out_test2
#Z = 0.60071, p-value = 0.548
#alternative hypothesis: true difference in AUC is not equal to 0
#sample estimates:
#AUC of roc1 AUC of roc2 
#0.5840108   0.5216802 

#create a function to do above for all comparisons
vec_ROCs1 <- c("roc.out_test1,", "roc.out_test2,", "roc.out_test3,")
vec_ROCs2 <- c("roc.out_test1", "roc.out_test2", "roc.out_test3")
ROCs2_specifications  <- paste0(vec_ROCs2, ",", "reuse.auc=TRUE")
test <- unlist(lapply(ROCs2_specifications, function(x) paste0(vec_ROCs1, x)))
test2 <- lapply(test, function(x) roc.test(x))

#Error in roc.test.default(x) : 
#  argument "predictor1" is missing, with no default 

請讓我知道您對如何解決此問題的想法和建議!

謝謝你。

以下應該有效,請檢查。 我沒有寫所有的細節,但如果你不理解代碼,你可以問我其他問題。

#load pROC
library(pROC)
#> Type 'citation("pROC")' for a citation.
#> 
#> Attaching package: 'pROC'
#> The following objects are masked from 'package:stats':
#> 
#>     cov, smooth, var

#generate df with random numbers
set.seed(123)
df <- data.frame(disease_status = rbinom(n=100, size=1, prob=0.20),
                 test1 = rnorm(100, mean=15, sd=4),
                 test2 = rnorm(100, mean=30, sd=2),
                 test3 = rnorm(100, mean=50, sd=3))

#create roc object for test1, test2, test3
roc.out_test1<-roc(df$disease_status, df$test1, plot=TRUE, smooth = FALSE)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases

roc.out_test2<-roc(df$disease_status, df$test2, plot=TRUE, smooth = FALSE)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases

roc.out_test3<-roc(df$disease_status, df$test3, plot=TRUE, smooth = FALSE)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases

# compare the AUC of test1 and test 2
roc.test(roc.out_test1, roc.out_test2, reuse.auc = TRUE, method = "delong", na.rm = TRUE)
#> 
#>  DeLong's test for two correlated ROC curves
#> 
#> data:  roc.out_test1 and roc.out_test2
#> Z = 0.60071, p-value = 0.548
#> alternative hypothesis: true difference in AUC is not equal to 0
#> sample estimates:
#> AUC of roc1 AUC of roc2 
#>   0.5840108   0.5216802

現在我們生成三個測試的所有可能組合的列表,並使用您設置的相同參數運行roc.test函數。

all_tests <- combn(
  list(
    "test1" = roc.out_test1,
    "test2" = roc.out_test2,
    "test3" = roc.out_test3
  ),
  FUN = function(x, ...) roc.test(x[[1]], x[[2]]),
  m = 2,
  simplify = FALSE, 
  reuse.auc = TRUE, 
  method = "delong", 
  na.rm = TRUE
)

輸出是一個choose(3, 2) = 3元素的列表(即一次取2 個n 個元素的組合數),列表中的每個元素都是一個測試。 例如,這與您之前的測試相同:

all_tests[[1]]
#> 
#>  DeLong's test for two correlated ROC curves
#> 
#> data:  x[[1]] and x[[2]]
#> Z = 0.60071, p-value = 0.548
#> alternative hypothesis: true difference in AUC is not equal to 0
#> sample estimates:
#> AUC of roc1 AUC of roc2 
#>   0.5840108   0.5216802

這里唯一的問題是很難識別在比較中使用了哪些測試,因此我們還可以添加一個名稱列表:

tests_names <- combn(
  list("test1", "test2", "test3"), 
  m = 2, 
  FUN = paste, 
  simplify = TRUE, 
  collapse = "_"
)
all_tests <- setNames(all_tests, tests_names)

這是結果。

names(all_tests)
#> [1] "test1_test2" "test1_test3" "test2_test3"

對象的名稱標記了比較中使用的測試。

all_tests$test1_test2
#> 
#>  DeLong's test for two correlated ROC curves
#> 
#> data:  x[[1]] and x[[2]]
#> Z = 0.60071, p-value = 0.548
#> alternative hypothesis: true difference in AUC is not equal to 0
#> sample estimates:
#> AUC of roc1 AUC of roc2 
#>   0.5840108   0.5216802

reprex 包(v0.3.0) 於 2020 年 3 月 14 日創建

roc.test() 函數需要一個 roc 對象作為輸入。 列表test只是所有參數的字符串,函數不知道如何處理。 該列表還包括測試與自身的比較,即“roc.out_test1,roc.out_test1,reuse.auc=TRUE”我假設您實際上不需要這樣做,並且只有 3 個比較需要 1v2、1v3 ,2v3。 purrr包提供了類似於lapply map函數, map2允許您同時迭代 2 個列表。 您需要創建 2 個實際 roc 對象的列表並遍歷這些列表。

#load pROC
library(pROC)
library(dplyr)
library(purrr) #For map2 function

#generate df with random numbers
set.seed(123)
df <- data.frame(disease_status = rbinom(n=100, size=1, prob=0.20),
                 test1 = rnorm(100, mean=15, sd=4),
                 test2 = rnorm(100, mean=30, sd=2),
                 test3 = rnorm(100, mean=50, sd=3))

#create roc object for test1, test2, test3
roc.out_test1<-roc(df$disease_status, df$test1, plot=TRUE, smooth = FALSE)
roc.out_test2<-roc(df$disease_status, df$test2, plot=TRUE, smooth = FALSE)
roc.out_test3<-roc(df$disease_status, df$test3, plot=TRUE, smooth = FALSE)

#compare the AUC of test1 and test 2
roc.test(roc.out_test1, roc.out_test2, reuse.auc=TRUE, method="delong", na.rm=TRUE)

roc_new <- function(test1,  test2){
  roc.test(test1, test2, reuse.auc=TRUE, method="delong", na.rm=TRUE)
}

#List of all tests
all_tests <- list(roc.out_test1,
                  roc.out_test2,
                  roc.out_test3) 

#Create unique combos of tests
unique_combos <- expand.grid(1:3, 1:3) %>% 
  filter(Var1 < Var2) %>% #exludes duplicate comparisons, 
                      #each col provides the index for the 2 lists to iterate over
  mutate(names = paste(Var1, " V ",  Var2)) #Create col to name final output list


#Create 2 lists to iterate over
#Create list 1
(test1 <- all_tests[as.numeric(unique_combos$Var1)])
#Create list 2
(test2 <- all_tests[as.numeric(unique_combos$Var2)])

#Iterate over both lists
output <- map2(test1, test2, roc_new)
names(output) <- unique_combos$names

暫無
暫無

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

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