cmirian
cmirian

Reputation: 2253

How to relevel in ROC curve in R/plotROC?

I have just started learning curves. I am trying to produce a ROC plot but it seems that the curve is bending the wrong direction than usual - please see attached

Can you help reverse the curve so that it bends in the "usual" direction?

enter image description here

My data is

p <- structure(list(t = c(29354L, 7445L, 22309L, 29699L, 29711L, 14765L, 22257L, 
29715L, 29772L, 13320L, 20905L, 12950L, 3400L, 14800L,7400L, 21890L, 19400L, 14800L, 14700L, 22200L, 1688L, 4500L, 8438L, 13500L, 14800L, 
12580L, 12950L, 13320L, 11840L, 13320L, 14800L, 13690L, 11250L, 12210L, 13320L, 13320L, 14800L, 12580L,20720L, 11840L, 14800L, 7030L, 14800L, 
14800L, 8325L, 11100L,10730L, 13690L, 12210L, 14800L), a = c(0L, 1L, 1L, 0L, 0L, 
1L,0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,1L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L,0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L)), .Names = c("t","a"), class = "data.frame", row.names = c(NA, 50L))

And I used the following

library(plotROC)
basicplot <- ggplot(p, aes(d = p$a, m = p$t)) + geom_roc() + theme_bw()
basicplot + style_roc() 

Bonus questions

I used Youden's to determine the best cut-off, which was 13410. Any ideas how to draw specific attention to this point - eg by highlighting a red dot?

Upvotes: 4

Views: 644

Answers (2)

Anonymous coward
Anonymous coward

Reputation: 2091

You can do two things, swap your 1 and 0 designations, or use geom_roc(increasing = FALSE). See the PlotROC vignette. This is assuming your model is actually performing this way, and doesn't truly have low sensitivity and a high false positive rate and you're just inverting it.

basicplot <- ggplot(p, aes(d = a, m = t)) + 
    geom_roc(increasing = FALSE) + 
    theme_bw()

basicplot + style_roc()

Upvotes: 5

Brian D
Brian D

Reputation: 2728

I find the examples provided to be quite obscure, so here's a more generic example that makes more sense to me:

library(plotROC)

roc1 <- roc(mtcars$am, mtcars$mpg)
roc2 <- roc(mtcars$am, mtcars$wt)
roc.test(roc1, roc2)

ggplot(mtcars) +
  style_roc(guide = F) + 
  geom_abline(slope=1, intercept = 0, color="black") + 
  geom_roc(aes(d = am, m=mpg, color="MPG"), pointsize = 0, increasing=T, labels = F) + 
  geom_roc(aes(d = am, m=wt, color="WT"), pointsize = 0, increasing=F, labels = F) + 
  annotate(geom="text", x=.5, y=.3, hjust=0, label = paste0("MPG AUC: ", round(as.numeric(roc1$auc),3))) + 
  annotate(geom="text", x=.5, y=.2, hjust=0, label = paste0("WT AUC: ", round(as.numeric(roc2$auc),3))) + 
  labs(title = "ROC Curves") +
  scale_color_manual(name = NULL, values =c("grey60","grey30")) + 
  theme(aspect.ratio = 1)

roc curves ggplot

Upvotes: 0

Related Questions