`step_select_linear` creates a *specification* of a recipe step that selects a subset of predictors based on the ranking of the magnitude of coefficients provided by a `parsnip::linear_reg`, `parsnip::logistic_reg` or `parsnip::multinom_reg` model. The type of model is selected automatically depending on the type of the outcome, whether it is numeric or the number of levels in the outcome for classification cases.

step_select_linear(
  recipe,
  ...,
  outcome = NULL,
  role = "predictor",
  trained = FALSE,
  engine = "glm",
  penalty = NULL,
  mixture = NULL,
  top_p = NA,
  threshold = NA,
  exclude = NULL,
  scores = NULL,
  skip = FALSE,
  id = recipes::rand_id("select_linear")
)

# S3 method for step_select_linear
tidy(x, ...)

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 "glmnet".

penalty

A non-negative number representing the total amount of regularization (specific engines only).

mixture

A number between zero and one (inclusive) that is the proportion of L1 regularization (i.e. lasso) in the model. When mixture = 1, it is a pure lasso model while mixture = 0 indicates that ridge regression is being used (specific engines only).

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. Features with scores that are _larger_ than the specified threshold will be retained, for example `threshold = 0.9` will retain only predictors with scores in the top 90th percentile. Note that this overrides `top_p`.

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_linear` object.

Value

a `step_select_linear` 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_linear(all_predictors(), outcome = "class", top_p = 10,
                     threshold = 0.9)

prepped <- prep(rec)

preproc_data <- juice(prepped)
prepped
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor         56
#> 
#> Training data contained 2019 data points and no missing data.
#> 
#> Operations:
#> 
#> Variable importance feature selection (50 excluded)