Supervised Collapsing of Factor LevelsSource:
step_collapse_cart() creates a specification of a recipe step that can
collapse factor levels into a smaller set using a supervised tree.
step_collapse_cart( recipe, ..., 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
tidymethod, 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
varsto 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 = TRUEas 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
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.
tidy() this step, a tibble with columns
(the column being modified),
"old" (the old levels),
"new" (the new
"id". If the CART model failed or could not find a good split,
the requested predictor will not be in the results.
data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) rec <- recipe(Sale_Price ~ ., data = ames) %>% step_collapse_cart( Sale_Type, Garage_Type, Neighborhood, outcome = vars(Sale_Price) ) %>% prep() tidy(rec, number = 1) #> # A tibble: 45 × 4 #> terms old new id #> <chr> <chr> <chr> <chr> #> 1 Sale_Type "ConLD" Sale_Type_1 step_collapse_cart_q2YzJ #> 2 Sale_Type "ConLw" Sale_Type_1 step_collapse_cart_q2YzJ #> 3 Sale_Type "Oth" Sale_Type_1 step_collapse_cart_q2YzJ #> 4 Sale_Type "COD" Sale_Type_2 step_collapse_cart_q2YzJ #> 5 Sale_Type "VWD" Sale_Type_2 step_collapse_cart_q2YzJ #> 6 Sale_Type "ConLI" Sale_Type_3 step_collapse_cart_q2YzJ #> 7 Sale_Type "WD " Sale_Type_4 step_collapse_cart_q2YzJ #> 8 Sale_Type "CWD" Sale_Type_5 step_collapse_cart_q2YzJ #> 9 Sale_Type "Con" Sale_Type_6 step_collapse_cart_q2YzJ #> 10 Sale_Type "New" Sale_Type_7 step_collapse_cart_q2YzJ #> # ℹ 35 more rows