-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbinary_metrics.R
47 lines (44 loc) · 1.56 KB
/
binary_metrics.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
binary_metrics <- function(predictions, reference, numeric = TRUE, class_of_interest = 1)
{
if(numeric)
{
if(class_of_interest == 1)
other_class = 2
else
other_class = 1
if(class_of_interest != 1 & class_of_interest !=2)
print("incorrect parameter for class_of_interest")
tp <- sum((predictions == class_of_interest) & (reference == class_of_interest))
fp <- sum((predictions == class_of_interest) & (reference == other_class))
fn <- sum((predictions == other_class) & (reference == class_of_interest))
tn <- sum((predictions == other_class) & (reference == other_class))
se <- tp/(tp+fn)
sp <- tn/(tn+fp)
ppv <- tp/(tp+fp)
npv <- tn/(tn+fn)
if(length(unique(predictions)) == 1)
{
area <- 0.50
} else {
if(class_of_interest == 1)
{
predictions_auc <- predictions
predictions_auc[predictions_auc == 2] <- 0
reference_auc <- reference
reference_auc[reference_auc == 2] <- 0
area <- auc(predictions_auc, reference)
} else if(class_of_interest == 2)
{
predictions_auc <- predictions
predictions_auc[predictions_auc == 1] <- 0
reference_auc <- reference
reference_auc[reference_auc == 1] <- 0
predictions_auc[predictions_auc == 2] <- 1
reference_auc[reference_auc == 2] <- 1
area <- auc(predictions_auc, reference)
}
}
metrics <- data.frame(se = round(se, 2), sp = round(sp, 2), ppv = round(ppv, 2), npv = round(npv, 2), auc = round(area, 2))
}
return (metrics)
}