Skip to contents

`step_select_forests` creates a *specification* of a recipe step that selects a subset of predictors based on the ranking of variable importance using a `parsnip::rand_forest` supported model.

Usage

step_select_forests(
  recipe,
  ...,
  outcome = NULL,
  role = "predictor",
  trained = FALSE,
  engine = "ranger",
  options = list(importance = "permutation"),
  mtry = NULL,
  trees = NULL,
  min_n = NULL,
  top_p = NA,
  threshold = NA,
  cutoff = NA,
  exclude = NULL,
  scores = NULL,
  skip = FALSE,
  id = recipes::rand_id("select_forests")
)

# S3 method for step_select_forests
tidy(x, type = "terms", ...)

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.

outcome

A character string with the name of the response variable to use to calculate the feature importance scores.

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.

engine

A supported rand_forest engine that is supported by parsnip. The default is "ranger".

options

A named list of options to pass to the rand_forest engine. For example, if `engine = 'ranger'` (the default) then options could be `list(permutation = 'importance`) because a feature importance method needs to be specified for this engine. This is the default.

mtry

An integer for the number of predictors that will be randomly sampled at each split when creating the tree models.

trees

An integer for the number of trees contained in the ensemble.

min_n

An integer for the minimum number of data points in a node that are required for the node to be split further.

top_p

An integer with the number of best scoring features to select.

threshold

A numeric value between 0 and 1 representing the percentile of best scoring features to select. For example `threshold = 0.9` will retain only predictors with scores in the top 90th percentile and a smaller threshold will select more features. Note that `top_p` and `threshold` are mutually exclusive but either can be used in conjunction with `cutoff` to select the top-ranked features and those that have filter scores that are larger than the cutoff value.

cutoff

A numeric value where predictors with _larger_ absolute filter scores than the cutoff will be retained. A value of `NA` implies that this criterion will be ignored.

exclude

A character vector of predictor names that will be removed from the data. This will be set when `prep()` is used on the recipe and should not be set by the user.

scores

A tibble with 'variable' and 'scores' columns containing the names of the variables and their feature importance scores. This parameter is only produced after the recipe has been trained.

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when prep.recipe() 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.

x

A `step_select_forests` object.

type

A character with either 'terms' (the default) to return a tibble containing the variables that have been removed by the filter step, or 'scores' to return the scores for each variable.

Value

a `step_select_forests` object.

Examples

library(recipes)
library(parsnip)

# load the example iris dataset
data(cells, package = "modeldata")

# create a preprocessing recipe
rec <-
 recipe(class ~ ., data = cells[, -1]) %>%
 step_select_forests(all_predictors(), outcome = "class", top_p = 10,
                     cutoff = 0.9)

prepped <- prep(rec)

preproc_data <- juice(prepped)
prepped
#> 
#> ── Recipe ──────────────────────────────────────────────────────────────────────
#> 
#> ── Inputs 
#> Number of variables by role
#> outcome:    1
#> predictor: 56
#> 
#> ── Training information 
#> Training data contained 2019 data points and no incomplete rows.
#> 
#> ── Operations 
#> Variable importance feature selection (4 excluded)