-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectMarchMadness.R
690 lines (618 loc) · 26.9 KB
/
ProjectMarchMadness.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
# https://www.kaggle.com/datasets/andrewsundberg/college-basketball-dataset/
# Data from the 2013-2023 Division I college basketball seasons.
# cbb.csv has seasons 2013-2019 and seasons 2021-2023 combined.
# cbb20.csv is kept separate from the other seasons, because there was no postseason due to the Coronavirus.
# csv files were downloaded Sunday November 12, 2023
library(tidyverse)
library(caret)
getwd() ##### run this command to get working directory ###
cbb <- file.path(getwd(), "Projects/R/Project/MarchMadness/cbb.csv")
#cbb20 <- file.path(getwd(), "Projects/R/Project/MarchMadness/cbb20.csv")
###########
# read csv
cbb_csv <- read_csv(cbb)
rm(cbb)
#################
# pre-processing
postseason_levels <- c("R68", "R64", "R32", "S16", "E8", "F4", "2ND", "Champions")
dat <- cbb_csv |>
mutate(SEED = as.integer(SEED), # coercion warning
POSTSEASON = factor(POSTSEASON, levels = postseason_levels)) |>
filter(!is.na(SEED))
levels(dat$POSTSEASON) <- c("R68", "R64", "R32", "S16", "E8", "F4", "2ND", "1ST")
names(dat) <- make.names(names(dat)) # fix invalid column names
edx <- dat |> filter(YEAR < 2023)
final_holdout_set <- dat |> filter(YEAR == 2023)
rm(postseason_levels, dat, cbb_csv)
##########################
# validate pre-processing
edx |>
group_by(YEAR) |>
summarize(n = n())
final_holdout_set |>
group_by(YEAR) |>
summarize(n = n())
# Trends
ylabels <- c("R68", "R64", "R32", "S16", "E8", "F4", "2ND", "1ST")
others <- c("R68", "R64", "R32", "S16")
blevels <- c("< E8", "E8", "F4", "2ND", "1ST")
# ADJOE trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(ADJOE, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Adjusted Offensive Efficiency") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 108, linetype = 2, color = "darkgray") +
coord_cartesian(xlim = c(100, 130), ylim = c(1, 8))
# ADJOE boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(ADJOE, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Adjusted Offensive Efficiency") + ylab("Postseason Levels")
# ADJDE trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(ADJDE, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Adjusted Defensive Efficiency") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 104, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# ADJDE boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(ADJDE, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Adjusted Defensive Efficiency") + ylab("Postseason Levels") +
scale_x_reverse()
# BARTHAG trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(BARTHAG, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Power Rating") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 0.66, linetype = 2, color = "darkgray") +
coord_cartesian(xlim = c(0.5, 1.0), ylim = c(1, 8))
# BARTHAG boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(BARTHAG, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Power Rating") + ylab("Postseason Levels")
# EFG_O trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(EFG_O, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Effective Field Goal Percentage Shot") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 47, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# EFG_O boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(EFG_O, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Effective Field Goal Percentage Shot") + ylab("Postseason Levels")
# EFG_D trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(EFG_D, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Effective Field Goal Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 53, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# EFG_D boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(EFG_D, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Effective Field Goal Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse()
# TOR trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(TOR, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Turnover Percentage Allowed (Turnover Rate)") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 20.4, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# TOR boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(TOR, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Turnover Percentage Allowed (Turnover Rate)") + ylab("Postseason Levels") +
scale_x_reverse()
# TORD trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(TORD, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Turnover Percentage Committed (Steal Rate)") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 13.4, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# TORD boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(TORD, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Turnover Percentage Committed (Steal Rate)") + ylab("Postseason Levels")
# ORB trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(ORB, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Offensive Rebound Rate") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 25.5, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# ORB boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(ORB, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Offensive Rebound Rate") + ylab("Postseason Levels")
# DRB trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(DRB, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Offensive Rebound Rate Allowed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 35.7, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# DRB boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(DRB, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Offensive Rebound Rate Allowed") + ylab("Postseason Levels") +
scale_x_reverse()
# FTR trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(FTR, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Free Throw Rate") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 26, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# FTR boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(FTR, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Free Throw Rate") + ylab("Postseason Levels")
# FTRD trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(FTRD, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Free Throw Rate Allowed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 45, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# FTRD boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(FTRD, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Free Throw Rate Allowed") + ylab("Postseason Levels") +
scale_x_reverse()
# X2P_O trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(X2P_O, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Two-Point Shooting Percentage") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 45.5, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# X2P_O boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(X2P_O, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Two-Point Shooting Percentage") + ylab("Postseason Levels")
# X2P_D trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(X2P_D, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Two-Point Shooting Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 52, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# X2P_D boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(X2P_D, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Two-Point Shooting Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse()
# X3P_O trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(X3P_O, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Three-Point Shooting Percentage") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 29.5, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# X3P_O boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(X3P_O, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Three-Point Shooting Percentage") + ylab("Postseason Levels")
# X3P_D trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(X3P_D, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Three-Point Shooting Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 37.2, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# X3P_D boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(X3P_D, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Three-Point Shooting Percentage Allowed") + ylab("Postseason Levels") +
scale_x_reverse()
# ADJ_T trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(ADJ_T, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Adjusted Tempo") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 59, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# ADJ_T boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(ADJ_T, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Adjusted Tempo") + ylab("Postseason Levels")
# WAB trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(WAB, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("Wins Above Bubble") + ylab("Postseason Levels") +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = -2, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# WAB boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(WAB, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("Wins Above Bubble") + ylab("Postseason Levels")
# SEED trend
edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
ggplot(aes(SEED, POSTSEASON)) +
geom_smooth() +
geom_point(aes(color = cut(POSTSEASON, c(0, 4, 5, 6, 8, Inf))),
alpha = 0.5, show.legend = FALSE, size = 3) +
scale_color_manual(values = c("slategray", "red", "orange", "darkgreen")) +
facet_wrap(~ YEAR) +
xlab("March Madness Seed") + ylab("Postseason Levels") +
scale_x_reverse() +
scale_y_continuous(breaks = 1:8, labels = ylabels) +
geom_vline(xintercept = 11.5, linetype = 2, color = "darkgray") +
coord_cartesian(ylim = c(1, 8))
# SEED boxplot
edx |>
mutate(OTHER = POSTSEASON %in% others) |>
mutate(POSTSEASON = ifelse(OTHER, "< E8", as.character(POSTSEASON))) |>
mutate(POSTSEASON = factor(POSTSEASON, levels = blevels)) |>
ggplot(aes(SEED, POSTSEASON, fill = POSTSEASON)) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) +
scale_fill_manual(values = c("slategray", "red", "orange", "darkgreen", "forestgreen")) +
xlab("March Madness Seed") + ylab("Postseason Levels") +
scale_x_reverse()
rm(ylabels, others, blevels)
################################
# build algorithm with finalists
# training/test
train_set <- edx |>
mutate(POSTSEASON = as.numeric(POSTSEASON))
test_set <- final_holdout_set |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
filter(ADJOE > 108 & ADJDE < 104) # ignore this line to run without filter
# train with 3 predictors
fit_glm <- train(POSTSEASON ~ BARTHAG + WAB + SEED, method = "glm", data = train_set)
fit_gam <- train(POSTSEASON ~ BARTHAG + WAB + SEED, method = "gamLoess", data = train_set)
fit_knn <- train(POSTSEASON ~ BARTHAG + WAB + SEED, method = "knn", data = train_set)
fit_rf <- train(POSTSEASON ~ BARTHAG + WAB + SEED, method = "rf", data = train_set, tuneGrid = data.frame(mtry = 2))
# predict
pred_glm <- predict(fit_glm, test_set)
pred_gam <- predict(fit_gam, test_set)
pred_knn <- predict(fit_knn, test_set)
pred_rf <- predict(fit_rf, test_set)
rm(fit_glm, fit_gam, fit_knn, fit_rf)
# test
test_set <- test_set |>
mutate(glm = pred_glm,
gam = pred_gam,
knn = pred_knn,
rf = pred_rf,
ensemble = (glm + gam + knn + rf) / 4)
elite_glm <- test_set |> arrange(desc(glm)) |> head(8)
elite_gam <- test_set |> arrange(desc(gam)) |> head(8)
elite_knn <- test_set |> arrange(desc(knn)) |> head(8)
elite_rf <- test_set |> arrange(desc(rf)) |> head(8)
elite_ensemble <- test_set |> arrange(desc(ensemble)) |> head(8)
rm(pred_glm, pred_gam, pred_knn, pred_rf)
# score function
score_for_elite <- function(elite_pred) {
first_round <- elite_pred |> filter(POSTSEASON >= 3) |> nrow()
second_round <- elite_pred |> filter(POSTSEASON >= 4) |> nrow()
sweet_sixteen <- elite_pred |> filter(POSTSEASON >= 5) |> nrow()
elite_eight <- elite_pred |> head(4) |> filter(POSTSEASON >= 6) |> nrow()
final_four <- elite_pred |> head(2) |> filter(POSTSEASON >= 7) |> nrow()
champion <- elite_pred[1,] |> filter(POSTSEASON == 8) |> nrow()
first_round + second_round * 2 + sweet_sixteen * 4 + elite_eight * 8 + final_four * 16 + champion * 32
}
# scores
score_for_elite(elite_glm) # with filter - 18, without filter - 18
score_for_elite(elite_gam) # 32, 32
score_for_elite(elite_knn) # 11, 11
score_for_elite(elite_rf) # ~31, ~31
score_for_elite(elite_ensemble) # 24, 24
rm(elite_glm, elite_gam, elite_knn, elite_rf, elite_ensemble)
# how many teams were filtered out?
final_holdout_set |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
filter(ADJOE > 108 & ADJDE < 104) |> # filter
nrow() # 50
final_holdout_set |>
mutate(POSTSEASON = as.numeric(POSTSEASON)) |>
nrow() # 68
rm(train_set, test_set)
#############
# algorithm 3
# index for training/test sets
test_index_for_year <- function(year) {
edx |>
mutate(index = as.numeric(rownames(edx))) |>
filter(YEAR == year) |>
select(index) |>
pull(index)
}
# prediction helper function
pred_for_elite_eight <- function(train_set, test_set, method, test_index) {
train_set <- train_set |>
select(-TEAM, -CONF, -G, -W, -YEAR) |>
mutate(POSTSEASON = as.numeric(POSTSEASON))
fit <- train(POSTSEASON ~ ., method = method, data = train_set)
y_hat <- predict(fit, test_set)
edx[test_index,] |>
mutate(POSTSEASON = as.numeric(POSTSEASON),
y = y_hat) |>
arrange(desc(y)) |>
select(TEAM, POSTSEASON, y) |>
head(8)
}
# cross validation - compute all scores by year (took ~14 min)
scores <- sapply(unique(edx$YEAR), function(year) {
test_index <- test_index_for_year(year)
test_set <- edx[test_index,]
train_set <- edx[-test_index,]
# glm
elite_glm <- pred_for_elite_eight(train_set, test_set, "glm", test_index)
score_glm <- score_for_elite(elite_glm)
# gam
elite_gam <- pred_for_elite_eight(train_set, test_set, "gamLoess", test_index)
score_gam <- score_for_elite(elite_gam)
# knn
elite_knn <- pred_for_elite_eight(train_set, test_set, "knn", test_index)
score_knn <- score_for_elite(elite_knn)
# random forest
elite_rf <- pred_for_elite_eight(train_set, test_set, "rf", test_index)
score_rf <- score_for_elite(elite_rf)
c(year, score_glm, score_gam, score_knn, score_rf)
})
scores
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,] 2016 2015 2018 2019 2017 2014 2013 2021 2022
# [2,] 100 84 91 72 44 53 106 81 44
# [3,] 100 118 97 68 50 45 110 65 92
# [4,] 101 72 51 68 49 44 59 41 61
# [5,] 68 116 83 56 56 41 50 65 55
tscores <- as_tibble(t(scores), .name_repair = NULL)
names(tscores) <- c("season", "glm", "gam", "knn", "rf")
tscores <- tscores |> arrange(season)
tscore_c <- as.integer(round(colMeans(tscores)))
tscores |>
mutate(season = as.character(season)) |>
add_row(season = "avg", glm = tscore_c[2], gam = tscore_c[3], knn = tscore_c[4], rf = tscore_c[5]) |>
mutate(avg = (glm + gam + knn + rf) / 4)
rm(scores, tscores, tscore_c)
###################################
# train=edx, test=final_holdout_set
train_set <- edx |>
select(-TEAM, -CONF, -G, -W, -YEAR) |>
mutate(POSTSEASON = as.numeric(POSTSEASON))
test_set <- final_holdout_set |>
mutate(POSTSEASON = as.numeric(POSTSEASON))
# train with all 18 predictors (took ~ 2min)
fit_glm <- train(POSTSEASON ~ ., method = "glm", data = train_set)
fit_gam <- train(POSTSEASON ~ ., method = "gamLoess", data = train_set)
fit_knn <- train(POSTSEASON ~ ., method = "knn", data = train_set)
fit_rf <- train(POSTSEASON ~ ., method = "rf", data = train_set)
# predict
pred_glm <- predict(fit_glm, test_set)
pred_gam <- predict(fit_gam, test_set)
pred_knn <- predict(fit_knn, test_set)
pred_rf <- predict(fit_rf, test_set)
rm(fit_glm, fit_gam, fit_knn, fit_rf)
# test
test_set <- test_set |>
mutate(glm = pred_glm,
gam = pred_gam,
knn = pred_knn,
rf = pred_rf,
ensemble = (glm + gam + knn + rf) / 4)
elite_glm <- test_set |> arrange(desc(glm)) |> head(8)
elite_gam <- test_set |> arrange(desc(gam)) |> head(8)
elite_knn <- test_set |> arrange(desc(knn)) |> head(8)
elite_rf <- test_set |> arrange(desc(rf)) |> head(8)
elite_ensemble <- test_set |> arrange(desc(ensemble)) |> head(8)
rm(pred_glm, pred_gam, pred_knn, pred_rf)
# scores
score_for_elite(elite_glm) # 87
score_for_elite(elite_gam) # 82
score_for_elite(elite_knn) # 55
score_for_elite(elite_rf) # ~32
score_for_elite(elite_ensemble) # 80
rm(elite_glm, elite_gam, elite_knn, elite_rf, elite_ensemble)
# x scaled
# x_scaled <- edx |>
# select(-TEAM, -CONF, -G, -W, -POSTSEASON, -YEAR) |>
# data.matrix()
# x_mean <- colMeans(x_scaled)
# x_sd <- colSds(x_scaled)
# x_scaled <- sweep(x_scaled, 2, x_mean)
# x_scaled <- sweep(x_scaled, 2, x_sd, FUN="/")
# pca <- prcomp(x_scaled)
# summary(pca)
# y
# y <- as.numeric(edx$POSTSEASON)