| step_rose | R Documentation |
step_rose() creates a specification of a recipe step that generates
samples of synthetic data by enlarging the feature space of minority and
majority class examples. Using ROSE::ROSE().
step_rose(
recipe,
...,
role = NA,
trained = FALSE,
column = NULL,
over_ratio = 1,
minority_prop = 0.5,
minority_smoothness = 1,
majority_smoothness = 1,
indicator_column = NULL,
skip = TRUE,
seed = sample.int(10^5, 1),
id = rand_id("rose")
)
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
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 |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the total size of the synthetic data relative to twice the size of the majority class. Unlike the other over-sampling steps this is not a per-class target, so a named vector of ratios is not accepted here. |
minority_prop |
A numeric value between 0 and 1 for the proportion of
synthetic observations from the minority class. Defaults to 0.5, which
generates an equal split of minority and majority synthetic observations.
This parameter controls the class balance within the synthetic data,
while |
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. |
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. |
indicator_column |
A single string or |
skip |
A logical. Should the step be skipped when the recipe is baked by
|
seed |
An integer that will be used as the seed when applied. |
id |
A character string that is unique to this step to identify it. |
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 generating new examples in its neighborhood, which is determined by a
smoothing matrix H_k. Smaller values of minority_smoothness and
majority_smoothness shrink the entries of H_k, producing tighter
neighborhoods. This is a cautious choice when there is a concern that
excessively large neighborhoods could blur the boundaries between classes.
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.
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.
When you tidy() this step, a tibble is returned with
columns terms and id:
character, the selectors or variables selected
character, id of this step
This step has 1 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)
The underlying operation does not allow for case weights. Supplying data with a case weights column to this step results in an error.
Lunardon, N., Menardi, G., and Torelli, N. (2014). ROSE: a Package for Binary Imbalanced Learning. R Journal, 6:79–89.
Menardi, G. and Torelli, N. (2014). Training and assessing classification rules with imbalanced data. Data Mining and Knowledge Discovery, 28:92–122.
rose() for direct implementation
Other Steps for over-sampling:
step_adasyn(),
step_bsmote(),
step_kmeans_smote(),
step_smogn(),
step_smote(),
step_smoten(),
step_smotenc(),
step_svmsmote(),
step_upsample()
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.