step_lencode | R Documentation |
step_lencode()
creates a specification of a recipe step that will convert
a nominal (i.e. factor) predictor into a single set of scores derived
analytically.
step_lencode(
recipe,
...,
role = NA,
trained = FALSE,
outcome = NULL,
smooth = TRUE,
mapping = NULL,
skip = FALSE,
id = rand_id("lencode")
)
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose variables. For
|
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 |
smooth |
A logical, default to |
mapping |
A list of tibble results that define the encoding. This is
|
skip |
A logical. Should the step be skipped when the recipe is baked by
|
id |
A character string that is unique to this step to identify it. |
Each selected nominal predictor will be replaced by a numeric predictor. Each unique value of the nominal predictor is replaced by a numeric value. Thse values are calculated differently depending on the type of the outcome.
For numeric outcomes each value is the average value of the outcome inside each of the levels of the predictor. Unseen levels of the predictor will be using the global mean of the predictor. If case weights are used then a weighted mean is calculated instead.
For nominal outcomes each value is the log odds of the of the first level of the outcome variable being present, within each level of the levels of the predictor. Unseen levels will be replaced by the global log odds without stratification. If case weights are used then a weighted log odds is calculated.
If no or all occurances happens then the log odds is calculated using
p = (2 * nrow(data) - 1) / (2 * nrow(data))
to avoid infinity that would
happen by taking the log of 0
.
For numeric outcomes where smooth = TRUE
, the following adjustment is done.
estimate = (n / global_{var}) /
(n / global_{var} + 1 / outcome_{var}) *
estimate +
(1 / outcome_{var}) / (n / global_{var} + 1 / outcome_{var}) * global_{mean}
Where n
is the number of observations in the group.
An updated version of recipe
with the new step added to the
sequence of existing steps (if any). For the tidy
method, a tibble with
columns terms
(the selectors or variables for encoding), level
(the
factor levels), and value
(the encodings).
When you tidy()
this step, a tibble is returned
with columns level
, value
, terms
, and id
:
character, the factor levels
numeric, the encoding
character, the selectors or variables selected
character, id of this step
This step performs an supervised operation that can utilize case weights.
To use them, see the documentation in recipes::case_weights and the examples on
tidymodels.org
.
Micci-Barreca D (2001) "A preprocessing scheme for high-cardinality categorical attributes in classification and prediction problems," ACM SIGKDD Explorations Newsletter, 3(1), 27-32.
Zumel N and Mount J (2017) "vtreat: a data.frame Processor for Predictive Modeling," arXiv:1611.09477
library(recipes)
library(dplyr)
library(modeldata)
data(grants)
set.seed(1)
grants_other <- sample_n(grants_other, 500)
reencoded <- recipe(class ~ sponsor_code, data = grants_other) |>
step_lencode(sponsor_code, outcome = vars(class), smooth = FALSE) |>
prep()
bake(reencoded, grants_other)
tidy(reencoded, 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.