-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
ms-perks-features.Rmd
350 lines (267 loc) · 10.3 KB
/
ms-perks-features.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
---
title: "modelStudio - perks and features"
author: "Hubert Baniecki"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{modelStudio - perks and features}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = FALSE,
comment = "#>",
warning = FALSE,
message = FALSE
)
```
The `modelStudio()` function computes various (instance and dataset level) model explanations
and produces a customisable dashboard, which consists of multiple panels for plots with their
short descriptions. Easily save the dashboard and share it with others. Tools for
[Explanatory Model Analysis](https://ema.drwhy.ai/) unite with tools for Exploratory Data Analysis
to give a broad overview of the model behavior.
Let's use `HR` dataset to explore `modelStudio` parameters:
```{r results="hide"}
train <- DALEX::HR
train$fired <- as.factor(ifelse(train$status == "fired", 1, 0))
train$status <- NULL
head(train)
```
```{r echo = FALSE, fig.align='center'}
knitr::kable(head(train), digits = 2, caption = "DALEX::HR dataset")
```
Prepare `HR_test` data and a `ranger` model for the explainer:
```{r results="hide", eval = FALSE}
# fit a ranger model
library("ranger")
model <- ranger(fired ~., data = train, probability = TRUE)
# prepare validation dataset
test <- DALEX::HR_test[1:1000,]
test$fired <- ifelse(test$status == "fired", 1, 0)
test$status <- NULL
# create an explainer for the model
explainer <- DALEX::explain(model,
data = test,
y = test$fired)
# start modelStudio
library("modelStudio")
```
-------------------------------------------------------------------
## modelStudio parameters
### instance explanations
Pass data points to the `new_observation` parameter for instance explanations
such as [Break Down](https://ema.drwhy.ai/breakDown.html),
[Shapley Values](https://ema.drwhy.ai/shapley.html) and
[Ceteris Paribus](https://ema.drwhy.ai/ceterisParibus.html) Profiles.
Use `new_observation_y` to show their true labels.
```{r eval = FALSE}
new_observation <- test[1:3,]
rownames(new_observation) <- c("John Snow", "Arya Stark", "Samwell Tarly")
true_labels <- test[1:3,]$fired
modelStudio(explainer,
new_observation = new_observation,
new_observation_y = true_labels)
```
If `new_observation = NULL`, then choose `new_observation_n` observations, evenly spread by the order of `y_hat`. This shall always include the observations, which ids are `which.min(y_hat)` and `which.max(y_hat)`.
```{r eval = FALSE}
modelStudio(explainer, new_observation_n = 5) # default is 3
```
### grid size
Achieve bigger or smaller `modelStudio` grid with `facet_dim` parameter.
```{r eval = FALSE}
# small dashboard with 2 panels
modelStudio(explainer,
facet_dim = c(1,2))
# large dashboard with 9 panels
modelStudio(explainer,
facet_dim = c(3,3))
```
### animations
Manipulate `time` parameter to set animation length. Value 0 will make
them invisible.
```{r eval = FALSE}
# slow down animations
modelStudio(explainer,
time = 1000)
# turn off animations
modelStudio(explainer,
time = 0)
```
### more calculations means more time
- `N` is a number of observations used for calculation of
[Partial Dependence](https://ema.drwhy.ai/partialDependenceProfiles.html)
and [Accumulated Dependence](https://ema.drwhy.ai/accumulatedLocalProfiles.html) Profiles (default is `300`).
- `N_fi` is a number of observations used for calculation of
[Feature Importance](https://ema.drwhy.ai/featureImportance.html) (default is `N*10`).
- `N_sv` is a number of observations used for calculation of
[Shapley Values](https://ema.drwhy.ai/shapley.html) (default is `N*3`).
- `B` is a number of permutation rounds used for calculation of
[Shapley Values](https://ema.drwhy.ai/shapley.html) (default is `10`).
- `B_fi` is a number of permutation rounds used for calculation of
[Feature Importance](https://ema.drwhy.ai/featureImportance.html) (default is `B`).
Decrease `N` and `B` parameters to lower the computation time or increase
them to get more accurate empirical results.
```{r eval = FALSE}
# faster, less precise
modelStudio(explainer,
N = 200, B = 5)
# slower, more precise
modelStudio(explainer,
N = 500, B = 15)
```
### no EDA mode
Don't compute the EDA plots if they are not needed. Set the `eda` parameter to `FALSE`.
```{r eval = FALSE}
modelStudio(explainer,
eda = FALSE)
```
### progress bar
Hide computation progress bar messages with `show_info` parameter.
```{r eval = FALSE}
modelStudio(explainer,
show_info = FALSE)
```
### viewer or browser?
Change `viewer` parameter to set where to display `modelStudio`.
[Best described in `r2d3` documentation](https://rstudio.github.io/r2d3/articles/visualization_options.html#viewer).
```{r eval = FALSE}
modelStudio(explainer,
viewer = "browser")
```
-------------------------------------------------------------------
## parallel computation
Speed up `modelStudio` computation by setting `parallel` parameter to `TRUE`.
It uses [`parallelMap`](https://www.rdocumentation.org/packages/parallelMap) package
to calculate local explainers faster. It is really useful when using `modelStudio` with
complicated models, vast datasets or **many observations are being processed**.
All options can be set outside of the function call.
[How to use parallelMap](https://github.com/mlr-org/parallelMap#being-lazy-configuration).
```{r eval = FALSE}
# set up the cluster
options(
parallelMap.default.mode = "socket",
parallelMap.default.cpus = 4,
parallelMap.default.show.info = FALSE
)
# calculations of local explanations will be distributed into 4 cores
modelStudio(explainer,
new_observation = test[1:16,],
parallel = TRUE)
```
--------------------------------------------------------------------
## additional options
Customize some of the `modelStudio` looks by overwriting default options returned
by the `ms_options()` function.
[Full list of options](https://modelstudio.drwhy.ai/reference/ms_options.html).
```{r eval = FALSE}
# set additional graphical parameters
new_options <- ms_options(
show_subtitle = TRUE,
bd_subtitle = "Hello World",
line_size = 5,
point_size = 9,
line_color = "pink",
point_color = "purple",
bd_positive_color = "yellow",
bd_negative_color = "orange"
)
modelStudio(explainer,
options = new_options)
```
All visual options can be changed after the calculations using `ms_update_options()`.
```{r eval = FALSE}
old_ms <- modelStudio(explainer)
old_ms
# update the options
new_ms <- ms_update_options(old_ms,
time = 0,
facet_dim = c(1,2),
margin_left = 150)
new_ms
```
-------------------------------------------------------------------
## update observations
Use `ms_update_observations()` to add more observations with their local explanations to the `modelStudio`.
```{r eval = FALSE}
old_ms <- modelStudio(explainer)
old_ms
# add new observations
plus_ms <- ms_update_observations(old_ms,
explainer,
new_observation = test[101:102,])
plus_ms
# overwrite old observations
new_ms <- ms_update_observations(old_ms,
explainer,
new_observation = test[103:104,],
overwrite = TRUE)
new_ms
```
-------------------------------------------------------------------
## Shiny
Use the `widget_id` argument and `r2d3` package to render the `modelStudio` output in Shiny.
See [Using r2d3 with Shiny](https://rstudio.github.io/r2d3/articles/shiny.html) and consider
the following example:
```{r eval = FALSE}
library(shiny)
library(r2d3)
ui <- fluidPage(
textInput("text", h3("Text input"),
value = "Enter text..."),
uiOutput('dashboard')
)
server <- function(input, output) {
#:# id of div where modelStudio will appear
WIDGET_ID = 'MODELSTUDIO'
#:# create modelStudio
library(modelStudio)
library(DALEX)
model <- glm(survived ~., data = titanic_imputed, family = "binomial")
explainer <- DALEX::explain(model,
data = titanic_imputed,
y = titanic_imputed$survived,
label = "Titanic GLM",
verbose = FALSE)
ms <- modelStudio(explainer,
widget_id = WIDGET_ID, #:# use the widget_id
show_info = FALSE)
ms$elementId <- NULL #:# remove elementId to stop the warning
#:# basic render d3 output
output[[WIDGET_ID]] <- renderD3({
ms
})
#:# use render ui to set proper width and height
output$dashboard <- renderUI({
d3Output(WIDGET_ID, width=ms$width, height=ms$height)
})
}
shinyApp(ui = ui, server = server)
```
-------------------------------------------------------------------
## DALEXtra
Use `explain_*()` functions from the [DALEXtra](https://github.com/ModelOriented/DALEXtra/)
package to explain various models.
Bellow basic example of making `modelStudio` for a `mlr` model using `explain_mlr()`.
```{r eval = FALSE}
library(DALEXtra)
library(mlr)
# fit a model
task <- makeClassifTask(id = "task", data = train, target = "fired")
learner <- makeLearner("classif.ranger", predict.type = "prob")
model <- train(learner, task)
# create an explainer for the model
explainer_mlr <- explain_mlr(model,
data = test,
y = test$fired,
label = "mlr")
# make a studio for the model
modelStudio(explainer_mlr)
```
-------------------------------------------------------------------
## References
* Theoretical introduction to the plots: [Explanatory Model Analysis. Explore, Explain, and Examine Predictive Models.](https://ema.drwhy.ai/)
* The input object is implemented in [DALEX](https://modeloriented.github.io/DALEX/)
* Feature Importance, Ceteris Paribus, Partial Dependence and Accumulated Dependence explanations
are implemented in [ingredients](https://modeloriented.github.io/ingredients/)
* Break Down and Shapley Values explanations are implemented in [iBreakDown](https://modeloriented.github.io/iBreakDown/)