8.49 Tuning the regression model
Now let us again try to tune the cost_complexity
to find the best performing model.
<- workflow() %>%
reg_tree_wf add_model(reg_tree_spec %>% set_args(cost_complexity = tune())) %>%
add_formula(Sales ~ .)
Create the bootstrap folds.
set.seed(4321)
# bootstraps_samples are defined in line 158
<- bootstraps(carseats_train, times = bootstraps_samples, apparent = TRUE)
carseats_boot
carseats_boot
## # Bootstrap sampling with apparent sample
## # A tibble: 101 × 2
## splits id
## <list> <chr>
## 1 <split [300/111]> Bootstrap001
## 2 <split [300/111]> Bootstrap002
## 3 <split [300/102]> Bootstrap003
## 4 <split [300/112]> Bootstrap004
## 5 <split [300/109]> Bootstrap005
## 6 <split [300/113]> Bootstrap006
## 7 <split [300/128]> Bootstrap007
## 8 <split [300/118]> Bootstrap008
## 9 <split [300/107]> Bootstrap009
## 10 <split [300/115]> Bootstrap010
## # ℹ 91 more rows
Create the tuning grid.
<- grid_regular(cost_complexity(range = c(-5, -1)), levels = 10)
param_grid
<- tune_grid(
tune_res
reg_tree_wf, resamples = carseats_boot,
grid = param_grid
)
tune_res
## # Tuning results
## # Bootstrap sampling with apparent sample
## # A tibble: 101 × 4
## splits id .metrics .notes
## <list> <chr> <list> <list>
## 1 <split [300/300]> Apparent <tibble [20 × 5]> <tibble [0 × 3]>
## 2 <split [300/111]> Bootstrap001 <tibble [20 × 5]> <tibble [0 × 3]>
## 3 <split [300/111]> Bootstrap002 <tibble [20 × 5]> <tibble [0 × 3]>
## 4 <split [300/102]> Bootstrap003 <tibble [20 × 5]> <tibble [0 × 3]>
## 5 <split [300/112]> Bootstrap004 <tibble [20 × 5]> <tibble [0 × 3]>
## 6 <split [300/109]> Bootstrap005 <tibble [20 × 5]> <tibble [0 × 3]>
## 7 <split [300/113]> Bootstrap006 <tibble [20 × 5]> <tibble [0 × 3]>
## 8 <split [300/128]> Bootstrap007 <tibble [20 × 5]> <tibble [0 × 3]>
## 9 <split [300/118]> Bootstrap008 <tibble [20 × 5]> <tibble [0 × 3]>
## 10 <split [300/107]> Bootstrap009 <tibble [20 × 5]> <tibble [0 × 3]>
## # ℹ 91 more rows