Multiple ROC curves in one graph

Dear R Studio Community,

I am trying to plot 2 ROC curves in one graph to nicely compare them.

I used the "cutpointr" package and I don't know how to merge the 2 results.

 y <- data[ ,  c( " pnf " ,  " lac " )]
 
 roc1 <- cutpointr( data = y , x= lac , class = pnf , 
                 method = maximize_metric, metric = sum_sens_spec, na.rm = FALSE)

 summary(roc1)
 plot(roc1)



 y <- data[ ,  c( " aed " ,  " lac " )]
 
 roc2 <- cutpointr( data = y , x= lac , class = aed , 
                 method = maximize_metric, metric = sum_sens_spec, na.rm = FALSE)

 summary(roc2)
 plot(roc2)

I read some previous post suggestiong to use pROC package too, and so I did

pred1 <- ifelse ( data$aed == TRUE, 1, 0)
 
 r.lac <- roc(pred1, data$lac)

but I kept stopping with an ERROR : "Error: Only strings can be converted to symbols".

Can you give me any suggestion on how to move forward?

Thank you!

We need a reproducible example (reprex) and especially some sample data.

A handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

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

# based on example from roc documentation
# data(aSAH)
# roc(aSAH$outcome, aSAH$s100b,
#     levels=c("Good", "Poor"))
# roc(aSAH$outcome, aSAH$s100b,
#     levels=c("Good", "Poor")) %>% plot()

# make a data set  
data(aSAH)
my_data <-aSAH %>% mutate(pred1 = s100b, 
                          # add a second prediction
                          pred2 = pmax(0, pred1 + runif(length(pred1), 0, .2)))

extract_roc_df <- function(roc_obj) {
  # extract sens and spec from rob_object as a tibble
  as_tibble(roc_obj[c("sensitivities", "specificities")])
}

# get roc curves for each predictor
map2(my_data[, c("pred1", "pred2")],
     list(my_data$outcome),
     ~roc(.y, .x, levels=c("Good", "Poor"))) %>%
  # extract sens and spec as data frames and bind rows
  map_dfr(extract_roc_df, .id = "pred") %>%
  # plot
  ggplot() + 
  geom_abline(slope = 1, intercept = 1, color = "darkgrey") +
  aes(specificities, sensitivities, color = pred) + 
  geom_line() + 
  scale_x_reverse()
#> Setting direction: controls < cases
#> Setting direction: controls < cases

Created on 2021-08-12 by the reprex package (v1.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.