并行比较多个 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 package (v0.3.0)

于 2020 年 3 月 14 日创建

roc.test() 函数需要一个 roc 对象作为输入。列表 test 只是所有参数的字符串,函数不知道如何处理。该列表还包括测试与自身的比较,即 "roc.out_test1,roc.out_test1,reuse.auc=TRUE" 我假设您实际上不需要这样做,并且只有 3 个比较需要 1v2、1v3、2v3。 purrr 包提供了 map 类似于 lapply 的功能, 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