0

I want to be able to do lots of AUCs at once from the pROC package. Here is a simple dataframe with two predictors and a binary outcome and my attempt to use sapply() along with auc() and roc() from the pROC library. What am I doing wrong?

library(pROC)
df <- data.frame(z = rnorm(100,0,1), x=rnorm(100,0,1), y = as.factor(sample(0:1, 100, replace=TRUE)))

#One AUC at a time
auc(roc(df$y, df$x))
auc(roc(df$y, df$z))

#Trying to get multiple
predictors <- c("z","x")
results <- lapply(df, function(x){auc(roc(y, predictors))})

This solution works using a for loop, is that the most elegant method or can sapply/lapply be used instead? Calculating multiple ROC curves in R using a for loop and pROC package. What variable to use in the predictor field?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
CineyEveryday
  • 127
  • 1
  • 8

1 Answers1

1

You can use lapply in the following way -

predictors <- c("z","x")
results <- lapply(predictors, function(x) auc(roc(df$y, df[[x]])))
results

#[[1]]
#Area under the curve: 0.6214

#[[2]]
#Area under the curve: 0.6238

sapply would return a numeric vector.

sapply(predictors, function(x) auc(roc(df$y, df[[x]])))

#        z         x 
#0.6213942 0.6237981 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213