step_collapse_cart: Supervised Collapsing of Factor Levels

View source: R/collapse_cart.R

step_collapse_cartR Documentation

Supervised Collapsing of Factor Levels

Description

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

Usage

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")
)

Arguments

recipe

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.

role

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

trained

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

outcome

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

cost_complexity

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.

min_n

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.

results

A list of results to convert to new factor levels.

skip

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

id

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

Details

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.

Value

An updated recipe step.

Tidying

When you tidy() this step, a tibble is retruned with columns terms, old, new, and id:

terms

character, the selectors or variables selected

old

character, the old levels

new

character, the new levels

id

character, id of this step

Case weights

The underlying operation does not allow for case weights.

Examples


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)


tidymodels/embed documentation built on March 25, 2024, 11:14 p.m.