individual_variable_effect: Individual Variable Effect

Description Usage Arguments Value Examples

View source: R/individual_variable_effect.R

Description

Individual Variable Effect

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
individual_variable_effect(x, ...)

## S3 method for class 'explainer'
individual_variable_effect(
  x,
  new_observation,
  method = "KernelSHAP",
  nsamples = "auto",
  ...
)

## Default S3 method:
individual_variable_effect(
  x,
  data,
  predict_function = predict,
  new_observation,
  label = tail(class(x), 1),
  method = "KernelSHAP",
  nsamples = "auto",
  ...
)

shap(x, ...)

Arguments

x

a model to be explained, or an explainer created with function explain.

...

other parameters.

new_observation

an observation/observations to be explained. Required for local/instance level explainers. Columns in should correspond to columns in the data argument. Data set should not contain any additional columns.

method

an estimation method of SHAP values. Currently the only availible is 'KernelSHAP'.

nsamples

number of samples or "auto". Note that number must be as integer. Use 'as.integer()'.

data

validation dataset. Used to determine univariate distributions, calculation of quantiles, correlations and so on. It will be extracted from 'x' if it’s an explainer.

predict_function

predict function that operates on the model 'x'. Since the model is a black box, the 'predict_function' is the only interface to access values from the model. It should be a function that takes at least a model 'x' and data and returns vector of predictions. If model response has more than a single number (like multiclass models) then this function should return a marix/data.frame of the size 'm' x 'd', where 'm' is the number of observations while 'd' is the dimensionality of model response. It will be extracted from 'x' if it’s an explainer.

label

name of the model. By default it’s extracted from the class attribute of the model

Value

an object of class individual_variable_effect with shap values of each variable for each new observation. Columns:

In order to use shapper with other python virtual environment following R command are required to execute reticulate::use_virtualenv("path_to_your_env") or for conda reticulate::use_conda("name_of_conda_env") before attaching shapper.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
have_shap <- reticulate::py_module_available("shap")

if(have_shap){
  library("shapper")
  library("DALEX")
  library("randomForest")
  Y_train <- HR$status
  x_train <- HR[ , -6]
  set.seed(123)
  model_rf <- randomForest(x = x_train, y = Y_train, ntree= 50)
  p_function <- function(model, data) predict(model, newdata = data, type = "prob")

  ive_rf <- individual_variable_effect(model_rf, data = x_train, predict_function = p_function,
                                     new_observation = x_train[1:2,], nsamples = 50)
  ive_rf
} else{
    print('Python testing environment is required.')
}

shapper documentation built on Aug. 28, 2020, 9:08 a.m.