Multi Class vtreat

John Mount

2018-07-20

Problem: try to prepare data to model multi-class y as a function of x using vtreat. vtreat does not directly do this, but can be used to do this. In this note we share functions to make this adaption.

library("vtreat")

The two functions needed (mkCrossFrameMExperiment() and the S3 method prepare.multinomial_plan()) are (as of version 1.2.4) part of vtreat.

Our specific example: try to model multi-class y as a function of x1 and x2.

# create example data
set.seed(326346)
sym_bonuses <- rnorm(3)
names(sym_bonuses) <- c("a", "b", "c")
n_row <- 1000
d <- data.frame(x1 = rnorm(n_row),
                x2 = sample(names(sym_bonuses), n_row, replace = TRUE),
                y = "NoInfo",
                stringsAsFactors = FALSE)
d$y[d$x1 > sym_bonuses[d$x2] + runif(n_row)] <- "Large1"
d$y[sym_bonuses[d$x2] > d$x1 + runif(n_row)] <- "Large2"

knitr::kable(head(d))
x1 x2 y
-1.5144859 a Large2
0.0219667 a NoInfo
2.0183914 c Large1
0.8178292 b Large1
0.5867139 b Large1
-0.6711920 b NoInfo

We define the problem controls and use mkCrossFrameMExperiment() to build both a cross-frame and a treatment plan.

# define problem
vars <- c("x1", "x2")
y_name <- "y"
y_levels <- sort(unique(d[[y_name]]))

# build the multi-class cross frame and treatments
cfe_m <- mkCrossFrameMExperiment(d, vars, y_name)

The cross-frame is the entity safest for training on (unless you have made separate data split for the treatment design step). It uses cross-validation to reduce nested model bias. Some notes on this issue are available here, and here.

# look at the data we would train models on
str(cfe_m$cross_frame)
## 'data.frame':    1000 obs. of  9 variables:
##  $ x1_clean      : num  -1.514 0.022 2.018 0.818 0.587 ...
##  $ x2_catP       : num  0.325 0.325 0.362 0.313 0.313 0.313 0.313 0.313 0.325 0.362 ...
##  $ x2_lev_x_a    : num  1 1 0 0 0 0 0 0 1 0 ...
##  $ x2_lev_x_b    : num  0 0 0 1 1 1 1 1 0 0 ...
##  $ x2_lev_x_c    : num  0 0 1 0 0 0 0 0 0 1 ...
##  $ Large1_x2_catB: num  -0.855 -1.062 0.4 0.504 0.424 ...
##  $ Large2_x2_catB: num  0.738 0.855 -0.478 -0.44 -0.44 ...
##  $ NoInfo_x2_catB: num  0.015 0.015 0.0804 -0.0968 -0.1434 ...
##  $ y             : chr  "Large2" "NoInfo" "Large1" "Large1" ...

prepare_m() is a function that can apply the designed treatments to new data. Here we are simulating new data by re-using our design data.

# pretend original data is new data to be treated
# NA out top row to show processing
for(vi in vars) {
  d[[vi]][[1]] <- NA
}
str(prepare(cfe_m$treat_m, d))
## 'data.frame':    1000 obs. of  9 variables:
##  $ x1_clean      : num  0.0219 0.022 2.0184 0.8178 0.5867 ...
##  $ x2_catP       : num  0.0005 0.325 0.362 0.313 0.313 0.313 0.313 0.313 0.325 0.362 ...
##  $ x2_lev_x_a    : num  0 1 0 0 0 0 0 0 1 0 ...
##  $ x2_lev_x_b    : num  0 0 0 1 1 1 1 1 0 0 ...
##  $ x2_lev_x_c    : num  0 0 1 0 0 0 0 0 0 1 ...
##  $ Large1_x2_catB: num  0 -0.974 0.345 0.452 0.452 ...
##  $ Large2_x2_catB: num  0 0.827 -0.546 -0.515 -0.515 ...
##  $ NoInfo_x2_catB: num  0 0.0744 0.0294 -0.1149 -0.1149 ...
##  $ y             : chr  "Large2" "NoInfo" "Large1" "Large1" ...

Obvious issues include: computing variable importance, and blow up and co-dependency of produced columns. These we leave for the next modeling step to deal with (this is our philosophy with most issues that involve joint distributions of variables).

We to have a start on variable importance.

knitr::kable(cfe_m$score_frame)
varName varMoves rsq sig outcome_level needsSplit extraModelDegrees origName code
x1_clean TRUE 0.5637189 0.0000000 Large1 FALSE 0 x1 clean
x2_catP TRUE 0.0035298 0.0287344 Large1 TRUE 2 x2 catP
x2_lev_x_a TRUE 0.0636457 0.0000000 Large1 FALSE 0 x2 lev
x2_lev_x_b TRUE 0.0172594 0.0000013 Large1 FALSE 0 x2 lev
x2_lev_x_c TRUE 0.0124961 0.0000387 Large1 FALSE 0 x2 lev
x1_clean TRUE 0.5621028 0.0000000 Large2 FALSE 0 x1 clean
x2_catP TRUE 0.0082667 0.0020257 Large2 TRUE 2 x2 catP
x2_lev_x_a TRUE 0.0717018 0.0000000 Large2 FALSE 0 x2 lev
x2_lev_x_b TRUE 0.0163417 0.0000143 Large2 FALSE 0 x2 lev
x2_lev_x_c TRUE 0.0223694 0.0000004 Large2 FALSE 0 x2 lev
x1_clean TRUE 0.0127639 0.0000602 NoInfo FALSE 0 x1 clean
x2_catP TRUE 0.0002601 0.5668496 NoInfo TRUE 2 x2 catP
x2_lev_x_a TRUE 0.0004739 0.4394474 NoInfo FALSE 0 x2 lev
x2_lev_x_b TRUE 0.0010108 0.2588675 NoInfo FALSE 0 x2 lev
x2_lev_x_c TRUE 0.0000863 0.7414693 NoInfo FALSE 0 x2 lev
Large1_x2_catB TRUE 0.0604250 0.0000000 Large1 TRUE 2 x2 catB
Large2_x2_catB TRUE 0.0606098 0.0000000 Large2 TRUE 2 x2 catB
NoInfo_x2_catB TRUE 0.0001461 0.6677701 NoInfo TRUE 2 x2 catB

One can relate these per-target and per-treatment performances back to original columns b aggregating.

tapply(cfe_m$score_frame$rsq, cfe_m$score_frame$origName, max)
##         x1         x2 
## 0.56371889 0.07170178
tapply(cfe_m$score_frame$sig, cfe_m$score_frame$origName, min)
##            x1            x2 
## 3.759634e-168  1.585749e-20