-
Notifications
You must be signed in to change notification settings - Fork 9
/
confusionMatrix_.cpp
164 lines (110 loc) · 3.71 KB
/
confusionMatrix_.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix confusionMatrix_(NumericVector actual, NumericVector predicted, double cutoff, bool use_names = false) {
NumericMatrix cMat = NumericMatrix(Dimension(2, 2));
// True Negatives
cMat(0,0) = sum((predicted <= cutoff) & (actual == 0));
// False Negatives
cMat(0,1) = sum((predicted <= cutoff) & (actual == 1));
// False positives
cMat(1,0) = sum((predicted > cutoff) & (actual == 0));
// True positives
cMat(1,1) = sum((predicted > cutoff) & (actual == 1));
if (use_names) {
colnames(cMat) = CharacterVector::create("actual_0", "actual_1");
rownames(cMat) = CharacterVector::create("predicted_0", "predicted_1");
}
return cMat;
}
// [[Rcpp::export]]
double ppv_(NumericVector actual, NumericVector predicted, double cutoff) {
NumericMatrix cMat = confusionMatrix_(actual, predicted, cutoff);
double Denom = (cMat(1,1) + cMat(1,0));
double ppv = 0;
if(Denom != 0){
ppv = cMat(1,1) / Denom;
}
return ppv;
}
// [[Rcpp::export]]
double npv_(NumericVector actual, NumericVector predicted, double cutoff) {
NumericMatrix cMat = confusionMatrix_(actual, predicted, cutoff);
double Denom (cMat(0,0) + cMat(0,1));
double npv = 0;
if(Denom != 0){
npv = cMat(0,0) / Denom;
}
return npv;
}
// [[Rcpp::export]]
double tnr_(NumericVector actual, NumericVector predicted, double cutoff) {
double TN = sum((predicted < cutoff) & (actual == 0));
double N = sum(actual == 0);
double tnr = TN/N;
return tnr;
}
// [[Rcpp::export]]
double recall_(NumericVector actual, NumericVector predicted, double cutoff) {
NumericMatrix cMat = confusionMatrix_(actual, predicted, cutoff);
double recall = cMat(1,1) / (cMat(1,1) + cMat(0,1));
return recall;
}
// [[Rcpp::export]]
double fScore_(NumericVector actual, NumericVector predicted, double cutoff, double beta){
double p = ppv_(actual, predicted, cutoff);
double r = recall_(actual, predicted, cutoff);
double F = 0;
if(p + r != 0){
F = ((beta*beta + 1)*(p * r / (beta*beta*p + r)));
}
return F;
}
// [[Rcpp::export]]
double f1Score_(NumericVector actual, NumericVector predicted, double cutoff){
double p = ppv_(actual, predicted, cutoff);
double r = recall_(actual, predicted, cutoff);
double f1 = 0;
if(p + r != 0){
f1 = (2*p*r)/(p + r);
}
return f1;
}
// [[Rcpp::export]]
double brier_(NumericVector actual, NumericVector predicted){
double brier = mean(pow(actual - predicted, 2));
return brier;
}
// [[Rcpp::export]]
double mcc_(NumericVector actual, NumericVector predicted, double cutoff){
// True Negatives
double TN = sum((predicted < cutoff) & (actual == 0));
// False Negatives
double FN = sum((predicted < cutoff) & (actual == 1));
// False positives
double FP = sum((predicted >= cutoff) & (actual == 0));
// True positives
double TP = sum((predicted >= cutoff) & (actual == 1));
double numerator = ((TP*TN) - (FP*FN));
double denom = sqrt((TP + FP)*(TP + FN)*(TN + FP)*(TN + FN));
double mcc = numerator/denom;
return mcc;
}
// [[Rcpp::export]]
double kappa_(NumericVector actual, NumericVector predicted, double cutoff){
// True Negatives d
double TN = sum((predicted < cutoff) & (actual == 0));
// False Negatives - c
double FN = sum((predicted < cutoff) & (actual == 1));
// False positives - b
double FP = sum((predicted >= cutoff) & (actual == 0));
// True positives - a
double TP = sum((predicted >= cutoff) & (actual == 1));
double N = (TP + FP + FN + TN);
double po = (TP + TN)/N;
double margin_a = ((TP + FP)*(TP + FN))/N;
double margin_b = ((FN + TN)*(FP + TN))/N;
double pe = (margin_a + margin_b)/N;
double kappa = (po - pe)/(1 - pe);
return kappa;
}