Out-of-fold target encoding with credibility
target_encode.RdEncodes a high-cardinality factor by each level's actual-to-expected
ratio, shrunk to 1 by the credibility z = n / (n + k) with n the
level's exposure: z A / E + (1 - z) (CAS Monograph 16, chapter 6). Each
training row is encoded from the other folds only, so its own response
never enters its encoding; encode_target() applies the encoding from all
the rows to new data (a level not seen in training gets 1). Its log
enters a GLM as a numeric column.
Usage
target_encode(values, y, expected, splits, k, exposure = NULL)
encode_target(encoding, newvalues)Arguments
- values
The factor's level per row.
- y
Actual responses.
- expected
Expected responses: the overall mean times exposure, or a first model's predictions.
- splits
Splits from
k_fold()orgroup_k_fold(), every row in exactly one test set.- k
The credibility constant.
- exposure
Optional credibility weights, 1 per row by default.
- encoding
A
target_encodinglist fromtarget_encode().- newvalues
Levels of new data.