-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_Lab7_Ensemble.qmd
321 lines (229 loc) · 7.77 KB
/
ML_Lab7_Ensemble.qmd
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
---
title: "Statistical and Machine Learning"
subtitle: "Lab7: Ensemble Learning - Bagging and Boosting"
author: "Tsai, Dai-Rong"
format:
revealjs:
theme: default
echo: true
fig-width: 7
smaller: true
scrollable: true
slide-number: true
auto-stretch: false
history: false
pdf-max-pages-per-slide: 5
embed-resources: true
tbl-cap-location: bottom
---
## Dataset
```{r echo = FALSE}
options(digits = 5, width = 100, max.print = 100)
```
```{css echo = FALSE}
.reveal table, ul ul li{
font-size: smaller;
}
```
> ***Sonar, Mines vs. Rocks***
The task is to discriminate between sonar signals bounced off a metal cylinder and those bounced off a roughly cylindrical rock.
```{r}
# Set random seed
set.seed(123)
# Packages
library(randomForest) # for randomForest
library(adabag) # for boosting
library(rpart.plot) # for rpart.plot
library(caTools) # for LogitBoost
# Data
data(Sonar, package = "mlbench")
```
- Response
- `Class`: `"R"` if the object is a rock and `"M"` if it is a mine (metal cylinder).
- Predictors
- `V1` to `V60`: the energy within a particular frequency band, integrated over a certain period of time.
---
::: {.panel-tabset}
### Preview
```{r}
dim(Sonar)
```
```{r}
#| layout-ncol: 2
head(Sonar)[c(1:5)]
head(Sonar)[c(56:61)]
```
```{r}
table(Sonar$Class)
```
### Data Structure
```{r}
str(Sonar)
```
:::
## Create Training/Testing Partitions
- Split data into 70% training set and 30% test set
```{r}
nr <- nrow(Sonar)
train.id <- sample(nr, nr * 0.7)
training <- Sonar[train.id, ]
testing <- Sonar[-train.id, ]
```
- Check dimension
```{r}
dim(training)
dim(testing)
```
# Bagging: <br> Bootstrap Aggregating
## Bagging for CART
Bagging for CART is simply a special case of a random forest with $m = p$.
```{r}
bag.tree <- randomForest(Class ~ ., data = training,
mtry = ncol(training)-1,
importance = TRUE)
bag.tree
```
The argument `mtry = ncol(training)-1` indicates that all `r ncol(training)-1` predictors should be considered for each split of the tree.
::: {.callout-tip}
### Arguments
- `ntree`: (default: 500) Number of trees to grow.
- `mtry`: Number of variables randomly sampled as candidates at each split. The default values are different for classification ($\sqrt{p}$ where $p$ is number of predictors) and regression ($\frac{p}{3}$).
- `nodesize`: Minimum size of terminal nodes. The default value is `1` for classification and `5` for regression.
- `importance`: Whether importance of predictors is assessed.
:::
## Random Forest
```{r}
rf <- randomForest(Class ~ ., data = training,
mtry = floor(sqrt(ncol(training)-1)), # default for classification
importance = TRUE)
rf
```
```{r}
plot(rf, lty = 1, main = "OOB Error for Bagging")
legend("topright", colnames(rf$err.rate), col = 1:3, lty = 1)
```
---
### Tune `randomForest` for the optimal `mtry` parameter
::: {.callout-tip}
### Arguments
- `mtryStart`: starting value of `mtry`; default is the same as in `randomForest()`.
- `ntreeTry`: number of trees used at the tuning step.
- `stepFactor`: at each iteration, `mtry` is inflated (or deflated) by this value.
- `improve`: the (relative) improvement in OOB error must be by this much for the search to continue.
:::
```{r}
mtry.tuned <- tuneRF(x = training[, -61], y = training[, 61],
ntreeTry = 200, stepFactor = 1.5, improve = 0.01)
mtry.tuned
```
```{r}
rf.tuned <- randomForest(Class ~ ., data = training,
mtry = mtry.tuned[which.min(mtry.tuned[, 2]), 1],
importance = TRUE)
```
---
### Variable Importance
::: {.callout-note}
### Definitions
- **Mean decrease in accuracy**[^1] (`type = 1`)
The prediction error (error rate for classification, MSE for regression)
on the OOB samples is recorded for each tree.
Then the same is done after ***permuting each predictor variable***.
The difference between the two are then averaged over all trees,
and normalized by the standard deviation of the differences.
- **Mean decrease in node impurity** (`type = 2`)
Add up the total amount that the node impurity (Gini index for classification, RSS for regression)
is decreased due to splits over a given predictor, averaged over all trees.
:::
[^1]: Christoph Molnar. [**Permutation Feature Importance**](https://christophm.github.io/interpretable-ml-book/feature-importance.html#feature-importance). *Interpretable Machine Learning*.
:::: {.columns}
```{r echo = FALSE}
op <- options(max.print = 10)
```
::: {.column width="50%"}
```{r}
importance(rf.tuned, type = 1)
```
:::
::: {.column width="50%"}
```{r}
importance(rf.tuned, type = 2)
```
:::
```{r echo = FALSE}
options(op)
```
::::
```{r}
varImpPlot(rf)
```
---
### Comparison bwtween Bagging and Random Forest
```{r}
oob.err <- cbind(bag = bag.tree$err.rate[, "OOB"], rf = rf.tuned$err.rate[, "OOB"])
matplot(1:bag.tree$ntree, oob.err,
type = 'l', col = c(4, 2), lty = 1, xlab = "Number of Trees", ylab = "Error")
legend("topright", c("OOB: Bagging", "OOB: Random Forest"), col = c(4, 2), lty = 1)
```
# Boosting
## AdaBoost: Adaptive Boosting
```{r}
adabst <- boosting(Class ~ ., data = training, mfinal = 500,
control = rpart.control(maxdepth = 3))
```
```{r}
#| layout-ncol: 3
#| fig-cap:
#| - "Tree 1"
#| - "Tree 2"
#| - "Tree 500"
rpart.plot(adabst$trees[[1]], roundint = FALSE)
rpart.plot(adabst$trees[[2]], roundint = FALSE)
rpart.plot(adabst$trees[[500]], roundint = FALSE)
```
```{r}
adabst$weights
# importanceplot(adabst, horiz = TRUE, cex.names = 0.7)
vimp10 <- sort(adabst$importance, decreasing = TRUE)[1:10]
barplot(rev(vimp10), horiz = TRUE, las = 1,
cex.names = 0.7, col = "skyblue",
main = "Variable relative importance")
```
```{r}
evol.train <- errorevol(adabst, newdata = training)
evol.test <- errorevol(adabst, newdata = testing)
plot.errorevol(evol.test, evol.train)
```
## LogitBoost
```{r}
logitbst <- LogitBoost(xlearn = training[, -61],
ylearn = training[, 61],
nIter = 501)
```
::: {.callout-note}
`Logitboost` algorithm relies on a voting scheme to make classifications. Many (`nIter` of them) weak classifiers are applied to each sample and their findings are used as votes to make the final classification. It's common for two cases have a tie (the same number of votes), especially if `nIter` is even. In that case `predict()` returns `NA`, instead of a label.
:::
## Gradient Boosting
[![](https://raw.githubusercontent.com/dmlc/dmlc.github.io/master/img/logo-m/xgboost.png){height=100}](https://xgboost.readthedocs.io/en/)
[![](https://raw.githubusercontent.com/microsoft/LightGBM/master/docs/logo/LightGBM_logo_black_text_small.png){height=100}](https://lightgbm.readthedocs.io/en/)
[![](https://raw.githubusercontent.com/catboost/catboost/master/logo/catboost.png){height=200}](https://catboost.ai/en/docs/)
## Prediction
```{r}
pred.bag.tree <- predict(bag.tree, testing)
pred.rf.tuned <- predict(rf.tuned, testing)
pred.adabst <- predict(adabst, testing)$class
pred.logitbst <- predict(logitbst, testing)
acc <- sapply(mget(ls(pattern = "^pred")), \(x) mean(x == testing$Class))
```
```{r echo = FALSE}
names(acc) <- sub("pred.", "", names(acc))
```
```{r}
sort(acc)
```
```{r echo = FALSE}
#| fig-width: 7
#| fig-height: 5
barplot(sort(acc), ylim = range(acc) + c(-0.05, 0.05), xpd = FALSE, col = "skyblue",
ylab = "Accuracy", main = "Prediction Performance")
```