Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Created October 18, 2018 14:01
Show Gist options
  • Save gadenbuie/9f99d8d15c3715d9f5764024446430b6 to your computer and use it in GitHub Desktop.
Save gadenbuie/9f99d8d15c3715d9f5764024446430b6 to your computer and use it in GitHub Desktop.
Demonstrating the use of plotROC

A quick example of how to use the plotROC package.

install.packages("plotROC")
library(tidyverse)
library(plotROC)

Here we’ll make up some quick example data, where prediction is the predicted probability or score for the event and actual indicates whether or not the event happened.

actual <- rbinom(200, size = 1, prob = 0.5)
ex <- data.frame(
  actual = actual,
  prediction = 1/(1 + exp(-1 * rnorm(200, mean = actual * 2, sd = 1)))
)

A quick look at the made up data…

head(ex)
#>   actual prediction
#> 1      0  0.5850483
#> 2      1  0.7033973
#> 3      0  0.3031526
#> 4      0  0.4730225
#> 5      0  0.6154252
#> 6      0  0.5373342
ggplot(ex) +
  aes(prediction, actual) +
  geom_point()

Now we can use plotROC to create the ROC curve. One part of this documentation that I found confusing is that the aesthetic d is for the status (or actual value) and m is for the marker (or predicted value).

ggplot(ex) +
  aes(d = actual, m = prediction) +
  geom_roc()

You can modify the style of the plot using the ggplot2 theme() functions or by adding extra components, like the random-choice line with geom_abline().

ggplot(ex) +
  aes(d = actual, m = prediction) +
  geom_abline(slope = 1, intercept = 0, linetype = 2, color = "grey50") +
  geom_roc() +
  theme_classic()

Or you can use the style_roc() function from plotROC.

ggplot(ex) +
  aes(d = actual, m = prediction) +
  # geom_abline(slope = 1, intercept = 0, linetype = 2, color = "grey50") +
  geom_roc() +
  style_roc()

Finally, there’s also the Shiny app that you can run locally.

shiny_plotROC()

Created on 2018-10-18 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment