-
Notifications
You must be signed in to change notification settings - Fork 7
/
confusion_matrix.R
346 lines (310 loc) · 11.9 KB
/
confusion_matrix.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#' @title Confusion Matrices (Contingency Tables)
#'
#' @description Construction of confusion matrices, accuracy, sensitivity,
#' specificity, confidence intervals (Wilson's method and (optional
#' bootstrapping)).
#'
#' @param truth a integer vector with the values \code{0} and \code{1}, or a logical vector.
#' A value of \code{0} or \code{FALSE} is an indication of condition negative;
#' \code{1} or \code{TRUE} is an indication of condition positive.
#' @param predicted a numeric vector. See Details.
#' @param formula of the form \code{truth ~ predicted}.
#' @param data a data.frame containing the variables reference in
#' \code{formula}.
#' @param thresholds a numeric vector of thresholds to be used to define the
#' confusion matrix (one threshold) or matrices (two or more thresholds). If
#' \code{NULL} the unique values of \code{predicted} will be used.
#' @param ... pass through
#' @param confint_method character string denoting if the logit (default),
#' binomial, or Wilson Score method for deriving confidence intervals
#' @param alpha alpha level for 100 * (1 - alpha)\% confidence intervals
#'
#' @details
#' The confusion matrix:
#'
#' \tabular{lccc}{
#' \tab \tab True \tab Condition \cr
#' \tab \tab + \tab - \cr
#' Predicted Condition \tab + \tab TP \tab FP \cr
#' Predicted Condition \tab - \tab FN \tab TN \cr
#' }
#' where
#' \itemize{
#' \item FN: False Negative = truth = 1 & prediction < threshold,
#' \item FP: False Positive = truth = 0 & prediction >= threshold,
#' \item TN: True Negative = truth = 0 & prediction < threshold, and
#' \item TP: True Positive = truth = 1 & prediction >= threshold.
#' }
#'
#' The statistics returned in the \code{stats} element are:
#' \itemize{
#' \item accuracy = (TP + TN) / (TP + TN + FP + FN)
#' \item sensitivity, aka true positive rate = TP / (TP + FN)
#' \item specificity, aka true negative rate = TN / (TN + FP)
#' \item positive predictive value (PPV), aka precision = TP / (TP + FP)
#' \item negative predictive value (NPV) = TN / (TN + FN)
#' \item false negative rate (FNR) = 1 - Sensitivity
#' \item false positive rate (FPR) = 1 - Specificity
#' \item false discovery rate (FDR) = 1 - PPV
#' \item false omission rate (FOR) = 1 - NPV
#' \item F1 score
#' \item Matthews Correlation Coefficient (MCC) =
#' ((TP * TN) - (FP * FN)) / sqrt((TP + FP) (TP+FN) (TN+FP) (TN+FN))
#' }
#'
#' Synonyms for the statistics:
#' \itemize{
#' \item Sensitivity: true positive rate (TPR), recall, hit rate
#' \item Specificity: true negative rate (TNR), selectivity
#' \item PPV: precision
#' \item FNR: miss rate
#' }
#'
#' Sensitivity and PPV could, in some cases, be indeterminate due to division by
#' zero. To address this we will use the following rule based on the DICE group
#' \url{https://github.com/dice-group/gerbil/wiki/Precision,-Recall-and-F1-measure}:
#' If TP, FP, and FN are all 0, then PPV, sensitivity, and F1 will be defined to
#' be 1. If TP are 0 and FP + FN > 0, then PPV, sensitivity, and F1 are all
#' defined to be 0.
#'
#' @return
#' \code{confusion_matrix} returns a data.frame with columns
#' \itemize{
#' \item
#' \item
#' \item
#' }
#'
#' @examples
#'
#' # Example 1: known truth and prediction status
#' df <-
#' data.frame(
#' truth = c(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0)
#' , pred = c(1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0)
#' )
#'
#' confusion_matrix(df$truth, df$pred, thresholds = 1)
#'
#' # Example 2: Use with a logistic regression model
#' mod <- glm(
#' formula = spam ~ word_freq_our + word_freq_over + capital_run_length_total
#' , data = spambase
#' , family = binomial()
#' )
#'
#' confusion_matrix(mod)
#' confusion_matrix(mod, thresholds = 0.5)
#'
#' @export
#' @rdname confusion_matrix
confusion_matrix <- function(..., thresholds = NULL, confint_method = "logit", alpha = getOption("qwraps2_alpha", 0.05)) {
UseMethod("confusion_matrix")
}
#' @export
#' @rdname confusion_matrix
confusion_matrix.default <- function(truth, predicted, ..., thresholds = NULL, confint_method = "logit", alpha = getOption("qwraps2_alpha", 0.05)) {
truth <- as.integer(truth)
# assumption checks
stopifnot(!any(is.na(truth)), !any(is.na(predicted)))
stopifnot(length(truth) == length(predicted))
stopifnot(all(truth %in% c(0L, 1L)))
stopifnot(is.numeric(predicted))
if (is.null(thresholds)) {
thresholds <- unique(predicted)
} else {
stopifnot(is.numeric(thresholds))
thresholds <- unique(thresholds)
}
thresholds <- sort(unique(c(-Inf, thresholds, Inf)))
cm_cells <-
lapply(thresholds,
function(threshold) {
cells <-
list( threshold = threshold
, TP = sum(truth == 1 & predicted >= threshold)
, TN = sum(truth == 0 & predicted < threshold)
, FP = sum(truth == 0 & predicted >= threshold)
, FN = sum(truth == 1 & predicted < threshold))
})
sen <- sapply(cm_cells, do.call, what = sensitivity)
spc <- sapply(cm_cells, do.call, what = specificity)
ppv <- sapply(cm_cells, do.call, what = PPV)
npv <- sapply(cm_cells, do.call, what = NPV)
acc <- sapply(cm_cells, do.call, what = accuracy)
auroc <- -traprule(x = 1 - spc, y = sen)
auprc <- -traprule(x = sen, y = ppv)
# marginal sums
N <- unique(sapply(cm_cells, function(x) x[["TP"]] + x[["FP"]] + x[["TN"]] + x[["FN"]]))
stopifnot(length(N) == 1L)
condition_P <- unique(sapply(cm_cells, function(x) x[["TP"]] + x[["FN"]]))
stopifnot(length(condition_P) == 1L)
condition_N <- unique(sapply(cm_cells, function(x) x[["TN"]] + x[["FP"]]))
stopifnot(length(condition_N) == 1L)
predicted_P <- sapply(cm_cells, function(x) x[["TP"]] + x[["FP"]])
predicted_N <- sapply(cm_cells, function(x) x[["FN"]] + x[["TN"]])
# confidence intervals
sen_ci <- do.call(rbind, Map(proportion_confint, p = sen, n = condition_P, method = confint_method, alpha = alpha))
spc_ci <- do.call(rbind, Map(proportion_confint, p = spc, n = condition_N, method = confint_method, alpha = alpha))
ppv_ci <- do.call(rbind, Map(proportion_confint, p = ppv, n = predicted_P, method = confint_method, alpha = alpha))
npv_ci <- do.call(rbind, Map(proportion_confint, p = npv, n = predicted_N, method = confint_method, alpha = alpha))
acc_ci <- do.call(rbind, Map(proportion_confint, p = acc, n = N, method = confint_method, alpha = alpha))
cm_stats <- lapply(cm_cells, as.data.frame)
cm_stats <- do.call(rbind, cm_stats)
cm_stats <- cbind(cm_stats
, sensitivity = sen
, sensitivity_lcl = sen_ci[, 1]
, sensitivity_ucl = sen_ci[, 2]
, specificity = spc
, specificity_lcl = spc_ci[, 1]
, specificity_ucl = spc_ci[, 2]
, ppv = ppv
, ppv_lcl = ppv_ci[, 1]
, ppv_ucl = ppv_ci[, 2]
, npv = npv
, npv_lcl = npv_ci[, 1]
, npv_ucl = npv_ci[, 2]
, accuracy = acc
, accuracy_lcl = acc_ci[, 1]
, accuracy_ucl = acc_ci[, 2]
, youden = sapply(cm_cells, do.call, what = youden)
, mcc = sapply(cm_cells, do.call, what = MCC)
, f1 = sapply(cm_cells, do.call, what = F1)
)
rtn <-
list(
cm_stats = cm_stats
, auroc = auroc
, auroc_ci = proportion_confint(p = auroc, n = N, method = confint_method, alpha = alpha)
, auprc = auprc
, auprc_ci = proportion_confint(p = auprc, n = N, method = confint_method, alpha = alpha)
, confint_method = confint_method
, alpha = alpha
, prevalence = condition_P / (condition_P + condition_N)
)
class(rtn) <- c("qwraps2_confusion_matrix")
rtn
}
#' @param formula column (known) ~ row (test) for building the confusion matrix
#' @param data environment containing the variables listed in the formula
#' @export
#' @rdname confusion_matrix
confusion_matrix.formula <- function(formula, data = parent.frame(), ..., thresholds = NULL, confint_method = "logit", alpha = getOption("qwraps2_alpha", 0.05)) {
cl <- as.list(match.call())[-1]
mf <- stats::model.frame(formula, data)
cl[["truth"]] <- mf[[1]]
cl[["predicted"]] <- mf[[2]]
cl[["formula"]] <- NULL
cl[["data"]] <- NULL
do.call(confusion_matrix, cl)
}
#' @param x a \code{glm} object
#' @export
#' @rdname confusion_matrix
confusion_matrix.glm <- function(x, ..., thresholds = NULL, confint_method = "logit", alpha = getOption("qwraps2_alpha", 0.05)) {
stopifnot(x[["family"]][["family"]] == "binomial")
truth <- x[["y"]]
pred <- stats::predict(x, type = "response")
confusion_matrix(truth = truth, predicted = pred, ..., thresholds = thresholds, confint_method = confint_method, alpha = alpha)
}
#' @rdname confusion_matrix
#' @export
print.qwraps2_confusion_matrix <- function(x, ...) {
NextMethod(print, x)
invisible(x)
}
################################################################################
# non-exported functions
accuracy <- function(TP, TN, FP, FN, ...) {
(TP + TN) / (TP + TN + FP + FN)
}
sensitivity <- function(TP, TN, FP, FN, ...) {
# The following rule to deal with division by zero is based on the DICE group
# <URL: https://github.com/dice-group/gerbil/wiki/Precision,-Recall-and-F1-measure>:
if ((TP + FP + FN) == 0) {
rtn <- 1
} else if ((TP == 0) & (FP + FN > 1)) {
rtn <- 0
} else {
rtn <- TP / (TP + FN)
}
rtn
}
specificity <- function(TP, TN, FP, FN, ...) {
TN / (TN + FP)
}
precision <- PPV <- function(TP, TN, FP, FN, ...) {
# The following rule to deal with division by zero is based on the DICE group
# <URL: https://github.com/dice-group/gerbil/wiki/Precision,-Recall-and-F1-measure>:
if ((TP + FP + FN) == 0) {
rtn <- 1
} else if ((TP == 0) & (FP + FN > 1)) {
rtn <- 0
} else {
rtn <- TP / (TP + FP)
}
rtn
}
NPV <- function(TP, TN, FP, FN, ...) {
rtn <- TN / (TN + FN)
rtn
}
FNR <- function(TP, TN, FP, FN, ...) {
1 - sensitivity(TP, TN, FP, FN)
}
FPR <- function(TP, TN, FP, FN, ...) {
1 - specificity(TP, TN, FP, FN)
}
FDR <- function(TP, TN, FP, FN, ...) {
1 - PPV(TP, TN, FP, FN)
}
# False Omission Rate
FOR <- function(TP, TN, FP, FN, ...) {
1 - NPV(TP, TN, FP, FN)
}
F1 <- function(TP, TN, FP, FN, ...) {
if ((TP + FP + FN) == 0) {
rtn <- 1
} else if ((TP == 0) & (FP + FN > 1)) {
rtn <- 0
} else {
rtn <- (2 * TP) / (2 * TP + FP + FN)
}
rtn
}
youden <- function(TP, TN, FP, FN, ...) {
sensitivity(TP, TN, FP, FN) + specificity(TP, TN, FP, FN) - 1
}
# Matthews Correlation Coefficient
MCC <- function(TP, TN, FP, FN, ...) {
# ((TP * TN) - (FP * FN)) / sqrt((TP + FP) * (TP+FN) * (TN+FP) * (TN+FN))
# because there can be very large numbers work with logs when needed
# denominator <- 0.5 * ( log(TP+FP) + log(TP+FN) + log(TN+FP) + log(TN+FN) )
if ( ((TP + FP) == 0) | ((TP + FN) == 0) | ((TN + FP) == 0) | ((TN + FN) == 0)) {
rtn <- NA_real_
} else {
denominator <- exp(0.5 * ( log(TP+FP) + log(TP+FN) + log(TN+FP) + log(TN+FN) ))
if (TP == 0 | TN == 0 | FP == 0 | FN == 0 ) {
numerator <- (TP * TN) - (FP * FN)
} else {
numerator <- exp(log(TP) + log(TN)) - exp(log(FP) + log(FN))
}
rtn <- numerator / denominator
}
rtn
}
proportion_confint <- function(p, n, method = "logit", alpha = getOption("qwraps2_alpha", 0.05)) {
z <- stats::qnorm(1 - alpha/2)
if (method == "logit") {
m <- stats::qlogis(p)
tau <- 1 / sqrt( n * p * (1 - p) )
rtn <- stats::plogis(m + c(-z, z) * tau)
} else if (method == "binomial") {
rtn <- p + c(-z, z) * sqrt( p * (1 - p) / n)
} else if (method == "wilson_score") {
rtn <- 1 / (1 + 1/n * z^2) * (p + 1 / (2 * n) * z^2 + c(-z, z) * sqrt( 1 / n * p * (1 - p) + 1 / (4 * n^2) * z^2))
} else {
stop("method not in c('logit', 'binomial', 'wilson_score')")
}
rtn
}