-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tidymodels/test-steps
Test all implemented recipes steps
- Loading branch information
Showing
11 changed files
with
146 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#' @export | ||
orbital.step_corr <- function(x, all_vars, ...) { | ||
NULL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#' @export | ||
orbital.step_normalize <- function(x, all_vars, ...) { | ||
means <- x$means | ||
sds <- x$sds | ||
|
||
used_vars <- names(means) %in% all_vars | ||
means <- means[used_vars] | ||
sds <- sds[used_vars] | ||
|
||
out <- paste0("(", names(means), " - ", means ,") / ", sds) | ||
names(out) <- names(means) | ||
out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#' @export | ||
orbital.step_nzv <- function(x, all_vars, ...) { | ||
NULL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' @export | ||
orbital.step_pca <- function(x, all_vars, ...) { | ||
rot <- x$res$rotation | ||
colnames(rot) <- recipes::names0(ncol(rot), x$prefix) | ||
|
||
used_vars <- pca_naming(colnames(rot), x$prefix) %in% | ||
pca_naming(all_vars, x$prefix) | ||
|
||
rot <- rot[, used_vars] | ||
|
||
row_nms <- rownames(rot) | ||
|
||
out <- character(length(all_vars)) | ||
for (i in seq_along(all_vars)) { | ||
out[i] <- paste(row_nms, "*", rot[, i], collapse = " + ") | ||
} | ||
|
||
names(out) <- all_vars | ||
out | ||
} | ||
|
||
pca_naming <- function(x, prefix) { | ||
gsub(paste0(prefix, "0"), prefix, x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
test_that("step_corr works", { | ||
skip_if_not_installed("recipes") | ||
|
||
mtcars0 <- mtcars | ||
mtcars0$disp1 <- mtcars$disp | ||
|
||
rec_exp <- recipes::recipe(mpg ~ ., data = mtcars) %>% | ||
recipes::step_corr(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
expect_null(orbital(rec_exp$steps[[1]])) | ||
|
||
rec <- recipes::recipe(mpg ~ ., data = mtcars0) %>% | ||
recipes::step_corr(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
expect_null(orbital(rec$steps[[1]])) | ||
|
||
expect_identical(orbital(rec), orbital(rec_exp)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
test_that("step_normalize works", { | ||
skip_if_not_installed("recipes") | ||
|
||
mtcars <- dplyr::as_tibble(mtcars) | ||
|
||
rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% | ||
recipes::step_normalize(recipes::all_predictors()) %>% | ||
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
test_that("step_nzv works", { | ||
skip_if_not_installed("recipes") | ||
|
||
mtcars0 <- mtcars | ||
mtcars0$zv <- 0 | ||
|
||
rec_exp <- recipes::recipe(mpg ~ ., data = mtcars) %>% | ||
recipes::step_nzv(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
expect_null(orbital(rec_exp$steps[[1]])) | ||
|
||
rec <- recipes::recipe(mpg ~ ., data = mtcars0) %>% | ||
recipes::step_nzv(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
expect_null(orbital(rec$steps[[1]])) | ||
|
||
expect_identical(orbital(rec), orbital(rec_exp)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
test_that("step_pca works", { | ||
skip_if_not_installed("recipes") | ||
|
||
mtcars <- dplyr::as_tibble(mtcars) | ||
mtcars$hp <- NULL | ||
|
||
rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% | ||
recipes::step_pca(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
exp <- recipes::bake(rec, new_data = mtcars) | ||
|
||
res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) | ||
res <- res[names(exp)] | ||
|
||
expect_equal(res, exp) | ||
}) | ||
|
||
test_that("step_pca works with more than 9 PCs", { | ||
skip_if_not_installed("recipes") | ||
|
||
mtcars <- dplyr::as_tibble(mtcars) | ||
|
||
rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% | ||
recipes::step_pca(recipes::all_predictors()) %>% | ||
recipes::prep() | ||
|
||
exp <- recipes::bake(rec, new_data = mtcars) | ||
|
||
res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) | ||
res <- res[names(exp)] | ||
|
||
expect_equal(res, exp) | ||
}) |