/
binarize.R
356 lines (283 loc) · 11.6 KB
/
binarize.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
#' Turn data with numeric, categorical features into binary data.
#'
#' \code{binarize} returns the binary data coverted from data in normal (numeric and categorical) format.
#'
#'
#' @param data A `tibble` or `data.frame`
#' @param n_bins The number of bins to for converting continuous (numeric features) into discrete features (bins)
#' @param thresh_infreq The threshold for converting categorical (character or factor features) into an "Other" Category.
#' @param name_infreq The name for infrequently appearing categories to be lumped into. Set to "-OTHER" by default.
#' @param one_hot If set to `TRUE`, binarization returns number of new columns = number of levels.
#' If `FALSE`, binarization returns number of new columns = number of levels - 1 (dummy encoding).
#'
#' @return A `tbl`
#'
#' @details
#' ## The Goal
#' The binned format helps correlation analysis to identify non-linear trends between a predictor (binned values) and a
#' response (the target)
#'
#' ## What Binarize Does
#' The `binarize()` function takes data in a "normal" format and converts to a binary format that is useful as a preparation
#' step before using [`correlate()`]:
#'
#' __Numeric Features__:
#' The "Normal Data" format has numeric features that are continuous values in numeric format (`double` or `integer`).
#' The `binarize()` function converts these to bins (categories) and then discretizes the bins using a one-hot encoding process.
#'
#' __Categorical Features__:
#' The "Normal Data" format has categorical features that are `character` or `factor` format.
#' The `binarize()` function converts these to binary features using a one-hot encoding process.
#'
#' @examples
#' library(dplyr)
#' library(correlationfunnel)
#'
#' marketing_campaign_tbl %>%
#' select(-ID) %>%
#' binarize()
#'
#'
#' @importFrom recipes "all_nominal" "all_numeric" "all_predictors"
#'
#' @export
binarize <- function(data, n_bins = 4, thresh_infreq = 0.01, name_infreq = "-OTHER", one_hot = TRUE) {
UseMethod("binarize", data)
}
#' @export
binarize.default <- function(data, n_bins = 4, thresh_infreq = 0.01, name_infreq = "-OTHER", one_hot = TRUE) {
stop("Error binarize(): Object is not of class `data.frame`.", call. = FALSE)
}
#' @export
binarize.data.frame <- function(data, n_bins = 4, thresh_infreq = 0.01, name_infreq = "-OTHER", one_hot = TRUE) {
# CHECKS ----
# Check data is charater, factor, or numeric
check_data_type(data,
classes_allowed = c("numeric", "integer", "logical", "character", "factor", "ordered"),
.fun_name = "binarize")
# Check missing
check_missing(data, .fun_name = "binarize")
# FIXES ----
# # Find & Isolate Binary Data
# binary_split <- split_binary(data)
# data <- binary_split[["not_binary_data"]]
# data_binary <- binary_split[["binary_data"]]
# Convert logical to integer
data <- logical_to_integer(data)
# NON-BINARY DATA ----
if (ncol(data) > 0) {
# Check & fix numeric factors
# - Numeric values with low cardinality (few unique) treated as categorical
data <- fix_low_cardinality_numeric(data, thresh = n_bins + 3)
# Check & fix skewed data
# - Highly skewed data with quantile values of 4 of 5 are same
# will be converted to factor
data <- fix_high_skew_numeric_data(data, n_bins = n_bins, unique_limit = 2)
# TRANSFORMATION STEPS ----
recipe_obj <- create_recipe(
data = data,
n_bins = n_bins,
thresh_infreq = thresh_infreq,
name_infreq = name_infreq,
one_hot = one_hot)
data_transformed_tbl <- data %>%
recipes::bake(recipe_obj, new_data = .)
# HANDLE COLUMN NAMES ----
num_count <- data %>% purrr::map_lgl(is.numeric) %>% sum()
if (num_count > 0) {
data_transformed_tbl <- handle_binned_names(
data = data_transformed_tbl,
recipe = recipe_obj)
}
data <- data_transformed_tbl
}
## RECOMBINE ----
#data <- dplyr::bind_cols(data_binary, data)
return(data)
}
# SUPPORTING FUNCTIONS -----
# Checks for missing values
check_data_type <- function(data, classes_allowed = c("numeric", "integer", "character", "factor", "ordered"), .fun_name = NULL) {
class_summary_tbl <- data %>%
purrr::map_df(~ class(.)[[1]]) %>%
tidyr::gather(key = "feature", value = "class") %>%
dplyr::mutate(unacceptable_class = !(class %in% classes_allowed))
if (sum(class_summary_tbl$unacceptable_class) > 0) {
columns_with_unnacceptable_classes <- class_summary_tbl %>%
dplyr::filter(unacceptable_class > 0) %>%
dplyr::pull(feature)
msg1 <- paste0(.fun_name, "(): ")
if (.fun_name == "binarize") {
bad_types <- "non-numeric or non-categorical"
} else if (.fun_name == "correlate") {
bad_types <- "non-numeric"
} else {
bad_types <- "bad"
}
msg2 <- stringr::str_glue("[Unnacceptable Columns Detected] The following columns contain {bad_types} data types: ")
msg3 <- paste0(columns_with_unnacceptable_classes, collapse = ", ")
msg <- paste0(msg1, msg2, msg3)
stop(msg, call. = FALSE)
}
}
# Checks for missing values
check_missing <- function(data, .fun_name = NULL) {
missing_summary_tbl <- data %>%
purrr::map_df(~ sum(is.na(.))) %>%
tidyr::gather(key = "feature", value = "count_na") %>%
dplyr::arrange(dplyr::desc(count_na))
if (sum(missing_summary_tbl$count_na) > 0) {
columns_with_na_values <- missing_summary_tbl %>%
dplyr::filter(count_na > 0) %>%
dplyr::pull(feature)
msg1 <- paste0(.fun_name, "(): ")
msg2 <- "[Missing Values Detected] The following columns contain NAs: "
msg3 <- paste0(columns_with_na_values, collapse = ", ")
msg <- paste0(msg1, msg2, msg3)
stop(msg, call. = FALSE)
}
}
# Checks and isolates any binary data
# split_binary <- function(data){
#
# # Format logical data as numeric
# data <- data %>%
# dplyr::mutate_if(is.logical, as.numeric)
#
# binary_data <- data %>%
# dplyr::select_if(is.binary)
#
# not_binary_data <- data %>%
# dplyr::select_if(~ !is.binary(.))
#
# return(list("binary_data" = binary_data, "not_binary_data" = not_binary_data))
#
# }
# Checks & Fixes Numeric Data that Should be factor (categorical)
fix_low_cardinality_numeric <- function(data, thresh = 6, .fun_name = NULL) {
num_class <- data %>% purrr::map_lgl(is.numeric)
if (any(num_class)) {
numeric_check_tbl <- data %>%
dplyr::select_if(is.numeric) %>%
dplyr::summarise_all(~ length(unique(.))) %>%
tidyr::gather() %>%
dplyr::arrange(dplyr::desc(value)) %>%
dplyr::mutate(check = dplyr::case_when(
value == 1 ~ "Zero Variance",
value < thresh ~ "Possible Factor",
TRUE ~ "Pass"
))
cols_num_to_factor <- numeric_check_tbl %>%
dplyr::filter(check == "Possible Factor") %>%
dplyr::pull(key)
if (length(cols_num_to_factor) > 0) {
data <- data %>%
dplyr::mutate_at(.vars = dplyr::vars(cols_num_to_factor), .funs = as.factor)
}
}
return(data)
}
# Checks and fixes numeric data with high skew
fix_high_skew_numeric_data <- function(data, n_bins, unique_limit) {
num_class <- data %>% purrr::map_lgl(is.numeric)
if (any(num_class)) {
# Inspect feature distributions
column_quantiles_tbl <- data %>%
dplyr::select_if(is.numeric) %>%
purrr::map_dfc(stats::quantile)
# Count unique quantiles
column_unique_count_tbl <- column_quantiles_tbl %>%
purrr::map_df(~ length(unique(.))) %>%
tidyr::gather(key = "feature", value = "count_unique_quantile")
# If less than three, convert to factor
cols_num_to_factor <- column_unique_count_tbl %>%
dplyr::filter(count_unique_quantile <= unique_limit) %>%
dplyr::pull(feature)
if (length(cols_num_to_factor) > 0) {
data <- data %>%
dplyr::mutate_at(.vars = dplyr::vars(cols_num_to_factor), .funs = ~ as.factor(.))
}
}
return(data)
}
create_recipe <- function(data, n_bins, thresh_infreq, name_infreq, one_hot) {
# Compute Binary Data
num_count <- data %>% purrr::map_lgl(is.numeric) %>% sum()
cat_count <- (data %>% purrr::map_lgl(is.character) %>% sum()) +
(data %>% purrr::map_lgl(is.factor) %>% sum())
# Remove zero variance variables
recipe_obj <- recipes::recipe(~ ., data = data) %>%
recipes::step_zv(all_predictors())
# Reduce cardinality of infrequent categorical levels
if (cat_count > 0) {
if (thresh_infreq == 0) thresh_infreq <- 1e-9 # Resolves error on thresh_infreq = 0
recipe_obj <- recipe_obj %>%
recipes::step_other(
all_nominal(),
threshold = thresh_infreq,
other = name_infreq)
}
# Convert continuous features to binned features
if (num_count > 0) {
recipe_obj <- recipe_obj %>%
recipes::step_discretize(
all_numeric(),
options = list(
cuts = n_bins,
min_unique = 1)
)
}
# Convert categorical and binned features to binary features
name_system <- function(var, lvl, ordinal = FALSE, sep = "__") {
if (!ordinal) {
lvl <- lvl %>%
stringr::str_trim() %>%
stringr::str_replace_all(" ", "_")
nms <- paste(var, lvl, sep = sep)
} else {
nms <- paste0(var, recipes::names0(length(lvl), sep))
}
nms
}
recipe_obj <- recipe_obj %>%
recipes::step_dummy(
all_nominal(),
one_hot = one_hot,
# naming = purrr::partial(recipes::dummy_names, sep = "__")
naming = name_system
) %>%
# Drop any features that have no variance
recipes::step_zv(all_predictors())
suppressWarnings(recipe_obj <- recipes::prep(recipe_obj))
return(recipe_obj)
}
# Handle Numeric Bin Names
handle_binned_names <- function(data, recipe) {
suppressWarnings({
bin_labels_tbl <- recipes::tidy(recipe) %>%
dplyr::filter(type == "discretize") %>%
dplyr::pull(number) %>%
# Get binary name labels
recipes::tidy(recipe, .) %>%
dplyr::group_by(terms) %>%
dplyr::mutate(value_lead = dplyr::lead(value, n = 1)) %>%
dplyr::slice(-dplyr::n()) %>%
dplyr::mutate(bin_label = stringr::str_glue("{value}_{value_lead}")) %>%
dplyr::select(-id, -dplyr::contains("value")) %>%
dplyr::mutate(bin = 1:(dplyr::n()) ) %>%
dplyr::mutate(label_current = stringr::str_glue("{terms}__bin{bin}")) %>%
dplyr::mutate(label_new = stringr::str_glue("{terms}__{bin_label}"))
new_names_tbl <- tibble::tibble(label_current = names(data)) %>%
dplyr::left_join(bin_labels_tbl, by = "label_current") %>%
dplyr::mutate(label_new = dplyr::case_when(
is.na(label_new) ~ label_current,
TRUE ~ label_new)) %>%
dplyr::select(label_current, label_new)
names(data) <- new_names_tbl$label_new
})
return(data)
}
logical_to_integer <- function(data) {
data %>%
dplyr::mutate_if(is.logical, as.integer)
}