-
Notifications
You must be signed in to change notification settings - Fork 11
/
Snippets.R
219 lines (129 loc) · 4.56 KB
/
Snippets.R
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
#
# 0. Make sure that you have all package installed
library("DALEX")
library("ranger")
library("ggplot2")
library("rms")
#
# 1. Look at the data we'll be working on
head(titanic_imputed)
#
# 2. Let's train two classification models
# you can try also other models if you like
model_ranger <- ranger(survived ~ ., data = titanic_imputed,
classification = TRUE, probability = TRUE)
model_rms <- lrm(survived ~ rcs(age)*gender + rcs(fare) +
class, data = titanic_imputed)
#
# 3. The models are different, to work with them in a uniform way we need an adapter
# created with explain() function
exp_ranger <- explain(model_ranger,
data = titanic_imputed[,1:7],
y = titanic_imputed$survived)
predict(exp_ranger, titanic_imputed[1,])
#
# 4. Other arguments are useful too
exp_rms <- explain(model_rms,
data = titanic_imputed[,1:7],
y = titanic_imputed$survived,
predict_function = function(m, x)
predict(m, x, type = "fitted"),
label = "Logistic with splines")
#
# 5. The adapter stores some useful information about the model
exp_rms$model_info
#
# 6. First step in the exploration are measures of performance
# performance is calculated on data in explainer
# use update_data to set a new dataset
mp_ranger <- model_performance(exp_ranger)
mp_ranger
mp_rms <- model_performance(exp_rms)
mp_rms
#
# 7. it is easier to compare models with charts
plot(mp_ranger, mp_rms, geom = "boxplot")
plot(mp_ranger, mp_rms, geom = "roc")
plot(mp_ranger, mp_rms, geom = "lift")
#
# 8. Let's focus on a single observations
henry <- titanic_imputed[1,]
predict(exp_ranger, henry)
#
# 9. Variable attributions - Shapley valyes
sh_ranger <- predict_parts(exp_ranger, henry,
type = "shap", B = 1)
plot(sh_ranger, show_boxplots = FALSE) +
ggtitle("Shapley values for Henry","")
#
# 10. Variable attributions - Break-down values
bd_ranger <- predict_parts(exp_ranger, henry,
type = "break_down_interactions")
bd_ranger
plot(bd_ranger, show_boxplots = FALSE) +
ggtitle("Break down values for Henry","") +
scale_y_continuous("",limits = c(0.09,0.33))
#
# 11. Variable attributions - Break-down with specified order
bd_ranger <- predict_parts(exp_ranger, henry,
order = c("age", "gender", "fare", "class",
"parch", "sibsp", "embarked"))
plot(bd_ranger) +
scale_y_continuous("",limits = c(0.09,0.33))
#
# 11. Variable importance
mp_ranger <- model_parts(exp_ranger, type = "difference")
plot(mp_ranger, show_boxplots = FALSE) +
ggtitle("Variable importance","")
#
# 12. Variable importance for two models
mp_ranger <- model_parts(exp_ranger, B = 1)
mp_rms <- model_parts(exp_rms, B = 1)
plot(mp_ranger, mp_rms, show_boxplots = FALSE, bar_width = 5) +
ggtitle("Variable importance","")
#
# 13. Profiles for a single observation
cp_ranger <- predict_profile(exp_ranger, henry)
plot(cp_ranger, variables = c("age", "fare"))
#
# 14. Profiles for two models
cp_rms <- predict_profile(exp_rms, henry)
plot(cp_ranger, cp_rms, variables = "age", color = "_label_") +
ggtitle("Ceteris Paribus for Henry")
#
# 15. How important are variables
cp_rms <- predict_parts(exp_rms, henry, type = "oscillations")
#
# 16. For categorical variables
plot(cp_ranger, variables = "class", categorical_type = "bars") +
ggtitle("Ceteris Paribus for Henry")
#
# 17. Model profile - aveage across individual CPs
mp_ranger <- model_profile(exp_ranger)
plot(mp_ranger, variables = "age")
plot(mp_ranger, variables = "age", geom = "points") +
ggtitle("Partial dependence","")
#
# 18. Model profile - aveage calculated in groups
mp_ranger <- model_profile(exp_ranger, groups = "gender")
plot(mp_ranger, variables = "age") +
ggtitle("Partial dependence","")
#
# 19. Model profile - aveage calculated in clusters
mp_ranger <- model_profile(exp_ranger, k = 3, center = TRUE)
plot(mp_ranger, variables = "age") +
ggtitle("Partial dependence 3 clusters","")
#
# 20. modelStudio dashboard
library(modelStudio)
modelStudio(exp_ranger)
model_apart <- ranger(m2.price ~ ., data = apartments)
exp_ranger <- explain(model_apart,
data = apartments_test,
y = apartments_test$m2.price)
predict(exp_ranger, apartments_test[1,])
mp_ranger <- model_performance(exp_ranger)
predict_parts(exp_ranger, apartments[1,])
plot(mp_ranger, geom = "boxplot")
mp_ranger <- model_profile(exp_ranger)
plot(mp_ranger)