require(tidyverse)
Loading required package: tidyverse
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages -----------------------------------
filter(): dplyr, stats
lag():    dplyr, stats
roc <- iris %>% 
  gather(Metric, Value, -Species) %>% 
  mutate(Positive = Species == "virginica") %>% 
  group_by(Metric, Value) %>% 
  summarise(Positive = sum(Positive), 
            Negative = n() - sum(Positive)) %>% 
  arrange(-Value) %>% 
  mutate(TPR = cumsum(Positive) / sum(Positive), 
         FPR = cumsum(Negative) / sum(Negative))
roc %>% 
  group_by(Metric) %>% 
  summarise(AUC = sum(diff(FPR) + na.omit(lead(TPR) + TPR)) / 2)
roc %>% 
  ggplot(aes( x= FPR, y = TPR, color = Metric)) +
  geom_line() +
  geom_abline(lty = 2) +
  xlab("False positive rate (1-specificity)") + 
  ylab("True positive rate (sensitivity)") +
  ggtitle("ROC at predicting Virginica iris species")

LS0tCnRpdGxlOiAiUk9DIEN1cnZlcyB3aXRoIGRwbHlyIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgpgYGB7cn0KcmVxdWlyZSh0aWR5dmVyc2UpCmBgYApgYGB7cn0Kcm9jIDwtIGlyaXMgJT4lIAogIGdhdGhlcihNZXRyaWMsIFZhbHVlLCAtU3BlY2llcykgJT4lIAogIG11dGF0ZShQb3NpdGl2ZSA9IFNwZWNpZXMgPT0gInZpcmdpbmljYSIpICU+JSAKICBncm91cF9ieShNZXRyaWMsIFZhbHVlKSAlPiUgCiAgc3VtbWFyaXNlKFBvc2l0aXZlID0gc3VtKFBvc2l0aXZlKSwgCiAgICAgICAgICAgIE5lZ2F0aXZlID0gbigpIC0gc3VtKFBvc2l0aXZlKSkgJT4lIAogIGFycmFuZ2UoLVZhbHVlKSAlPiUgCiAgbXV0YXRlKFRQUiA9IGN1bXN1bShQb3NpdGl2ZSkgLyBzdW0oUG9zaXRpdmUpLCAKICAgICAg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