tidymodels / recipes

Pipeable steps for feature engineering and data preprocessing to prepare for modeling
https://recipes.tidymodels.org
Other
565 stars 111 forks source link

fix bug in step_ns where knots didn't get passed through #1338

Closed EmilHvitfeldt closed 3 months ago

EmilHvitfeldt commented 3 months ago

to close #1297 and close https://github.com/tidymodels/recipes/issues/1050.

The main problem was that knots wasn't passed through if it was set in options argument

library(recipes)
d <- tibble(x = seq(-2, 2, 0.01))

rec_res <- recipe(~., data = d) %>% 
  step_ns(x, options = list(knots = seq(-1, 1, 0.125), 
                            Boundary.knots = c(-1.5, 1.5))) %>% 
  prep() %>% 
  bake(new_data = d)

# -1 in `model.matrix()` to avoid intercept
mm_res <- model.matrix(~splines::ns(x, 
                          knots = seq(-1, 1, 0.125), 
                          Boundary.knots = c(-1.5, 1.5)) - 1, data = d)

attr(mm_res, "assign") <- NULL

identical(
  rec_res,
  setNames(as_tibble(mm_res), names(rec_res))
)
#> [1] TRUE
github-actions[bot] commented 3 months ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex https://reprex.tidyverse.org) and link to this issue.