Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

tunable() is necessary to tune a param #264

@rorynolan

Description

@rorynolan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions