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