Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

creating a tuning grid for Regression Trees in R

Attempting my first randomForest model in R and am working through tuning hyperparameters. I created a spec first:
tune_spec<- decision_tree() %>% set_engine("rpart") %>% set_mode("regression")

And then I tried to create a tuning grid:
tree_grid<- grid_regular(parameters(tune_spec), levels=3)

but I got this error:
Error in parameters():
! parameters objects cannot be created from objects of class decision_tree.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The parameters() function was deprecated in 0.2.0 of {tune} (2022-03-18). The function to use is extract_parameter_set_dials().

library(tidymodels)

tune_spec <- decision_tree() |>
  set_engine("rpart") |>
  set_mode("regression")

tune_spec |> 
  extract_parameter_set_dials()
#> Collection of 0 parameters for tuning
#> 
#> [1] identifier type       object    
#> <0 rows> (or 0-length row.names)

We are getting back a empty parameters object, because you need to specify which variables you want to use with tune()

tune_spec <- decision_tree(tree_depth = tune(), min_n = tune()) |>
  set_engine("rpart") |>
  set_mode("regression")

tune_spec |> 
  extract_parameter_set_dials()
#> Collection of 2 parameters for tuning
#> 
#>  identifier       type    object
#>  tree_depth tree_depth nparam[+]
#>       min_n      min_n nparam[+]

And once you have done that, then you can pass it to grid_regular() or the other functions

tune_spec |> 
  extract_parameter_set_dials() |>
  grid_regular(levels = 3)
#> # A tibble: 9 × 2
#>   tree_depth min_n
#>        <int> <int>
#> 1          1     2
#> 2          8     2
#> 3         15     2
#> 4          1    21
#> 5          8    21
#> 6         15    21
#> 7          1    40
#> 8          8    40
#> 9         15    40
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading