step_collapse_cart: Supervised Collapsing of Factor Levels

View source: R/collapse_cart.R

step_collapse_cartR Documentation

Supervised Collapsing of Factor Levels


step_collapse_cart() creates a specification of a recipe step that can collapse factor levels into a smaller set using a supervised tree.


  role = NA,
  trained = FALSE,
  outcome = NULL,
  cost_complexity = 1e-04,
  min_n = 5,
  results = NULL,
  skip = FALSE,
  id = rand_id("step_collapse_cart")



A recipe object. The step will be added to the sequence of operations for this recipe.


One or more selector functions to choose which variables are affected by the step. See selections() for more details. For the tidy method, these are not currently used.


Not used by this step since no new variables are created.


A logical to indicate if the quantities for preprocessing have been estimated.


A call to vars to specify which variable is used as the outcome to train CART models in order to pool factor levels.


A non-negative value that regulates the complexity of the tree when pruning occurs. Values near 0.1 usually correspond to a tree with a single splits. Values of zero correspond to unpruned tree.


An integer for how many data points are required to make further splits during the tree growing process. Larger values correspond to less complex trees.


A list of results to convert to new factor levels.


A logical. Should the step be skipped when the recipe is baked by bake()? While all operations are baked when prep() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations


A character string that is unique to this step to identify it.


This step uses a CART tree (classification or regression) to group the existing factor levels into a potentially smaller set. It changes the levels in the factor predictor (and the tidy() method can be used to understand the translation).

There are a few different ways that the step will not be able to collapse levels. If the model fails or, if the results have each level being in its own split, the original factor levels are retained. There are also cases where there is "no admissible split" which means that the model could not find any signal in the data.


An updated recipe step.


When you tidy() this step, a tibble with columns "terms" (the column being modified), "old" (the old levels), "new" (the new levels), and "id". If the CART model failed or could not find a good split, the requested predictor will not be in the results.

Case weights

The underlying operation does not allow for case weights.


data(ames, package = "modeldata")
ames$Sale_Price <- log10(ames$Sale_Price)

rec <-
  recipe(Sale_Price ~ ., data = ames) %>%
    Sale_Type, Garage_Type, Neighborhood,
    outcome = vars(Sale_Price)
  ) %>%
tidy(rec, number = 1)

embed documentation built on Nov. 2, 2023, 6:20 p.m.