-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalAUPR.R
40 lines (30 loc) · 1.08 KB
/
calAUPR.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
# function to calculate the AUCR and AUC
calAUPR <- function(obsLabel, predProb) {
unique_labels <- unique(obsLabel)
if (length(unique_labels) != 2) stop("The first argument 'obsLabel' should be two classes!\n")
# calculate AUC using ROCR
# library('ROCR')
pred <- ROCR::prediction(predProb, obsLabel)
perf <- ROCR::performance(pred, "auc")
auc <- as.numeric([email protected])
# Calculate AUPR using ROCR
perf <- ROCR::performance(pred, 'rec', 'prec')
Precision <- unlist([email protected])
Recall <- unlist([email protected])
# library('MESS')
aupr_spline <- try(MESS::auc(Recall, Precision, type = 'spline'), silent = TRUE)
# Save the result
statRes <- matrix(0, nrow = 1, ncol = 2)
colnames(statRes) <- c("auc", "aupr")
if (class(aupr_spline) == 'try-error') {
# library('Bolstad2')
# uses Simpson's rule: numerical integrating, solve the area
aupr_simpson <- Bolstad2::sintegral(Recall, Precision)$int
statRes[, "auc"] <- auc
statRes[, "aupr"] <- aupr_simpson
} else {
statRes[, "auc"] <- auc
statRes[, "aupr"] <- aupr_spline
}
return(statRes)
}