recipes icon indicating copy to clipboard operation
recipes copied to clipboard

options are ignored in step_ns

Open DemetriPananos opened this issue 4 months ago • 3 comments

Possible bug as the options argument is not returning the same output as splines::ns.

I'd like to specify a spline for a continuous variable using step_ns. The docs say I can pass arguments k (knot locations) and Boundary.knots via options argument to step_ns.

However, doing so does not return the same number of basis vectors that I would get were I to use model.matrix and spline::ns. See below

library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stats':
#> 
#>     step

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

recipe(~., data=d) %>% 
  step_ns(x, options = list(k = seq(-1, 1, 0.125), Boundary.knots = c(-1.5, 1.5))) %>% 
  prep %>% 
  bake(new_data=d) %>% 
  ncol
#> [1] 2


model.matrix(~splines::ns(x, k = seq(-1, 1, 0.125), Boundary.knots = c(-1.5, 1.5)), data=d) %>% 
  ncol 
#> [1] 19

Created on 2024-04-12 with reprex v2.0.2

DemetriPananos avatar Apr 12 '24 18:04 DemetriPananos