forked from mayer79/statistical_computing_material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_Model_Selection_and_Validation.Rmd
1225 lines (989 loc) · 38.1 KB
/
4_Model_Selection_and_Validation.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: "4. Model Selection and Validation"
author: "Michael Mayer"
date: "`r Sys.Date()`"
output:
html_document:
toc: yes
toc_float: yes
number_sections: yes
df_print: paged
theme: paper
code_folding: show
math_method: katex
subtitle: "Statistical Computing"
bibliography: biblio.bib
link-citations: yes
editor_options:
chunk_output_type: console
markdown:
wrap: 72
knit: (function(input, ...) {rmarkdown::render(input, output_dir = "docs")})
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
eval = TRUE
)
```
# Introduction
In the previous chapter, we have met performance measures like the RMSE
or the total deviance to measure how good our models are. Unfortunately,
we cannot fully rely on these values due to overfitting: The more our
models overfit, the less we can trust in their "in-sample" performance,
i.e., the performance on the data used to calculate the models.
Selecting models based on their in-sample performance is equally bad.
Overfitting should not be rewarded!
In this chapter, we will meet ways to estimate the performance of a
model in a fair way and use it to select the best model among
alternatives. They are all based on data splitting techniques, where the
models are evaluated on fresh data not used for model calculation. A
fantastic reference for this chapter is @hastie01statisticallearning.
Before introducing these techniques, we will fix some notation and meet
a competitor of the linear model.
Notation:
- Loss function $L$: Used to measure loss of single observation, e.g.
the squared error $L(y, z) = (y - z)^2$.
- Total loss
$Q(f, D) = \sum_{(y_i, \boldsymbol x_i) \in D} L(y_i, f(\boldsymbol x_i))$
of a data set $D$, e.g. the sum of squared errors. Used as objective
criterion to fit $f$ on data $D$. Sometimes, the objective also
contains a penalty term.
- Average loss $\bar Q(f, D) = Q(f, D) / |D|$, e.g. the mean-squared
error. Easier-to-read version of $Q$ as it does not grow with the
sample size $|D|$.
- Performance measure or evaluation metric $S(f, D)$ of interest,
often $S = \bar Q$ or a function of it, e.g. the RMSE. Used to
compare models or select hyper-parameters. Ideally consistent with
$Q$.
While loss functions are used by the algorithm to *fit* the model, the
evaluation metric helps to measure performance and to select models.
# Nearest-neighbor
A very simple and intuitive alternative to the linear model is the
$k$-nearest-neighbor approach, originally introduced by Evelyn Fix and
J. L. Hodges in an unpublished technical report in 1951. It can be
applied for both regression and classification and works without fitting
anything. The prediction for an observation is obtained by
1. searching the closest $k$ neighbors in the data set and then
2. combining their responses.
By "nearest" we usually mean Euclidean distance in the covariate space.
If covariates are not on the same scale, it makes sense to *standardize*
them first by subtracting the mean and dividing by the standard
deviation. Otherwise, distances would be dominated by the covariate on
the largest scale. Categorical features need to be one-hot- or
integer-encoded first. Note that one-hot-encoded covariates may or may
not be standardized.
For regression tasks, the responses of the $k$ nearest neighbors are
often combined by computing their arithmetic mean. For classification
tasks, they are condensed to their most frequent value or by providing
class probabilities.
## Example
What prediction (on logarithmic price scale) would we get with
5-nearest-neighbor regression for the 10'000th row of the diamonds data
set?
```{r}
library(ggplot2)
library(FNN) # fast nearest neighbor
diamonds <- transform(diamonds, log_price = log(price), log_carat = log(carat))
# log carat quite important for euclidean distances (?)
y <- "log_price"
x <- c("log_carat", "color", "cut", "clarity")
# Scaled numeric feature matrix
X <- scale(data.matrix(diamonds[, x]))
#apply(X, 2, FUN = sd) column-wise sd, every thing 1 (so properly scaled)
# The 10'000th observation
diamonds[10000, c("price", "carat", x)]
# Its prediction
knn.reg(X, test = X[10000, ], k = 5, y = diamonds[[y]])
# reg = regression
# this value lives in log space, so use exp(...) to get other value
# Its five nearest neighbors
neighbors <- c(knnx.index(X, X[10000, , drop = FALSE], k = 5))
diamonds[neighbors, ]
```
We see that they are quite similar in most of the covariates Ugly: the
predicted diamond is a neighbor (overfitting) Ugly: probably the same
diamond multiple times in the table! (problem of the diamond data). We
have to keep that in mind.
**Comments**
- The five nearest diamonds are extremely similar. One of them is the
observation of interest itself, introducing a relevant amount of
overfitting.
- The average log price of these five observations gives us the
nearest-neighbor prediction for the 10'000th diamond.
- Would we get better results for a different choice of the number of
neighbors $k$?
- Three lines are identical up to the perspective variables (`depth`,
`table`, `x`, `y`, `z`). These rows most certainly represent the
same diamond, which leads to an additional overfit. We need to keep
this problematic aspect of the diamonds data in mind.
**Motivation for this chapter:** In-sample, a 1-nearest-neighbor
regression predicts without error, a consequence of pure overfitting.
This hypothetical example indicates that in-sample performance is often
not worth a penny. Models need to be evaluated on fresh, independent
data not used for model calculation. This leads us to *simple
validation*.
# Simple Validation
With simple validation, the original data set is partitioned into a
*training* data set $D_\text{train}$ used to calculate the models and a
separate *validation* data set $D_\text{valid}$ used to estimate the
true model performance and/or to select models. Typically, $10\% - 30\%$
of rows are used for validation.
We can use the validation performance $S(\hat f, D_\text{valid})$ to
compare *algorithms* (e.g., linear regression versus
$k$-nearest-neighbor) and also to choose their *hyperparameters* like
the $k$ of $k$-nearest-neighbor.
Furthermore, the performance difference
$S(\hat f, D_\text{valid}) - S(\hat f, D_\text{train})$ gives an
impression on the amount of overfitting (or rather of the *optimism*).
Ideally, the difference is small.
## Example
We now use a 80%/20% split on the diamonds data to calculate the RMSE of
5-nearest-neighbor for both the training and the validation data.
```{r}
library(ggplot2)
library(FNN)
library(withr)
diamonds <- transform(diamonds, log_price = log(price), log_carat = log(carat))
y <- "log_price"
x <- c("log_carat", "color", "cut", "clarity")
# Split diamonds into 80% for "training" and 20% for validation
with_seed(
9838,
ix <- sample(nrow(diamonds), 0.8 * nrow(diamonds))
)
y_train <- diamonds[ix, y]
X_train <- diamonds[ix, x]
y_valid <- diamonds[-ix, y]
X_valid <- diamonds[-ix, x]
# Standardize training data
X_train <- scale(data.matrix(X_train))
# Apply training scale to validation data
# would be really bad to use other scaling here!
X_valid <- scale(
data.matrix(X_valid),
center = attr(X_train, "scaled:center"),
scale = attr(X_train, "scaled:scale")
)
# Performance
RMSE <- function(y, pred) {
sqrt(mean((y - pred)^2))
}
pred_train <- knn.reg(X_train, test = X_train, k = 5, y = y_train)$pred
cat("Training RMSE:", RMSE(y_train, pred_train))
pred_valid <- knn.reg(X_train, test = X_valid, k = 5, y = y_train)$pred
cat("Validation RMSE:", RMSE(y_valid, pred_valid))
```
We see quite a difference on RMSE for train/validation data.
**Comment:** Validation RMSE is substantially worse than training RMSE,
a clear sign of overfitting. However, it is still much better than the
(full-sample) performance of linear regression (residual standard error
was 0.1338).
Can we find a $k$ with better validation RMSE?
```{r}
library(tidyr)
# Tuning grid with different values for parameter k
paramGrid <- data.frame(train = NA, valid = NA, k = 1:20)
# Calculate performance for each row in the parameter grid
for (i in 1:nrow(paramGrid)) {
k <- paramGrid[i, "k"]
# Performance on training data
pred_train <- knn.reg(X_train, test = X_train, k = k, y = y_train)$pred
paramGrid[i, "train"] <- RMSE(y_train, pred_train)
# Performance on valid data
pred_valid <- knn.reg(X_train, test = X_valid, k = k, y = y_train)$pred
paramGrid[i, "valid"] <- RMSE(y_valid, pred_valid)
}
# Best validation RMSE
head(paramGrid[order(paramGrid$valid), ], 2)
# Plot results
pivot_longer(paramGrid, cols = -k, values_to = "RMSE", names_to = "Data") %>%
ggplot(aes(x = k, y = RMSE, group = Data, color = Data)) +
geom_point() +
geom_line()
```
which validation performance is the best? for k = 6 validation
performance is always worse than training performance (clear, since the
predicted value is a neighbor too) typical plot: there is an optimum,
not k the bigger the better why k=1 not 0 for training data? strange,
because multiple diamonds with the same covariates -\> random selection
of one? typical exam question
**Comments**
- The amount of overfitting decreases for growing $k$, which makes
sense.
- Selecting $k$ based on the training data would lead to a suboptimal
model.
- Based on the validation data, we would choose $k=6$. It has a
minimal RMSE of 11.29%.
- Why is the RMSE on the training data not 0 for 1-nearest-neighbor?
- Why is it problematic that some diamonds appear multiple times in
the dataset?
# Cross-Validation (CV)
If our data set is large and training takes long, then the simple
validation strategy presented above is usually good enough. For smaller
data sets or when training is fast, there is a better alternative that
uses the data in a more economical way and makes more robust decisions.
It is called $K$-fold cross-validation and works as follows:
1. Split the data into $K$ pieces $D = \{D_1, \dots, D_K\}$ called
*folds*. Typical values for $K$ are five or ten.
2. Set aside one of the pieces ($D_k$) for validation.
3. Fit the model $\hat f_k$ on all other pieces, i.e., on
$D \setminus D_k$.
4. Calculate the model performance $\hat S_k = S(\hat f_k, D_k)$ on the
validation data $D_k$.
5. Repeat Steps 2 to 4 until each piece was used for validation once.
6. The average of the $K$ model performances yields the *CV
performance* $$
\hat S_{CV} = \frac{1}{K} \sum_{k = 1}^K \hat S_k.
$$
The CV performance is a good basis to choose the best and final model
among alternatives. **The final model is re-trained on all folds.**
**Notes**
- The "best" model is typically the one with best CV performance.
Depending on the situation, it could also be a model with "good CV
performance and not too heavy overfit compared to in-sample
performance" or some other reasonable criterion.
- The standard deviation of $\hat S_1, \dots, \hat S_K$, and/or the
standard error of $\hat S_{CV}$ gives an impression on the stability
of the results.
- If cross-validation is fast, you can repeat the process for
additional data splits. Such *repeated* cross-validation leads to
even more robust results.
## Example
We now use five-fold CV on the diamonds data to find the optimal $k$ of
$k$-nearest-neighbor, i.e., we *tune* our model.
```{r}
library(ggplot2)
library(FNN)
library(withr)
RMSE <- function(y, pred) {
sqrt(mean((y - pred)^2))
}
diamonds <- transform(diamonds, log_price = log(price), log_carat = log(carat))
y <- "log_price"
x <- c("log_carat", "color", "cut", "clarity")
# Scaled feature matrix
X <- scale(data.matrix(diamonds[x]))
# Why do we scale?
# https://medium.com/analytics-vidhya/why-is-scaling-required-in-knn-and-k-means-8129e4d88ed7
# KNN needs scaled features (since it is distance-based between features)
# not totally correct: perfect would be to scale it dependent on the folds
# we have a small leakage of training data to validation data
# Split diamonds into folds
nfolds <- 5
with_seed(
9838,
fold_ix <- sample(1:nfolds, nrow(diamonds), replace = TRUE)
)
table(fold_ix)
# Tuning grid with different values for parameter k
paramGrid <- data.frame(RMSE = NA, k = 1:20)
# Calculate performance for each row in the parameter grid
# important: We have two for loops!!
for (i in 1:nrow(paramGrid)) {
# Why don't we use i directly?
# k does not have to coincide with i!
k <- paramGrid[i, "k"]
scores <- numeric(nfolds)
for (fold in 1:nfolds) {
insample <- fold_ix != fold
X_train <- X[insample, ]
y_train <- diamonds[insample, y]
# why ! and not -? insample is a list of booleans!
# we use ! when working with booleans
X_valid <- X[!insample, ]
y_valid <- diamonds[!insample, y]
pred <- knn.reg(X_train, test = X_valid, k = k, y = y_train)$pred
scores[fold] <- RMSE(y_valid, pred)
}
paramGrid[i, "RMSE"] <- mean(scores)
}
# Best CV-scores
head(paramGrid[order(paramGrid$RMSE), ], 2)
ggplot(paramGrid, aes(x = k, y = RMSE)) +
geom_point(color = "chartreuse4") +
geom_line(color = "chartreuse4") +
ggtitle("Performance by cross-validation")
```
Meta seach: caret package mlr3: object oriented (R6) approach (Python,
C++) tidymodels (strange but modern)
Often we can delegate it to other function
Curve: k is from NN, so probably 7 seems to be the best choice Repeated
CV necessary? Look at SE, SD.
**Comment:** Using 7 neighbors seems to be the best choice regarding CV
RMSE. Again, the fact that certain diamonds show up multiple times
leaves a slightly bad feeling. Should we really trust these results?
## Grid Search
In the above example, we have systematically compared the CV-performance
of $k$-nearest-neighbor by iterating over a grid of possible values for
$k$. Such strategy to *tune* models, i.e., to select hyperparameters of
a model is called **grid search CV**. In the next chapter, we will meet
situations where multiple parameters have to be optimized
simultaneously. Then, the number of parameter combinations and the grid
size explode. To save time, we could evaluate only a random subset of
parameter combinations, an approach called **randomized search CV**.
# Test Data and Final Workflow
Modeling often requires many decisions to be made. Even when guided by
(cross-)validation, each decision tends to make the final model look
better than it effectively is, an effect that can be called *overfitting
on the validation data*.
As a consequence, we often do not know how well the final model will
perform in reality. As a solution, we can set aside a small *test* data
set $D_\text{test}$ used to assess the performance
$S(\hat f, D_\text{test})$ of the *final* model $\hat f$. A size of
$5\% - 20\%$ is usually sufficient. It is important to look at the test
data just once at the very end of the modeling process - after each
decision has been made. The difference between
$S(\hat f, D_\text{test})$ and the corresponding (cross-)validation
score gives an impression of the validation optimism/overfit.
Note: Such an additional test data set is only necessary if one uses the
validation data set to *make decisions*. If the validation data set is
only used to estimate the true performance of a model, then we do not
need this additional data set. In that case, the terms "validation data"
and "test data" are interchangeable.
Depending on whether one is performing simple validation or
cross-validation, the typical workflow is as follows:
**Workflow A**
1. Split data into train/valid/test, e.g., by ratios 60%/20%/20%.
2. Train different models on the training data and assess their
performance on the validation data. Choose the best model, re-train
it on the combination of training and validation data, and call it
"final model".
3. Assess performance of the final model on the test data.
**Workflow B**
1. Split data into train/test, e.g., by ratios 80%/20%.
2. Evaluate and tune different models by $K$-fold cross-validation on
the training data. Select the best model and re-train it on the full
training data.
3. Assess performance of the final model on the test data.
The only difference between the two workflows is whether simple
validation or cross-validation is used for decision making.
Remark: For simplicity, Workflow A is sometimes done without refitting
on the combination of training and validation data. In that case, the
final model is fitted on the training data only.
## Example: Workflow B
We will now follow Workflow B for our diamond price model. We will (1)
tune the $k$ of our nearest-neighbor regression and (2) compare its
result with a linear regression. The model with best CV performance will
be evaluated on the test data.
not only tune k but also give linear regression a chance:
```{r}
library(ggplot2)
library(FNN)
library(withr)
RMSE <- function(y, pred) {
sqrt(mean((y - pred)^2))
}
diamonds <- transform(diamonds, log_price = log(price), log_carat = log(carat))
y <- "log_price"
x <- c("log_carat", "color", "cut", "clarity")
# Split diamonds into 80% for training and 20% for testing
with_seed(
9838,
ix <- sample(nrow(diamonds), 0.8 * nrow(diamonds))
)
train <- diamonds[ix, ]
test <- diamonds[-ix, ]
y_train <- train[[y]]
y_test <- test[[y]]
# Standardize training data
X_train <- scale(data.matrix(train[, x]))
# Apply training scale to test data
X_test <- scale(
data.matrix(test[, x]),
center = attr(X_train, "scaled:center"),
scale = attr(X_train, "scaled:scale")
)
# Split training data into folds
nfolds <- 5
with_seed(
9838,
fold_ix <- sample(1:nfolds, nrow(train), replace = TRUE)
)
# Cross-validation performance of k-nearest-neighbor for k = 1-20
paramGrid <- data.frame(RMSE = NA, k = 1:20)
for (i in 1:nrow(paramGrid)) {
k <- paramGrid[i, "k"]
scores <- numeric(nfolds)
for (fold in 1:nfolds) {
insample <- fold_ix != fold
X_train_cv <- X_train[insample, ]
y_train_cv <- y_train[insample]
X_valid_cv <- X_train[!insample, ]
y_valid_cv <- y_train[!insample]
pred <- knn.reg(X_train_cv, test = X_valid_cv, k = k, y = y_train_cv)$pred
scores[fold] <- RMSE(y_valid_cv, pred)
}
paramGrid[i, "RMSE"] <- mean(scores)
}
# Best CV performance
head(paramGrid[order(paramGrid$RMSE), ], 2)
# Cross-validation performance of linear regression
rmse_reg <- numeric(nfolds)
for (fold in 1:nfolds) {
insample <- fold_ix != fold
fit <- lm(reformulate(x, y), data = train[insample, ])
pred <- predict(fit, newdata = train[!insample, ])
rmse_reg[fold] <- RMSE(y_train[!insample], pred)
}
# try reformulate(x,y), just expands the formula
(rmse_reg <- mean(rmse_reg))
# NN is much better than linear regression
# The overall best model is 6-nearest-neighbor
pred <- knn.reg(X_train, test = X_test, k = 6, y = y_train)$pred
# Test performance for the best model
RMSE(y_test, pred)
```
With caret, mlr3, tidymodels -\> reduce code by around 50%. but not 5
loc! a bit of unfairness: nearest neighbor had more chances than linear
regression. We could add splines, interactions, ... . But it would be
really hard to beat the NN.
Statistician would use p-values. But often we don't have p-values.
**Comments**
- 6-nearest-neighbor regression performs clearly better than linear
regression.
- Its performance on the independent test data is even better than CV
suggests.
# Excursion: Ridge Regression
A ridge regression is a penalized linear regression. It assumes the same
model equation $$
\mathbb E(Y \mid \boldsymbol x) = f(\boldsymbol x) = \beta_0 + \beta_1 x^{(1)} + \dots + \beta_p x^{(p)}
$$ as a "normal" linear regression, but with an L2 penalty added to the
least squares criterion: $$
Q(f, D_{\text{train}}) = \sum_{(y_i, \boldsymbol x_i) \in D_\text{train}} (y_i - f(\boldsymbol x_i))^2 + \lambda \sum_{j = 1}^p \beta_j^2.
$$ Adding such an L2 penalty has the effect of pulling the coefficients
slightly towards zero, fighting overfitting. The optimal penalization
strength is controlled by $\lambda \ge 0$, which usually is determined
by simple validation or cross-validation.
**Remarks**
- To avoid biased average predictions, the penalty is usually not
applied to the intercept.
- To make penalization fair between covariates of different scale, the
regressors are often standardized to variance 1. Most software do
this internally.
- A model using an L1 penalty $\lambda \sum_{j = 1}^p |\beta_j|$ is
called a Lasso model. A model with both L1 and L2 penalties is an
"elastic net" model.
## Example: Taxi
To show an example of ridge regression and Workflow A, we revisit the
taxi trip example of the last chapter. We first split the data into
70%/20%/10% training/validation/test data. Then, we let H2O use the
validation data to find the optimal L2 penalty. Using this penalty, the
final model will be fitted on the combination of training and validation
data, and then evaluated on the 10% test data.
```{r, eval=FALSE}
library(arrow)
library(data.table)
library(ggplot2)
library(h2o)
system.time( # 3 seconds
dim(df <- read_parquet("taxi/yellow_tripdata_2018-01.parquet"))
)
setDT(df)
head(df)
# Data prep
system.time({ # 5 seconds
df[, duration := as.numeric(
difftime(tpep_dropoff_datetime, tpep_pickup_datetime, units = "mins")
)]
df = df[between(trip_distance, 0.2, 100) & between(duration, 1, 120)]
df[, `:=`(
pu_hour = factor(data.table::hour(tpep_pickup_datetime)),
weekday = factor(data.table::wday(tpep_pickup_datetime)), # 1 = Sunday
pu_loc = forcats::fct_lump_min(factor(PULocationID), 1e5),
log_duration = log(duration),
log_distance = log(trip_distance)
)]
})
x <- c("log_distance", "weekday", "pu_hour", "pu_loc")
y <- "log_duration"
h2o.init(min_mem_size = "6G")
h2o_df <- as.h2o(df[, c(x, y), with = FALSE])
h2o_split <- h2o.splitFrame(
h2o_df, c(0.7, 0.2), destination_frames = c("train", "valid", "test")
)
system.time( # 12 s
fit <- h2o.glm(
x = x,
y = "log_duration",
training_frame = "train",
validation_frame = "valid",
lambda_search = TRUE,
alpha = 0 # controls ratio of L1 to L2 penalty strength (0 means no L1)
)
)
fit # Validation R2: 0.782; best lambda essentially 0 (no penalty)
# Combine training + validation
h2o_trainvalid <- h2o.rbind(h2o_split[[1]], h2o_split[[2]])
# Fit model with optimal lambda (0)
fit_final <- h2o.glm(
x = x,
y = "log_duration",
training_frame = h2o_trainvalid,
validation_frame = "test",
lambda = 0,
alpha = 0
)
fit_final # Test R^2: 0.7814
```
**Comment:** According to simple validation, no L2 penalty is needed.
Thus, the final model is fitted without penalty on the pooled 90%
training and validation data. Its test R-squared is very similar to the
validation R-squared, indicating that there is no problematic overfit on
the validation data. Why adding a penalty did not help to improve the
model? Usually, ridge regression shines when the $n/p$ ratio is small.
In our case, however, it is very large.
# Random Splitting?
The data is often split *randomly* into partitions or folds. As long as
the rows are *independent*, this leads to honest estimates of model
performance.
However, if the rows are not independent, e.g. for time series data or
grouped data, such a strategy is flawed and usually leads to overly
optimistic results. **This is a common mistake in modeling.**
## Time-series data
When data represents a time series, splitting is best done in a way that
does not destroy the temporal order. For simple validation, e.g., the
first 80% of rows could be used for training and the remaining 20% for
validation. The specific strategy depends on how the model will be
applied.
## Grouped data
Often, data is grouped or clustered by some (hopefully known) ID
variable, e.g.,
- multiple rows belong to the same patient/customer or
- duplicated rows (accidental or not).
Then, instead of distributing *rows* into partitions, we should
distribute *groups*/IDs in order to not destroy the data structure and
to get honest performance estimates. We speak of *grouped splitting* and
*group K-fold CV*.
In our example with diamonds data, it would be useful to have a column
with diamond "id" that could be used for grouped splitting. (How would
you create a proxy for this?)
## Stratification
*If rows are independent*, there is a variant of random splitting that
can provide slightly better models: *stratified splitting*. With
stratified splitting or stratified K-fold CV, rows are split to enforce
similar distribution of a key variable across partitions/folds.
Stratified splitting is often used when the response variable is binary
and unbalanced. Unbalanced means that the proportion of "1" is close to
0 or 1.
# Excursion: SQL and Spark
> Data science is 80% preparing data, 20% complaining about preparing
> data.
Data tables are usually stored as files on a hard drive or as tables in
a database (DB). Before starting with data analysis and modeling, these
tables have to be preprocessed (filter rows, combine tables,
restructure, rename, transform, ...). How much preprocessing is required
depends strongly on the situation. While this process often feels like a
waste of time, it is actually a good opportunity to learn
- how the data is structured,
- what information the columns bear, and
- what possible sources of bias might be present.
For small data sets, the raw data is usually loaded into R/Python and
then preprocessed in memory. For large data sets, this becomes slow or
even impossible. One option is to externalize the preprocessing to a
database server with its out-of-core capabilities or to a big data
technology like Spark. In this section, we take a short trip into that
world.
## SQL
Communication with the database is usually done with *SQL* (Structured
Query Language). This is one of the most frequently used programming
languages in data science. The SQL code is usually written directly in a
Database Management System (DBMS) or in R/Python. SQL is pronounced as
"es-kiu-el" or "si-kwel".
We will introduce SQL with examples written in the in-process database
[DuckDB](duckdb.org). "In-process" means that when DuckDB is run from
R/Python, it is embedded in the R/Python process itself. DuckDB is
convenient for several reasons:
- It is easy to install.
- It has no external dependencies.
- It is fast.
- It supports working with csv/Parquet files as well as with R/Python
tables.
- It plays well together with Apache Arrow.
- It works out-of-core, i.e., when data does not fit into RAM.
DuckDB has been released in 2018 as an open-source project written in
C++. Later, we will also take a look at Spark, the famous big data
technology.
Remark: While there is an ISO norm for SQL, specific implementations
(DuckDB, Spark, Oracle, SQL Server, ...) extend this standard command
palette, resulting in different dialects. Thus, when googling a command,
it will help to add the name of the implementation, like "extract hour
from date in spark sql".
### Example: SQL
To familiarize you with SQL, we will write some basic SQL queries (=
questions) with DuckDB about the diamond data. (Also remember the
"Translator" table of Chapter 1.)
**Note that we use `head()` to keep the output slim. Usually, you would
not need it.**
```{r}
library(duckdb)
library(tidyverse)
# Initialize virtual DB and register diamonds data
con = dbConnect(duckdb())
duckdb_register(con, name = "dia", df = diamonds)
# Select every column (and every row)
con %>%
dbSendQuery("SELECT * FROM dia") %>%
dbFetch() %>%
head() # See note above
# carat and log(price) sorted by carat in descending order
query <- "
SELECT
carat, LOG(price) AS log_price
FROM dia
ORDER BY carat DESC
" # in reality: Database name, Database scheme name (like folders in database world), Table name. Query as string
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head()
# Filter on carat > 2 and color = 'E'
query <- "
SELECT *
FROM dia
WHERE
carat > 2
AND color = 'E'
" # Filter for some condition
# Use single quotes instead of double quotes
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head()
# Aggregate
query <- "
SELECT
COUNT(*) AS N
, AVG(price) AS avg_price
, SUM(price) / 1e6 AS sum_price_mio
FROM dia
"
con %>%
dbSendQuery(query) %>%
dbFetch()
# so just one row (like summarize in dplyr)
# Grouped aggregate
query <- "
SELECT
color
, COUNT(*) AS N
, AVG(price) AS avg_price
, SUM(price) / 1e6 AS sum_price_mio
FROM dia
GROUP BY color
ORDER BY color
"
con %>%
dbSendQuery(query) %>%
dbFetch()
# Join average price per color to original data as nested query
# using color as join "key"
query <- "
SELECT D.*, avg_price
FROM dia D
LEFT JOIN
(
SELECT color, AVG(price) AS avg_price
FROM dia
GROUP BY color
) G
ON D.color = G.color
"
# Left join: left side dia table (color, ...), right table color (color, x)
# Left join: do not change left table's rows (no additional rows) but enrich it with data from right table (lookup table). It is important, that on right table no multiple rows for the same key, otherwise we will add more rows to the left side.
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head()
# Instead of nesting, we can use "WITH" to connect multiple queries
query <- "
WITH G AS
(
SELECT color, AVG(price) AS avg_price
FROM dia
GROUP BY color
)
SELECT D.*, avg_price
FROM dia D
LEFT JOIN G
ON D.color = G.color
"
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head()
# Some SQL implementations also offer "Window" functions to do things like this
query <- "
SELECT *, AVG(price) OVER (PARTITION BY color) AS avg_price
FROM dia
"
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head()
dbDisconnect(con)
```
### Example: Taxi
Here we will work directly with the taxi Parquet file. The goal is to
answer the following four questions/tasks:
1. How do the first five rows look like?
2. How many rows does the table have?
3. Which pickup location IDs occur at least 100,000 times?
4. Prepare the data similar to our original workflow "Parquet -\> Arrow
-\> data.table".
```{r}
library(duckdb)
library(tidyverse)
con = dbConnect(duckdb())
# Show first five rows of Parquet file
query <- "
SELECT *
FROM 'taxi/yellow_tripdata_2018-01.parquet'
LIMIT 5
"
con %>%
dbSendQuery(query) %>%
dbFetch()
# How many rows does the file have?
query <- "
SELECT COUNT(*) AS N
FROM 'taxi/yellow_tripdata_2018-01.parquet'
"
con %>%
dbSendQuery(query) %>%
dbFetch()
# get number of rows WITHOUT reading in the data
# Pickup location IDs with at least 100'000 rows
# 'HAVING' is like a 'WHERE', but after a GROUP BY
query <- "
SELECT
PULocationID AS pu_loc
, COUNT(*) AS N
FROM 'taxi/yellow_tripdata_2018-01.parquet'
GROUP BY PULocationID
HAVING N >= 100000
"
# WHERE: before aggregation/group by
# HAVING: after aggregation/group by
con %>%
dbSendQuery(query) %>%
dbFetch() %>%
head() # just show top five
# Prepare taxi model data directly from Parquet
query <- "
WITH LOC AS (
SELECT
PULocationID AS pu_loc
, COUNT(*) AS N
FROM 'taxi/yellow_tripdata_2018-01.parquet'
GROUP BY PULocationID
HAVING N >= 100000
)
SELECT
LOG(trip_distance) AS log_distance
, LOG(DATE_DIFF('minutes', tpep_pickup_datetime, tpep_dropoff_datetime)) AS log_duration
, DAYNAME(tpep_pickup_datetime) AS weekday
, EXTRACT(hour FROM tpep_pickup_datetime) as pu_hour
, COALESCE(L.pu_loc, 'Other') AS pu_loc
FROM 'taxi/yellow_tripdata_2018-01.parquet' A
LEFT JOIN LOC L
ON A.PULocationID = L.pu_loc
WHERE
trip_distance BETWEEN 0.2 AND 100
AND DATE_DIFF('minutes', tpep_pickup_datetime, tpep_dropoff_datetime) BETWEEN 1 AND 120
"
system.time(
df <- con %>%
dbSendQuery(query) %>%
dbFetch()
)
# we do basically the same as with data.table but entirely in SQL
# only needs 3 seconds
# in data.table (fastest in R) was around 10 seconds (amazing that DuckDB is so much faster)