| Title: | Extra Recipes Steps for Dealing with Unbalanced Data |
| Version: | 1.0.3 |
| Description: | A dataset with an uneven number of cases in each class is said to be unbalanced. Many models produce a subpar performance on unbalanced datasets. A dataset can be balanced by increasing the number of minority cases using SMOTE 2011 <doi:10.48550/arXiv.1106.1813>, BorderlineSMOTE 2005 <doi:10.1007/11538059_91> and ADASYN 2008https://ieeexplore.ieee.org/document/4633969. Or by decreasing the number of majority cases using NearMiss 2003https://www.site.uottawa.ca/~nat/Workshop2003/jzhang.pdf or Tomek link removal 1976https://ieeexplore.ieee.org/document/4309452. |
| License: | MIT + file LICENSE |
| URL: | https://github.com/tidymodels/themis,https://themis.tidymodels.org |
| BugReports: | https://github.com/tidymodels/themis/issues |
| Depends: | R (≥ 3.6), recipes (≥ 1.1.0) |
| Imports: | cli, gower, lifecycle (≥ 1.0.3), dplyr, generics (≥ 0.1.0),purrr, RANN, rlang (≥ 1.1.0), ROSE, tibble, withr, glue,hardhat, vctrs |
| Suggests: | covr, dials (≥ 1.2.0), ggplot2, modeldata, testthat (≥3.0.0) |
| Config/Needs/website: | tidyverse/tidytemplate |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| LazyData: | true |
| RoxygenNote: | 7.3.2 |
| NeedsCompilation: | no |
| Packaged: | 2025-01-22 23:40:53 UTC; emilhvitfeldt |
| Author: | Emil Hvitfeldt |
| Maintainer: | Emil Hvitfeldt <emil.hvitfeldt@posit.co> |
| Repository: | CRAN |
| Date/Publication: | 2025-01-23 00:10:02 UTC |
themis: Extra Recipes Steps for Dealing with Unbalanced Data
Description

A dataset with an uneven number of cases in each class is said to be unbalanced. Many models produce a subpar performance on unbalanced datasets. A dataset can be balanced by increasing the number of minority cases using SMOTE 2011arXiv:1106.1813, BorderlineSMOTE 2005doi:10.1007/11538059_91 and ADASYN 2008https://ieeexplore.ieee.org/document/4633969. Or by decreasing the number of majority cases using NearMiss 2003https://www.site.uottawa.ca/~nat/Workshop2003/jzhang.pdf or Tomek link removal 1976https://ieeexplore.ieee.org/document/4309452.
Author(s)
Maintainer: Emil Hvitfeldtemil.hvitfeldt@posit.co (ORCID)
Other contributors:
Posit Software, PBC [copyright holder, funder]
See Also
Useful links:
Report bugs athttps://github.com/tidymodels/themis/issues
Adaptive Synthetic Algorithm
Description
Generates synthetic positive instances using ADASYN algorithm.
Usage
adasyn(df, var, k = 5, over_ratio = 1)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
k | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
Details
All columns used in this function must be numeric with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer,W. P. (2002). Smote: Synthetic minority over-sampling technique.Journal of Artificial Intelligence Research, 16:321-357.
See Also
step_adasyn() for step function of this method
Other Direct Implementations:bsmote(),nearmiss(),smote(),smotenc(),tomek()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- adasyn(circle_numeric, var = "class")res <- adasyn(circle_numeric, var = "class", k = 10)res <- adasyn(circle_numeric, var = "class", over_ratio = 0.8)borderline-SMOTE Algorithm
Description
BSMOTE generates generate new examples of the minority class using nearestneighbors of these cases in the border region between classes.
Usage
bsmote(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
k | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
all_neighbors | Type of two borderline-SMOTE method. Defaults to FALSE.See details. |
Details
This methods works the same way assmote(), expect that instead ofgenerating points around every point of of the minority class each point isfirst being classified into the boxes "danger" and "not". For each point thek nearest neighbors is calculated. If all the neighbors comes from adifferent class it is labeled noise and put in to the "not" box. If more thenhalf of the neighbors comes from a different class it is labeled "danger.
Ifall_neighbors = FALSE then points will be generated between nearestneighbors in its own class. Ifall_neighbors = TRUE then points will begenerated between any nearest neighbors. See examples for visualization.
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.
All columns used in this step must be numeric with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Hui Han, Wen-Yuan Wang, and Bing-Huan Mao. Borderline-smote:a new over-sampling method in imbalanced data sets learning. InInternational Conference on Intelligent Computing, pages 878–887. Springer,2005.
See Also
step_bsmote() for step function of this method
Other Direct Implementations:adasyn(),nearmiss(),smote(),smotenc(),tomek()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- bsmote(circle_numeric, var = "class")res <- bsmote(circle_numeric, var = "class", k = 10)res <- bsmote(circle_numeric, var = "class", over_ratio = 0.8)res <- bsmote(circle_numeric, var = "class", all_neighbors = TRUE)Synthetic Dataset With a Circle
Description
A random dataset with two classes one of which is inside a circle. Used forexamples to show how the different methods handles borders.
Usage
circle_exampleFormat
A data frame with 200 rows and 4 variables:
- x
Numeric.
- y
Numeric.
- class
Factor, values "Circle" and "Rest".
- id
character, ID variable.
Remove Points Near Other Classes
Description
Generates synthetic positive instances using nearmiss algorithm.
Usage
nearmiss(df, var, k = 5, under_ratio = 1)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
k | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
under_ratio | A numeric value for the ratio of themajority-to-minority frequencies. The default value (1) meansthat all other levels are sampled down to have the samefrequency as the least occurring level. A value of 2 would meanthat the majority levels will have (at most) (approximately)twice as many rows than the minority level. |
Details
All columns used in this function must be numeric with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Inderjeet Mani and I Zhang. knn approach to unbalanced datadistributions: a case study involving information extraction. In Proceedingsof workshop on learning from imbalanced datasets, 2003.
See Also
step_nearmiss() for step function of this method
Other Direct Implementations:adasyn(),bsmote(),smote(),smotenc(),tomek()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- nearmiss(circle_numeric, var = "class")res <- nearmiss(circle_numeric, var = "class", k = 10)res <- nearmiss(circle_numeric, var = "class", under_ratio = 1.5)Objects exported from other packages
Description
These objects are imported from other packages. Follow the linksbelow to see their documentation.
- generics
S3 methods for tracking which additional packages are needed for steps.
Description
S3 methods for tracking which additional packages are needed for steps.
Usage
## S3 method for class 'step_adasyn'required_pkgs(x, ...)## S3 method for class 'step_bsmote'required_pkgs(x, ...)## S3 method for class 'step_downsample'required_pkgs(x, ...)## S3 method for class 'step_nearmiss'required_pkgs(x, ...)## S3 method for class 'step_rose'required_pkgs(x, ...)## S3 method for class 'step_smote'required_pkgs(x, ...)## S3 method for class 'step_smotenc'required_pkgs(x, ...)## S3 method for class 'step_tomek'required_pkgs(x, ...)## S3 method for class 'step_upsample'required_pkgs(x, ...)Arguments
x | A recipe step |
Value
A character vector
SMOTE Algorithm
Description
SMOTE generates new examples of the minority class using nearest neighborsof these cases.
Usage
smote(df, var, k = 5, over_ratio = 1)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
k | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
Details
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.All columns used in this function must be numeric with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer,W. P. (2002). Smote: Synthetic minority over-sampling technique.Journal of Artificial Intelligence Research, 16:321-357.
See Also
step_smote() for step function of this method
Other Direct Implementations:adasyn(),bsmote(),nearmiss(),smotenc(),tomek()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- smote(circle_numeric, var = "class")res <- smote(circle_numeric, var = "class", k = 10)res <- smote(circle_numeric, var = "class", over_ratio = 0.8)SMOTENC Algorithm
Description
SMOTENC generates new examples of the minority class using nearest neighborsof these cases, and can handle categorical variables
Usage
smotenc(df, var, k = 5, over_ratio = 1)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
k | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
Details
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.Columns can be numeric and categorical with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer,W. P. (2002). Smote: Synthetic minority over-sampling technique.Journal of Artificial Intelligence Research, 16:321-357.
See Also
step_smotenc() for step function of this method
Other Direct Implementations:adasyn(),bsmote(),nearmiss(),smote(),tomek()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- smotenc(circle_numeric, var = "class")res <- smotenc(circle_numeric, var = "class", k = 10)res <- smotenc(circle_numeric, var = "class", over_ratio = 0.8)Apply Adaptive Synthetic Algorithm
Description
step_adasyn() creates aspecification of a recipe step that generatessynthetic positive instances using ADASYN algorithm.
Usage
step_adasyn( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("adasyn"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
neighbors | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whenapplied. |
id | A character string that is unique to this step to identify it. |
Details
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 2 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)neighbors: # Nearest Neighbors (type: integer, default: 5)
Case weights
The underlying operation does not allow for case weights.
References
He, H., Bai, Y., Garcia, E. and Li, S. 2008. ADASYN: Adaptivesynthetic sampling approach for imbalanced learning. Proceedings ofIJCNN 2008. (IEEE World Congress on Computational Intelligence). IEEEInternational Joint Conference. pp.1322-1328.
See Also
adasyn() for direct implementation
Other Steps for over-sampling:step_bsmote(),step_rose(),step_smote(),step_smotenc(),step_upsample()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_adasyn(class, over_ratio = 0.4523) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 ADASYN")recipe(class ~ x + y, data = circle_example) %>% step_adasyn(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With ADASYN")Apply borderline-SMOTE Algorithm
Description
step_bsmote() creates aspecification of a recipe step that generate newexamples of the minority class using nearest neighbors of these cases in theborder region between classes.
Usage
step_bsmote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, all_neighbors = FALSE, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("bsmote"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
neighbors | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
all_neighbors | Type of two borderline-SMOTE method. Defaults to FALSE.See details. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whensmote-ing. |
id | A character string that is unique to this step to identify it. |
Details
This methods works the same way asstep_smote(), expect that instead ofgenerating points around every point of of the minority class each point isfirst being classified into the boxes "danger" and "not". For each point thek nearest neighbors is calculated. If all the neighbors comes from adifferent class it is labeled noise and put in to the "not" box. If more thenhalf of the neighbors comes from a different class it is labeled "danger.
If all_neighbors = FALSE then points will be generated between nearestneighbors in its own class. If all_neighbors = TRUE then points will begenerated between any nearest neighbors. See examples for visualization.
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 3 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)neighbors: # Nearest Neighbors (type: integer, default: 5)all_neighbors: All Neighbors (type: logical, default: FALSE)
Case weights
The underlying operation does not allow for case weights.
References
Hui Han, Wen-Yuan Wang, and Bing-Huan Mao. Borderline-smote:a new over-sampling method in imbalanced data sets learning. InInternational Conference on Intelligent Computing, pages 878–887. Springer,2005.
See Also
bsmote() for direct implementation
Other Steps for over-sampling:step_adasyn(),step_rose(),step_smote(),step_smotenc(),step_upsample()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_bsmote(class, over_ratio = 0.4523) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 SMOTE")recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = FALSE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = FALSE")recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = TRUE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = TRUE")Down-Sample a Data Set Based on a Factor Variable
Description
step_downsample() creates aspecification of a recipe step that willremove rows of a data set to make the occurrence of levels in a specificfactor level equal.
Usage
step_downsample( recipe, ..., under_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("downsample"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
under_ratio | A numeric value for the ratio of themajority-to-minority frequencies. The default value (1) meansthat all other levels are sampled down to have the samefrequency as the least occurring level. A value of 2 would meanthat the majority levels will have (at most) (approximately)twice as many rows than the minority level. |
ratio | Deprecated argument; same as |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
target | An integer that will be used to subsample. Thisshould not be set by the user and will be populated by |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed when downsampling. |
id | A character string that is unique to this step to identify it. |
Details
Down-sampling is intended to be performed on thetraining setalone. For this reason, the default isskip = TRUE.
If there are missing values in the factor variable that is usedto define the sampling, missing data are selected at random inthe same way that the other factor levels are sampled. Missingvalues are not used to determine the amount of data in theminority level
For any data with factor levels occurring with the samefrequency as the minority level, all data will be retained.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
Keep in mind that the location of down-sampling in the stepmay have effects. For example, if centering and scaling,it is not clear whether those operations should be conductedbefore orafter rows are removed.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 1 tuning parameters:
under_ratio: Under-Sampling Ratio (type: double, default: 1)
Case weights
This step performs an unsupervised operation that can utilize case weights.To use them, see the documentation inrecipes::case_weights and the examples ontidymodels.org.
See Also
Other Steps for under-sampling:step_nearmiss(),step_tomek()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_downsample(class, under_ratio = 3.862) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 downsample")recipe(class ~ x + y, data = circle_example) %>% step_downsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With downsample")Remove Points Near Other Classes
Description
step_nearmiss() creates aspecification of a recipe step that removesmajority class instances by undersampling points in the majority class basedon their distance to other points in the same class.
Usage
step_nearmiss( recipe, ..., role = NA, trained = FALSE, column = NULL, under_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("nearmiss"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
under_ratio | A numeric value for the ratio of themajority-to-minority frequencies. The default value (1) meansthat all other levels are sampled down to have the samefrequency as the least occurring level. A value of 2 would meanthat the majority levels will have (at most) (approximately)twice as many rows than the minority level. |
neighbors | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whenapplied. |
id | A character string that is unique to this step to identify it. |
Details
This method retains the points from the majority class which have thesmallest mean distance to the k nearest points in the minority class.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 2 tuning parameters:
under_ratio: Under-Sampling Ratio (type: double, default: 1)neighbors: # Nearest Neighbors (type: integer, default: 5)
Case weights
The underlying operation does not allow for case weights.
References
Inderjeet Mani and I Zhang. knn approach to unbalanced datadistributions: a case study involving information extraction. In Proceedingsof workshop on learning from imbalanced datasets, 2003.
See Also
nearmiss() for direct implementation
Other Steps for under-sampling:step_downsample(),step_tomek()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_nearmiss(class, under_ratio = 3.862) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15))recipe(class ~ x + y, data = circle_example) %>% step_nearmiss(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15))Apply ROSE Algorithm
Description
step_rose() creates aspecification of a recipe step that generatessample of synthetic data by enlarging the features space of minority andmajority class example. UsingROSE::ROSE().
Usage
step_rose( 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"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
minority_prop | A numeric. Determines the of over-sampling of theminority class. Defaults to 0.5. |
minority_smoothness | A numeric. Shrink factor to be multiplied by thesmoothing parameters to estimate the conditional kernel density of theminority class. Defaults to 1. |
majority_smoothness | A numeric. Shrink factor to be multiplied by thesmoothing parameters to estimate the conditional kernel density of themajority class. Defaults to 1. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whenrose-ing. |
id | A character string that is unique to this step to identify it. |
Details
The factor variable used to balance around must only have 2 levels.
The ROSE algorithm works by selecting an observation belonging to class kand generates new examples in its neighborhood is determined by some matrixH_k. Smaller values of these arguments have the effect of shrinking theentries of the corresponding smoothing matrix H_k, Shrinking would be acautious choice if there is a concern that excessively large neighborhoodscould lead to blur the boundaries between the regions of the feature spaceassociated with each class.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 1 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)
Case weights
The underlying operation does not allow for case weights.
References
Lunardon, N., Menardi, G., and Torelli, N. (2014). ROSE: aPackage for Binary Imbalanced Learning. R Jorunal, 6:82–92.
Menardi, G. and Torelli, N. (2014). Training and assessingclassification rules with imbalanced data. Data Mining and KnowledgeDiscovery, 28:92–122.
See Also
Other Steps for over-sampling:step_adasyn(),step_bsmote(),step_smote(),step_smotenc(),step_upsample()
Examples
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")origup_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 effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")bakedorig %>% 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")Apply SMOTE Algorithm
Description
step_smote() creates aspecification of a recipe step that generate newexamples of the minority class using nearest neighbors of these cases.
Usage
step_smote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smote"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
neighbors | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whensmote-ing. |
id | A character string that is unique to this step to identify it. |
Details
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 2 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)neighbors: # Nearest Neighbors (type: integer, default: 5)
Case weights
The underlying operation does not allow for case weights.
References
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer,W. P. (2002). Smote: Synthetic minority over-sampling technique.Journal of Artificial Intelligence Research, 16:321-357.
See Also
smote() for direct implementation
Other Steps for over-sampling:step_adasyn(),step_bsmote(),step_rose(),step_smotenc(),step_upsample()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smote(class, over_ratio = 0.4523) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 SMOTE")recipe(class ~ x + y, data = circle_example) %>% step_smote(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With SMOTE")Apply SMOTENC algorithm
Description
step_smotenc() creates aspecification of a recipe step that generate newexamples of the minority class using nearest neighbors of these cases.Gower's distance is used to handle mixed data types. For categoricalvariables, the most common category along neighbors is chosen.
Usage
step_smotenc( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smotenc"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
neighbors | An integer. Number of nearest neighbor that are usedto generate the new examples of the minority class. |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whensmote-ing. |
id | A character string that is unique to this step to identify it. |
Details
The parameterneighbors controls the way the new examples are created.For each currently existing minority class example X new examples will becreated (this is controlled by the parameterover_ratio as mentionedabove). These examples will be generated by using the information from theneighbors nearest neighbor of each example of the minority class.The parameterneighbors controls how many of these neighbor are used.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
Columns can be numeric and categorical with no missing data.
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 2 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)neighbors: # Nearest Neighbors (type: integer, default: 5)
Case weights
The underlying operation does not allow for case weights.
References
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer,W. P. (2002). Smote: Synthetic minority over-sampling technique.Journal of Artificial Intelligence Research, 16:321-357.
See Also
smotenc() for direct implementation
Other Steps for over-sampling:step_adasyn(),step_bsmote(),step_rose(),step_smote(),step_upsample()
Examples
library(recipes)library(modeldata)data(hpc_data)orig <- count(hpc_data, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data) %>% step_impute_knn(all_predictors()) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smotenc(class, over_ratio = 0.4523) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone:orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class")Remove Tomek’s Links
Description
step_tomek() creates aspecification of a recipe step that removesmajority class instances of tomek links.
Usage
step_tomek( recipe, ..., role = NA, trained = FALSE, column = NULL, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("tomek"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed whenapplied. |
id | A character string that is unique to this step to identify it. |
Details
The factor variable used to balance around must only have 2 levels. Allother variables must be numerics with no missing data.
A tomek link is defined as a pair of points from different classes and areeach others nearest neighbors.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
When used in modeling, users should strongly consider using theoptionskip = TRUE so that the extra sampling isnotconducted outside of the training set.
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Case weights
The underlying operation does not allow for case weights.
References
Tomek. Two modifications of cnn. IEEE Trans. Syst. Man Cybern.,6:769-772, 1976.
See Also
tomek() for direct implementation
Other Steps for under-sampling:step_downsample(),step_nearmiss()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% step_tomek(class) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")bakedorig %>% 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 Tomek") + xlim(c(1, 15)) + ylim(c(1, 15))recipe(class ~ x + y, data = circle_example) %>% step_tomek(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With Tomek") + xlim(c(1, 15)) + ylim(c(1, 15))Up-Sample a Data Set Based on a Factor Variable
Description
step_upsample() creates aspecification of a recipe step that willreplicate rows of a data set to make the occurrence of levels in a specificfactor level equal.
Usage
step_upsample( recipe, ..., over_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("upsample"))Arguments
recipe | A recipe object. The step will be added to thesequence of operations for this recipe. |
... | One or more selector functions to choose whichvariable is used to sample the data. Seerecipes::selectionsfor more details. The selection should result insinglefactor variable. For the |
over_ratio | A numeric value for the ratio of theminority-to-majority frequencies. The default value (1) meansthat all other levels are sampled up to have the samefrequency as the most occurring level. A value of 0.5 would meanthat the minority levels will have (at most) (approximately)half as many rows than the majority level. |
ratio | Deprecated argument; same as |
role | Not used by this step since no new variables arecreated. |
trained | A logical to indicate if the quantities forpreprocessing have been estimated. |
column | A character string of the variable name that willbe populated (eventually) by the |
target | An integer that will be used to subsample. Thisshould not be set by the user and will be populated by |
skip | A logical. Should the step be skipped when therecipe is baked by |
seed | An integer that will be used as the seed when upsampling. |
id | A character string that is unique to this step to identify it. |
Details
Up-sampling is intended to be performed on thetraining setalone. For this reason, the default isskip = TRUE.
If there are missing values in the factor variable that is usedto define the sampling, missing data are selected at random inthe same way that the other factor levels are sampled. Missingvalues are not used to determine the amount of data in themajority level (see example below).
For any data with factor levels occurring with the samefrequency as the majority level, all data will be retained.
All columns in the data are sampled and returned byrecipes::juice()andrecipes::bake().
Value
An updated version ofrecipe with the new stepadded to the sequence of existing steps (if any). For thetidy method, a tibble with columnsterms which isthe variable used to sample.
Tidying
When youtidy() this step, a tibble is retruned withcolumnsterms andid:
- terms
character, the selectors or variables selected
- id
character, id of this step
Tuning Parameters
This step has 1 tuning parameters:
over_ratio: Over-Sampling Ratio (type: double, default: 1)
Case weights
This step performs an unsupervised operation that can utilize case weights.To use them, see the documentation inrecipes::case_weights and the examples ontidymodels.org.
See Also
Other Steps for over-sampling:step_adasyn(),step_bsmote(),step_rose(),step_smote(),step_smotenc()
Examples
library(recipes)library(modeldata)data(hpc_data)hpc_data0 <- hpc_data %>% select(-protocol, -day)orig <- count(hpc_data0, class, name = "orig")origup_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_upsample(class, over_ratio = 0.4523) %>% prep()training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training")training# Since `skip` defaults to TRUE, baking the step has no effectbaked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked")baked# Note that if the original data contained more rows than the# target n (= ratio * majority_n), the data are left alone: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 upsample")recipe(class ~ x + y, data = circle_example) %>% step_upsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_jitter(width = 0.1, height = 0.1) + labs(title = "With upsample (with jittering)")Remove Tomek's links
Description
Removed observations that are part of tomek links.
Usage
tomek(df, var)Arguments
df | data.frame or tibble. Must have 1 factor variable and remainingnumeric variables. |
var | Character, name of variable containing factor variable. |
Details
All columns used in this function must be numeric with no missing data.
Value
A data.frame or tibble, depending on type ofdf.
References
Tomek. Two modifications of cnn. IEEE Trans. Syst. Man Cybern.,6:769-772, 1976.
See Also
step_tomek() for step function of this method
Other Direct Implementations:adasyn(),bsmote(),nearmiss(),smote(),smotenc()
Examples
circle_numeric <- circle_example[, c("x", "y", "class")]res <- tomek(circle_numeric, var = "class")tunable methods for themis
Description
These functions define what parameterscan be tuned for specific steps.They also define the recommended objects from thedials package that canbe used to generate new parameter values and other characteristics.
Usage
## S3 method for class 'step_adasyn'tunable(x, ...)## S3 method for class 'step_bsmote'tunable(x, ...)## S3 method for class 'step_downsample'tunable(x, ...)## S3 method for class 'step_nearmiss'tunable(x, ...)## S3 method for class 'step_rose'tunable(x, ...)## S3 method for class 'step_smote'tunable(x, ...)## S3 method for class 'step_smotenc'tunable(x, ...)## S3 method for class 'step_upsample'tunable(x, ...)Arguments
x | A recipe step object |
... | Not used. |
Value
A tibble object.