embed (version 0.0.2)

step_lencode_bayes: Supervised Factor Conversions into Linear Functions using Bayesian Likelihood Encodings

Description

step_lencode_bayes creates a specification of a recipe step that will convert a nominal (i.e. factor) predictor into a single set of scores derived from a generalized linear model estimated using Bayesian analysis.

Usage

step_lencode_bayes(recipe, ..., role = NA, trained = FALSE,
  outcome = NULL, options = list(seed = sample.int(10^5, 1)),
  verbose = FALSE, mapping = NULL, skip = FALSE,
  id = rand_id("lencode_bayes"))

# S3 method for step_lencode_bayes 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 variables. For step_lencode_bayes, this indicates the variables to be encoded into a numeric format. See recipes::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 in the generalized linear model. Only numeric and two-level factors are currently supported.

options

A list of options to pass to rstanarm::stan_glmer().

verbose

A logical to control the default printing by rstanarm::stan_glmer().

mapping

A list of tibble results that define the encoding. This is NULL until the step is trained by recipes::prep.recipe().

skip

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

Value

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

Details

For each factor predictor, a generalized linear model is fit to the outcome and the coefficients are returned as the encoding. These coefficients are on the linear predictor scale so, for factor outcomes, they are in log-odds units. The coefficients are created using a no intercept model and, when two factor outcomes are used, the log-odds reflect the event of interest being the first level of the factor.

For novel levels, a slightly timmed average of the coefficients is returned.

A hierarchical generalized linear model is fit using rstanarm::stan_glmer() and no intercept via

  stan_glmer(outcome ~ (1 | predictor), data = data, ...)

where the ... include the family argument (automatically set by the step) as well as any arguments given to the options argument to the step. Relevant options include chains, iter, cores, and arguments for the priors (see the links in the References below). prior_intercept is the argument that has the most effect on the amount of shrinkage.

References

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

"Hierarchical Partial Pooling for Repeated Binary Trials" https://tinyurl.com/stan-pooling

"Prior Distributions for `rstanarm`` Models" https://tinyurl.com/stan-priors

"Estimating Generalized (Non-)Linear Models with Group-Specific Terms with rstanarm" https://tinyurl.com/stan-glm-grouped

Examples

Run this code
# NOT RUN {
library(recipes)
library(dplyr)

data(okc)

reencoded <- recipe(Class ~ age + location, data = okc) %>%
  step_lencode_bayes(location, outcome = vars(Class))

# See https://tidymodels.github.io/embed/ for examples
# }

Run the code above in your browser using DataLab