tune_grid() and gam model incl. gam-formula

How to use tune_grid() and a gam model (gen_additive_mod()) incl. gam-formula. please provide a code-snippet. thx.

Here's an example:

#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
#> Warning: package 'broom' was built under R version 4.1.2

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)

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.

1 Like

Thank you Max. Post must be at least 20 characters.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.