1

I found this function for ROC calculation

  getROC_AUC = function(probs, true_Y){
    probsSort = sort(probs, decreasing = TRUE, index.return = TRUE)
    val = unlist(probsSort$x)
    idx = unlist(probsSort$ix)  

    roc_y = true_Y[idx];
    stack_x = cumsum(roc_y == 0)/sum(roc_y == 0)
    stack_y = cumsum(roc_y == 1)/sum(roc_y == 1)    

    auc = sum((stack_x[2:length(roc_y)]-stack_x[1:length(roc_y)-1])*stack_y[2:length(roc_y)])
    return(list(stack_x=stack_x, stack_y=stack_y, auc=auc))
  }

I am trying to use this function to draw ROC curve for different models in the same plot.

I store the target variable and the probabilities for the different models in a data.frame

temp:

    target Model.L Model.R Model.N Model.Comb
  1        0   0.150   0.400   0.125      0.225
  2        0   0.195   0.610   0.141      0.315
  3        1   0.315   0.455   0.646      0.472
  4        0   0.100   0.195   0.053      0.116
  5        0   0.635   0.394   0.565      0.531
  6        0   0.172   0.394   0.149      0.238
  7        0   0.090   0.195   0.075      0.120
  8        1   0.188   0.222   0.282      0.231
  9        0   0.582   0.333   0.743      0.553
  10       0   0.201   0.195   0.084      0.160
  11       0   0.166   0.394   0.115      0.225
  12       1   0.422   0.688   0.623      0.578
  13       0   0.173   0.688   0.361      0.407
  14       0   0.239   0.333   0.212      0.261
  15       0   0.094   0.636   0.123      0.284
  16       1   0.869   0.579   0.778      0.742
  17       0   0.350   0.195   0.314      0.286
  18       0   0.102   0.195   0.153      0.150
  19       0   0.216   0.394   0.211      0.274
  20       1   0.097   0.727   0.180      0.335
  21       0   0.306   0.195   0.147      0.216
  22       0   0.081   0.562   0.188      0.277
  23       0   0.102   0.195   0.085      0.127
  24       0   0.118   0.195   0.071      0.128
  25       1   0.702   0.394   0.587      0.561
  26       1   0.520   0.769   0.774      0.688
  27       0   0.485   0.769   0.625      0.626
  28       0   0.093   0.688   0.110      0.297
  29       0   0.198   0.562   0.405      0.388
  30       0   0.039   0.195   0.019      0.084
  31       0   0.387   0.195   0.250      0.277
  32       0   0.283   0.195   0.209      0.229
  33       1   0.461   0.455   0.720      0.545
  34       0   0.365   0.195   0.251      0.270
  35       0   0.059   0.195   0.041      0.098
  36       0   0.063   0.195   0.030      0.096
  37       0   0.210   0.195   0.099      0.168
  38       0   0.285   0.195   0.192      0.224
  39       0   0.161   0.610   0.174      0.315
  40       0   0.307   0.195   0.306      0.269
  41       0   0.477   0.195   0.458      0.377
  42       0   0.231   0.394   0.311      0.312
  43       0   0.123   0.610   0.148      0.294
  44       0   0.100   0.195   0.203      0.166
  45       1   0.453   0.195   0.336      0.328
  46       0   0.188   0.727   0.153      0.356
  47       0   0.020   0.394   0.025      0.146
  48       0   0.477   0.195   0.419      0.364
  49       1   0.497   0.610   0.566      0.558
  50       1   0.302   0.857   0.515      0.558
  51       1   0.407   0.195   0.372      0.325
  52       1   0.171   0.222   0.490      0.294
  53       0   0.194   0.195   0.098      0.162
  54       0   0.131   0.394   0.085      0.203
  55       1   0.875   0.286   0.875      0.679
  56       1   0.939   0.769   0.986      0.898
  57       0   0.448   0.394   0.328      0.390
  58       0   0.729   0.333   0.735      0.599
  59       0   0.827   0.286   0.863      0.659
  60       0   0.225   0.195   0.098      0.173
  61       0   0.114   0.195   0.106      0.138
  62       0   0.218   0.394   0.150      0.254
  63       0   0.112   0.195   0.076      0.128
  64       0   0.163   0.394   0.147      0.235
  65       0   0.250   0.286   0.227      0.254
  66       0   0.258   0.394   0.166      0.273
  67       0   0.194   0.562   0.362      0.373
  68       0   0.459   0.400   0.563      0.474
  69       0   0.103   0.579   0.142      0.275
  70       1   0.408   0.769   0.525      0.567
  71       1   0.469   0.394   0.423      0.429
  72       0   0.187   0.195   0.127      0.170
  73       0   0.129   0.610   0.166      0.302
  74       0   0.148   0.195   0.122      0.155
  75       0   0.107   0.195   0.121      0.141
  76       0   0.281   0.195   0.175      0.217
  77       0   0.148   0.727   0.182      0.352
  78       0   0.621   0.286   0.602      0.503
  79       1   0.382   0.610   0.277      0.423
  80       0   0.098   0.333   0.106      0.179
  81       0   0.247   0.195   0.096      0.179
  82       0   0.223   0.195   0.168      0.195
  83       0   0.214   0.195   0.129      0.179
  84       0   0.497   0.195   0.455      0.382
  85       0   0.388   0.195   0.299      0.294
  86       0   0.140   0.579   0.307      0.342
  87       1   0.530   0.195   0.491      0.405
  88       0   0.346   0.579   0.403      0.443
  89       1   0.338   0.562   0.618      0.506
  90       1   0.782   0.195   0.553      0.510
  91       0   0.155   0.195   0.115      0.155
  92       0   0.245   0.562   0.504      0.437
  93       0   0.290   0.400   0.361      0.350
  94       1   0.303   0.769   0.576      0.549
  95       0   0.076   0.195   0.052      0.108
  96       1   0.595   0.688   0.650      0.644
  97       0   0.122   0.394   0.103      0.206
  98       1   0.814   0.769   0.942      0.842
  99       0   0.034   0.195   0.019      0.083
  100      1   0.261   0.455   0.553      0.423
  101      0   0.403   0.195   0.214      0.271
  102      1   0.484   0.455   0.548      0.496
  103      0   0.083   0.195   0.068      0.115
  104      0   0.386   0.125   0.503      0.338
  105      1   0.860   0.195   0.788      0.614
  106      0   0.172   0.727   0.190      0.363
  107      0   0.099   0.195   0.116      0.137
  108      1   0.840   0.636   0.767      0.748
  109      0   0.136   0.195   0.126      0.152
  110      0   0.174   0.195   0.087      0.152
  111      0   0.092   0.394   0.065      0.184
  112      0   0.288   0.394   0.143      0.275
  113      1   0.661   0.769   0.860      0.763
  114      1   0.791   0.579   0.745      0.705
  115      0   0.439   0.195   0.347      0.327
  116      1   0.902   0.195   0.930      0.676
  117      0   0.058   0.195   0.041      0.098
  118      0   0.780   0.195   0.609      0.528
  119      0   0.154   0.195   0.074      0.141
  120      0   0.211   0.195   0.161      0.189
  121      1   0.797   0.579   0.850      0.742
  122      1   0.830   0.333   0.900      0.688
  123      0   0.227   0.195   0.290      0.237

Thats my code:

aList<-getROC_AUC(temp[,Names[1]],temp[,"target"])
        stack_x = unlist(aList$stack_x)
        stack_y = unlist(aList$stack_y)
        temp.data<-data.frame(stack_x,stack_y)
        p<-ggplot(temp.data,aes(x=stack_x,y=stack_y))+geom_line(aes(colour=Names[1]))+
          labs(x="probability of false alarm", sub = "(1-Specificity)",y="probability of detection\n(Sensitivity)")


        for(i in Names[-1]){
          aList<-getROC_AUC(temp[,i],temp[,"target"])
          stack_x = unlist(aList$stack_x)
          stack_y = unlist(aList$stack_y)
          temp.data<-data.frame(stack_x,stack_y)
          p<-p+geom_line(data=temp.data,aes(x=stack_x,y=stack_y,colour=i))
        }
        p

And thats the plot which i am getting: enter image description here

How can i get different colour per model?

Thanks

2
  • how is your question related to shiny? Commented May 8, 2018 at 13:27
  • ggplot does weird things when you try to assemble layers in a loop. The way you're intended to use ggplot is to assemble your data into a single data frame and then color based on a model_id variable. Commented May 8, 2018 at 13:27

1 Answer 1

1

Consider base R's reshape, by, and do.call to transform your dataframe into long format for a grouped calculation to build a singular master long dataframe. As the rule of thumb, ggplot (and any other data analysis needs) always works best with long data.

Your function will then have to be adjusted to receive and return a dataframe as input parameter for by call wrapped in do.call(rbind, ...) for final dataframe build. Finally pass the master long dataframe into your plotting for one call without any loops using colour set to Model variable.

long_df <- reshape(temp, varying = names(temp)[-1], direction = "long",
                   timevar = "name", new.row.names = 1:10000)

# ADJUSTED FUNCTION
getROC_AUC <- function(df){
  probsSort <- sort(df$Model, decreasing = TRUE, index.return = TRUE)
  val <- unlist(probsSort$x)
  idx <- unlist(probsSort$ix)  

  roc_y <- df$target[idx];
  stack_x <- cumsum(roc_y == 0)/sum(roc_y == 0)
  stack_y <- cumsum(roc_y == 1)/sum(roc_y == 1)    

  auc <- sum((stack_x[2:length(roc_y)]-stack_x[1:length(roc_y)-1])
              *stack_y[2:length(roc_y)])

  df <- data.frame(Model=max(df$name), stack_x, stack_y)

  return(df)
}

# STACK ALL GROUPED DATAFRAMES
model_df <- do.call(rbind, unname(by(long_df, long_df$name, getROC_AUC)))

p <- ggplot(model_df, aes(x=stack_x, y=stack_y, colour=Model)) + geom_line() +
  labs(x = "probability of false alarm", sub = "(1-Specificity)",
       y = "probability of detection\n(Sensitivity)")

p

Plot Output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.