Search code examples
rmachine-learningpredictrocauc

Calculating ROC and AUC in loop


I created several models for a binary classification problem and I'd like to calculate and plot the AUC and the ROC for each model (ideally in one plot). How could I do this?

This is the code I currently use:

library(pROC)
model <- load_model_hdf5('M1.h5')
pred <- model %>% predict(test[,1:10]) 
roc_object <- roc(test[,11], pred) # create ROC curve
auc(roc_object) # calculate area under curve

test[,1:10] is the test data and test[,11] is the ground truth.


Solution

  • Using sample data of mtcars, I have created an example involving several ROC in one single plot.

    After your first call to plot(), the subsequent calls using lines() will append to the existing plot instead of creating a new plot.

    data(mtcars)
    
    roc1 <- roc(mtcars[, "am"], mtcars[, "mpg"])
    roc2 <- roc(mtcars[, "am"], mtcars[, "disp"])
    roc3 <- roc(mtcars[, "am"], mtcars[, "hp"])
    
    plot(1 - roc1[["specificities"]], roc1[["sensitivities"]], type = "l", col = "green")
    lines(1 - roc2[["specificities"]], roc2[["sensitivities"]], col = "blue")
    lines(1 - roc3[["specificities"]], roc3[["sensitivities"]], col = "red")