Automate you work

Krystian Igras

2018-12-12

This document presents how you can customize xspliner usage to you make it more easier and automated.

Hierarchy of parameters

Local transition and effect

In previous sections we learned how to specify parameters for the effect and transition of single variable. Let’s name them local parameters.

A quick example you can see below:

## 
## Call:
## glm(formula = Petal.Width ~ Sepal.Length + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.66915  -0.07150  -0.02611   0.10336   0.45972  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.29722    0.21258  -6.102 9.06e-09 ***
## Sepal.Length           0.03266    0.03549   0.920    0.359    
## xs(Petal.Length)       1.85065    0.35508   5.212 6.31e-07 ***
## xf(Species)versicolor  0.04001    0.17915   0.223    0.824    
## xf(Species)virginica   0.34864    0.23755   1.468    0.144    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.03100874)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.4963  on 145  degrees of freedom
## AIC: -88.427
## 
## Number of Fisher Scoring iterations: 2

When the black box model is based on higher amount of variables it can be problematic to specify local parameters for each predictor. Also formula becomes large and hard to read.

Global transition and effect

Its more common that we use similar configuration for each variable, as it’s simple and allows us to build and experiment faster. To do this you can specify xs_opts and xf_opts of xspline function. Let’s name them global parameters.

Each of global parameters can specify effect and transition, that should be used for xs and xf transformations respectively. Then you just need to use base xs and xf symbols without parameters:

## 
## Call:
## glm(formula = Petal.Width ~ Sepal.Length + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.66915  -0.07150  -0.02611   0.10336   0.45972  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.29722    0.21258  -6.102 9.06e-09 ***
## Sepal.Length           0.03266    0.03549   0.920    0.359    
## xs(Petal.Length)       1.85065    0.35508   5.212 6.31e-07 ***
## xf(Species)versicolor  0.04001    0.17915   0.223    0.824    
## xf(Species)virginica   0.34864    0.23755   1.468    0.144    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.03100874)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.4963  on 145  degrees of freedom
## AIC: -88.427
## 
## Number of Fisher Scoring iterations: 2

But still you can specify local parameters that override the global ones.

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67393  -0.07675  -0.03086   0.09725   0.46086  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.40641    0.24939  -5.639 8.64e-08 ***
## xs(Sepal.Length)       0.28291    0.29806   0.949    0.344    
## xs(Petal.Length)       1.80983    0.37825   4.785 4.17e-06 ***
## xf(Species)versicolor  0.05159    0.18386   0.281    0.779    
## xf(Species)virginica   0.36727    0.24519   1.498    0.136    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.03099724)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.4946  on 145  degrees of freedom
## AIC: -88.482
## 
## Number of Fisher Scoring iterations: 2

In this case last_evaluation variable will be transformed with thin plate regression spline (bs = "tp" is default for mgcv::s) with basis dimension equal to 10. At the same time average_monthly_hours will be transformed with cubic splines.

What if you forget specifying local and global parameters?

Default transition and effect

As you can see in project objects reference, you may find there xs_opts_edfault and xf_opts_default objects. These objects specify default parameters for xs and xf transformations.

## $effect
## $effect$type
## [1] "pdp"
## 
## 
## $transition
## $transition$alter
## [1] "always"
## 
## $transition$monotonic
## [1] "not"
## $effect
## $effect$type
## [1] "ice"
## 
## 
## $transition
## $transition$alter
## [1] "always"
## 
## $transition$stat
## [1] "GIC"
## 
## $transition$value
## [1] 3

Default parameters are overwritten by global and local ones. So the hierarchy of parameters importance is as follows:

LOCAL > GLOBAL > DEFAULT

But having model based on the huge amount of variables still requires a lot of our work to build the final model. Especially we need to use xs and xf symbols for each variable. How can we make it easier?

Automatic transformation

Transform each formula predictor

If you want to transform each predictor of specified formula and not using xs and xf variables you can omit it using consider parameter for xspline function.

Possible values are "specials" the default one and "all". For automatic transformation of each variable without specifying xs and xf symbols just set consider = "all" and pass standard formula into xspline function:

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)       0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)       1.75429    0.37109   4.727 5.33e-06 ***
## xf(Species)versicolor  0.08059    0.17954   0.449   0.6542    
## xf(Species)virginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

Then each predictor is transformed with xs and xf symbols and use of default parameters or global ones when specified.

xs is used for integer and numeric variables - xf for factors.

By default xspline function tries to extract the data from model (rf_model) call and xspline’s parent.frame() environment then uses it to determine predictor types. So to be sure that variable types are sourced correctly a good practice here is to add data parameter, storing black box’s training data.

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)       0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)       1.75429    0.37109   4.727 5.33e-06 ***
## xf(Species)versicolor  0.08059    0.17954   0.449   0.6542    
## xf(Species)virginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

Transform only continuous or discrete variables

In some cases you may want to transform only quantitative or qualitative predictors. Looking into default parameters xs_opts_default and xf_opts_default we may find alter parameter for transition.

The parameter is used to specify if predictor for which xs or xf was specified needs to be transformed or used as a bare variable in formula. You can specify it in the local or global transition parameter. In this case using the global one is more reasonable.

So, in order to transform only continuous variables just set alter = "always" (what is default) for xs_opts and alter = "never" for xf_opts:

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     Species, family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)   0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)   1.75429    0.37109   4.727 5.33e-06 ***
## Speciesversicolor  0.08059    0.17954   0.449   0.6542    
## Speciesvirginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

For transformation of factors only:

## 
## Call:
## glm(formula = Petal.Width ~ Sepal.Length + Petal.Length + xf(Species), 
##     family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.63796  -0.07848  -0.01257   0.09863   0.47749  
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -0.084592   0.172136  -0.491  0.62387    
## Sepal.Length          -0.001693   0.044135  -0.038  0.96945    
## Petal.Length           0.231921   0.052796   4.393 2.15e-05 ***
## xf(Species)versicolor  0.432659   0.125048   3.460  0.00071 ***
## xf(Species)virginica   0.834121   0.173212   4.816 3.65e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.03249376)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.7116  on 145  degrees of freedom
## AIC: -81.41
## 
## Number of Fisher Scoring iterations: 2

Even having learned the above listed options, we still can have problems with handling model with a huge amount of variables.

Model based dot formula

For many existing models in R we usually can specify “dot formulas”, when used predictors are sourced from provided data. xspliner can also handle the form. Let’s return here for iris random forest model.

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)       0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)       1.75429    0.37109   4.727 5.33e-06 ***
## xf(Species)versicolor  0.08059    0.17954   0.449   0.6542    
## xf(Species)virginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

Good practice here is to provide data parameter as well to detect predictors classes, and model type (classification or regression).

How predictors are sourced?

xspline function tries to establish data in the following order (if any way is not possible, it tries the next one):

To assure correct predictors usage, you may also specify predictor names vector in predictors parameter, and data (optional) to assure source of variable classes:

## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)       0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)       1.75429    0.37109   4.727 5.33e-06 ***
## xf(Species)versicolor  0.08059    0.17954   0.449   0.6542    
## xf(Species)virginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

In above examples each predictor is transformed by default. You can exclude needed, by specifying global alter = "never" parameters, or bare.

This way we are ready to work with many predictors. Can the approach be simpler?

Omit formula

As we could see in previous section, using dot formula the predictors are sourced from provided black box. Why cannot we fully extract formula from black box? We can.

Let’s use previously built rf_iris model:

model_xs <- xspline(rf_iris)
summary(model_xs)
## 
## Call:
## glm(formula = Petal.Width ~ xs(Sepal.Length) + xs(Petal.Length) + 
##     xf(Species), family = family, data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.67652  -0.07571  -0.03050   0.09563   0.46918  
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -1.39649    0.25141  -5.555 1.29e-07 ***
## xs(Sepal.Length)       0.31020    0.30086   1.031   0.3042    
## xs(Petal.Length)       1.75429    0.37109   4.727 5.33e-06 ***
## xf(Species)versicolor  0.08059    0.17954   0.449   0.6542    
## xf(Species)virginica   0.40542    0.23986   1.690   0.0931 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0310788)
## 
##     Null deviance: 86.5699  on 149  degrees of freedom
## Residual deviance:  4.5064  on 145  degrees of freedom
## AIC: -88.088
## 
## Number of Fisher Scoring iterations: 2

Works! Can it be simpler? Actually not because of black box based transformation and theory, but we can provide some model based parameters upfront using DALEX’s explainer object (see next section).

Unfortunately this simplicity works thanks to a few approaches that randomForest follows.

They are:

Most of R based ML models satisfy the above condition. One of exceptions is XGBoost which in actual xspliner version needs to be handled in the more complex xspline call. You can see it in use cases

Excluding predictors from transformation

For this example consider again Boston Housing Data from pdp package, and build simple svm model for predicting chas variable:

##  int [1:506] 1 2 2 3 3 3 5 5 5 5 ...
## [1]  1  2  3  5  4  8  6  7 24

As we can see rad variable is integer and has only 9 unique values. As a result spline approximation may be misleading, and not possible to perform. We decide here to omit rad variable when performing transformation, nevertheless remaining predictors should be transformed.

At first setup model based transformation:

## Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots): A term has fewer unique covariate combinations than specified maximum degrees of freedom

As we can see, the error was returned due to the form of rad variable.

How to exclude rad from transformation? We can use xspline’s bare parameter, responsible for specifying predictor which shouldn’t be transformed.

## 
## Call:
## glm(formula = chas ~ xs(cmedv) + rad + xs(lstat), family = family, 
##     data = data)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -0.8193  -0.3415  -0.3391  -0.3367   2.4276  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)   
## (Intercept) 23.9680458 30.3303985   0.790  0.42939   
## xs(cmedv)   23.8760057  9.2352658   2.585  0.00973 **
## rad         -0.0008441  0.0214852  -0.039  0.96866   
## xs(lstat)   -3.5136063 28.8592214  -0.122  0.90310   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 254.5  on 505  degrees of freedom
## Residual deviance: 244.4  on 502  degrees of freedom
## AIC: 252.4
## 
## Number of Fisher Scoring iterations: 5

As we can see in the summary, rad variable was omitted during transformation.

Note

The option is available only for xspline build on top of model or dot formula.

Integration with DALEX

As mentioned in the previous section, xspliner is integrated with DALEX package. The main function from the package explain returns useful black box data (such as bare black model or training data) that can be used by xspline function.

Just check below example

library(DALEX)
rf_boston <- randomForest(lstat ~ cmedv + crim + chas, data = boston)
explainer <- explain(rf_boston, label = "boston")
model <- xspline(
  explainer
)
summary(model)
## 
## Call:
## glm(formula = lstat ~ xs(cmedv) + xs(crim) + xf(chas), family = family, 
##     data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -11.5973   -2.3902   -0.6499    1.7778   21.2226  
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -15.46194    1.40794 -10.982  < 2e-16 ***
## xs(cmedv)     1.61985    0.06909  23.445  < 2e-16 ***
## xs(crim)      0.58862    0.14018   4.199 3.17e-05 ***
## xf(chas)1     1.40692    0.69515   2.024   0.0435 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 15.4446)
## 
##     Null deviance: 25752.4  on 505  degrees of freedom
## Residual deviance:  7753.2  on 502  degrees of freedom
## AIC: 2827
## 
## Number of Fisher Scoring iterations: 2

You can provide your own xspline’s parameters that overwrite that sourced from explainer.