Learn R Programming

recipes (version 0.1.0)

step_other: Collapse Some Categorical Levels

Description

step_other creates a specification of a recipe step that will potentially pool infrequently occurring values into an "other" category.

Usage

step_other(recipe, ..., role = NA, trained = FALSE, threshold = 0.05,
  other = "other", objects = NULL)

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 that will potentially be reduced. See selections for more details.

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.

threshold

A single numeric value in (0, 1) for pooling.

other

A single character value for the "other" category.

objects

A list of objects that contain the information to pool infrequent levels that is determined by prep.recipe.

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any).

Details

The overall proportion of the categories are computed. The "other" category is used in place of any categorical levels whose individual proportion in the training set is less than threshold.

If no pooling is done the data are unmodified (although character data may be changed to factors based on the value of stringsAsFactors in prep.recipe). Otherwise, a factor is always returned with different factor levels.

If threshold is less than the largest category proportion, all levels except for the most frequent are collapsed to the other level.

If the retained categories include the value of other, an error is thrown. If other is in the list of discarded levels, no error occurs.

Examples

Run this code
# NOT RUN {
data(okc)

set.seed(19)
in_train <- sample(1:nrow(okc), size = 30000)

okc_tr <- okc[ in_train,]
okc_te <- okc[-in_train,]

rec <- recipe(~ diet + location, data = okc_tr)


rec <- rec %>%
  step_other(diet, location, threshold = .1, other = "other values")
rec <- prep(rec, training = okc_tr)

collapsed <- bake(rec, okc_te)
table(okc_te$diet, collapsed$diet, useNA = "always")
# }

Run the code above in your browser using DataLab