forked from uc-r/Advanced-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-supervised-modeling-process.Rmd
1071 lines (751 loc) · 26.3 KB
/
03-supervised-modeling-process.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Supervised Modeling Process"
author: "Brad Boehmke"
date: "2019-02-28"
output:
xaringan::moon_reader:
css: ["scrollable.css", "mtheme_max.css", "fonts_mtheme_max.css"]
self_contained: false
lib_dir: libs
chakra: libs/remark-latest.min.js
nature:
ratio: '16:9'
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
seal: false
---
```{r setup, include=FALSE, cache=FALSE}
# Set global R options
options(htmltools.dir.version = FALSE, servr.daemon = TRUE)
# Set global knitr chunk options
knitr::opts_chunk$set(
fig.align = "center",
cache = TRUE,
error = FALSE,
message = FALSE,
warning = FALSE,
collapse = TRUE
)
# set ggplot to black and white theme
library(ggplot2)
theme_set(theme_bw())
```
class: clear, center, middle
background-image: url(images/process-icon.svg)
background-position: center
background-size: contain
<br><br><br><br><br>
.font200.grey[Supervised Modeling Process]
---
# Introduction
This module introduces concepts that are useful for any type of machine learning model:
- modeling process versus a model
- data splitting
- nuances of the R modeling ecosystem
- resampling
- bias-variance trade-off
- model evaluation
<br>
.center.bold[Many of these topics will be put into action in later sections.]
---
# Overview
.pull-left[
* the machine learning process is very iterative and heurstic-based
* common for many ML approaches to be applied, evaluated, and modified before a final, optimal model can be determined
* A proper process needs to be implemented to have confidence in our results
<br><br>
.center.bold.blue[_Not a short sprint!_]
]
.pull-right[
<br><br>
```{r general-process, echo=FALSE}
knitr::include_graphics("images/modeling_process.png")
```
]
---
# Prerequisites .red[`r anicon::faa("hand-point-right", color = "red", animate = "horizontal")` code chunk 1]
.pull-left[
.center.bold.font110[Packages]
```{r prereq-pkg}
library(rsample)
library(caret)
library(tidyverse)
```
]
.pull-right[
.center.bold.font110[Data]
```{r prereq-data}
# ames data
ames <- AmesHousing::make_ames()
# attrition data
churn <- rsample::attrition
```
]
---
class: center, middle, inverse
.font300.white[Data Splitting]
---
# Generalizability
__Generalizability__: we want an algorithm that not only fits well to our past data, but more importantly, one that .blue[predicts a future outcome accurately].
--
- .bold[Training Set]: these data are used to develop feature sets, train our algorithms, tune hyper-parameters, compare across models, and all of the other activities required to reach a final model decision.
- .bold[Test Set]: having chosen a final model, these data are used to estimate an unbiased assessment of the model’s performance (generalization error).
--
.pull-left[
<br><br>
.center.bold.red[DO NOT TOUCH THE TEST SET UNTIL THE VERY END!!!]
]
.pull-right[
```{r nope, echo=FALSE, out.height="30%", out.width="30%"}
knitr::include_graphics("images/nope.png")
```
]
---
# What's the right split?
* typical recommendations for splitting your data into training-testing splits include 60% (training) - 40% (testing), 70%-30%, or 80%-20%
* as data sets get smaller ( $n<500$ ):
- spending too much in training (*> 80%*) won’t allow us to get a good assessment of predictive performance. We may find a model that fits the training data very well, but is not generalizable (overfitting),
- sometimes too much spent in testing (*< 40%*) won’t allow us to get a good assessment of model parameters
* as *n* gets larger ( $n>100K$ ):
- marginal gains with larger sample sizes
- may use a smaller training sample to increase computation speed
* as *p* gets larger ( $p \geq n$ )
- larger samples sizes are often required to identify consistent signals in the features
---
# Mechanics of data splitting .red[`r anicon::faa("hand-point-right", color = "red", animate = "horizontal")` code chunk 2]
Two most common ways of splitting data include:
* .bold[simple random sampling]: randomly select observations
* .bold[stratified sampling]: preserving distributions
- <u>classification</u>: sampling within the classes to preserve the distribution of the outcome in the training and test sets
- <u>regression</u>: determine the quartiles of the data set and sample within those artificial groups
.pull-left[
```{r split-diamonds, eval=FALSE}
set.seed(123) # for reproducibility
split <- initial_split(diamonds, strata = "price", prop = 0.7)
train <- training(split)
test <- testing(split)
# Do the distributions line up?
ggplot(train, aes(x = price)) +
geom_line(stat = "density",
trim = TRUE) +
geom_line(data = test,
stat = "density",
trim = TRUE, col = "red")
```
]
.pull-right[
```{r split-diamonds2, echo=FALSE, fig.height=3.5}
set.seed(123) # for reproducibility
split <- initial_split(diamonds, strata = "price", prop = 0.7)
train <- training(split)
test <- testing(split)
# Do the distributions line up?
ggplot(train, aes(x = price)) +
geom_line(stat = "density",
trim = TRUE) +
geom_line(data = test,
stat = "density",
trim = TRUE, col = "red")
```
]
---
class: yourturn
# Your Turn! .red[`r anicon::faa("hand-point-right", color = "red", animate = "horizontal")` code chunk 3]
1. Use __rsample__ to split the Ames housing data (`ames`) and the Employee attrition data (`churn`) using stratified sampling and with a 70% split.
```r
# ames data
set.seed(123)
ames_split <- initial_split(ames, prop = _____, strata = "Sale_Price")
ames_train <- training(_____)
ames_test <- testing(_____)
# attrition data
set.seed(123)
churn_split <- initial_split(churn, prop = _____, strata = "Attrition")
churn_train <- training(_____)
churn_test <- testing(_____)
```
2. Verify that the distribution between training and test sets are similar.
---
class: yourturn
# Your Turn!
.scrollable90[
.pull-left[
.center.bold[Ames Housing]
```{r split-ames, fig.height=3}
set.seed(123)
ames_split <- initial_split(ames, prop = 0.7, strata = "Sale_Price")
ames_train <- training(ames_split)
ames_test <- testing(ames_split)
# Do the distributions line up?
ggplot(ames_train, aes(x = Sale_Price)) +
geom_line(stat = "density",
trim = TRUE) +
geom_line(data = ames_test,
stat = "density",
trim = TRUE, col = "red")
```
]
.pull-right[
.center.bold[Employee Attrition]
```{r split-churn}
set.seed(123)
churn_split <- initial_split(churn, prop = 0.7, strata = "Attrition")
churn_train <- training(churn_split)
churn_test <- testing(churn_split)
# consistent response ratio between train & test
table(churn_train$Attrition) %>% prop.table()
table(churn_test$Attrition) %>% prop.table()
```
]
]
---
class: center, middle, inverse
.font300.white[Creating models in R]
---
# Many interfaces
To fit a model to our data, the model terms must be specified. There are .bold[three main interfaces] for doing this:
.pull-left[
1. .blue[Formula interface]
]
.pull-right[
```{r formula-interface, eval=FALSE}
# Variables + interactions
model_fn(Sale_Price ~ Neighborhood + Year_Sold + Neighborhood:Year_Sold, data = ames_train)
# Shorthand for all predictors
model_fn(Sale_Price ~ ., data = ames_train)
# Inline functions / transformations
model_fn(log10(Sale_Price) ~ ns(Longitude, df = 3) + ns(Latitude, df = 3), data = ames_train)
```
]
---
# Many interfaces
To fit a model to our data, the model terms must be specified. There are .bold[three main interfaces] for doing this:
.pull-left[
1. Formula interface
2. .blue[XY interface]
]
.pull-right[
```{r XY-interface, eval=FALSE}
# Usually, the variables must all be numeric
features <- c("Year_Sold", "Longitude", "Latitude")
model_fn(x = ames_train[, features], y = ames_train$Sale_Price)
```
]
---
# Many interfaces
To fit a model to our data, the model terms must be specified. There are .bold[three main interfaces] for doing this:
.pull-left[
1. Formula interface
2. XY interface
3. .blue[Variable name specification]
]
.pull-right[
```{r name-interface, eval=FALSE}
# specify x & y by character strings
model_fn(
x = c("Year_Sold", "Longitude", "Latitude"),
y = "Sale_Price",
data = ames.h2o
)
```
]
--
<br><br><br><br>
.center.bold.font110.red[_We can get around these inconsistencies with meta-engines_]
---
# Many engines
With the prevelance of ML packages available, this results in an abundance of direct and meta engines that can be used.
For example, the following all produce the same linear regression model output:
```{r lm-engines, eval=FALSE}
lm_lm <- lm(Sale_Price ~ ., data = ames_train)
lm_glm <- glm(Sale_Price ~ ., data = ames_train, family = gaussian)
lm_caret <- train(Sale_Price ~ ., data = ames_train, method = "lm")
```
* `lm()` and `glm()` are two different engines that can be used to fit the linear model
* `caret::train()` is a meta engine (aggregator) that allows you to apply almost any direct engine with `method = ?`
---
# Many engines
* Using direct engines provides .green[more flexibility] but
* requires you to be familiar with .red[syntax nuances]
<hr style="height:5px; visibility:hidden;" />
| Algorithm | Package | Code |
| --------- | ------- | ------------- |
| Linear discriminant analysis | __MASS__ | `predict(obj)` |
| Generalized linear model | __stats__ | `predict(obj, type = "response")` |
| Mixture discriminant analysis | __mda__ | `predict(obj, type = "posterior")` |
| Decision tree | __rpart__ | `predict(obj, type = "prob")` |
| Random Forest | __ranger__ | `predict(obj)$predictions` |
| Gradient boosting machine | __gbm__ | `predict(obj, type = "response", n.trees)` |
.center.font60[[Revised from Max Kuhn's 2019 RStudio Conference talk](https://resources.rstudio.com/rstudio-conf-2019/parsnip-a-tidy-model-interface)]
.center.bold[You'll be exposed to both direct and meta engines]
---
class: center, middle, inverse
.font300.white[Resampling Methods]
---
# Resampling methods
___Resampling___ provides an approach for us to repeatedly fit a model of interest to parts of the training data and testing the performance on other parts.
.pull-left[
* Allows us to _estimate_ the generalization error while training, tuning, and comparing models without using the test data set
* The two most commonly used resampling methods include
- _k_-fold cross validation
- bootstrapping.
]
.pull-right[
```{r resampling, echo=FALSE}
knitr::include_graphics("images/resampling.png")
```
.center.font70[[Image by Max Kuhn](https://bookdown.org/max/FES/review-predictive-modeling-process.html#fig:review-resamp-scheme)]
]
---
# _K_-fold cross valiation
.pull-left[
* randomly divides the training data into k groups of approximately equal size
* assign one block as the .orange[test block] and the rest as .blue[training block]
* train model on each folds' .blue[training block] and evaluate on .orange[test block]
* average performance across all folds
]
.pull-right[
<br>
```{r cv-icon, echo=FALSE}
knitr::include_graphics("images/cv.png")
```
.center.bold[_k_ is usually taken to be 5 or 10]
]
---
# _K_-fold cross valiation
.pull-left[
* randomly divides the training data into k groups of approximately equal size
* assign one block as the .orange[test block] and the rest as .blue[training block]
* train model on each folds' .blue[training block] and evaluate on .orange[test block]
* average performance across all folds
* .blue[Pro tip]: for smaller data sets ( $n < 10,000$ ), 10-fold cross validation repeated 5 or 10 times will improve accuracy of your estimated performance
]
.pull-right[
```{r cv-demo, echo=FALSE, fig.height=4}
cv <- vfold_cv(mtcars, 10)
cv_plot <- cv$splits %>%
map2_dfr(seq_along(cv$splits), ~ mtcars %>% mutate(
Resample = paste0("Fold_", str_pad(.y, 2, pad = 0)),
ID = row_number(),
Data = ifelse(ID %in% .x$in_id, "Training", "Validation"))
) %>%
ggplot(aes(Resample, ID, fill = Data)) +
geom_tile() +
scale_fill_manual(values = c("#f2f2f2", "#AAAAAA")) +
scale_y_reverse("Observation ID", breaks = 1:nrow(mtcars), expand = c(0, 0)) +
scale_x_discrete(NULL, expand = c(0, 0)) +
theme_classic() +
theme(legend.title=element_blank())
cv_plot
```
.center.bold[10-fold cross validation with _n = 32_]
]
---
# Applying
Three main approaches to apply cross-validation:
.scrollable90[
.bold[Within a direct engine:]
```{r cv-h2o, eval=FALSE}
# example of 10 fold CV in h2o
h2o_cv <- h2o.glm(
x = x,
y = y,
training_frame = train.h2o,
nfolds = 10 #<<
)
```
.bold[Within a meta engine:]
```{r cv-caret, eval=FALSE}
# example of 10 fold CV in caret
caret_cv <- train(
Sale_Price ~ .,
data = ames_train,
method = "lm",
trControl = trainControl(method = "cv", number = 10) #<<
)
```
.bold[External to engine:]
```{r cv-external}
# example of 10 fold CV with rsample::vfold_cv
ext_cv <- vfold_cv(ames_train, v = 10)
ext_cv
names(ext_cv$splits$`1`)
```
]
---
# Bootstrapping
A bootstrap sample is the same size as the training set but each data point is selected ___with replacement___.
.pull-left[
* .bold[Analysis set]: Will contain more than one replicate of a training set instance.
* .bold[Assessment set]: Contains all samples that were never included in the corresponding bootstrap set. Often called the ___"out-of-bag"___ sample and can vary in size!
]
.pull-right[
```{r bootstrap-icon, echo=FALSE}
knitr::include_graphics("images/bootstrap-scheme.png")
```
]
<br><br>
.center.bold[On average, 63.21% of the training set is contained at least once in the bootstrap sample.]
---
# Bootstrapping
A bootstrap sample is the same size as the training set but each data point is selected ___with replacement___.
.scrollable90[
.pull-left[
* .bold[Analysis set]: Will contain more than one replicate of a training set instance.
* .bold[Assessment set]: Contains all samples that were never included in the corresponding bootstrap set. Often called the ___"out-of-bag"___ sample and can vary in size!
* .blue[Pro tip]: For smaller data sets (*n < 500*), bootstrapping can have biased error estimates; use repeated k-fold or corrected bootstrapping methods.
]
.pull-right[
```{r sampling-comparison, echo=FALSE, fig.height=7.5}
boots <- rsample::bootstraps(mtcars, 10)
boots_plot <- boots$splits %>%
map2_dfr(seq_along(boots$splits), ~ mtcars %>%
mutate(
Resample = paste0("Bootstrap_", str_pad(.y, 2, pad = 0)),
ID = row_number()
) %>%
group_by(ID) %>%
mutate(Replicates = factor(sum(ID == .x$in_id)))) %>%
ggplot(aes(Resample, ID, fill = Replicates)) +
geom_tile() +
scale_fill_manual(values = c("#FFFFFF", "#F5F5F5", "#BEBEBE", "#808080", "#404040", "#000000")) +
scale_y_reverse("Observation ID", breaks = 1:nrow(mtcars), expand = c(0, 0)) +
scale_x_discrete(NULL, expand = c(0, 0)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Bootstrap sampling")
cv_plot <- cv_plot +
ggtitle("10-fold cross validation") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
cowplot::plot_grid(boots_plot, cv_plot, align = "v", nrow = 2)
```
]
]
---
# Applying
.pull-left[
* Similiar to cross validation, we can incorporate bootstrapping with:
- meta engines
- external to engines
* however, bootstrapping is more of an internal resampling procedure that is naturally built into certain ML algorithms:
- bagging
- random forests
- GBMs
]
.pull-right[
.bold[Within a meta engine:]
```{r boot-caret, eval=FALSE}
# example of 10 bootstrap samples in caret
caret_boot <- train(
Sale_Price ~ .,
data = ames_train,
method = "lm",
trControl = trainControl(method = "boot", number = 10) #<<
)
```
.bold[External to engine:]
```{r boot-external}
# example of 10 bootstrapped samples with rsample::bootstraps
bootstraps(ames, times = 10)
```
]
]
---
class: center, middle, inverse
.font300.white[Bias-Variance Trade-off]
---
# Bias-variance trade-off
* Prediction errors can be decomposed into two main subcomponents we have control over:
- error due to “bias”
- error due to “variance”
* There is a tradeoff between a model’s ability to minimize bias and variance.
* Understanding how different sources of error lead to bias and variance helps us improve the data fitting process resulting in more accurate models.
--
.pull-left[
```{r bias-model, fig.height=3.5, echo=FALSE}
# Simulate some nonlinear monotonic data
set.seed(123) # for reproducibility
x <- seq(from = 0, to = 2 * pi, length = 500)
y <- sin(x) + rnorm(length(x), sd = 0.3)
df <- data.frame(x, y) %>%
filter(x < 4.5)
# single model fit
bias_model <- lm(y ~ I(x^3), data = df)
df$predictions <- predict(bias_model, df)
p1 <- ggplot(df, aes(x, y)) +
geom_point(alpha = .3) +
geom_line(aes(x, predictions), size = 1.5, color = "dodgerblue") +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
ggtitle("Single biased model fit")
# bootstrapped model fit
bootstrap_n <- 25
bootstrap_results <- NULL
for(i in seq_len(bootstrap_n)) {
# reproducible sampled data frames
set.seed(i)
index <- sample(seq_len(nrow(df)), nrow(df), replace = TRUE)
df_sim <- df[index, ]
# fit model and add predictions to results data frame
fit <- lm(y ~ I(x^3), data = df_sim)
df_sim$predictions <- predict(fit, df_sim)
df_sim$model <- paste0("model", i)
df_sim$ob <- index
bootstrap_results <- rbind(bootstrap_results, df_sim)
}
p2 <- ggplot(bootstrap_results, aes(x, predictions, color = model)) +
geom_line(show.legend = FALSE, size = .5) +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
ggtitle("25 biased models (bootstrapped)")
gridExtra::grid.arrange(p1, p2, nrow = 1)
```
]
--
.pull-right[
```{r variance-model, fig.height=3.5, echo=FALSE}
# Simulate some nonlinear monotonic data
set.seed(123) # for reproducibility
x <- seq(from = 0, to = 2 * pi, length = 500)
y <- sin(x) + rnorm(length(x), sd = 0.3)
df <- data.frame(x, y) %>%
filter(x < 4.5)
# single model fit
variance_model <- knnreg(y ~ x, k = 3, data = df)
df$predictions <- predict(variance_model, df)
p1 <- ggplot(df, aes(x, y)) +
geom_point(alpha = .3) +
geom_line(aes(x, predictions), size = 1.5, color = "dodgerblue") +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
ggtitle("Single high variance model fit")
# bootstrapped model fit
bootstrap_n <- 25
bootstrap_results <- NULL
for(i in seq_len(bootstrap_n)) {
# reproducible sampled data frames
set.seed(i)
index <- sample(seq_len(nrow(df)), nrow(df), replace = TRUE)
df_sim <- df[index, ]
# fit model and add predictions to results data frame
fit <- knnreg(y ~ x, k = 3, data = df_sim)
df_sim$predictions <- predict(fit, df_sim)
df_sim$model <- paste0("model", i)
df_sim$ob <- index
bootstrap_results <- rbind(bootstrap_results, df_sim)
}
p2 <- ggplot(bootstrap_results, aes(x, predictions, color = model)) +
geom_line(show.legend = FALSE) +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
ggtitle("25 high variance models (bootstrapped)")
gridExtra::grid.arrange(p1, p2, nrow = 1)
```
]
---
# Bias-variance trade-off
* Prediction errors can be decomposed into two main subcomponents we care about:
- error due to “bias”
- error due to “variance”
* There is a tradeoff between a model’s ability to minimize bias and variance.
* Understanding how different sources of error lead to bias and variance helps us improve the data fitting process resulting in more accurate models.
* Some models are naturally...
- .bold[high bias]: generalized linear models
- .bold[high variance]: tree-based models, NNets, KNN, etc.
<br>
.center.bold.red.font120[Hyperparameters can help to control bias-variance trade-off]
---
# Hyperparameter tuning
Hyperparameters (aka _tuning parameters_) are the "knobs to twiddle" to control of complexity of machine learning algorithms and, therefore, the bias-variance trade-off
```{r example-knn, fig.width=10, fig.height=5, echo=FALSE}
k_results <- NULL
k <- c(2, 5, 10, 20, 50, 150)
# fit many different models
for(i in seq_along(k)) {
df_sim <- df
fit <- knnreg(y ~ x, k = k[i], data = df_sim)
df_sim$predictions <- predict(fit, df_sim)
df_sim$model <- paste0("k = ", str_pad(k[i], 3, pad = " "))
k_results <- rbind(k_results, df_sim)
}
ggplot() +
geom_point(data = df, aes(x, y), alpha = .3) +
geom_line(data = k_results, aes(x, predictions), color = "dodgerblue", size = 1.5) +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
facet_wrap(~ model)
```
.center[k-nearest neighbor model with differing values for *k*. Small *k* value has too much variance. Big *k* value has too much bias. .red[How do we find the optimal value?]]
---
# Grid search
A grid search is an automated approach to searching across many combinations of hyperparameter values
.scrollable90[
.pull-left[
```{r grid-search}
# resampling procedure
cv <- trainControl(method = "repeatedcv", number = 10, repeats = 10)
# define grid search
hyper_grid <- expand.grid(k = seq(2, 150, by = 2))
# perform grid search with caret
knn_fit <- train(
x ~ y,
data = df,
method = "knn",
trControl = cv,
tuneGrid = hyper_grid
)
```
]
.pull-right[
```{r plot-grid-search-results, echo=FALSE, fig.height=4}
ggplot() +
geom_line(data = knn_fit$results, aes(k, RMSE)) +
geom_point(data = knn_fit$results, aes(k, RMSE)) +
geom_point(data = filter(knn_fit$results, k == as.numeric(knn_fit$bestTune)),
aes(k, RMSE),
shape = 21,
fill = "yellow",
color = "black",
stroke = 1,
size = 2) +
scale_y_continuous("Error (RMSE)")
```
```{r plot-best-k, echo=FALSE, fig.height=4}
# single model fit
best_model <- knnreg(y ~ x, k = knn_fit$bestTune$k, data = df)
df$predictions <- predict(best_model, df)
df$truth <- sin(df$x)
ggplot(df, aes(x, y)) +
geom_point(alpha = .3) +
geom_line(aes(y = truth), lty = "dashed", size = 1.5) +
geom_line(aes(x, predictions), size = 1.5, color = "dodgerblue") +
scale_y_continuous("Response", limits = c(-1.75, 1.75), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 4.5), expand = c(0, 0)) +
ggtitle("Optimal KNN model")
```
]
]
---
class: center, middle, inverse
.font300.white[Model Evaluation Metrics]
---
# Model evaluation
.pull-left[
.center.bold.font110[Regression]
- Mean Square Error (MSE)
- Root Mean Square Error (RMSE)
- Mean Absolute Error (MAE)
- Mean Absolute Percent Error (MAPE)
- Root Mean Squared Logarithmic Error (RMSLE)
]
.pull-right[
.center.bold.font110[Classification]
- Classification Accuracy
- Recall vs. Specificity
- $F_1$ Score
- Log Loss
]
---
# Model evaluation
.pull-left[
.center.bold.font110[Regression]
- .bold[Mean Square Error (MSE)]
- .bold[Root Mean Square Error (RMSE)]
- Mean Absolute Error (MAE)
- Mean Absolute Percent Error (MAPE)
- Root Mean Squared Logarithmic Error (RMSLE)
<br><br>
$$MSE = \frac{1}{n}\sum^n_{i=1}(y_i - \hat y_i)^2 $$
]
.pull-right[
.center.bold.font110[Classification]
- .bold[Classification Accuracy]
- .bold[Recall vs. Specificity]
- $F_1$ Score
- Log Loss
```{r cm, echo=FALSE, out.width="80%", out.height="80%"}
knitr::include_graphics("images/confusion-matrix.png")
```
]
---
class: center, middle, inverse
.font300.white[Putting the process together]
---
# Putting the process together
.pull-left[
Let's put these pieces together and analyze the Ames housing data:
1. Split into training vs testing data
2. Specify a resampling procedure
3. Create our hyperparameter grid
4. Execute grid search
5. Evaluate performance
]
---
# Putting the process together .red[`r anicon::faa("hand-point-right", color = "red", animate = "horizontal")` code chunk 4]
.scrollable90[
.pull-left[
Let's put these pieces together and analyze the Ames housing data:
1. Split into training vs testing data
2. Specify a resampling procedure
3. Create our hyperparameter grid
4. Execute grid search
5. Evaluate performance
]
.pull-right[
.center.bold[`r anicon::faa("exclamation-triangle", color = "red", animate = FALSE)` This grid search takes ~2 min `r anicon::faa("exclamation-triangle", color = "red", animate = FALSE)`]
```{r example-process-splitting, fig.height=4}
# 1. stratified sampling with the rsample package
set.seed(123)
split <- initial_split(ames, prop = 0.7, strata = "Sale_Price")