Introduction to localModel package"

  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  error = FALSE

This vignette shows how localModel package can be used to explain regression models. We will use the apartments dataset from DALEX2 package. For more information about the dataset, please refer to the Gentle introduction to DALEX.

We will need localModel and DALEX2 packages. Random forest from randomForest package will serve as an example model, and linear regression as a simple model for comparison.

mrf <- randomForest(m2.price ~., data = apartments, ntree = 50)
mlm <- lm(m2.price ~., data = apartments)

First, we need to create an explainer object, using the explain function. We will explain the prediction for fifth observation in the test dataset.

explainer <- DALEX::explain(model = mrf,
                     data = apartments_test[, -1])
explainer2 <- DALEX::explain(model = mlm, 
                      data = apartments_test[, -1])
new_observation <- apartments_test[5, -1]

Local explanation is created via individual_surrogate_model function, which takes the explainer, observation of interest and number of new observations to simulate as argument. Optionally, we can set seed using the seed parameter for reproducibility.

model_lok <- individual_surrogate_model(explainer, new_observation,
                                        size = 500, seed = 17)
model_lok2 <- individual_surrogate_model(explainer2, new_observation,
                                         size = 500, seed = 17)

First, local interpretable features are created. Numerical features are discretized by using decision tree to model relationship between the feature and the corresponding Ceteris Paribus profile. Categorical features are also discretized by merging levels using the marginal relationship between the feature and the model response. Then, new dataset is simulated by switching a random number of interpretable inputs in the explained instance. This procedure mimics "graying out" areas (superpixels) in the original LIME method. LASSO regression model is fitted to the model response for these new observations, which makes the final explanations sparse and thus readable. More details can be found in the Methodology behind localModel package vignette.

The explanation can be plotted using generic plot function. The plot shows interpretable features and weights associated with them, starting at the model intercept. Negative weights are associated with features that decrease the apartment price, while positive weights increase it. We can plot explanation for two or more models together by passing several local explainer to plot function.

plot(model_lok, model_lok2)

We can see that for this observation, the price predicted by Random Forest is negatively influeced mostly by the district and construction year, while linear regression ignores the effect of construction year.

Try the localModel package in your browser

Any scripts or data that you put into this service are public.

localModel documentation built on Sept. 14, 2021, 5:12 p.m.