Impact Encoding with Random Intercept ModelsSource:
Encodes columns of type
PipeOpEncodeLmer() converts factor levels of each factorial column to the
estimated coefficients of a simple random intercept model.
Models are fitted with the glmer function of the lme4 package and are
of the type
target ~ 1 + (1 | factor).
If the task is a regression task, the numeric target
variable is used as dependent variable and the factor is used for grouping.
If the task is a classification task, the target variable is used as dependent variable
and the factor is used for grouping.
If the target variable is multiclass, for each level of the multiclass target variable,
binary "one vs. rest" models are fitted.
For training, multiple models can be estimated in a cross-validation scheme to ensure that the same factor level does not always result in identical values in the converted numerical feature. For prediction, a global model (which was fitted on all observations during training) is used for each factor. New factor levels are converted to the value of the intercept coefficient of the global model for prediction. NAs are ignored by the CPO.
$affect_columns functionality to only encode a subset of
columns, or only encode columns of a certain type.
Identifier of resulting object, default
List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Default
Input and output channels are inherited from
The output is the input
Task with all affected
ordered parameters encoded according to the
$state is a named
list with the
$state elements inherited from
PipeOpTaskPreproc, as well as:
Levels of the target columns.
control:: a named
List of coefficients learned via
TRUE. If "fast_optim" is
TRUE(default), a faster (up to 50 percent) optimizer from the nloptr package is used when fitting the lmer models. This uses additional stopping criteria which can give suboptimal results.
lme4::glmer. This is relatively inefficient for features with a large number of levels.
library("mlr3") poe = po("encodelmer") task = TaskClassif$new("task", data.table::data.table( x = factor(c("a", "a", "a", "b", "b")), y = factor(c("a", "a", "b", "b", "b"))), "x") poe$train(list(task))[]$data() #> x y #> 1: a -0.5525584 #> 2: a -0.5525584 #> 3: a -0.3310264 #> 4: b -0.3310264 #> 5: b -0.3310264 poe$state #> $target_levels #>  "a" "b" #> #> $control #> $control$y #> a b ..new..level.. #> -0.5525584 -0.3310264 -0.4429541 #> #> #> $dt_columns #>  "y" #> #> $affected_cols #>  "y" #> #> $intasklayout #> id type #> 1: y factor #> #> $outtasklayout #> id type #> 1: y numeric #> #> $outtaskshell #> Empty data.table (0 rows and 2 cols): x,y #>