5

How to obtain AUC (area under curve) of a Precision Recall Curve by using package ROCR..?

library(ROCR)
data(ROCR.simple)
pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels)
perf <- performance(pred,"tpr","fpr")
plot(perf)
## precision/recall curve (x-axis: recall, y-axis: precision)
perf1 <- performance(pred, "prec", "rec")
plot(perf1)
Newbie
  • 411
  • 5
  • 18

3 Answers3

2

You can first get the precision and recall values

x <- perf1@x.values[[1]] # Recall values
y <- perf1@y.values[[1]] # Precision values

and then calculate Area under the curve using any of the methods from calculate area under the curve

Community
  • 1
  • 1
user2715182
  • 653
  • 2
  • 10
  • 23
1

It looks like there are 2 measures for ROCR. auc and aucpr. This worked for me

For ROC

perfauc <- performance(pred, "auc")

For PR

perf1auc <- performance(pred, "aucpr")
Levon Ipdjian
  • 786
  • 7
  • 14
user72036
  • 11
  • 1
-1

ROCR can calculate AUC directly:

perf <- performance(pred, "auc")

Get AUC

perf@y.values[[1]]
USER_1
  • 2,409
  • 1
  • 28
  • 28
  • @user-1 and how to get AUC of Precision Recall plot? – Newbie Sep 06 '16 at 17:20
  • Wasn't that your original question? – USER_1 Sep 06 '16 at 17:36
  • 1
    @user-1: In above example code, *perf* is my ROC plot and *perf1* is my Precision-Recall plot. Both of them use the same *pred* object to calculate the curve. I think the answer you gave is the AUC of ROC plot not the Precision-Recall plot. Can you please guide me how to get it? – Newbie Sep 07 '16 at 09:40
  • Here is how to do it (you have to integrate the function): `f <- approxfun(data.frame(perf1@x.values , perf1@y.values) ) auc <- integrate(f, 0, 1)$value` – USER_1 Sep 07 '16 at 12:22
  • Also, from a different package: AUPRC() from {PerfMeas} – USER_1 Sep 07 '16 at 12:25