rrandom-foresthyperparameterstidymodelsr-parsnip

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.


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