-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmachine-learning.qmd
1172 lines (935 loc) · 39.7 KB
/
machine-learning.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
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
# Machine Learning {#sec-machineLearning}
## Getting Started {#sec-machineLearningGettingStarted}
### Load Packages {#sec-machineLearningLoadPackages}
```{r}
library("petersenlab")
library("parallel")
library("doParallel")
library("missRanger")
library("powerjoin")
library("caret")
library("gpboost")
library("tidyverse")
```
### Load Data {#sec-machineLearningLoadData}
```{r}
#| eval: false
#| include: false
# Downloaded Data - Processed
load(file = "./data/nfl_players.RData")
load(file = "./data/nfl_teams.RData")
load(file = "./data/nfl_rosters.RData")
load(file = "./data/nfl_rosters_weekly.RData")
load(file = "./data/nfl_schedules.RData")
load(file = "./data/nfl_combine.RData")
load(file = "./data/nfl_draftPicks.RData")
load(file = "./data/nfl_depthCharts.RData")
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/nfl_pbp.RData", fsep = ""))
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/nfl_4thdown.RData", fsep = ""))
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/nfl_participation.RData", fsep = ""))
load(file = "./data/nfl_actualFantasyPoints_weekly.RData")
load(file = "./data/nfl_injuries.RData")
load(file = "./data/nfl_snapCounts.RData")
load(file = "./data/nfl_espnQBR_seasonal.RData")
load(file = "./data/nfl_espnQBR_weekly.RData")
load(file = "./data/nfl_nextGenStats_weekly.RData")
load(file = "./data/nfl_advancedStatsPFR_seasonal.RData")
load(file = "./data/nfl_advancedStatsPFR_weekly.RData")
load(file = "./data/nfl_playerContracts.RData")
load(file = "./data/nfl_ftnCharting.RData")
load(file = "./data/nfl_playerIDs.RData")
load(file = "./data/nfl_rankings_draft.RData")
load(file = "./data/nfl_rankings_weekly.RData")
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/nfl_expectedFantasyPoints_weekly.RData", fsep = ""))
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/nfl_expectedFantasyPoints_pbp.RData", fsep = ""))
# Calculated Data - Processed
load(file = "./data/nfl_actualStats_career.RData")
load(file = "./data/nfl_actualStats_seasonal.RData")
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/player_stats_weekly.RData", fsep = ""))
load(file = file.path(path, "/OneDrive - University of Iowa/Teaching/Courses/Fantasy Football/Data/player_stats_seasonal.RData", fsep = ""))
```
```{r}
# Downloaded Data - Processed
load(file = "./data/nfl_players.RData")
load(file = "./data/nfl_teams.RData")
load(file = "./data/nfl_rosters.RData")
load(file = "./data/nfl_rosters_weekly.RData")
load(file = "./data/nfl_schedules.RData")
load(file = "./data/nfl_combine.RData")
load(file = "./data/nfl_draftPicks.RData")
load(file = "./data/nfl_depthCharts.RData")
load(file = "./data/nfl_pbp.RData")
load(file = "./data/nfl_4thdown.RData")
load(file = "./data/nfl_participation.RData")
load(file = "./data/nfl_actualFantasyPoints_weekly.RData")
load(file = "./data/nfl_injuries.RData")
load(file = "./data/nfl_snapCounts.RData")
load(file = "./data/nfl_espnQBR_seasonal.RData")
load(file = "./data/nfl_espnQBR_weekly.RData")
load(file = "./data/nfl_nextGenStats_weekly.RData")
load(file = "./data/nfl_advancedStatsPFR_seasonal.RData")
load(file = "./data/nfl_advancedStatsPFR_weekly.RData")
load(file = "./data/nfl_playerContracts.RData")
load(file = "./data/nfl_ftnCharting.RData")
load(file = "./data/nfl_playerIDs.RData")
load(file = "./data/nfl_rankings_draft.RData")
load(file = "./data/nfl_rankings_weekly.RData")
load(file = "./data/nfl_expectedFantasyPoints_weekly.RData")
load(file = "./data/nfl_expectedFantasyPoints_pbp.RData")
# Calculated Data - Processed
load(file = "./data/nfl_actualStats_career.RData")
load(file = "./data/nfl_actualStats_seasonal.RData")
load(file = "./data/player_stats_weekly.RData")
load(file = "./data/player_stats_seasonal.RData")
```
### Specify Options {#machineLearningSpecifyOptions}
```{r}
options(scipen = 999) # prevent scientific notation
```
## Overview of Machine Learning {#sec-machineLearningOverview}
Machine learning takes us away from focusing on [causal inference](#sec-causalInference).
Machine learning does not care about which processes are causal—i.e., which processes influence the outcome.
Instead, machine learning cares about prediction—it cares about a predictor variable to the extent that it increases predictive accuracy regardless of whether it is causally related to the outcome.
Machine learning can be useful for leveraging big data and lots of predictor variable to develop predictive models with greater accuracy.
However, many machine learning techniques are black boxes—it is often unclear how or why certain predictions are made, which can make it difficult to interpret the model's decisions and understand the underlying relationships between variables.
Machine learning tends to be a data-driven, atheoretical technique.
This can result in overfitting.
Thus, when estimating machine learning models, it is common to keep a hold-out sample for use in cross-validation to evaluate the extent of shrinkage of model coefficients.
The data that the model is trained on is known as the "training data".
The data that the model was not trained on but is then is independently tested on—i.e., the hold-out sample—is the "test data".
Shrinkage occurs when predictor variables explain some random error variance in the original model.
When the model is applied to an independent sample (i.e., the test data), the predictive model will likely not perform quite as well, and the regressions coefficients will tend to get smaller (i.e., shrink).
If the test data were collected as part of the same processes as the original data and were merely held out for purposes of analysis, this is called internal cross-validation.
If the test data were collected separately from the original data used to train the model, this is called external cross-validation.
Most machine learning methods were developed with cross-sectional data in mind.
That is, they assume that each person has only one observation on the outcome variable.
However, with longitudinal data, each person has multiple observations on the outcome variable.
When performing machine learning, various approaches may help address this:
- transform data from [long to wide](#sec-longToWide) form, so that each person has only one row
- when designing the training and test sets, keep all measurements from the same person in the same data object (either the training or test set); do not have some measurements from a given person in the training set and other measurements from the same person in the test set
- use a machine learning approach that accounts for the clustered/nested nature of the data
## Types of Machine Learning {#sec-machineLearningTypes}
There are many approaches to machine learning.
This chapter discusses several key ones:
- supervised learning
- continuous outcome (i.e., regression)
- linear regression
- lasso regression
- ridge regression
- elastic net regression
- categorical outcome (i.e., classification)
- logistic regression
- support vector machine
- random forest
- extreme gradient boosting
- unsupervised learning
- clustering
- principal component analysis
- semi-supervised learning
- reinforcement learning
- deep learning
*Ensemble* machine learning methods combine multiple machine learning approaches with the goal that combining multiple approaches might lead to more accurate predictions that any one method might be able to achieve on its own.
### Supervised Learning {#sec-machineLearningTypesSupervised}
[DEFINE SUPERVISED LEARNING]
Unlike linear and logistic regression, various machine learning techniques can handle multicollinearity, including LASSO regression, ridge regression, and elastic net regression.
Least absolute shrinkage and selection operator (LASSO) regression helps perform selection of which predictor variables to keep in the model by shrinking some coefficients to zero.
Ridge regression shrinks the coefficients of predictor variables toward zero, but not to zero, so it does not perform selection of which predictor variables to retain; this allows it to allow nonzero coefficients for multiple correlated predictor variables in the context of multicollinearity.
Elastic net involves a combination of LASSO and ridge regression; it performs selection of which predictor variables to keep by shrinking the coefficients of some predictor variables to zero, and it shrinks the coefficients of some predictor variables toward zero, to address multicollinearity.
Unless interactions or nonlinear terms are specified, linear, logistic, LASSO, ridge, and elastic net regresstion do not account for interactions among the predictor variables or for nonlinear associations between the predictor variables and the outcome variable.
By contrast, random forests and extreme gradient boosting do account for interactions among the predictor variables and for nonlinear associations between the predictor variables and the outcome variable.
### Unsupervised Learning {#sec-machineLearningTypesUnsupervised}
[DEFINE UNSUPERVISED LEARNING]
We describe [cluster analysis](#sec-clusterAnalysis) in @sec-clusterAnalysis.
We describe [principal component analysis](#sec-pca) in @sec-pca.
### Semi-supervised Learning {#sec-machineLearningTypesSemisupervised}
[DEFINE SEMI-SUPERVISED LEARNING]
### Reinforcement Learning {#sec-machineLearningTypesReinforcement}
[DEFINE REINFORCEMENT LEARNING]
## Data Processing {#sec-machineLearningDataProcessing}
```{r}
#| eval: false
#| include: false
varNames <- c(
names(nfl_players),
names(nfl_teams),
names(nfl_rosters),
names(nfl_rosters_weekly),
names(nfl_schedules),
names(nfl_combine),
names(nfl_draftPicks),
names(nfl_depthCharts),
names(nfl_pbp),
names(nfl_4thdown),
names(nfl_participation),
names(nfl_actualFantasyPoints_player_weekly),
names(nfl_injuries),
names(nfl_snapCounts),
names(nfl_espnQBR_seasonal),
names(nfl_espnQBR_weekly),
names(nfl_nextGenStats_weekly),
names(nfl_advancedStatsPFR_seasonal),
names(nfl_advancedStatsPFR_weekly),
names(nfl_playerContracts),
names(nfl_ftnCharting),
names(nfl_playerIDs),
names(nfl_rankings_draft),
names(nfl_rankings_weekly),
names(nfl_expectedFantasyPoints_weekly),
names(nfl_expectedFantasyPoints_pbp)
)
varNames <- unique(varNames)
write.csv(
varNames,
file = "./data/varNames.csv",
row.names = FALSE
)
nfl_players$gsis_id
nfl_rosters$gsis_id
nfl_rosters_weekly$gsis_id
nfl_draftPicks$gsis_id
nfl_depthCharts$gsis_id
nfl_advancedStatsPFR_seasonal$gsis_id
nfl_actualStats_offense_weekly$player_id
nfl_expectedFantasyPoints_weekly$player_id
nfl_rankings$id
nfl_combine$pfr_id
nfl_advancedStatsPFR_seasonal$pfr_id
#nfl_advancedStatsPFR_seasonal$pfr_player_id
nfl_playerIDs$gsis_id
nfl_playerIDs$pfr_id
```
### Prepare Data for Merging {#sec-machineLearningPrepareDataForMerging}
```{r}
# Prepare data for merging
#-todo: calculate years_of_experience
## Use common name for the same (gsis_id) ID variable
nfl_actualFantasyPoints_player_weekly <- nfl_actualFantasyPoints_player_weekly %>%
rename(gsis_id = player_id)
nfl_actualFantasyPoints_player_seasonal <- nfl_actualFantasyPoints_player_seasonal %>%
rename(gsis_id = player_id)
player_stats_seasonal_offense <- player_stats_seasonal %>%
filter(position_group %in% c("QB","RB","WR","TE")) %>%
rename(gsis_id = player_id)
player_stats_weekly_offense <- player_stats_weekly %>%
filter(position_group %in% c("QB","RB","WR","TE")) %>%
rename(gsis_id = player_id)
nfl_expectedFantasyPoints_weekly <- nfl_expectedFantasyPoints_weekly %>%
rename(gsis_id = player_id)
## Rename other variables to ensure common names
## Ensure variables with the same name have the same type
nfl_players <- nfl_players %>%
mutate(
birth_date = as.Date(birth_date),
jersey_number = as.character(jersey_number),
gsis_it_id = as.character(gsis_it_id),
years_of_experience = as.integer(years_of_experience))
player_stats_seasonal_offense <- player_stats_seasonal_offense %>%
mutate(
birth_date = as.Date(birth_date),
jersey_number = as.character(jersey_number),
gsis_it_id = as.character(gsis_it_id))
nfl_rosters <- nfl_rosters %>%
mutate(
draft_number = as.integer(draft_number))
nfl_rosters_weekly <- nfl_rosters %>%
mutate(
draft_number = as.integer(draft_number))
nfl_depthCharts <- nfl_depthCharts %>%
mutate(
season = as.integer(season))
nfl_expectedFantasyPoints_weekly <- nfl_expectedFantasyPoints_weekly %>%
mutate(
season = as.integer(season),
receptions = as.integer(receptions))
## Rename variables
#-todo: rename variables in expected fantasy points so they don't get coalesced with actual points
nfl_draftPicks <- nfl_draftPicks %>%
rename(
games_career = games,
pass_completions_career = pass_completions,
pass_attempts_career = pass_attempts,
pass_yards_career = pass_yards,
pass_tds_career = pass_tds,
pass_ints_career = pass_ints,
rush_atts_career = rush_atts,
rush_yards_career = rush_yards,
rush_tds_career = rush_tds,
receptions_career = receptions,
rec_yards_career = rec_yards,
rec_tds_career = rec_tds,
def_solo_tackles_career = def_solo_tackles,
def_ints_career = def_ints,
def_sacks_career = def_sacks
)
# Check duplicate ids
player_stats_seasonal_offense %>%
group_by(gsis_id, season) %>%
filter(n() > 1) %>%
head()
nfl_advancedStatsPFR_seasonal %>%
group_by(gsis_id, season) %>%
filter(n() > 1, !is.na(gsis_id)) %>%
select(gsis_id, pfr_id, season, team, everything()) %>%
head()
```
### Merge Data {#sec-machineLearningMergeData}
```{r}
# Create lists of objects to merge, depending on data structure: id; or id-season; or id-season-week
#-todo: remove redundant variables
playerListToMerge <- list(
nfl_players %>% filter(!is.na(gsis_id)),
nfl_draftPicks %>% filter(!is.na(gsis_id)) %>% select(-season)
)
playerSeasonListToMerge <- list(
player_stats_seasonal_offense %>% filter(!is.na(gsis_id), !is.na(season)),
nfl_advancedStatsPFR_seasonal %>% filter(!is.na(gsis_id), !is.na(season))
)
playerSeasonWeekListToMerge <- list(
nfl_rosters_weekly %>% filter(!is.na(gsis_id), !is.na(season), !is.na(week)),
#nfl_actualStats_offense_weekly,
nfl_expectedFantasyPoints_weekly %>% filter(!is.na(gsis_id), !is.na(season), !is.na(week))
#nfl_advancedStatsPFR_weekly,
)
playerSeasonWeekPositionListToMerge <- list(
nfl_depthCharts %>% filter(!is.na(gsis_id), !is.na(season), !is.na(week))
)
# Merge data
playerMerged <- playerListToMerge %>%
reduce(
powerjoin::power_full_join,
by = c("gsis_id"),
conflict = coalesce_xy)
playerSeasonMerged <- playerSeasonListToMerge %>%
reduce(
powerjoin::power_full_join,
by = c("gsis_id","season"),
conflict = coalesce_xy)
playerSeasonWeekMerged <- playerSeasonWeekListToMerge %>%
reduce(
powerjoin::power_full_join,
by = c("gsis_id","season","week"),
conflict = coalesce_xy)
seasonalData <- powerjoin::power_full_join(
playerSeasonMerged,
playerMerged %>% select(-age, -years_of_experience, -team, -team_abbr, -team_seq, -current_team_id),
by = "gsis_id",
conflict = coalesce_xy
) %>%
filter(!is.na(season)) %>%
select(gsis_id, season, player_display_name, position, team, games, everything())
seasonalAndWeeklyData <- powerjoin::power_full_join(
playerSeasonWeekMerged,
seasonalData,
by = c("gsis_id","season"),
conflict = coalesce_xy
) %>%
filter(!is.na(week))
# Duplicate cases
seasonalData %>%
group_by(gsis_id, season) %>%
filter(n() > 1) %>%
head()
seasonalAndWeeklyData %>%
group_by(gsis_id, season, week) %>%
filter(n() > 1) %>%
head()
```
### Additional Processing {#sec-mlAdditionalProcessing}
```{r}
# Convert character and logical variables to factors
seasonalData <- seasonalData %>%
mutate(
across(
where(is.character),
as.factor
),
across(
where(is.logical),
as.factor
)
)
```
### Fill in Missing Data for Static Variables {#sec-fillMissingData}
```{r}
seasonalData <- seasonalData %>%
arrange(gsis_id, season) %>%
group_by(gsis_id) %>%
fill(
player_name, player_display_name, pos, position, position_group,
.direction = "downup") %>%
ungroup()
```
### Lag Fantasy Points {#sec-lagFantasyPoints}
```{r}
seasonalData_lag <- seasonalData %>%
arrange(gsis_id, season) %>%
group_by(gsis_id) %>%
mutate(
fantasyPoints_lag = lead(fantasyPoints)
) %>%
ungroup()
seasonalData_lag %>%
select(gsis_id, player_display_name, season, fantasyPoints, fantasyPoints_lag)
```
### Subset to Predictor Variables and Outcome Variable {#sec-subsetToPredictorsAndOutcome}
```{r}
seasonalData_lag %>% select_if(~class(.) == "Date")
seasonalData_lag %>% select_if(is.character)
seasonalData_lag %>% select_if(is.factor)
seasonalData_lag %>% select_if(is.logical)
dropVars <- c(
"birth_date", "loaded", "full_name", "player_name", "player_display_name", "display_name", "suffix", "headshot_url", "player", "pos",
"espn_id", "sportradar_id", "yahoo_id", "rotowire_id", "pff_id", "fantasy_data_id", "sleeper_id", "pfr_id",
"pfr_player_id", "cfb_player_id", "pfr_player_name", "esb_id", "gsis_it_id", "smart_id",
"college", "college_name", "team_abbr", "current_team_id", "college_conference", "draft_club", "status_description_abbr",
"status_short_description", "short_name", "headshot", "uniform_number", "jersey_number", "first_name", "last_name",
"football_name", "team")
seasonalData_lag_subset <- seasonalData_lag %>%
dplyr::select(-any_of(dropVars))
```
### Separate by Position {#sec-separateByPosition}
```{r}
seasonalData_lag_subsetQB <- seasonalData_lag_subset %>%
filter(position == "QB") %>%
select(
gsis_id, season, games, gs, years_of_experience, age, ageCentered20, ageCentered20Quadratic,
height, weight, rookie_year, draft_number,
fantasy_points, fantasy_points_ppr, fantasyPoints, fantasyPoints_lag,
completions:rushing_2pt_conversions, special_teams_tds, contains(".pass"), contains(".rush"))
seasonalData_lag_subsetRB <- seasonalData_lag_subset %>%
filter(position == "RB") %>%
select(
gsis_id, season, games, gs, years_of_experience, age, ageCentered20, ageCentered20Quadratic,
height, weight, rookie_year, draft_number,
fantasy_points, fantasy_points_ppr, fantasyPoints, fantasyPoints_lag,
carries:special_teams_tds, contains(".rush"), contains(".rec"))
seasonalData_lag_subsetWR <- seasonalData_lag_subset %>%
filter(position == "WR") %>%
select(
gsis_id, season, games, gs, years_of_experience, age, ageCentered20, ageCentered20Quadratic,
height, weight, rookie_year, draft_number,
fantasy_points, fantasy_points_ppr, fantasyPoints, fantasyPoints_lag,
carries:special_teams_tds, contains(".rush"), contains(".rec"))
seasonalData_lag_subsetTE <- seasonalData_lag_subset %>%
filter(position == "TE") %>%
select(
gsis_id, season, games, gs, years_of_experience, age, ageCentered20, ageCentered20Quadratic,
height, weight, rookie_year, draft_number,
fantasy_points, fantasy_points_ppr, fantasyPoints, fantasyPoints_lag,
carries:special_teams_tds, contains(".rush"), contains(".rec"))
```
### Split into Test and Training Data {#sec-splitTestTraining}
CURRENTLY (WILL CHANGE):
- seasonalData: 1999-2023
- seasonalData_lag: 1999-2022 (predicting fantasy points in 2023)
- newData_seasonal: 2023 (to be used for predicting fantasy points in 2024)
to create:
- seasonalData_lag_all: 1999-2023 (predicting fantasy points in 2024)
- seasonalData_lag_train: 1999-2022 (predicting fantasy points in 2023), most players
- seasonalData_lag_test: 1999-2022 (predicting fantasy points in 2023), some retired players
- (eventually, newData_seasonal, which is derived from the imputed version of seasonalData_lag_all, and then keeps only data from 2023, thus predicting fantasy points in 2024, and removing the fantasy_points_lag variable)
To impute:
- seasonalData_lag_all: 1999-2023 (predicting fantasy points in 2024)
- seasonalData_lag_train: 1999-2022 (predicting fantasy points in 2023), most players
- seasonalData_lag_test: 1999-2022 (predicting fantasy points in 2023), some retired players
IMPUTATION AND MODEL PROCESS:
IMPUTATION:
- training data
- test data
- all data (training and test data), used for generating next year predictions
MODEL:
- training data (imputed version of seasonalData_lag_all): all seasons (except 2023 predicting 2024), most players
EVALUATE MODEL PREDICTIONS:
- test data (imputed version of seasonalData_lag_test): all seasons (except 2023 predicting 2024), some retired players
GENERATE MODEL PREDICTIONS
- next year data (newData_seasonal): 2023 (predicting 2024)
```{r}
seasonalData_lag_qb_all <- seasonalData_lag_subsetQB
seasonalData_lag_rb_all <- seasonalData_lag_subsetRB
seasonalData_lag_wr_all <- seasonalData_lag_subsetWR
seasonalData_lag_te_all <- seasonalData_lag_subsetTE
set.seed(52242)
activeQBs <- unique(seasonalData_lag_qb_all$gsis_id[which(seasonalData_lag_qb_all$season == max(seasonalData_lag_qb_all$season, na.rm = TRUE))])
retiredQBs <- unique(seasonalData_lag_qb_all$gsis_id[which(seasonalData_lag_qb_all$gsis_id %ni% activeQBs)])
numQBs <- length(unique(seasonalData_lag_qb_all$gsis_id))
qbHoldoutIDs <- sample(retiredQBs, size = ceiling(.2 * numQBs)) # holdout 20% of players
activeRBs <- unique(seasonalData_lag_rb_all$gsis_id[which(seasonalData_lag_rb_all$season == max(seasonalData_lag_rb_all$season, na.rm = TRUE))])
retiredRBs <- unique(seasonalData_lag_rb_all$gsis_id[which(seasonalData_lag_rb_all$gsis_id %ni% activeRBs)])
numRBs <- length(unique(seasonalData_lag_rb_all$gsis_id))
rbHoldoutIDs <- sample(retiredRBs, size = ceiling(.2 * numRBs)) # holdout 20% of players
activeWRs <- unique(seasonalData_lag_wr_all$gsis_id[which(seasonalData_lag_wr_all$season == max(seasonalData_lag_wr_all$season, na.rm = TRUE))])
retiredWRs <- unique(seasonalData_lag_wr_all$gsis_id[which(seasonalData_lag_wr_all$gsis_id %ni% activeWRs)])
numWRs <- length(unique(seasonalData_lag_wr_all$gsis_id))
wrHoldoutIDs <- sample(retiredWRs, size = ceiling(.2 * numWRs)) # holdout 20% of players
activeTEs <- unique(seasonalData_lag_te_all$gsis_id[which(seasonalData_lag_te_all$season == max(seasonalData_lag_te_all$season, na.rm = TRUE))])
retiredTEs <- unique(seasonalData_lag_te_all$gsis_id[which(seasonalData_lag_te_all$gsis_id %ni% activeTEs)])
numTEs <- length(unique(seasonalData_lag_te_all$gsis_id))
teHoldoutIDs <- sample(retiredTEs, size = ceiling(.2 * numTEs)) # holdout 20% of players
seasonalData_lag_qb_train <- seasonalData_lag_qb_all %>%
filter(gsis_id %ni% qbHoldoutIDs)
seasonalData_lag_qb_test <- seasonalData_lag_qb_all %>%
filter(gsis_id %in% qbHoldoutIDs)
seasonalData_lag_rb_train <- seasonalData_lag_rb_all %>%
filter(gsis_id %ni% rbHoldoutIDs)
seasonalData_lag_rb_test <- seasonalData_lag_rb_all %>%
filter(gsis_id %in% rbHoldoutIDs)
seasonalData_lag_wr_train <- seasonalData_lag_wr_all %>%
filter(gsis_id %ni% wrHoldoutIDs)
seasonalData_lag_wr_test <- seasonalData_lag_wr_all %>%
filter(gsis_id %in% wrHoldoutIDs)
seasonalData_lag_te_train <- seasonalData_lag_te_all %>%
filter(gsis_id %ni% teHoldoutIDs)
seasonalData_lag_te_test <- seasonalData_lag_te_all %>%
filter(gsis_id %in% teHoldoutIDs)
```
### Impute the Missing Data {#sec-missingDataImputation}
NOTES:
- CONSIDER USING RETIRED PLAYERS AS THE HOLDOUT SAMPLE
- CONSIDER WIDENING THE DATA TO AVOID MULTILEVEL IMPUTATION
- CONSIDER IMPUTING THE TRAINING AND TEST DATA SEPARATELY BY POSITION
Here is a vignette demonstrating how to impute missing data using `missForest()`: <https://rpubs.com/lmorgan95/MissForest> (archived at: <https://perma.cc/6GB4-2E22>).
Below, we impute the training data (and all data) separately by position.
We then use the imputed training data to make out-of-sample predictions to fill in the missing data for the testing data.
We do not want to impute the training and testing data together so that we can keep them separate for the purposes of cross-validation.
However, we impute all data (training and test data together) for purposes of making out-of-sample predictions from the machine learning models to predict players' performance next season (when actuals are not yet available for evaluating their accuracy).
::: {#nte-machineLearningImputeMissingData .callout-note title="Impute missing data for machine learning"}
Note: the following code takes a while to run.
:::
```{r}
# QBs
seasonalData_lag_qb_all_imp <- missRanger::missRanger(
seasonalData_lag_qb_all,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_qb_all_imp
data_all_qb <- seasonalData_lag_qb_all_imp$data
data_all_qb_matrix <- data_all_qb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
newData_qb <- data_all_qb %>%
filter(season == max(season, na.rm = TRUE)) %>%
select(-fantasyPoints_lag)
newData_qb_matrix <- data_all_qb_matrix[
data_all_qb_matrix[, "season"] == max(data_all_qb_matrix[, "season"], na.rm = TRUE), # keep only rows with the most recent season
, # all columns
drop = FALSE]
dropCol_qb <- which(colnames(newData_qb_matrix) == "fantasyPoints_lag")
newData_qb_matrix <- newData_qb_matrix[, -dropCol_qb, drop = FALSE]
seasonalData_lag_qb_train_imp <- missRanger::missRanger(
seasonalData_lag_qb_train,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_qb_train_imp
data_train_qb <- seasonalData_lag_qb_train_imp$data
data_train_qb_matrix <- data_train_qb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
seasonalData_lag_qb_test_imp <- predict(
object = seasonalData_lag_qb_train_imp,
newdata = seasonalData_lag_qb_test,
seed = 52242)
data_test_qb <- seasonalData_lag_qb_test_imp
data_test_qb_matrix <- data_test_qb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
# RBs
seasonalData_lag_rb_all_imp <- missRanger::missRanger(
seasonalData_lag_rb_all,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_rb_all_imp
data_all_rb <- seasonalData_lag_rb_all_imp$data
data_all_rb_matrix <- data_all_rb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
newData_rb <- data_all_rb %>%
filter(season == max(season, na.rm = TRUE)) %>%
select(-fantasyPoints_lag)
newData_rb_matrix <- data_all_rb_matrix[
data_all_rb_matrix[, "season"] == max(data_all_rb_matrix[, "season"], na.rm = TRUE), # keep only rows with the most recent season
, # all columns
drop = FALSE]
dropCol_rb <- which(colnames(newData_rb_matrix) == "fantasyPoints_lag")
newData_rb_matrix <- newData_rb_matrix[, -dropCol_rb, drop = FALSE]
seasonalData_lag_rb_train_imp <- missRanger::missRanger(
seasonalData_lag_rb_train,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_rb_train_imp
data_train_rb <- seasonalData_lag_rb_train_imp$data
data_train_rb_matrix <- data_train_rb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
seasonalData_lag_rb_test_imp <- predict(
object = seasonalData_lag_rb_train_imp,
newdata = seasonalData_lag_rb_test,
seed = 52242)
data_test_rb <- seasonalData_lag_rb_test_imp
data_test_rb_matrix <- data_test_rb %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
# WRs
seasonalData_lag_wr_all_imp <- missRanger::missRanger(
seasonalData_lag_wr_all,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_wr_all_imp
data_all_wr <- seasonalData_lag_wr_all_imp$data
data_all_wr_matrix <- data_all_wr %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
newData_wr <- data_all_wr %>%
filter(season == max(season, na.rm = TRUE)) %>%
select(-fantasyPoints_lag)
newData_wr_matrix <- data_all_wr_matrix[
data_all_wr_matrix[, "season"] == max(data_all_wr_matrix[, "season"], na.rm = TRUE), # keep only rows with the most recent season
, # all columns
drop = FALSE]
dropCol_wr <- which(colnames(newData_wr_matrix) == "fantasyPoints_lag")
newData_wr_matrix <- newData_wr_matrix[, -dropCol_wr, drop = FALSE]
seasonalData_lag_wr_train_imp <- missRanger::missRanger(
seasonalData_lag_wr_train,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_wr_train_imp
data_train_wr <- seasonalData_lag_wr_train_imp$data
data_train_wr_matrix <- data_train_wr %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
seasonalData_lag_wr_test_imp <- predict(
object = seasonalData_lag_wr_train_imp,
newdata = seasonalData_lag_wr_test,
seed = 52242)
data_test_wr <- seasonalData_lag_wr_test_imp
data_test_wr_matrix <- data_test_wr %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
# TEs
seasonalData_lag_te_all_imp <- missRanger::missRanger(
seasonalData_lag_te_all,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_te_all_imp
data_all_te <- seasonalData_lag_te_all_imp$data
data_all_te_matrix <- data_all_te %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
newData_te <- data_all_te %>%
filter(season == max(season, na.rm = TRUE)) %>%
select(-fantasyPoints_lag)
newData_te_matrix <- data_all_te_matrix[
data_all_te_matrix[, "season"] == max(data_all_te_matrix[, "season"], na.rm = TRUE), # keep only rows with the most recent season
, # all columns
drop = FALSE]
dropCol_te <- which(colnames(newData_te_matrix) == "fantasyPoints_lag")
newData_te_matrix <- newData_te_matrix[, -dropCol_te, drop = FALSE]
seasonalData_lag_te_train_imp <- missRanger::missRanger(
seasonalData_lag_te_train,
pmm.k = 5,
verbose = 2,
seed = 52242,
keep_forests = TRUE)
seasonalData_lag_te_train_imp
data_train_te <- seasonalData_lag_te_train_imp$data
data_train_te_matrix <- data_train_te %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
seasonalData_lag_te_test_imp <- predict(
object = seasonalData_lag_te_train_imp,
newdata = seasonalData_lag_te_test,
seed = 52242)
data_test_te <- seasonalData_lag_te_test_imp
data_test_te_matrix <- data_test_te %>%
mutate(across(where(is.factor), ~ as.numeric(as.integer(.)))) %>%
as.matrix()
```
## Identify Cores for Parallel Processing {#sec-coresParallel}
```{r}
#| include: false
num_cores <- parallel::detectCores()
num_true_cores <- parallel::detectCores(logical = FALSE)
```
```{r}
#| eval: false
num_cores <- detectCores() - 1
num_true_cores <- parallel::detectCores(logical = FALSE) - 1
```
```{r}
num_cores
```
## Fitting the Traditional Regression Models {#sec-fittingModels-regression}
### Regression with One Predictor {#sec-regressionOnePredictor}
### Regression with Multiple Predictors {#sec-regressionMultiplePredictors}
## Fitting the Machine Learning Models {#sec-fittingModels-machineLearning}
### Least Absolute Shrinkage and Selection Option (LASSO) {#sec-lasso}
### Ridge Regression {#sec-ridgeRegression}
### Elastic Net {#sec-elasticNet}
### Random Forest Machine Learning {#sec-randomForest}
#### Cross-Sectional Data {#sec-randomForestCrossSectionalData}
```{r}
cl <- parallel::makeCluster(num_cores)
doParallel::registerDoParallel(cl)
set.seed(52242)
randomForest_qb <- caret::train(
fantasyPoints_lag ~ ., # use all predictors
data = seasonalData_lag_subsetQB_imp$ximp,
method = "rf",
trControl = trainControl(
method = "cv",
number = 10)) # 10-fold cross-validation
randomForest_rb <- caret::train(
fantasyPoints_lag ~ ., # use all predictors
data = seasonalData_lag_subsetRB_imp$ximp,
method = "rf",
trControl = trainControl(
method = "cv",
number = 10)) # 10-fold cross-validation
randomForest_wr <- caret::train(
fantasyPoints_lag ~ ., # use all predictors
data = seasonalData_lag_subsetWR_imp$ximp,
method = "rf",
trControl = trainControl(
method = "cv",
number = 10)) # 10-fold cross-validation
randomForest_te <- caret::train(
fantasyPoints_lag ~ ., # use all predictors
data = seasonalData_lag_subsetTE_imp$ximp,
method = "rf",
trControl = trainControl(
method = "cv",
number = 10)) # 10-fold cross-validation
stopCluster(cl)
print(randomForest_qb)
print(randomForest_rb)
print(randomForest_wr)
print(randomForest_te)
newData_seasonalQB_imp$ximp$fantasyPoints_lag <- predict(
randomForest_qb,
newdata = newData_seasonalQB_imp$ximp
)
newData_seasonalRB_imp$ximp$fantasyPoints_lag <- predict(
randomForest_rb,
newdata = newData_seasonalRB_imp$ximp
)
newData_seasonalWR_imp$ximp$fantasyPoints_lag <- predict(
randomForest_wr,
newdata = newData_seasonalWR_imp$ximp
)
newData_seasonalTE_imp$ximp$fantasyPoints_lag <- predict(
randomForest_te,
newdata = newData_seasonalTE_imp$ximp
)
newData_seasonalQB$fantasyPoints_lag <- newData_seasonalQB_imp$ximp$fantasyPoints_lag
newData_seasonalRB$fantasyPoints_lag <- newData_seasonalRB_imp$ximp$fantasyPoints_lag
newData_seasonalWR$fantasyPoints_lag <- newData_seasonalWR_imp$ximp$fantasyPoints_lag
newData_seasonalTE$fantasyPoints_lag <- newData_seasonalTE_imp$ximp$fantasyPoints_lag
newData_seasonalQB <- left_join(
newData_seasonalQB,
newData_seasonal %>% select(gsis_id, player_display_name, team),
by = "gsis_id"
)
newData_seasonalRB <- left_join(
newData_seasonalRB,
newData_seasonal %>% select(gsis_id, player_display_name, team),
by = "gsis_id"
)
newData_seasonalWR <- left_join(
newData_seasonalWR,
newData_seasonal %>% select(gsis_id, player_display_name, team),
by = "gsis_id"
)
newData_seasonalTE <- left_join(
newData_seasonalTE,
newData_seasonal %>% select(gsis_id, player_display_name, team),
by = "gsis_id"
)
newData_seasonalQB %>%
arrange(-fantasyPoints_lag) %>%
select(gsis_id, player_display_name, fantasyPoints_lag)
newData_seasonalRB %>%
arrange(-fantasyPoints_lag) %>%
select(gsis_id, player_display_name, fantasyPoints_lag)
newData_seasonalWR %>%
arrange(-fantasyPoints_lag) %>%
select(gsis_id, player_display_name, fantasyPoints_lag)
newData_seasonalTE %>%
arrange(-fantasyPoints_lag) %>%
select(gsis_id, player_display_name, fantasyPoints_lag)
```
#### Longitudinal Data {#sec-randomForestLongitudinalData}
[@Hu2023]
```{r}
#| eval: false
library("LongituRF")
smerf <- LongituRF::MERF(
X = seasonalData_subsetQB_imp$ximp[,c("passing_epa")] %>% as.matrix(),
Y = seasonalData_subsetQB$fantasyPoints_lag,
Z = seasonalData_subsetQB_imp$ximp[,c("pacr")] %>% as.matrix(),
id = seasonalData_subsetQB$gsis_id,
time = seasonalData_subsetQB_imp$ximp[,c("ageCentered20")] %>% as.matrix(),
ntree = 500,
sto = "BM")
smerf$forest # the fitted random forest (obtained at the last iteration)
smerf$random_effects # the predicted random effects for each player
smerf$omega # the predicted stochastic processes
plot(smerf$Vraisemblance) # evolution of the log-likelihood
smerf$OOB # OOB error at each iteration
```
### *k*-Fold Cross-Validation {#sec-kfoldCV}
### Leave-One-Out (LOO) Cross-Validation {#sec-looCV}
### Combining Tree-Boosting with Mixed Models {#sec-treeBoosting}
Adapted from here:
<https://towardsdatascience.com/mixed-effects-machine-learning-for-longitudinal-panel-data-with-gpboost-part-iii-523bb38effc>
#### Process Data {#sec-treeBoostingProcessData}
If using a gamma distribution, it requires positive-only values:
```{r}
#data_train_qb_matrix[,"fantasyPoints_lag"][data_train_qb_matrix[,"fantasyPoints_lag"] <= 0] <- 0.01
```