`po('imputelearner')` generates an extra level `factor`

Viewed 18

After factor encoding imputation, I am expecting to have a feature with 2 levels but I am getting an extra level named 'factor' that no observation has it.

See example below, I really don't know if it's expected behavior or not.

library(mlr3verse)
#> Loading required package: mlr3

set.seed(42)
# `var1` is a factor with 2 levels (a, b) and 2 NA's
# It's constructed so that one NA is imputed with a and the other with b (based on `var2`)
data = data.table::data.table(y = runif(100),
  var1 = as.factor(c(NA, rep('a', 49), rep('b', 49), NA)),
  var2 = c(runif(50, min = 3, max = 5), runif(50)),
  var3 = runif(10))

task = TaskRegr$new("example", data, target = "y")
task$missings()
#>    y var1 var2 var3 
#>    0    2    0    0

imp = po('imputelearner', lrn('classif.rpart'))
pre = imp %>>% po('encode', method = 'treatment')

task2 = pre$train(task)[[1L]]
task2$missings() # var1.factor?
#>           y        var2        var3      var1.b var1.factor 
#>           0           0           0           0           0
task2$data(cols = 'var1.factor')[[1L]] # all zeros
#>   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#>  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#>  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

# happens during imputation...
task3 = imp$train(list(task))[[1L]]
task3$col_info # 'factor' level added - but no observation has it!
#>          id    type     levels label fix_factor_levels
#> 1: ..row_id integer             <NA>             FALSE
#> 2:     var1  factor a,b,factor  <NA>             FALSE
#> 3:     var2 numeric             <NA>             FALSE
#> 4:     var3 numeric             <NA>             FALSE
#> 5:        y numeric             <NA>             FALSE

Created on 2022-09-20 with reprex v2.0.2

0 Answers
Related