/
blr-plots-data.R
195 lines (161 loc) · 4.75 KB
/
blr-plots-data.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
#' ROC curve data
#'
#' Data for generating ROC curve.
#'
#' @param gains_table An object of clas \code{blr_gains_table}
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#' gt <- blr_gains_table(model)
#' blr_prep_roc_data(gt)
#'
#' @export
#'
blr_prep_roc_data <- function(gains_table) {
d <- gains_table$gains_table[c('sensitivity', 'specificity')]
d$sensitivity_per <- d$sensitivity / 100
d$`1 - specificity` <- 1 - (d$specificity / 100)
d1 <- data.frame(NA, NA, 0, 0)
colnames(d1) <- c('sensitivity', 'specificity',
'sensitivity_per', '1 - specificity')
rbind(d1 , d)
}
#' Lorenz curve data
#'
#' Data for generating Lorenz curve.
#'
#' @param model An object of class \code{glm}.
#' @param data A \code{tibble} or \code{data.frame}.
#' @param test_data Logical; \code{TRUE} if data is test data and \code{FALSE} if training data.
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#' data <- model$data
#' blr_prep_lorenz_data(model, data, FALSE)
#'
#' @export
#'
blr_prep_lorenz_data <- function(model, data = NULL, test_data = FALSE) {
decile_count <- lorenz_decile_count(data)
x <- gains_table_prep(model, data, test_data)
y <- gains_table_modify(x, decile_count = decile_count)
z <- gains_table_mutate(y)
lorenz_plot_data(z)
}
lorenz_decile_count <- function(data) {
round(nrow(data) / 10)
}
lorenz_plot_data <- function(gains_table) {
d <- gains_table[c('cum_0s_%', 'cum_1s_%')]
d$cum_0s_per <- d$`cum_0s_%` / 100
d$cum_1s_per <- d$`cum_1s_%` / 100
d <- d[c('cum_0s_per', 'cum_1s_per')]
d1 <- data.frame(cum_0s_per = 0, cum_1s_per = 0)
d2 <- data.frame(cum_0s_per = 1, cum_1s_per = 1)
rbind(rbind(d1, d), d2)
}
#' Decile capture rate data
#'
#' Data for generating decile capture rate.
#'
#' @param gains_table An object of clas \code{blr_gains_table}
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#' gt <- blr_gains_table(model)
#' blr_prep_dcrate_data(gt)
#'
#' @export
#'
blr_prep_dcrate_data <- function(gains_table) {
d <- gains_table$gains_table[c('decile', 'total', '1')]
d$decile_mean <- d$`1` / d$total
return(d)
}
#' Lift Chart data
#'
#' Data for generating lift chart.
#'
#' @param gains_table An object of clas \code{blr_gains_table}.
#' @param global_mean Overall conversion rate.
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#' gt <- blr_gains_table(model)
#' globalmean <- blr_prep_lchart_gmean(gt)
#' blr_prep_lchart_data(gt, globalmean)
#'
#' @export
#'
blr_prep_lchart_gmean <- function(gains_table) {
d <- gains_table$gains_table[c('total', '1')]
d <- data.frame(lapply(d, sum))
d$X1 / d$total
}
#' @rdname blr_prep_lchart_gmean
#' @export
#'
blr_prep_lchart_data <- function(gains_table, global_mean) {
d <- gains_table$gains_table[c('decile', 'total', '1')]
d$decile_mean <- d$`1` / d$total
d$d_by_g_mean <- d$decile_mean / global_mean
return(d)
}
#' KS Chart data
#'
#' Data for generating KS chart.
#'
#' @param gains_table An object of clas \code{blr_gains_table}.
#' @param ks_line Overall conversion rate.
#'
#' @examples
#' model <- glm(honcomp ~ female + read + science, data = hsb2,
#' family = binomial(link = 'logit'))
#' gt <- blr_gains_table(model)
#' blr_prep_kschart_data(gt)
#' ks_line <- blr_prep_kschart_line(gt)
#' blr_prep_kschart_stat(ks_line)
#' blr_prep_ksannotate_y(ks_line)
#' blr_prep_ksannotate_x(ks_line)
#'
#' @export
#'
blr_prep_kschart_data <- function(gains_table) {
d <- gains_table$gains_table[c('cum_total_%', 'cum_1s_%', 'cum_0s_%')]
d$cum_total_per <- d$`cum_total_%` / 100
d$cum_1s_per <- d$`cum_1s_%` / 100
d$cum_0s_per <- d$`cum_0s_%` / 100
d <- d[c('cum_total_per', 'cum_1s_per', 'cum_0s_per')]
rbind(data.frame(cum_total_per = 0, cum_1s_per = 0, cum_0s_per = 0), d)
}
#' @rdname blr_prep_kschart_data
#' @export
#'
blr_prep_kschart_line <- function(gains_table) {
d <- gains_table$gains_table[c('cum_total_%', 'cum_1s_%', 'cum_0s_%', 'ks')]
d[d$ks == max(d$ks), ] / 100
}
#' @rdname blr_prep_kschart_data
#' @export
#'
blr_prep_ksannotate_y <- function(ks_line) {
ks_line$ann_loc <- (ks_line$`cum_1s_%` - ks_line$`cum_0s_%`) / 2
ks_line$ann_locate <- ks_line$`cum_0s_%` + ks_line$ann_loc
ks_line$ann_locate
}
#' @rdname blr_prep_kschart_data
#' @export
#'
blr_prep_kschart_stat <- function(ks_line) {
round(ks_line[[4]], 2) * 100
}
#' @rdname blr_prep_kschart_data
#' @export
#'
blr_prep_ksannotate_x <- function(ks_line) {
ks_line[[1]] + 0.1
}