-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucas_ml_workshop.R
370 lines (236 loc) · 6.6 KB
/
lucas_ml_workshop.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
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
351
352
353
354
355
356
357
358
359
360
361
# Slide 5
library(boot)
library(ggplot2)
library(pdp)
library(rpart.plot)
library(caret)
# Followed until 1977.
# So censoring that I will ignore.
data(melanoma, package = "boot")
head(melanoma)
dim(melanoma)
# Remove year variable.
# Due to the censoring this variable
# is very highly correlated to death time.
melanoma <- melanoma[, -5]
# Quick look at the data
featurePlot(melanoma[, -1], melanoma$time)
hist(melanoma$time)
# Slide 6
tr1 <- trainControl(
method = 'LGOCV',
number = 1,
p = 0.75,
savePredictions = TRUE)
m1 <- train(time ~ .,
data = melanoma,
method = 'rpart2',
tuneLength = 3,
metric = 'MAE',
trControl = tr1)
print(m1)
#############################################
# Slide 46
plot(m1)
# Uses model trained on full dataset.
# Use this to test on a outer validation dataset.
predict(m1)
m1$results # Validation results.
m1$pred # All validation predictions (all hyperpars)
m1$finalModel # The final fitted model
class(m1$finalModel)
# Slide 47
# Annoyingly caret doesn't have a function
# that plots obs vs preds of the hold out data.
# I have written my own here it is.
# plotObsVsPred() plots in sample and a completely seperate hold out if you specify it.
# Which isn't typically what you want.
plotCV <- function(t, print = TRUE, smooth = TRUE, alpha = 1){
stopifnot(inherits(t, 'train'))
d <- best_tune_preds(t)
if('weights' %in% names(d)){
p <- ggplot(d, aes(pred, obs, size = weights, colour = 'a'))
} else {
p <- ggplot(d, aes(pred, obs, colour = 'a'))
}
p <- p +
geom_point(alpha = alpha) +
geom_abline(slope = 1, intercept = 0) +
theme(legend.position = "none")
if(smooth){
p <- p + geom_smooth()
}
if(print) print(p)
return(invisible(p))
}
# This function finds the best tuning parameters and pulls
# out the relevant preditions.
best_tune_preds <- function (t){
stopifnot(inherits(t, 'train'))
row_matches <- sapply(1:length(t$bestTune), function(x) t$pred[, names(t$bestTune)[x]] == t$bestTune[[x]])
best_rows <- rowMeans(row_matches) == 1
d <- t$pred[best_rows, ]
}
plotCV(m1)
###############################################################
# Slide 48
# Random search instead of grid search.
# Good for models with lots of hyperparameters.
tr_random <- trainControl(
search = 'random',
savePredictions = TRUE)
m_random <- train(time ~ .,
data = melanoma,
method = 'enet',
tuneLength = 20,
metric = 'MAE',
trControl = tr_random)
plot(m_random)
# Slide 49
# Give an explicit dataframe of parameters
# Need to look up the exact names
gr <- data.frame(lambda = c(1e-4, 1e-5, 1e-6),
fraction = c(0.1, 0.5, 0.5))
m_df <- train(time ~ .,
data = melanoma,
method = 'enet',
tuneGrid = gr,
metric = 'MAE',
trControl = tr1)
plot(m_df)
gr_expand <- expand.grid(lambda = c(1e-4, 1e-5, 1e-6),
fraction = c(0.1, 0.5, 0.5))
m_df2 <- train(time ~ .,
data = melanoma,
method = 'enet',
tuneGrid = gr_expand,
metric = 'MAE',
trControl = tr1)
plot(m_df2)
########################################################
# Slide 51
pl <- read.csv(
file = 'https://raw.githubusercontent.com/timcdlucas/ml_workshop/master/planets.csv')
set.seed(31281)
pl1 <- train(g ~ .,
data = pl,
method = 'rpart2',
trControl = tr1)
pl1
set.seed(31281)
pl2 <- train(g ~ 0 + I(m1 * m2 / d ^ 2),
data = pl,
method = 'lm',
trControl = tr1)
pl2
######################################################
# Fuller workflow
# Slide 55
tr2 <- trainControl(
method = 'repeatedcv',
number = 5,
repeats = 3,
savePredictions = TRUE)
# Slide 56
my_metric <- 'MAE'
# Slide 58
# A good benchmark
# Penalised linear regression.
# Regularise model by pushing coefficients towards zero.
# A blend of LASSO and ridge penalties
set.seed(131210)
m1 <- train(time ~ .,
data = melanoma,
method = 'enet',
tuneLength = 10,
metric = my_metric,
trControl = tr2)
plot(m1)
plotCV(m1)
# Slide 59
# A good benchmark
set.seed(131210)
m2 <- train(time ~ .,
data = melanoma,
method = 'ppr',
tuneLength = 10,
metric = my_metric,
trControl = tr2)
plot(m2)
plotCV(m2)
# Slide 60
# A good benchmark
set.seed(131210)
m3 <- train(time ~ .,
data = melanoma,
method = 'ranger',
tuneLength = 5,
metric = my_metric,
trControl = tr2)
m3
plot(m3)
plotCV(m3)
pdp::partial(m3,
pred.var = c('thickness'),
plot = TRUE)
# Slide 62
# Try a few models. No free lunch.
# This one is slower. And you might not have it installed.
# No need to run it during the workshop.
# xgboost has lots of parameters.
# So probably use grid search instead.
tr2_random <- trainControl(
method = 'repeatedcv',
number = 5,
repeats = 3,
search = 'random',
savePredictions = TRUE)
set.seed(131210)
m4 <- train(time ~ .,
data = melanoma,
method = 'xgbTree',
tuneLength = 10,
metric = my_metric,
trControl = tr2_random)
plotCV(m4)
m4$results
min(m4$results$MAE)
# Slide 63
# A little bit slow.
set.seed(131210)
m5 <- train(time ~ .,
data = melanoma,
method = 'nnet',
tuneLength = 10,
linout = TRUE,
metric = my_metric,
trControl = tr2)
plotCV(m5)
##############################################################
# Extras
# How to plot the rpart2 model.
set.seed(1312)
m2 <- train(time ~ .,
data = melanoma,
method = 'rpart2',
tuneGrid = data.frame(maxdepth = 3),
trControl = tr1)
m2
rpart.plot(m2$finalModel)
# Overfitting/underfitting example
library(dplyr)
melanoma %>%
sample_frac(16, replace = TRUE) %>%
mutate(resamp = rep(1:16, nrow(melanoma))) %>%
ggplot(aes(time, thickness)) +
geom_point() +
facet_wrap(~ resamp) +
geom_smooth(method = 'lm', colour = 'red', se = FALSE) +
geom_smooth(span = 0.2, se = FALSE)
melanoma %>%
sample_frac(16, replace = TRUE) %>%
mutate(resamp = rep(1:16, nrow(melanoma))) %>%
ggplot(aes(time, thickness)) +
geom_point() +
facet_wrap(~ resamp) +
geom_smooth(span = 1, se = FALSE)