diff --git a/DESCRIPTION b/DESCRIPTION index 4afdafe..54681ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,6 +29,7 @@ Suggests: sparklyr, testthat (>= 3.0.0), tidypredict, + themis, workflows Config/testthat/edition: 3 Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index eefa54e..bdd25f8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,8 +6,11 @@ S3method(orbital,model_fit) S3method(orbital,model_spec) S3method(orbital,recipe) S3method(orbital,step_BoxCox) +S3method(orbital,step_adasyn) +S3method(orbital,step_bsmote) S3method(orbital,step_center) S3method(orbital,step_corr) +S3method(orbital,step_downsample) S3method(orbital,step_dummy) S3method(orbital,step_filter_missing) S3method(orbital,step_impute_mean) @@ -20,6 +23,7 @@ S3method(orbital,step_lag) S3method(orbital,step_lincomb) S3method(orbital,step_log) S3method(orbital,step_mutate) +S3method(orbital,step_nearmiss) S3method(orbital,step_normalize) S3method(orbital,step_novel) S3method(orbital,step_nzv) @@ -29,10 +33,15 @@ S3method(orbital,step_range) S3method(orbital,step_ratio) S3method(orbital,step_rename) S3method(orbital,step_rm) +S3method(orbital,step_rose) S3method(orbital,step_scale) S3method(orbital,step_select) +S3method(orbital,step_smote) +S3method(orbital,step_smotenc) S3method(orbital,step_sqrt) +S3method(orbital,step_tomek) S3method(orbital,step_unknown) +S3method(orbital,step_upsample) S3method(orbital,step_zv) S3method(orbital,workflow) S3method(predict,orbital_class) diff --git a/NEWS.md b/NEWS.md index d66ef2d..17e7f40 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,6 +36,8 @@ * Support for `step_rename()` has been added. (#17) +* Support for `step_upsample()`, `step_smote()`, `step_smotenc()`, `step_bsmote()`, `step_adasyn()`, `step_rose()`, `step_downsample()`, `step_nearmiss()`, and `step_tomek()` has been added. (#21) + * `orbital()` now works on `tune::last_fit()` objects. (#13) * `orbital_predict()` has been removed and replaced with the more idiomatic `predict()` method. (#10) diff --git a/R/step_adasyn.R b/R/step_adasyn.R new file mode 100644 index 0000000..a07f58d --- /dev/null +++ b/R/step_adasyn.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_adasyn <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_adasyn} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_bsmote.R b/R/step_bsmote.R new file mode 100644 index 0000000..fb63384 --- /dev/null +++ b/R/step_bsmote.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_bsmote <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_bsmote} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_downsample.R b/R/step_downsample.R new file mode 100644 index 0000000..6dfa7ce --- /dev/null +++ b/R/step_downsample.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_downsample <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_downsample} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_nearmiss.R b/R/step_nearmiss.R new file mode 100644 index 0000000..86ea3c2 --- /dev/null +++ b/R/step_nearmiss.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_nearmiss <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_nearmiss} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_rose.R b/R/step_rose.R new file mode 100644 index 0000000..a428eec --- /dev/null +++ b/R/step_rose.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_rose <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_rose} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_smote.R b/R/step_smote.R new file mode 100644 index 0000000..0b9870a --- /dev/null +++ b/R/step_smote.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_smote <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_smote} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_smotenc.R b/R/step_smotenc.R new file mode 100644 index 0000000..7b7eed1 --- /dev/null +++ b/R/step_smotenc.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_smotenc <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_smotenc} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_tomek.R b/R/step_tomek.R new file mode 100644 index 0000000..a4d2853 --- /dev/null +++ b/R/step_tomek.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_tomek <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_tomek} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/R/step_upsample.R b/R/step_upsample.R new file mode 100644 index 0000000..8759027 --- /dev/null +++ b/R/step_upsample.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_upsample <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_upsample} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_adasyn.md b/tests/testthat/_snaps/step_adasyn.md new file mode 100644 index 0000000..ad8080a --- /dev/null +++ b/tests/testthat/_snaps/step_adasyn.md @@ -0,0 +1,8 @@ +# step_bsmote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_bsmote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_bsmote.md b/tests/testthat/_snaps/step_bsmote.md new file mode 100644 index 0000000..ad8080a --- /dev/null +++ b/tests/testthat/_snaps/step_bsmote.md @@ -0,0 +1,8 @@ +# step_bsmote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_bsmote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_downsample.md b/tests/testthat/_snaps/step_downsample.md new file mode 100644 index 0000000..9fd0ff5 --- /dev/null +++ b/tests/testthat/_snaps/step_downsample.md @@ -0,0 +1,8 @@ +# step_downsample errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_downsample()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_nearmiss.md b/tests/testthat/_snaps/step_nearmiss.md new file mode 100644 index 0000000..105bf28 --- /dev/null +++ b/tests/testthat/_snaps/step_nearmiss.md @@ -0,0 +1,8 @@ +# step_nearmiss errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_nearmiss()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_rose.md b/tests/testthat/_snaps/step_rose.md new file mode 100644 index 0000000..7c1862b --- /dev/null +++ b/tests/testthat/_snaps/step_rose.md @@ -0,0 +1,8 @@ +# step_rose errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_rose()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_smote.md b/tests/testthat/_snaps/step_smote.md new file mode 100644 index 0000000..95100c9 --- /dev/null +++ b/tests/testthat/_snaps/step_smote.md @@ -0,0 +1,8 @@ +# step_smote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_smote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_smotenc.md b/tests/testthat/_snaps/step_smotenc.md new file mode 100644 index 0000000..4e32424 --- /dev/null +++ b/tests/testthat/_snaps/step_smotenc.md @@ -0,0 +1,8 @@ +# step_smotenc errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_smotenc()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_tomek.md b/tests/testthat/_snaps/step_tomek.md new file mode 100644 index 0000000..55fb940 --- /dev/null +++ b/tests/testthat/_snaps/step_tomek.md @@ -0,0 +1,8 @@ +# step_tomek errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_tomek()` when `skip` is `FALSE`. + diff --git a/tests/testthat/_snaps/step_upsample.md b/tests/testthat/_snaps/step_upsample.md new file mode 100644 index 0000000..fc9e337 --- /dev/null +++ b/tests/testthat/_snaps/step_upsample.md @@ -0,0 +1,8 @@ +# step_upsample errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_upsample()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_adasyn.R b/tests/testthat/test-step_adasyn.R new file mode 100644 index 0000000..b6bc9d3 --- /dev/null +++ b/tests/testthat/test-step_adasyn.R @@ -0,0 +1,60 @@ +test_that("step_adasyn works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_adasyn(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_adasyn errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_adasyn(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_adasyn works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_adasyn <- dplyr::as_tibble(mtcars) + mtcars_adasyn$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_adasyn) %>% + themis::step_adasyn(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_adasyn, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_adasyn") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_bsmote.R b/tests/testthat/test-step_bsmote.R new file mode 100644 index 0000000..71683c2 --- /dev/null +++ b/tests/testthat/test-step_bsmote.R @@ -0,0 +1,60 @@ +test_that("step_bsmote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_bsmote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_bsmote errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_bsmote(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_bsmote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_bsmote <- dplyr::as_tibble(mtcars) + mtcars_bsmote$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_bsmote) %>% + themis::step_bsmote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_bsmote, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_bsmote") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_downsample.R b/tests/testthat/test-step_downsample.R new file mode 100644 index 0000000..5d49897 --- /dev/null +++ b/tests/testthat/test-step_downsample.R @@ -0,0 +1,60 @@ +test_that("step_downsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_downsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_downsample errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_downsample(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_downsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_downsample <- dplyr::as_tibble(mtcars) + mtcars_downsample$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_downsample) %>% + themis::step_downsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_downsample, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_downsample") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_nearmiss.R b/tests/testthat/test-step_nearmiss.R new file mode 100644 index 0000000..c0a33d6 --- /dev/null +++ b/tests/testthat/test-step_nearmiss.R @@ -0,0 +1,60 @@ +test_that("step_nearmiss works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_nearmiss(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_nearmiss errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_nearmiss(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_nearmiss works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_nearmiss <- dplyr::as_tibble(mtcars) + mtcars_nearmiss$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_nearmiss) %>% + themis::step_nearmiss(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_nearmiss, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_nearmiss") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_rose.R b/tests/testthat/test-step_rose.R new file mode 100644 index 0000000..b1756b9 --- /dev/null +++ b/tests/testthat/test-step_rose.R @@ -0,0 +1,60 @@ +test_that("step_rose works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_rose(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_rose errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_rose(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_rose works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_rose <- dplyr::as_tibble(mtcars) + mtcars_rose$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_rose) %>% + themis::step_rose(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_rose, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_rose") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_smote.R b/tests/testthat/test-step_smote.R new file mode 100644 index 0000000..c0a1b76 --- /dev/null +++ b/tests/testthat/test-step_smote.R @@ -0,0 +1,60 @@ +test_that("step_smote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_smote errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smote(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_smote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_smote <- dplyr::as_tibble(mtcars) + mtcars_smote$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_smote) %>% + themis::step_smote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_smote, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_smote") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_smotenc.R b/tests/testthat/test-step_smotenc.R new file mode 100644 index 0000000..fc7144a --- /dev/null +++ b/tests/testthat/test-step_smotenc.R @@ -0,0 +1,60 @@ +test_that("step_smotenc works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smotenc(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_smotenc errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smotenc(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_smotenc works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_smotenc <- dplyr::as_tibble(mtcars) + mtcars_smotenc$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_smotenc) %>% + themis::step_smotenc(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_smotenc, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_smotenc") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_tomek.R b/tests/testthat/test-step_tomek.R new file mode 100644 index 0000000..6fd1264 --- /dev/null +++ b/tests/testthat/test-step_tomek.R @@ -0,0 +1,60 @@ +test_that("step_tomek works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_tomek(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_tomek errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_tomek(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_tomek works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_tomek <- dplyr::as_tibble(mtcars) + mtcars_tomek$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_tomek) %>% + themis::step_tomek(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_tomek, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_tomek") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) diff --git a/tests/testthat/test-step_upsample.R b/tests/testthat/test-step_upsample.R new file mode 100644 index 0000000..28f0c1c --- /dev/null +++ b/tests/testthat/test-step_upsample.R @@ -0,0 +1,60 @@ +test_that("step_upsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_upsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_upsample errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_upsample(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_upsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_upsample <- dplyr::as_tibble(mtcars) + mtcars_upsample$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_upsample) %>% + themis::step_upsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_upsample, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_upsample") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +})