Here's an example:
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
#> Warning: package 'broom' was built under R version 4.1.2
tidymodels_prefer()
gam_spec <- gen_additive_mod(select_features = tune()) %>% set_mode("regression")
gam_wflow <-
workflow() %>%
# smoothing must be specified here:
add_model(gam_spec, formula = mpg ~ s(disp) + wt + gear) %>%
add_variables(predictors = c(everything()), outcomes = mpg)
set.seed(1)
car_folds <- bootstraps(mtcars, times = 5)
gam_res <-
gam_wflow %>% tune_grid(resamples = car_folds)
show_best(gam_res, metric = "rmse")
#> # A tibble: 2 × 7
#> select_features .metric .estimator mean n std_err .config
#> <lgl> <chr> <chr> <dbl> <int> <dbl> <chr>
#> 1 FALSE rmse standard 3.48 5 0.848 Preprocessor1_Model2
#> 2 TRUE rmse standard 3.48 5 0.848 Preprocessor1_Model1
Created on 2022-01-24 by the reprex package (v2.0.1)
You can't optimize the smoothing parameters in tune_grid(). You could set up multiple workflows with different smoothing specifications and use a workflow set to try different ideas.