Skip to content

Commit

Permalink
add scale and center functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Jun 25, 2024
1 parent f47a06c commit 234d1c1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ S3method(orbital,default)
S3method(orbital,model_fit)
S3method(orbital,model_spec)
S3method(orbital,recipe)
S3method(orbital,step_center)
S3method(orbital,step_corr)
S3method(orbital,step_filter_missing)
S3method(orbital,step_lincomb)
S3method(orbital,step_normalize)
S3method(orbital,step_nzv)
S3method(orbital,step_pca)
S3method(orbital,step_rm)
S3method(orbital,step_scale)
S3method(orbital,step_select)
S3method(orbital,step_zv)
S3method(orbital,workflow)
Expand Down
11 changes: 11 additions & 0 deletions R/step_center.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' @export
orbital.step_center <- function(x, all_vars, ...) {
means <- x$means

used_vars <- names(means) %in% all_vars
means <- means[used_vars]

out <- paste0(names(means), " - ", means)
names(out) <- names(means)
out
}
11 changes: 11 additions & 0 deletions R/step_scale.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' @export
orbital.step_scale <- function(x, all_vars, ...) {
sds <- x$sds

used_vars <- names(sds) %in% all_vars
sds <- sds[used_vars]

out <- paste0(names(sds) ," / ", sds)
names(out) <- names(sds)
out
}
16 changes: 16 additions & 0 deletions tests/testthat/test-step_center.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("step_center works", {
skip_if_not_installed("recipes")

mtcars <- dplyr::as_tibble(mtcars)

rec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_center(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)
})
16 changes: 16 additions & 0 deletions tests/testthat/test-step_scale.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("step_scale works", {
skip_if_not_installed("recipes")

mtcars <- dplyr::as_tibble(mtcars)

rec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_scale(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)
})

0 comments on commit 234d1c1

Please sign in to comment.