/
GainCurve.R
728 lines (690 loc) · 26.4 KB
/
GainCurve.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# define some helper and reporting functions
# calculate area under the curve of numeric vectors x,y
# length(x)==length(y)
# y>=0, 0<=x<=1 x non-empty, and x strictly increasing
areaCalc <- function(x, y) {
# append extra points to get rid of degenerate cases
if (!all(diff(x) > 0)) {
stop("areaCalc x wasn't strinctly increasing")
}
if (x[1] < 0) {
x <- c(0, x)
y <- c(0, y)
}
if (x[length(x)] < 1) {
x <- c(x, 1)
y <- c(y, 1)
}
n <- length(x)
sum(0.5 * (y[-1] + y[-n]) * (x[-1] - x[-n]))
}
relativeGiniScore <- function(modelValues, yValues) {
d = data.frame(predcol = modelValues, truthcol = yValues)
n <- nrow(d)
predord = order(d[['predcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE) # reorder, with highest first
wizard = order(d[['truthcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE)
npop = dim(d)[1]
# data frame the cumulative prediction/truth as a function
# of the fraction of the population we're considering, highest first
results = data.frame(
pctpop = (1:npop) / npop,
model = cumsum(d[predord, 'truthcol']) / sum(d[['truthcol']]),
wizard = cumsum(d[wizard, 'truthcol']) / sum(d[['truthcol']])
)
# calculate the areas under each curve
# gini score is 2* (area - 0.5)
idealArea = areaCalc(results$pctpop, results$wizard) - 0.5
modelArea = areaCalc(results$pctpop, results$model) - 0.5
modelArea / idealArea # actually, normalized gini score
}
# sample with respect to multiple orders to get smooth sampling
thin_frame_by_orders <- function(d, cols, groupcol, large_count) {
n <- nrow(d)
if(n<=(length(cols)+1)*large_count) {
return(d)
}
takes <- c()
for(ci in cols) {
ordi <- order(d[[groupcol]],
d[[ci]],
sample.int(n, n, replace = FALSE))
takesi <- seq(1, n, length.out = large_count)
sg <- d[[groupcol]][ordi]
deltas <- which(sg[-1]!=sg[-n])
boundsi <- NULL
if(length(deltas>1)) {
boundsi <- pmin(n, c(deltas, 1+deltas))
}
invperm <- wrapr::invert_perm(ordi)
takes <- sort(unique(c(takes,
invperm[takesi],
invperm[boundsi])))
}
if(2*length(takes)>=n) {
return(d)
}
d[takes, , drop = FALSE]
}
#' Plot the cumulative gain curve of a sort-order.
#'
#' Plot the cumulative gain curve of a sort-order.
#'
#' The use case for this visualization is to compare a predictive model
#' score to an actual outcome (either binary (0/1) or continuous). In this case the
#' gain curve plot measures how well the model score sorts the data compared
#' to the true outcome value.
#'
#' The x-axis represents the fraction of items seen when sorted by score, and the
#' y-axis represents the cumulative summed true outcome represented by the items seen so far.
#' See, for example,
#' \url{https://www.ibm.com/support/knowledgecenter/SSLVMB_24.0.0/spss/tutorials/mlp_bankloan_outputtype_02.html}.
#'
#' For comparison, \code{GainCurvePlot} also plots the "wizard curve": the gain curve when the
#' data is sorted according to its true outcome.
#'
#' To improve presentation quality, the plot is limited to approximately \code{large_count} points (default: 1000).
#' For larger data sets, the data is appropriately randomly sampled down before plotting.
#'
#'
#' @param frame data frame to get values from
#' @param xvar name of the independent (input or model score) column in frame
#' @param truthVar name of the dependent (output or result to be modeled) column in frame
#' @param title title to place on plot
#' @param ... no unnamed argument, added to force named binding of later arguments.
#' @param estimate_sig logical, if TRUE compute significance.
#' @param large_count numeric, upper bound target for number of plotting points.
#' @param truth_target if not NULL compare to this scalar value.
#' @param model_color color for the model curve
#' @param wizard_color color for the "wizard" (best possible) curve
#' @param shadow_color color for the shaded area under the curve
#' @examples
#'
#' set.seed(34903490)
#' y = abs(rnorm(20)) + 0.1
#' x = abs(y + 0.5*rnorm(20))
#' frm = data.frame(model=x, value=y)
#' WVPlots::GainCurvePlot(frm, "model", "value",
#' title="Example Continuous Gain Curve")
#'
#' @export
GainCurvePlot = function(frame, xvar, truthVar, title,
...,
estimate_sig = FALSE,
large_count = 1000,
truth_target = NULL,
model_color='darkblue',
wizard_color='darkgreen',
shadow_color='darkgray') {
frame <- check_frame_args_list(...,
frame = frame,
name_var_list = list(xvar = xvar, truthVar = truthVar),
title = title,
funname = "WVPlots::GainCurvePlot")
pctpop <- NULL # used as a symbol, declare not an unbound variable
pct_outcome <-
NULL # used as a symbol, declare not an unbound variable
sort_criterion <-
NULL # used as a symbol, declare not an unbound variable
if(!is.null(truth_target)) {
truthcol <- as.numeric(frame[[truthVar]]==truth_target)
} else {
truthcol <- as.numeric(frame[[truthVar]])
}
predcol <- as.numeric(frame[[xvar]])
# data frame of pred and truth, sorted in order of the predictions
d = data.frame(predcol = predcol, truthcol = truthcol)
n <- nrow(d)
predord = order(d[['predcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE) # reorder, with highest first
wizard = order(d[['truthcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE)
npop = dim(d)[1]
# data frame the cumulative prediction/truth as a function
# of the fraction of the population we're considering, highest first
results = data.frame(
pctpop = (1:npop) / npop,
model = cumsum(d[predord, 'truthcol']) / sum(d[['truthcol']]),
wizard = cumsum(d[wizard, 'truthcol']) / sum(d[['truthcol']])
)
# calculate the areas under each curve
# gini score is 2* (area - 0.5)
idealArea = areaCalc(results$pctpop, results$wizard) - 0.5
modelArea = areaCalc(results$pctpop, results$model) - 0.5
relGiniScore = modelArea / idealArea # actually, normalized gini score
# transform the frame into the tall form, for plotting
r1 <- data.frame(pctpop = results$pctpop,
pct_outcome = results$model,
sort_criterion = "model",
stringsAsFactors = FALSE)
r2 <- data.frame(pctpop = results$pctpop,
pct_outcome = results$wizard,
sort_criterion = "wizard",
stringsAsFactors = FALSE)
results <- rbind(r1, r2, stringsAsFactors = FALSE)
# rename sort_criterion
sortKeyM <- c('model' = paste('model: sort by', xvar),
'wizard' = paste('wizard: sort by', truthVar))
results$sort_criterion <- sortKeyM[results$sort_criterion]
# rename levels of sort criterion
colorKey = as.character(sortKeyM) %:=% c(model_color, wizard_color)
names(colorKey) = c(paste('model: sort by', xvar),
paste('wizard: sort by', truthVar))
modelKey = names(colorKey)[[1]]
pString <- ''
if(estimate_sig && requireNamespace('sigr', quietly = TRUE)) {
sp <-
sigr::permutationScoreModel(predcol, truthcol, relativeGiniScore)
pString <-
sigr::render(sigr::wrapSignificance(sp$pValue), format = 'ascii')
pString <-
paste0('\nalt. hyp.: relGini(',
xvar,
')>permuted relGini, ',
pString)
}
# cut down the number of points
results <- thin_frame_by_orders(results,
c("pctpop", "pct_outcome"),
"sort_criterion",
large_count)
# plot
gplot = ggplot2::ggplot(data = results) +
ggplot2::geom_point(
mapping = ggplot2::aes(
x = pctpop,
y = pct_outcome,
color = sort_criterion,
shape = sort_criterion
),
alpha = 0.5
) +
ggplot2::geom_line(
mapping = ggplot2::aes(
x = pctpop,
y = pct_outcome,
color = sort_criterion,
linetype = sort_criterion
)
) +
ggplot2::geom_abline(
color = 'gray',
slope = 1,
intercept = 0
) +
ggplot2::geom_ribbon(
data = results[results$sort_criterion == modelKey, , drop = FALSE],
mapping = ggplot2::aes(
x = pctpop,
ymin = pctpop,
ymax = pct_outcome,
color = sort_criterion
),
alpha = 0.3,
color = NA,
fill = shadow_color
) +
ggplot2::ggtitle(
paste0(
title,
'\n',
truthVar,
'~',
xvar),
subtitle=paste0(
'Gini score: ',
format(modelArea, digits = 2),
', relative Gini score: ',
format(relGiniScore, digits = 2),
pString
)
) +
ggplot2::xlab("fraction items in sort order") +
ggplot2::ylab(paste("fraction total sum", truthVar)) +
ggplot2::scale_x_continuous(breaks = seq(0, 1, 0.1)) +
ggplot2::scale_y_continuous(breaks = seq(0, 1, 0.1)) +
ggplot2::scale_color_manual(values = colorKey) +
ggplot2::coord_fixed() +
ggplot2::theme(legend.position = "bottom")
gplot
}
makeRelativeGiniCostScorer <- function(costcol) {
force(costcol)
function(modelValues, yValues) {
truthcol <- yValues
predcol <- modelValues
# data frame of pred and truth, sorted in order of the predictions
d = data.frame(predcol = predcol,
truthcol = truthcol,
costcol = costcol)
n <- nrow(d)
predord = order(d[['predcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE) # reorder, with highest first
wizard = order(d[['truthcol']] / d[['costcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE)
npop = dim(d)[1]
# data frame the cumulative prediction/truth as a function
# of the fraction of the population we're considering, highest first
mName = paste("model: sort by model")
resultsM = data.frame(
pctpop = cumsum(d[predord, 'costcol']) / sum(d[['costcol']]),
pct_outcome = cumsum(d[predord, 'truthcol']) /
sum(d[['truthcol']]),
sort_criterion = mName
)
wName = paste("wizard: sort by varlue/cost")
resultsW = data.frame(
pctpop = cumsum(d[wizard, 'costcol']) / sum(d[['costcol']]),
pct_outcome = cumsum(d[wizard, 'truthcol']) /
sum(d[['truthcol']]),
sort_criterion = wName
)
results = rbind(resultsM, resultsW, stringsAsFactors = FALSE)
# calculate the areas under each curve
# gini score is 2* (area - 0.5)
idealArea = areaCalc(resultsW$pctpop, resultsW$pct_outcome) - 0.5
modelArea = areaCalc(resultsM$pctpop, resultsM$pct_outcome) - 0.5
modelArea / idealArea # actually, normalized gini score
}
}
#' Plot the cumulative gain curve of a sort-order with costs.
#'
#' Plot the cumulative gain curve of a sort-order with costs.
#'
#' \code{GainCurvePlotC} plots a cumulative gain curve for the case where
#' items have an additional cost, in addition to an outcome value.
#'
#' The x-axis represents the fraction of total cost experienced when items are sorted by score, and the
#' y-axis represents the cumulative summed true outcome represented by the items seen so far.
#'
#' For comparison, \code{GainCurvePlotC} also plots the "wizard curve": the gain curve when the
#' data is sorted according to its true outcome/cost (the optimal sort order).
#'
#' To improve presentation quality, the plot is limited to approximately \code{large_count} points (default: 1000).
#' For larger data sets, the data is appropriately randomly sampled down before plotting.
#'
#'
#' @param frame data frame to get values from
#' @param xvar name of the independent (input or model score) column in frame
#' @param costVar cost of each item (drives x-axis sum)
#' @param truthVar name of the dependent (output or result to be modeled) column in frame
#' @param title title to place on plot
#' @param ... no unnamed argument, added to force named binding of later arguments.
#' @param estimate_sig logical, if TRUE compute significance
#' @param large_count numeric, upper bound target for number of plotting points
#' @param model_color color for the model curve
#' @param wizard_color color for the "wizard" (best possible) curve
#' @param shadow_color color for the shaded area under the curve
#'
#' @seealso \code{\link{GainCurvePlot}}
#'
#' @examples
#'
#' set.seed(34903490)
#' y = abs(rnorm(20)) + 0.1
#' x = abs(y + 0.5*rnorm(20))
#' frm = data.frame(model=x, value=y)
#' frm$costs=1
#' frm$costs[1]=5
#' WVPlots::GainCurvePlotC(frm, "model", "costs", "value",
#' title="Example Continuous Gain CurveC")
#'
#' @export
GainCurvePlotC = function(frame, xvar, costVar, truthVar, title,
...,
estimate_sig = FALSE,
large_count = 1000,
model_color='darkblue',
wizard_color='darkgreen',
shadow_color='darkgray') {
frame <- check_frame_args_list(...,
frame = frame,
name_var_list = list(xvar = xvar, costVar= costVar, truthVar = truthVar),
title = title,
funname = "WVPlots::GainCurvePlotC")
pctpop <- NULL # used as a symbol, declare not an unbound variable
pct_outcome <-
NULL # used as a symbol, declare not an unbound variable
sort_criterion <-
NULL # used as a symbol, declare not an unbound variable
truthcol <- as.numeric(frame[[truthVar]])
predcol <- as.numeric(frame[[xvar]])
costcol <- as.numeric(frame[[costVar]])
# data frame of pred and truth, sorted in order of the predictions
d = data.frame(predcol = predcol,
truthcol = truthcol,
costcol = costcol)
n <- nrow(d)
predord = order(d[['predcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE) # reorder, with highest first
wizard = order(d[['truthcol']] / d[['costcol']],
sample.int(n, n, replace = FALSE),
decreasing = TRUE)
npop = dim(d)[1]
# data frame the cumulative prediction/truth as a function
# of the fraction of the population we're considering, highest first
mName = paste("model: sort by", xvar)
resultsM = data.frame(
pctpop = cumsum(d[predord, 'costcol']) / sum(d[['costcol']]),
pct_outcome = cumsum(d[predord, 'truthcol']) / sum(d[['truthcol']]),
sort_criterion = mName
)
wName = paste("wizard: sort by ", truthVar, '/', costVar)
resultsW = data.frame(
pctpop = cumsum(d[wizard, 'costcol']) / sum(d[['costcol']]),
pct_outcome = cumsum(d[wizard, 'truthcol']) / sum(d[['truthcol']]),
sort_criterion = wName
)
results = rbind(resultsM, resultsW, stringsAsFactors = FALSE)
# calculate the areas under each curve
# gini score is 2* (area - 0.5)
idealArea = areaCalc(resultsW$pctpop, resultsW$pct_outcome) - 0.5
modelArea = areaCalc(resultsM$pctpop, resultsM$pct_outcome) - 0.5
relGiniScore = modelArea / idealArea # actually, normalized gini score
# map names to colors
colorKey = c('model' = model_color, 'wizard' = wizard_color)
names(colorKey) = c(mName, wName)
modelKey = mName
pString <- ''
if (estimate_sig && requireNamespace('sigr', quietly = TRUE)) {
relativeGiniCostScorer <- makeRelativeGiniCostScorer(costcol)
sp <-
sigr::permutationScoreModel(predcol, truthcol, relativeGiniCostScorer)
pString <-
sigr::render(sigr::wrapSignificance(sp$pValue), format = 'ascii')
pString <-
paste0('\nalt. hyp.: relGini(',
xvar,
')>permuted relGini, ',
pString)
}
# cut down the number of points
results <- thin_frame_by_orders(results,
c("pctpop", "pct_outcome"),
"sort_criterion",
large_count)
# plot
gplot = ggplot2::ggplot(data = results) +
ggplot2::geom_point(
mapping = ggplot2::aes(
x = pctpop,
y = pct_outcome,
color = sort_criterion,
shape = sort_criterion
),
alpha = 0.5
) +
ggplot2::geom_line(
mapping = ggplot2::aes(
x = pctpop,
y = pct_outcome,
color = sort_criterion,
linetype = sort_criterion
)
) +
ggplot2::geom_abline(
color = "gray",
slope = 1,
intercept = 0
) +
ggplot2::geom_ribbon(
data = results[results$sort_criterion == modelKey, , drop = FALSE],
mapping = ggplot2::aes(
x = pctpop,
ymin = pctpop,
ymax = pct_outcome,
color = sort_criterion
),
alpha = 0.2,
color = NA,
fill = shadow_color
) +
ggplot2::ggtitle(
paste0(
title,
'\n',
truthVar,
'~',
xvar),
subtitle=paste0(
'Gini score: ',
format(modelArea, digits = 2),
', relative Gini score: ',
format(relGiniScore, digits = 2),
pString
)
) +
ggplot2::xlab(paste("fraction of sum", costVar, " in sort order")) +
ggplot2::ylab(paste("fraction total sum", truthVar)) +
ggplot2::scale_x_continuous(breaks = seq(0, 1, 0.1)) +
ggplot2::scale_y_continuous(breaks = seq(0, 1, 0.1)) +
ggplot2::scale_color_manual(values = colorKey) +
ggplot2::coord_fixed() +
ggplot2::theme(legend.position = "bottom")
gplot
}
# --------------------------------------------------------------
# find the y value that approximately corresponds to an x value on the gain curve
get_gainy = function(frame, xvar, truthVar, gainx) {
# The sort order for predicted salary, decreasing
n <- nrow(frame)
ord = order(frame[[xvar]],
sample.int(n, n, replace = FALSE),
decreasing = TRUE)
# top 25 predicted salaries
n = round(nrow(frame) * gainx)
topN = ord[1:n]
truth_topN = sum(frame[topN, truthVar])
totalY = sum(frame[[truthVar]])
truth_topN / totalY
}
#' Plot the cumulative gain curve of a sort-order with extra notation
#'
#' Plot the cumulative gain curve of a sort-order with extra notation.
#'
#' This is the standard gain curve plot (see \code{\link{GainCurvePlot}}) with
#' a label attached to a particular value of x. The label is created by
#' a function \code{labelfun}, which takes as inputs the x and y coordinates
#' of a label and returns a string (the label).
#'
#' By default, uses the model to calculate the y value of the calculated point;
#' to use the wizard curve, set \code{sort_by_model = FALSE}
#'
#' @param frame data frame to get values from
#' @param xvar name of the independent (input or model score) column in frame
#' @param truthVar name of the dependent (output or result to be modeled) column in frame
#' @param title title to place on plot
#' @param gainx the point on the x axis corresponding to the desired label
#' @param labelfun a function to return a label for the marked point
#' @param ... no unnamed argument, added to force named binding of later arguments.
#' @param sort_by_model logical, if TRUE use the model to calculate gainy, else use wizard.
#' @param estimate_sig logical, if TRUE compute significance
#' @param large_count numeric, upper bound target for number of plotting points
#' @param model_color color for the model curve
#' @param wizard_color color for the "wizard" (best possible) curve
#' @param shadow_color color for the shaded area under the curve
#' @param crosshair_color color for the annotation location lines
#' @param text_color color for the annotation text
#' @seealso \code{\link{GainCurvePlot}}
#'
#' @examples
#'
#' set.seed(34903490)
#' y = abs(rnorm(20)) + 0.1
#' x = abs(y + 0.5*rnorm(20))
#' frm = data.frame(model=x, value=y)
#' gainx = 0.25 # get the predicted top 25% most valuable points as sorted by the model
#' # make a function to calculate the label for the annotated point
#' labelfun = function(gx, gy) {
#' pctx = gx*100
#' pcty = gy*100
#'
#' paste("The predicted top ", pctx, "% most valuable points by the model\n",
#' "are ", pcty, "% of total actual value", sep='')
#' }
#' WVPlots::GainCurvePlotWithNotation(frm, "model", "value",
#' title="Example Gain Curve with annotation",
#' gainx=gainx,labelfun=labelfun)
#'
#' # now get the top 25% actual most valuable points
#'
#'labelfun = function(gx, gy) {
#' pctx = gx*100
#' pcty = gy*100
#'
#' paste("The actual top ", pctx, "% most valuable points\n",
#' "are ", pcty, "% of total actual value", sep='')
#' }
#'
#' WVPlots::GainCurvePlotWithNotation(frm, "model", "value",
#' title="Example Gain Curve with annotation",
#' gainx=gainx,labelfun=labelfun, sort_by_model=FALSE)
#'
#' @export
GainCurvePlotWithNotation = function(frame,
xvar,
truthVar,
title,
gainx,
labelfun,
...,
sort_by_model = TRUE,
estimate_sig = FALSE,
large_count = 1000,
model_color='darkblue',
wizard_color='darkgreen',
shadow_color='darkgray',
crosshair_color = 'red',
text_color='black') {
frame <- check_frame_args_list(...,
frame = frame,
name_var_list = list(xvar = xvar, truthVar = truthVar),
title = title,
funname = "WVPlots::GainCurvePlotWithNotation")
if(sort_by_model) {
gainy = get_gainy(frame, xvar, truthVar, gainx)
}
else {
gainy = get_gainy(frame, truthVar, truthVar, gainx)
}
gainy_p = round(100 * gainy) / 100 # two sig figs
label = labelfun(gainx, gainy_p)
gp = GainCurvePlot(frame, xvar, truthVar, title,
estimate_sig = estimate_sig,
large_count = large_count,
model_color = model_color,
wizard_color = wizard_color,
shadow_color = shadow_color) +
ggplot2::geom_vline(xintercept = gainx,
color = crosshair_color,
alpha = 0.5) +
ggplot2::geom_hline(yintercept = gainy,
color = crosshair_color,
alpha = 0.5) +
ggplot2::scale_shape_discrete(guide = FALSE) +
ggplot2::annotate(
geom = "text",
x = gainx + 0.01,
y = gainy - 0.01,
color = text_color,
label = label,
vjust = "top",
hjust = "left"
)
gp
}
#' Plot the cumulative gain curves of a sort-order.
#'
#' Plot the cumulative gain curves of a sort-order.
#'
#' The use case for this visualization is to compare a predictive model
#' score to an actual outcome (either binary (0/1) or continuous). In this case the
#' gain curve plot measures how well the model score sorts the data compared
#' to the true outcome value.
#'
#' The x-axis represents the fraction of items seen when sorted by score, and the
#' y-axis represents the gain seen so far (cumulative value of model over cummulative value of random selection)..
#'
#'
#'
#' @param frame data frame to get values from
#' @param xvars name of the independent (input or model score) columns in frame
#' @param truthVar name of the dependent (output or result to be modeled) column in frame
#' @param title title to place on plot
#' @param ... no unnamed argument, added to force named binding of later arguments.
#' @param truth_target if not NULL compare to this scalar value.
#' @param palette color palette for the model curves
#' @examples
#'
#' set.seed(34903490)
#' y = abs(rnorm(20)) + 0.1
#' x = abs(y + 0.5*rnorm(20))
#' frm = data.frame(model=x, value=y)
#' WVPlots::GainCurvePlotList(frm, c("model", "value"), "value",
#' title="Example Continuous gain Curves")
#'
#' @export
GainCurvePlotList = function(frame, xvars, truthVar, title,
...,
truth_target = NULL,
palette = 'Dark2') {
frame <- check_frame_args_list(...,
frame = frame,
name_var_list = c(xvars = xvars, truthVar = truthVar),
title = title,
funname = "WVPlots::GainCurvePlot")
curve <- percent_total <- NULL # mark as not unbound
pct_outcome <- pctpop <- sort_criterion <- NULL # mark as not unbound variables
if(!is.null(truth_target)) {
truthcol <- as.numeric(frame[[truthVar]]==truth_target)
} else {
truthcol <- as.numeric(frame[[truthVar]])
}
n <- nrow(frame)
# data frame the cumulative prediction/truth as a function
# of the fraction of the population we're considering, highest first
results <- data.frame(
pctpop = (1:n) / n
)
for(xvar in xvars) {
predcol <- as.numeric(frame[[xvar]])
# data frame of pred and truth, sorted in order of the predictions
d = data.frame(predcol = predcol, truthcol = truthcol)
predord <- order(d$predcol,
sample.int(n, n, replace = FALSE),
decreasing = TRUE) # reorder, with highest first
gain <- cumsum(d[predord, 'truthcol']) / sum(d[['truthcol']])
results[[xvar]] <- gain
}
# transform the frame into the tall form, for plotting
results <- cdata::pivot_to_blocks(results,
nameForNewKeyColumn = 'curve',
nameForNewValueColumn = 'percent_total',
columnsToTakeFrom = setdiff(colnames(results), 'pctpop'))
# plot
gplot = ggplot2::ggplot(
data = results,
mapping = ggplot2::aes(
x = pctpop,
y = percent_total,
color = curve)) +
ggplot2::geom_point(alpha = 0.5) +
ggplot2::geom_line() +
ggplot2::scale_color_brewer(palette = palette) +
ggplot2::xlab("fraction items in sort order") +
ggplot2::ylab("percent of total value") +
ggplot2::coord_fixed() +
ggplot2::theme(legend.position = "bottom")
gplot
}
#' @export
#' @rdname GainCurvePlotList
GainCurveListPlot <- GainCurvePlotList