tidymodels / tidymodels.org-legacy

Legacy Source of tidymodels.org
https://github.com/tidymodels/tidymodels_dot_org
Creative Commons Attribution Share Alike 4.0 International
99 stars 31 forks source link

`tunable()` is necessary to tune a param #264

Open rorynolan opened 1 year ago

rorynolan commented 1 year ago

At https://www.tidymodels.org/learn/develop/recipes/ it says that

To work with tune it is helpful (but not required) to use an S3 method called tunable()

I contend that it actually is necessary to have a tunable() method. The best way I could think of to demonstrate this was to create a branch of recipes without tunable.step_ns() and then run the tune_grid() example and show it wouldn't work.

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
library(rsample)
library(parsnip)
library(workflows)
library(ggplot2)
library(tune)

set.seed(6735)
folds <- vfold_cv(mtcars, v = 5)

# tuning recipe parameters:

spline_rec <-
  recipe(mpg ~ ., data = mtcars) %>%
  step_ns(disp, deg_free = tune("disp")) %>%
  step_ns(wt, deg_free = tune("wt"))

lin_mod <-
  linear_reg() %>%
  set_engine("lm")

# manually create a grid
spline_grid <- expand.grid(disp = 2:5, wt = 2:5)

# Warnings will occur from making spline terms on the holdout data that are
# extrapolations.
spline_res <-
  tune_grid(lin_mod, spline_rec, resamples = folds, grid = spline_grid)
#> Warning: No tuning parameters have been detected, performance will be evaluated
#> using the resamples with no tuning. Did you want to [tune()] parameters?
#> x Fold1: preprocessor 1/1:
#>   Error in `recipes::prep()`:
#>   ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'd...
#> x Fold2: preprocessor 1/1:
#>   Error in `recipes::prep()`:
#>   ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'd...
#> x Fold3: preprocessor 1/1:
#>   Error in `recipes::prep()`:
#>   ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'd...
#> x Fold4: preprocessor 1/1:
#>   Error in `recipes::prep()`:
#>   ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'd...
#> x Fold5: preprocessor 1/1:
#>   Error in `recipes::prep()`:
#>   ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'd...
#> Warning: All models failed. Run `show_notes(.Last.tune.result)` for more
#> information.

Created on 2022-09-21 with reprex v2.0.2

So lack of a tunable() means lack of ability to use tune_grid().

The branch is available at rorynolan/recipes, branch name ns-not-tunable.

EmilHvitfeldt commented 1 year ago

Hello @rorynolan 👋

You appears to be correct, That section is indeed over 3 years old so it isn't surprising that the requirements have changed.

Thank you once again, we will have this paragraph fixed!