-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrose.R
289 lines (270 loc) · 8.54 KB
/
rose.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
#' Apply ROSE Algorithm
#'
#' `step_rose()` creates a *specification* of a recipe step that generates
#' sample of synthetic data by enlarging the features space of minority and
#' majority class example. Using [ROSE::ROSE()].
#'
#' @inheritParams recipes::step_center
#' @inheritParams step_upsample
#' @param ... One or more selector functions to choose which
#' variable is used to sample the data. See [recipes::selections]
#' for more details. The selection should result in _single
#' factor variable_. For the `tidy` method, these are not
#' currently used.
#' @param role Not used by this step since no new variables are
#' created.
#' @param column A character string of the variable name that will
#' be populated (eventually) by the `...` selectors.
#' @param minority_prop A numeric. Determines the of over-sampling of the
#' minority class. Defaults to 0.5.
#' @param minority_smoothness A numeric. Shrink factor to be multiplied by the
#' smoothing parameters to estimate the conditional kernel density of the
#' minority class. Defaults to 1.
#' @param majority_smoothness A numeric. Shrink factor to be multiplied by the
#' smoothing parameters to estimate the conditional kernel density of the
#' majority class. Defaults to 1.
#' @param seed An integer that will be used as the seed when
#' rose-ing.
#' @return An updated version of `recipe` with the new step
#' added to the sequence of existing steps (if any). For the
#' `tidy` method, a tibble with columns `terms` which is
#' the variable used to sample.
#'
#' @details
#' The factor variable used to balance around must only have 2 levels.
#'
#' The ROSE algorithm works by selecting an observation belonging to class k
#' and generates new examples in its neighborhood is determined by some matrix
#' H_k. Smaller values of these arguments have the effect of shrinking the
#' entries of the corresponding smoothing matrix H_k, Shrinking would be a
#' cautious choice if there is a concern that excessively large neighborhoods
#' could lead to blur the boundaries between the regions of the feature space
#' associated with each class.
#'
#' All columns in the data are sampled and returned by [recipes::juice()]
#' and [recipes::bake()].
#'
#' When used in modeling, users should strongly consider using the
#' option `skip = TRUE` so that the extra sampling is _not_
#' conducted outside of the training set.
#'
#' # Tidying
#'
#' When you [`tidy()`][recipes::tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
#' }
#'
#' ```{r, echo = FALSE, results="asis"}
#' step <- "step_rose"
#' result <- knitr::knit_child("man/rmd/tunable-args.Rmd")
#' cat(result)
#' ```
#'
#' @template case-weights-not-supported
#'
#' @references Lunardon, N., Menardi, G., and Torelli, N. (2014). ROSE: a
#' Package for Binary Imbalanced Learning. R Jorunal, 6:82–92.
#' @references Menardi, G. and Torelli, N. (2014). Training and assessing
#' classification rules with imbalanced data. Data Mining and Knowledge
#' Discovery, 28:92–122.
#'
#' @family Steps for over-sampling
#'
#' @export
#' @examplesIf rlang::is_installed("modeldata")
#' library(recipes)
#' library(modeldata)
#' data(hpc_data)
#'
#' hpc_data0 <- hpc_data %>%
#' mutate(class = factor(class == "VF", labels = c("not VF", "VF"))) %>%
#' select(-protocol, -day)
#'
#' orig <- count(hpc_data0, class, name = "orig")
#' orig
#'
#' up_rec <- recipe(class ~ ., data = hpc_data0) %>%
#' step_rose(class) %>%
#' prep()
#'
#' training <- up_rec %>%
#' bake(new_data = NULL) %>%
#' count(class, name = "training")
#' training
#'
#' # Since `skip` defaults to TRUE, baking the step has no effect
#' baked <- up_rec %>%
#' bake(new_data = hpc_data0) %>%
#' count(class, name = "baked")
#' baked
#'
#' orig %>%
#' left_join(training, by = "class") %>%
#' left_join(baked, by = "class")
#'
#' library(ggplot2)
#'
#' ggplot(circle_example, aes(x, y, color = class)) +
#' geom_point() +
#' labs(title = "Without ROSE")
#'
#' recipe(class ~ x + y, data = circle_example) %>%
#' step_rose(class) %>%
#' prep() %>%
#' bake(new_data = NULL) %>%
#' ggplot(aes(x, y, color = class)) +
#' geom_point() +
#' labs(title = "With ROSE")
step_rose <-
function(recipe, ..., role = NA, trained = FALSE,
column = NULL, over_ratio = 1, minority_prop = 0.5,
minority_smoothness = 1, majority_smoothness = 1, skip = TRUE,
seed = sample.int(10^5, 1), id = rand_id("rose")) {
check_number_decimal(minority_prop, min = 0)
check_number_decimal(minority_smoothness, min = 0)
check_number_decimal(majority_smoothness, min = 0)
check_number_whole(seed)
add_step(
recipe,
step_rose_new(
terms = enquos(...),
role = role,
trained = trained,
column = column,
over_ratio = over_ratio,
minority_prop = minority_prop,
minority_smoothness = minority_smoothness,
majority_smoothness = majority_smoothness,
predictors = NULL,
skip = skip,
seed = seed,
id = id
)
)
}
step_rose_new <-
function(terms, role, trained, column, over_ratio, minority_prop,
minority_smoothness, majority_smoothness, predictors, skip, seed,
id) {
step(
subclass = "rose",
terms = terms,
role = role,
trained = trained,
column = column,
over_ratio = over_ratio,
minority_prop = minority_prop,
minority_smoothness = minority_smoothness,
majority_smoothness = majority_smoothness,
predictors = predictors,
skip = skip,
seed = seed,
id = id
)
}
#' @export
prep.step_rose <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
check_number_decimal(x$over_ratio, arg = "over_ratio", min = 0)
check_1_selected(col_name)
check_column_factor(training, col_name)
check_2_levels_only(training, col_name)
predictors <- setdiff(get_from_info(info, "predictor"), col_name)
check_na(select(training, all_of(col_name)))
step_rose_new(
terms = x$terms,
role = x$role,
trained = TRUE,
column = col_name,
over_ratio = x$over_ratio,
minority_prop = x$minority_prop,
minority_smoothness = x$minority_smoothness,
majority_smoothness = x$majority_smoothness,
predictors = predictors,
skip = x$skip,
seed = x$seed,
id = x$id
)
}
#' @export
bake.step_rose <- function(object, new_data, ...) {
col_names <- unique(c(object$predictors, object$column))
check_new_data(col_names, object, new_data)
if (length(object$column) == 0L) {
# Empty selection
return(new_data)
}
if (any(is.na(new_data[[object$column]]))) {
missing <- new_data[is.na(new_data[[object$column]]), ]
} else {
missing <- NULL
}
new_data <- as.data.frame(new_data)
predictor_data <- new_data[, col_names]
# rose with seed for reproducibility
majority_size <- max(table(predictor_data[[object$column]])) * 2
with_seed(
seed = object$seed,
code = {
original_levels <- levels(predictor_data[[object$column]])
synthetic_data <- ROSE(
string2formula(object$column),
predictor_data,
N = floor(majority_size * object$over_ratio),
p = object$minority_prop,
hmult.majo = object$majority_smoothness,
hmult.mino = object$minority_smoothness
)
synthetic_data <- synthetic_data$data
synthetic_data[[object$column]] <- factor(
synthetic_data[[object$column]],
levels = original_levels
)
synthetic_data <- as_tibble(synthetic_data)
}
)
new_data <- na_splice(new_data, synthetic_data, object)
new_data
}
#' @export
print.step_rose <-
function(x, width = max(20, options()$width - 26), ...) {
title <- "ROSE based on "
print_step(x$column, x$terms, x$trained, title, width)
invisible(x)
}
#' @rdname step_rose
#' @usage NULL
#' @export
tidy.step_rose <- function(x, ...) {
if (is_trained(x)) {
res <- tibble(terms = unname(x$column))
} else {
term_names <- sel2char(x$terms)
res <- tibble(terms = unname(term_names))
}
res$id <- x$id
res
}
#' @export
#' @rdname tunable_themis
tunable.step_rose <- function(x, ...) {
tibble::tibble(
name = c("over_ratio"),
call_info = list(
list(pkg = "dials", fun = "over_ratio")
),
source = "recipe",
component = "step_rose",
component_id = x$id
)
}
#' @rdname required_pkgs.step
#' @export
required_pkgs.step_rose <- function(x, ...) {
c("themis", "ROSE")
}