ceteris_paribus: Ceteris Paribus Explainer

Description Usage Arguments Value Examples

View source: R/ceteris_paribus.R

Description

This function calculate ceteris paribus profiles for selected data points.

Usage

1
2
3
4
5
6
7
8
ceteris_paribus(
  explainer,
  observations,
  y = NULL,
  variable_splits = NULL,
  variables = NULL,
  grid_points = 101
)

Arguments

explainer

a model to be explained, preprocessed by function 'DALEX::explain()'.

observations

set of observarvation for which profiles are to be calculated

y

true labels for 'observations'. If specified then will be added to ceteris paribus plots.

variable_splits

named list of splits for variables, in most cases created with 'calculate_variable_splits()'. If NULL then it will be calculated based on validation data avaliable in the 'explainer'.

variables

names of variables for which profiles shall be calculated. Will be passed to 'calculate_variable_splits()'. If NULL then all variables from the validation data will be used.

grid_points

number of points for profile. Will be passed to 'calculate_variable_splits()'.

Value

An object of the class 'ceteris_paribus_explainer'. It's a data frame with calculated average responses.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
library("DALEX")
 ## Not run: 
library("randomForest")
set.seed(59)

apartments_rf_model <- randomForest(m2.price ~ construction.year + surface + floor +
      no.rooms + district, data = apartments)

explainer_rf <- explain(apartments_rf_model,
      data = apartmentsTest[,2:6], y = apartmentsTest$m2.price)

apartments_small <- select_sample(apartmentsTest, 10)

cp_rf <- ceteris_paribus(explainer_rf, apartments_small)
cp_rf

cp_rf <- ceteris_paribus(explainer_rf, apartments_small, y = apartments_small$m2.price)
cp_rf

## End(Not run)

Example output

Loading required package: ggplot2
Loading required package: gower
Welcome to DALEX (version: 2.0.1).
Find examples and detailed introduction at: https://pbiecek.github.io/ema/

randomForest 4.6-14
Type rfNews() to see new features/changes/bug fixes.

Attaching package:randomForestThe following object is masked frompackage:ggplot2:

    margin

Preparation of a new explainer is initiated
  -> model label       :  randomForest  ( [33m default [39m )
  -> data              :  9000  rows  5  cols 
  -> target variable   :  9000  values 
  -> predict function  :  yhat.randomForest  will be used ( [33m default [39m )
  -> predicted values  :  numerical, min =  2010.982 , mean =  3505.971 , max =  5840.604  
  -> model_info        :  package randomForest , ver. 4.6.14 , task regression ( [33m default [39m ) 
  -> residual function :  difference between y and yhat ( [33m default [39m )
  -> residuals         :  numerical, min =  -727.4451 , mean =  5.552194 , max =  1262.554  
 [32m A new explainer has been created! [39m 
Top profiles    : 
       m2.price construction.year surface floor no.rooms    district   _yhat_
9707       5670              1920      98     3        3 Srodmiescie 4833.992
9707.1     5670              1921      98     3        3 Srodmiescie 4893.628
9707.2     5670              1922      98     3        3 Srodmiescie 4901.514
9707.3     5670              1923      98     3        3 Srodmiescie 4897.756
9707.4     5670              1923      98     3        3 Srodmiescie 4897.756
9707.5     5670              1924      98     3        3 Srodmiescie 4868.728
                 _vname_ _ids_      _label_
9707   construction.year  9707 randomForest
9707.1 construction.year  9707 randomForest
9707.2 construction.year  9707 randomForest
9707.3 construction.year  9707 randomForest
9707.4 construction.year  9707 randomForest
9707.5 construction.year  9707 randomForest


Top observations:
     m2.price construction.year surface floor no.rooms    district   _yhat_
9707     5670              2008      98     3        3 Srodmiescie 4665.263
9796     2696              1932     110    10        4       Ursus 2883.856
9644     3466              1980      73    10        2     Mokotow 3798.682
7567     2818              1940      63     8        3       Praga 3121.349
4090     3803              1955     105     3        3      Ochota 3903.151
8594     3643              1999      36     9        2       Ursus 3601.772
          _label_
9707 randomForest
9796 randomForest
9644 randomForest
7567 randomForest
4090 randomForest
8594 randomForest
Top profiles    : 
       m2.price construction.year surface floor no.rooms    district   _yhat_
9707       5670              1920      98     3        3 Srodmiescie 4833.992
9707.1     5670              1921      98     3        3 Srodmiescie 4893.628
9707.2     5670              1922      98     3        3 Srodmiescie 4901.514
9707.3     5670              1923      98     3        3 Srodmiescie 4897.756
9707.4     5670              1923      98     3        3 Srodmiescie 4897.756
9707.5     5670              1924      98     3        3 Srodmiescie 4868.728
                 _vname_ _ids_      _label_
9707   construction.year  9707 randomForest
9707.1 construction.year  9707 randomForest
9707.2 construction.year  9707 randomForest
9707.3 construction.year  9707 randomForest
9707.4 construction.year  9707 randomForest
9707.5 construction.year  9707 randomForest


Top observations:
     m2.price construction.year surface floor no.rooms    district   _yhat_
9707     5670              2008      98     3        3 Srodmiescie 4665.263
9796     2696              1932     110    10        4       Ursus 2883.856
9644     3466              1980      73    10        2     Mokotow 3798.682
7567     2818              1940      63     8        3       Praga 3121.349
4090     3803              1955     105     3        3      Ochota 3903.151
8594     3643              1999      36     9        2       Ursus 3601.772
      _y_      _label_
9707 5670 randomForest
9796 2696 randomForest
9644 3466 randomForest
7567 2818 randomForest
4090 3803 randomForest
8594 3643 randomForest

ceterisParibus documentation built on March 31, 2020, 5:22 p.m.