-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauc-pr.r
65 lines (54 loc) · 1.67 KB
/
auc-pr.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# Functions for Precision-recall plot and AUC-PR
#
library(ROCR)
library(caTools)
#' Area under the ROC curve
#'
#' improt ROCR
auc_roc <- function(obs, pred) {
pred <- prediction(pred, obs)
auc <- performance(pred, "auc")@y.values[[1]]
return(auc)
}
#' Area under Precision-recall curve
#'
#' import ROCR
#' import caTools
auc_pr <- function(obs, pred) {
xx.df <- prediction(pred, obs)
perf <- performance(xx.df, "prec", "rec")
xy <- data.frame([email protected][[1]], [email protected][[1]])
# take out division by 0 for lowest threshold
xy <- subset(xy, !is.nan(xy$precision))
# Designate recall = 0 as precision = x...arbitrary
xy <- rbind(c(0, 0), xy)
#xy <- xy[!(rowSums(xy)==0), ]
res <- trapz(xy$recall, xy$precision)
res
}
# Function to create raw data needed to plot Precision against recall
#
# For a vector of observed and predicted, creates x-y coordinates for a ROC
# or PR curve.
rocdf <- function(pred, obs, data=NULL, type=NULL) {
# plot_type is "roc" or "pr"
if (!is.null(data)) {
pred <- eval(substitute(pred), envir=data)
obs <- eval(substitute(obs), envir=data)
}
rocr_xy <- switch(type, roc=c("tpr", "fpr"), pr=c("prec", "rec"))
rocr_df <- prediction(pred, obs)
rocr_pr <- performance(rocr_df, rocr_xy[1], rocr_xy[2])
xy <- data.frame([email protected][[1]], [email protected][[1]])
# If PR, designate first (undefined) point as recall = 0, precision = x
if (type=="pr") {
xy[1, 2] <- 0
#xy <- xy[!(rowSums(xy)==0), ]
}
colnames(xy) <- switch(type, roc=c("tpr", "fpr"), pr=c("rec", "prec"))
return(xy)
}
# To plot, something like:
#xy <- rocdf(pred, obs, type="pr")
#plot(xy[, 1], xy[, 2])