Skip to contents

Applies an encoding_info recipe produced by rr_encode_factors() to new data, reproducing the training-time columns in the same order. The result is length-preserving; an out-of-vocabulary factor level triggers an error. Means encoding uses the full-sample level means stored at fit time (predict has no folds).

Usage

rr_encode_apply(newdata, encoding_info, prefix = "encode")

Arguments

newdata

A data frame containing at least the original columns.

encoding_info

The recipe returned by rr_encode_factors().

prefix

Label used in error / warning messages.

Value

A list with x, the encoded numeric matrix for newdata.

Examples

df  <- data.frame(z = rnorm(20),
                  g = factor(sample(c("a", "b", "c"), 20, TRUE)))
enc <- rr_encode_factors(df, mode = "means")
new <- data.frame(z = rnorm(3),
                  g = factor(c("a", "b", "c"), levels = c("a", "b", "c")))
rr_encode_apply(new, enc$encoding_info)$x
#>            z   g_mean_z
#> 1  0.8771848  0.1867274
#> 2  0.4537332 -0.2768466
#> 3 -0.2324641 -0.5080400