Data for HR example

In the following vignette, we will walk through a multilabel classification example with DALEX. The purpose of this tutorial is that for some of DALEX functionalities binary classification is a default one, and therefore we need to put some self-made code to work here. All of the examples will be performed with HR dataset that is available in DALEX, it’s target column is status with three-level factor. For all cases our model will be ranger.

#>   gender      age    hours evaluation salary   status
#> 1   male 32.58267 41.88626          3      1    fired
#> 2 female 41.21104 36.34339          2      5    fired
#> 3   male 37.70516 36.81718          3      0    fired
#> 4 female 30.06051 38.96032          3      2    fired
#> 5   male 21.10283 62.15464          5      3 promoted
#> 6   male 40.11812 69.53973          2      0    fired

Creation of model and explainer

Ok, now it is time to create a model.

library("ranger")
model_HR_ranger <- ranger(status~.,  data = HR, probability = TRUE, num.trees = 50)
model_HR_ranger
#> Ranger result
#> 
#> Call:
#>  ranger(status ~ ., data = HR, probability = TRUE, num.trees = 50) 
#> 
#> Type:                             Probability estimation 
#> Number of trees:                  50 
#> Sample size:                      7847 
#> Number of independent variables:  5 
#> Mtry:                             2 
#> Target node size:                 10 
#> Variable importance mode:         none 
#> Splitrule:                        gini 
#> OOB prediction error (Brier s.):  0.2182466
library("DALEX")
explain_HR_ranger <- explain(model_HR_ranger,
                              data = HR[,-6],
                              y = HR$status,
                              label = "Ranger Multilabel Classification",
                              colorize = FALSE)
#> Preparation of a new explainer is initiated
#>   -> model label       :  Ranger Multilabel Classification 
#>   -> data              :  7847  rows  5  cols 
#>   -> target variable   :  7847  values 
#>   -> predict function  :  yhat.ranger  will be used (  default  )
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package ranger , ver. 0.12.1 , task multiclass (  default  ) 
#>   -> predicted values  :  predict function returns multiple columns:  3  (  default  ) 
#>   -> residual function :  difference between 1 and probability of true class (  default  )
#>   -> residuals         :  numerical, min =  0 , mean =  0.2800496 , max =  0.8999456  
#>   A new explainer has been created!

The sixth column, that we have omitted during the creation of the explainer, stands for the target column (status). It is good practice not to put it in data. Keep in mind that the default yhat function for ranger, and for any other package that is supported by DALEX, enforces probability output. Therefore residuals cannot be standard \(y - \hat{y}\). Since DALEX 1.2.2 in the case of multiclass classification one minus probability of the TRUE class is a standard residual function.

Model Parts

In order to use model_parts() (former variable_importance()) function it is necessary to switch default loss_function argument to one that handle multiple classes. DALEX has implemented one function like that and it is called loss_cross_entropy(). To use it, y parameter passed to explain function should have exactly the same format as the target vector used for the training process (ie. the same number of levels and names of those levels).

Also, we need probability outputs so there is no need to change the default predict_function parameter.

library("DALEX")
explain_HR_ranger_new_y <- explain(model_HR_ranger,
                              data = HR[,-6],
                              y = HR$status,
                              label = "Ranger Multilabel Classification",
                              colorize = FALSE)
#> Preparation of a new explainer is initiated
#>   -> model label       :  Ranger Multilabel Classification 
#>   -> data              :  7847  rows  5  cols 
#>   -> target variable   :  7847  values 
#>   -> predict function  :  yhat.ranger  will be used (  default  )
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package ranger , ver. 0.12.1 , task multiclass (  default  ) 
#>   -> predicted values  :  predict function returns multiple columns:  3  (  default  ) 
#>   -> residual function :  difference between 1 and probability of true class (  default  )
#>   -> residuals         :  numerical, min =  0 , mean =  0.2800496 , max =  0.8999456  
#>   A new explainer has been created!

And now we can use model_parts()

mp <- model_parts(explain_HR_ranger_new_y, loss_function = loss_cross_entropy)
plot(mp)

As we see above, we can enjoy perfectly fine variable importance plot.

Model Profile

There is no need for tricks in order to use model_profile() (former variable_effect()). Our target will be one-hot-encoded, and all of the explanations will be performed for each of class separately.

partial_dependency

mp_p <- model_profile(explain_HR_ranger, variables = "salary", type = "partial")
mp_p$color <- "_label_"
plot(mp_p)

accumulated_dependency

mp_a <- model_profile(explain_HR_ranger, variables = "salary", type = "accumulated")
mp_a$color = "_label_"
plot(mp_a)

Instance level explanations

As above, predict_parts() (former variable_attribution()) works perfectly fine with multilabel classification and default explainer. Just like before, our target will be split into variables standing for each factor level and computations will be performed then.

break_down

bd <- predict_parts(explain_HR_ranger, HR[1,], type = "break_down")
plot(bd)

shap

shap <- predict_parts(explain_HR_ranger, HR[1,], type = "shap")
plot(shap)

model_performance and predict_diagnostics

The description of those two functions is merged into one paragraph because they require the same action to get them to work with multilabel classification. The most important thing here is to realize that both functions are based on residuals. Since DALEX 1.2.2, explain function recognizes if a model is a multiclass classification task and uses a dedicated residual function as default.

Model Performance

(mp <- model_performance(explain_HR_ranger))
#> Measures for:  multiclass
#> micro_F1   : 0.8703963 
#> macro_F1   : 0.8684409 
#> w_macro_F1 : 0.8692607 
#> accuracy   : 0.8703963 
#> w_macro_auc: 0.9769866
#> 
#> Residuals:
#>         0%        10%        20%        30%        40%        50%        60% 
#> 0.00000000 0.02842815 0.06092220 0.11686884 0.17856637 0.24848705 0.31911217 
#>        70%        80%        90%       100% 
#> 0.39883627 0.48658659 0.59915478 0.89994559
plot(mp)

Predict diagnostics

pd_all <- predict_diagnostics(explain_HR_ranger, HR[1,])
plot(pd_all)

pd_salary <- predict_diagnostics(explain_HR_ranger, HR[1,], variables = "salary")
plot(pd_salary)

Session info

#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Catalina 10.15.7
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ranger_0.12.1 DALEX_2.1.0  
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.5        gower_0.2.2       compiler_4.0.3    pillar_1.4.7     
#>  [5] ingredients_2.0   tools_4.0.3       digest_0.6.27     lattice_0.20-41  
#>  [9] evaluate_0.14     memoise_1.1.0     lifecycle_0.2.0   tibble_3.0.4     
#> [13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.9       Matrix_1.2-18    
#> [17] yaml_2.2.1        pkgdown_1.6.1     xfun_0.18         stringr_1.4.0    
#> [21] dplyr_1.0.2       knitr_1.30        desc_1.2.0        generics_0.0.2   
#> [25] fs_1.5.0          vctrs_0.3.5       systemfonts_0.3.2 rprojroot_2.0.2  
#> [29] grid_4.0.3        tidyselect_1.1.0  glue_1.4.2        R6_2.5.0         
#> [33] textshaping_0.1.2 iBreakDown_1.3.1  rmarkdown_2.5     farver_2.0.3     
#> [37] ggplot2_3.3.2     purrr_0.3.4       magrittr_2.0.1    scales_1.1.1     
#> [41] htmltools_0.5.0   ellipsis_0.3.1    assertthat_0.2.1  colorspace_2.0-0 
#> [45] labeling_0.4.2    ragg_0.4.0        stringi_1.5.3     munsell_0.5.0    
#> [49] crayon_1.3.4