Home > Back-end >  tidymodels, bonsai: Setting partykit::ctree parameters: mincriterion (the significance level), testt
tidymodels, bonsai: Setting partykit::ctree parameters: mincriterion (the significance level), testt

Time:08-30

Using the partykit::ctree function to create a ctree:

ctree = ctree(formula,         # Outcome variables and features
                 data=sample,  # Name of dataset
                 control=ctree_control(mincriterion=1-0.05, # 1 minus p-value
                                       testtype="Bonferroni", # Corrects for multiple hypothesis testing
                                       maxdepth=10)) # The maximum number of steps from the top to the bottom of the tree

Using the tidymodels and bonsai packages to create a ctree:

model_ctree <- decision_tree() %>% 
  set_mode("regression") %>% 
  set_engine("partykit") %>% 
  fit(formula, data = sample)

And I notice it seems that there are only 3 parameters can be set where use tidymodels (Decision trees via partykit) Is it possible for me to change the parameters mincriterion=0.95 and testtype="Bonferroni"?

CodePudding user response:

Dials (part of tidymodels) is the library related to tunable hyperparameters. mincriterion and testtype are what's considered as engine-specific hyperparameters i.e. not useable for other decision tree engines like rpart or C5.
There might be another more intuitive way to do this within RStudio, but what I'd generally do is first head to Dials reference page, do a ctrl-F for the engine you're using (in this case, search for "partykit").
You'll see conditional_min_criterion() - click it and you'll be linked to Parameters for possible engine parameters for partykit models.

More reading can be found here.

library(tidymodels)
library(bonsai)

#documentation states conditional_test_type(values = values_test_type)
#check what values are available for conditional_test_type
values_test_type
#> [1] "Bonferroni"    "MonteCarlo"    "Univariate"    "Teststatistic"

model_ctree <- decision_tree() %>%
  set_mode("regression") %>%
  #specify engine-specific hyperparamters in set_engine()
  set_engine("partykit",
             conditional_min_criterion = 0.95,
             conditional_test_type = "Bonferroni")

model_ctree
#> Decision Tree Model Specification (regression)
#> 
#> Engine-Specific Arguments:
#>   conditional_min_criterion = 0.95
#>   conditional_test_type = Bonferroni
#> 
#> Computational engine: partykit

results <- model_ctree |> 
  fit(mpg ~ ., data = mtcars)


results
#> parsnip model object
#> 
#> 
#> Model formula:
#> mpg ~ cyl   disp   hp   drat   wt   qsec   vs   am   gear   carb
#> 
#> Fitted party:
#> [1] root
#> |   [2] wt <= 2.32: 29.029 (n = 7, err = 89.8)
#> |   [3] wt > 2.32
#> |   |   [4] disp <= 258: 20.755 (n = 11, err = 38.3)
#> |   |   [5] disp > 258: 15.100 (n = 14, err = 85.2)
#> 
#> Number of inner nodes:    2
#> Number of terminal nodes: 3

Created on 2022-08-29 with reprex v2.0.2

  • Related