7.5 R Code Examples

Example uses titanic dataset, and new instance of a fictitious passenger, Henry.

We’re also using the DALEX package to explain predictions from a pre-loaded random forest model.

Load the data.

titanic_imputed <- archivist::aread("pbiecek/models/27e5c")
titanic_rf <- archivist:: aread("pbiecek/models/4e0fc")
(henry <- archivist::aread("pbiecek/models/a6538"))
##   class gender age sibsp parch fare  embarked
## 1   1st   male  47     0     0   25 Cherbourg

Build explainer from DALEX library.

library(DALEX)
library(randomForest)


explain_rf <- DALEX::explain(model = titanic_rf,  
                        data = titanic_imputed[, -9],
                           y = titanic_imputed$survived == "yes", 
                       label = "Random Forest")
## Preparation of a new explainer is initiated
##   -> model label       :  Random Forest 
##   -> data              :  2207  rows  8  cols 
##   -> target variable   :  2207  values 
##   -> predict function  :  yhat.randomForest  will be used (  default  )
##   -> predicted values  :  No value for predict function target column. (  default  )
##   -> model_info        :  package randomForest , ver. 4.7.1.1 , task classification (  default  ) 
##   -> model_info        :  Model info detected classification task but 'y' is a logical . Converted to numeric.  (  NOTE  )
##   -> predicted values  :  numerical, min =  0 , mean =  0.2353095 , max =  1  
##   -> residual function :  difference between y and yhat (  default  )
##   -> residuals         :  numerical, min =  -0.892 , mean =  0.0868473 , max =  1  
##   A new explainer has been created!

Calculate contributions of top variables.

bd_rf <- predict_parts(explainer = explain_rf,
                 new_observation = henry,
                            type = "break_down_interactions")
bd_rf
##                                             contribution
## Random Forest: intercept                           0.235
## Random Forest: class = 1st                         0.185
## Random Forest: gender = male                      -0.124
## Random Forest: embarked:fare = Cherbourg:25        0.107
## Random Forest: age = 47                           -0.125
## Random Forest: sibsp = 0                          -0.032
## Random Forest: parch = 0                          -0.001
## Random Forest: prediction                          0.246

Output break-down plot.

plot(bd_rf)

Compare to break-down plot without interactions.

bd_rf_noint <- predict_parts(explainer = explain_rf,
                 new_observation = henry,
                            type = "break_down")
plot(bd_rf_noint)