-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathprob-gain_curve.R
More file actions
359 lines (320 loc) · 10.9 KB
/
prob-gain_curve.R
File metadata and controls
359 lines (320 loc) · 10.9 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
#' Gain curve
#'
#' `gain_curve()` constructs the full gain curve and returns a tibble. See
#' [gain_capture()] for the relevant area under the gain curve. Also see
#' [lift_curve()] for a closely related concept.
#'
#' There is a [ggplot2::autoplot()] method for quickly visualizing the curve.
#' This works for binary and multiclass output, and also works with grouped data
#' (i.e. from resamples). See the examples.
#'
#' The greater the area between the gain curve and the baseline, the better the
#' model.
#'
#' Gain curves are identical to CAP curves (cumulative accuracy profile). See
#' the Engelmann reference for more information on CAP curves.
#'
#' @section Gain and Lift Curves:
#'
#' The motivation behind cumulative gain and lift charts is as a visual method to
#' determine the effectiveness of a model when compared to the results one
#' might expect without a model. As an example, without a model, if you were
#' to advertise to a random 10% of your customer base, then you might expect
#' to capture 10% of the of the total number of positive responses had you
#' advertised to your entire customer base. Given a model that predicts
#' which customers are more likely to respond, the hope is that you can more
#' accurately target 10% of your customer base and capture
#' `>`10% of the total number of positive responses.
#'
#' The calculation to construct gain curves is as follows:
#'
#' 1. `truth` and `estimate` are placed in descending order by the `estimate`
#' values (`estimate` here is a single column supplied in `...`).
#'
#' 2. The cumulative number of samples with true results relative to the
#' entire number of true results are found. This is the y-axis in a gain chart.
#'
#' @family curve metrics
#' @templateVar fn gain_curve
#' @template multiclass-curve
#' @template event_first
#'
#' @inheritParams pr_auc
#'
#' @return
#' A tibble with class `gain_df` or `gain_grouped_df` having columns:
#'
#' - `.n` The index of the current sample.
#'
#' - `.n_events` The index of the current _unique_ sample. Values with repeated
#' `estimate` values are given identical indices in this column.
#'
#' - `.percent_tested` The cumulative percentage of values tested.
#'
#' - `.percent_found` The cumulative percentage of true results relative to the
#' total number of true results.
#'
#' If using the `case_weights` argument, all of the above columns will be
#' weighted. This makes the most sense with frequency weights, which are integer
#' weights representing the number of times a particular observation should be
#' repeated.
#'
#' @references
#'
#' Engelmann, Bernd & Hayden, Evelyn & Tasche, Dirk (2003).
#' "Measuring the Discriminative Power of Rating Systems,"
#' Discussion Paper Series 2: Banking and Financial Studies 2003,01,
#' Deutsche Bundesbank.
#'
#' @seealso
#' Compute the relevant area under the gain curve with [gain_capture()].
#'
#' @author Max Kuhn
#'
#' @template examples-binary-prob
#' @examplesIf rlang::is_installed(c("ggplot2"))
#' # ---------------------------------------------------------------------------
#' # `autoplot()`
#'
#' library(ggplot2)
#' library(dplyr)
#'
#' # Use autoplot to visualize
#' # The top left hand corner of the grey triangle is a "perfect" gain curve
#' autoplot(gain_curve(two_class_example, truth, Class1))
#'
#' # Multiclass one-vs-all approach
#' # One curve per level
#' hpc_cv %>%
#' filter(Resample == "Fold01") %>%
#' gain_curve(obs, VF:L) %>%
#' autoplot()
#'
#' # Same as above, but will all of the resamples
#' # The resample with the minimum (farthest to the left) "perfect" value is
#' # used to draw the shaded region
#' hpc_cv %>%
#' group_by(Resample) %>%
#' gain_curve(obs, VF:L) %>%
#' autoplot()
#'
#' @export
#'
gain_curve <- function(data, ...) {
UseMethod("gain_curve")
}
#' @rdname gain_curve
#' @export
gain_curve.data.frame <- function(data,
truth,
...,
na_rm = TRUE,
event_level = yardstick_event_level(),
case_weights = NULL) {
result <- curve_metric_summarizer(
name = "gain_curve",
fn = gain_curve_vec,
data = data,
truth = !!enquo(truth),
...,
na_rm = na_rm,
event_level = event_level,
case_weights = !!enquo(case_weights)
)
curve_finalize(result, data, "gain_df", "grouped_gain_df")
}
# dont export gain_curve_vec / lift_curve_vec
# not as meaningful to return a list of vectors
# maybe it could return the tibble?
gain_curve_vec <- function(truth,
estimate,
na_rm = TRUE,
event_level = yardstick_event_level(),
case_weights = NULL,
...) {
abort_if_class_pred(truth)
estimator <- finalize_estimator(truth, metric_class = "gain_curve")
check_prob_metric(truth, estimate, case_weights, estimator)
if (na_rm) {
result <- yardstick_remove_missing(truth, estimate, case_weights)
truth <- result$truth
estimate <- result$estimate
case_weights <- result$case_weights
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
cli::cli_abort(c(
x = "Missing values were detected and {.code na_ra = FALSE}.",
i = "Not able to perform calculations."
))
}
gain_curve_estimator_impl(
truth = truth,
estimate = estimate,
estimator = estimator,
event_level = event_level,
case_weights = case_weights
)
}
gain_curve_estimator_impl <- function(truth,
estimate,
estimator,
event_level,
case_weights) {
if (is_binary(estimator)) {
gain_curve_binary(truth, estimate, event_level, case_weights)
} else {
gain_curve_multiclass(truth, estimate, case_weights)
}
}
gain_curve_binary <- function(truth, estimate, event_level, case_weights) {
gain_list <- gain_curve_binary_impl(truth, estimate, event_level, case_weights)
dplyr::tibble(!!!gain_list)
}
gain_curve_multiclass <- function(truth, estimate, case_weights) {
one_vs_all_with_level(
fn = gain_curve_binary,
truth = truth,
estimate = estimate,
case_weights = case_weights
)
}
# Following the Example Problem 2 of:
# http://www2.cs.uregina.ca/~dbd/cs831/notes/lift_chart/lift_chart.html
gain_curve_binary_impl <- function(truth,
estimate,
event_level,
case_weights) {
truth <- unclass(truth)
# Events are re-coded as 1, non-events are 0. Easier for cumulative calcs.
if (is_event_first(event_level)) {
truth <- as.integer(truth == 1L)
} else {
truth <- as.integer(truth == 2L)
}
if (is.null(case_weights)) {
case_weights <- rep(1, times = length(truth))
}
case_weights <- vec_cast(case_weights, to = double())
# arrange in decreasing order by class probability score
estimate_ord <- order(estimate, decreasing = TRUE)
estimate <- estimate[estimate_ord]
truth <- truth[estimate_ord]
case_weights <- case_weights[estimate_ord]
case_weights_event <- ifelse(truth, case_weights, 0)
n_events <- sum(case_weights_event)
n_predictions <- sum(case_weights)
# uninformative model x and y axis
# this is also the x axis in the gain / lift charts
cumulative_tested <- cumsum(case_weights)
cumulative_percent_tested <- (cumulative_tested / n_predictions) * 100
cumulative_found <- cumsum(case_weights_event)
cumulative_percent_found <- (cumulative_found / n_events) * 100
# remove all but the last of any duplicated estimates
# doing this after the fact allows us to still use cumsum()
where_dups <- duplicated(estimate, fromLast = TRUE)
cumulative_found <- cumulative_found[!where_dups]
cumulative_tested <- cumulative_tested[!where_dups]
cumulative_percent_tested <- cumulative_percent_tested[!where_dups]
cumulative_percent_found <- cumulative_percent_found[!where_dups]
# edge values
cumulative_found <- c(0, cumulative_found)
cumulative_tested <- c(0, cumulative_tested)
cumulative_percent_tested <- c(0, cumulative_percent_tested)
cumulative_percent_found <- c(0, cumulative_percent_found)
list(
.n = cumulative_tested,
.n_events = cumulative_found,
.percent_tested = cumulative_percent_tested,
.percent_found = cumulative_percent_found
)
}
# autoplot ---------------------------------------------------------------------
# dynamically exported in .onLoad()
autoplot.gain_df <- function(object, ...) {
`%+%` <- ggplot2::`%+%`
`%>%` <- dplyr::`%>%`
# Base chart
chart <- ggplot2::ggplot(data = object)
# Grouped specific chart features
if (dplyr::is_grouped_df(object)) {
# Construct the color interaction group
grps <- dplyr::groups(object)
interact_expr <- list(
color = expr(interaction(!!!grps, sep = "_"))
)
# Add group legend label
grps_chr <- paste0(dplyr::group_vars(object), collapse = "_")
chart <- chart %+%
ggplot2::labs(color = grps_chr)
} else {
interact_expr <- list()
}
# Generic enough to be used in the pipe chain
# for multiclass and binary curves
maybe_group_by_level <- function(object, with_old = TRUE) {
if (with_old) {
grps <- dplyr::groups(object)
} else {
grps <- list()
}
if (".level" %in% colnames(object)) {
# .level should be the first group b/c of how summarise()
# drops a group
object <- dplyr::group_by(object, .level, !!!grps)
}
object
}
# Construct poly_data
# If grouped (ie resamples), we take the min of all "perfect" values
# to ensure we capture all lines in the polygon
# If multiclass, we calculate each level separately
poly_data <- object %>%
maybe_group_by_level() %>%
dplyr::summarise(slope = 1 / (max(.n_events) / dplyr::last(.n))) %>%
dplyr::mutate(perfect = 100 / slope) %>%
maybe_group_by_level(with_old = FALSE) %>%
dplyr::summarise(perfect = min(perfect)) %>%
maybe_group_by_level() %>%
dplyr::do(
dplyr::tibble(
x = c(0, .$perfect, 100),
y = c(0, 100, 100)
)
)
# Avoid cran check for "globals"
.percent_tested <- as.name(".percent_tested")
.percent_found <- as.name(".percent_found")
x <- as.name("x")
y <- as.name("y")
chart <- chart %+%
# boundary poly
ggplot2::geom_polygon(
mapping = ggplot2::aes(
x = !!x,
y = !!y
),
data = poly_data,
# fill
fill = "lightgrey",
alpha = 0.4
) %+%
# gain curve
ggplot2::geom_line(
mapping = ggplot2::aes(
x = !!.percent_tested,
y = !!.percent_found,
!!!interact_expr
),
data = object
) %+%
ggplot2::labs(
x = "% Tested",
y = "% Found"
) %+%
ggplot2::theme_bw()
# facet by .level if this was a multiclass computation
if (".level" %in% colnames(object)) {
chart <- chart %+%
ggplot2::facet_wrap(~.level)
}
chart
}