forked from ModelOriented/DALEX-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-exercises.Rmd
57 lines (42 loc) · 2.35 KB
/
05-exercises.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
# Exercises
Examples in previous chapters were based on random forest model and linear model.
But there are so many different approaches for regression modeling.
Try `gbm` (Generalized Boosted Regression Models), `knn` (k-Nearest Neighbour), `svm` (Support Vector Machines), `nnet` (Neural Networks) or other models.
1. Prepare explainers for model performance,
2. Prepare explainers for variable importance,
3. Prepare explainers for variable response for `construction.year`,
4. Prepare explainers for model predictions.
Below you will find fits for few different models. Try them out.
```{r Exercises, message=FALSE, warning=FALSE, fig.height=4, fig.cap="Distribution of residuals for fourn new models"}
library("DALEX")
library("gbm")
apartments_gbm_model <- gbm(m2.price ~ construction.year + surface + floor +
no.rooms + district, data = apartments, n.trees = 1000)
explainer_gbm <- explain(apartments_gbm_model,
data = apartmentsTest[,2:6], y = apartmentsTest$m2.price,
predict_function = function(m, d) predict(m, d, n.trees = 1000))
library("nnet")
apartments_nnet_model <- nnet(m2.price ~ construction.year + surface + floor +
no.rooms + district, data = apartments,
linout=TRUE,
size = 50, maxit=100)
explainer_nnet <- explain(apartments_nnet_model,
data = apartmentsTest[,2:6], y = apartmentsTest$m2.price)
library("e1071")
apartments_svm_model <- svm(m2.price ~ construction.year + surface + floor +
no.rooms + district, data = apartments)
explainer_svm <- explain(apartments_svm_model,
data = apartmentsTest[,2:6], y = apartmentsTest$m2.price)
library("caret")
mapartments <- model.matrix(m2.price ~ ., data = apartments)
mapartmentsTest <- model.matrix(m2.price ~ ., data = apartmentsTest)
apartments_knn_model <- knnreg(mapartments, apartments[,1], k = 5)
explainer_knn <- explain(apartments_knn_model,
data = mapartmentsTest, y = apartmentsTest$m2.price)
# Model performance
mp_knn <- model_performance(explainer_knn)
mp_svm <- model_performance(explainer_svm)
mp_gbm <- model_performance(explainer_gbm)
mp_nnet <- model_performance(explainer_nnet)
plot(mp_gbm, mp_nnet, mp_svm, mp_knn, geom = "boxplot")
```