-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarch_Madness_2024_Simulation.Rmd
399 lines (347 loc) · 16.3 KB
/
March_Madness_2024_Simulation.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
---
title: "March Madness"
output:
word_document: default
pdf_document: default
html_document: default
editor_options:
chunk_output_type: console
---
**FOR FUTURE REFERENCE**
**For any given year the only changes needed are the new 538 file, dat24 becomes dat~CURRENT_YEAR~ throughout, and regional for loop must be altered to represent the regions including the first four matchups. Everything else is seemless in theory.**
Download the probabilties data from Five Thirty Eight. (I manually formatted CSV to only be Mens Tournament on the back end)
Order data for the functions we create below. (Line 21)
```{r}
library(tidyverse)
library(mosaic)
library(dplyr)
library(ggplot2)
```
```{r}
dat <- read.csv("~/Desktop/madness24.csv", stringsAsFactors = FALSE)
dat24 <- dat
dat24[, 5:10] <- dat[, 5:10] / dat[, 4:9]
head(dat24)
view(dat24)
```
Create a general tournament function (the best team plays the worst team, the second best team plays the second worst team...)
So if you order the data frames as above,this makes picking the teams being simulated very easy (Line 34)
The line of code after that simulates a game by pulling one sample (or team) according to their win probability in a given round.
Collect our winners and output them as mat.small (Line 39)
```{r}
Tournament.Round <- function(mat.big = mat.16, rd = "rd2_win"){
vec.small <- c()
for(i in 1:(nrow(mat.big) / 2)){
ind <- c(i, nrow(mat.big) + 1 - i)
temp <- sample(x = ind, size = 1, prob = mat.big[ind, rd])
vec.small <- c(vec.small, temp)
}
mat.small <- mat.big[vec.small, ]
}
```
Now we work by region in a for loop. This is nessecary to work through the BULLSHIT created by the First Four.
For 2021 the If statments are only functioning on the West and East regions but for future reference you can see how it would be structured for a region with 0, 1 or 2 first four matchups.
Then take the field of 64 and run through each following round based on the original probabilites. (Lines 94-103)
```{r}
Region.Simulation <- function(region = "Midwest", dat = dat24){
mat.17 <- dat[which(dat24$team_region == region),]
if(region == "West"){
first.four <- which(mat.17$team_seed == "16a" | mat.17$team_seed == "16b")
mat.16 <- rbind(mat.17[-first.four, ],
mat.17[sample(x = first.four, size = 1,
prob = mat.17[first.four, "rd1_win"]), ])
mat.16[16, "team_seed"] <- "16"
mat.16[, "team_seed"] <- as.numeric(mat.16[, "team_seed"])
mat.16 <- mat.16[order(mat.16$team_seed), ]
}
if(region == "East"){
mat.16 <- mat.17
mat.16[, "team_seed"] <- as.numeric(mat.16[, "team_seed"])
mat.16 <- mat.16[order(mat.16$team_seed), ]
}
if(region == "Midwest"){
first.four <- which(mat.17$team_seed == "16a" | mat.17$team_seed == "16b")
mat.17 <- rbind(mat.17[-first.four, ],
mat.17[sample(x = first.four, size = 1,
prob = mat.17[first.four, "rd1_win"]), ])
mat.17[17, "team_seed"] <- "16"
first.four <- which(mat.17$team_seed == "10a" | mat.17$team_seed == "10b")
mat.16 <- rbind(mat.17[-first.four, ],
mat.17[sample(x = first.four, size = 1,
prob = mat.17[first.four, "rd1_win"]), ])
mat.16[16, "team_seed"] <- "10"
mat.16[, "team_seed"] <- as.numeric(mat.16[, "team_seed"])
mat.16 <- mat.16[order(mat.16$team_seed), ]
}
if(region == "South"){
first.four <- which(mat.17$team_seed == "10a" | mat.17$team_seed == "10b")
mat.16 <- rbind(mat.17[-first.four, ],
mat.17[sample(x = first.four, size = 1,
prob = mat.17[first.four, "rd1_win"]), ])
mat.16[16, "team_seed"] <- "10"
mat.16[, "team_seed"] <- as.numeric(mat.16[, "team_seed"])
mat.16 <- mat.16[order(mat.16$team_seed), ]
}
# Round of 16
mat.8 <- Tournament.Round(mat.16, rd = "rd2_win")
mat.4 <- Tournament.Round(mat.8, rd = "rd3_win")
mat.2 <- Tournament.Round(mat.4, rd = "rd4_win")
mat.1 <- Tournament.Round(mat.2, rd = "rd5_win")
list(First_Four = mat.16[c(10, 16), "team_name"],
First_Round = mat.8[, "team_name"],
Second_Round = mat.4[, "team_name"],
Regional_Semis = mat.2[, "team_name"],
Regional_Final = mat.1[, c("team_name", "rd6_win", "rd7_win")])
}
```
Take the previous functions and use outputs to simulate the Final Four and the championship.
```{r}
March.Madness.Simulation <- function(dat = dat24){
midwest <- Region.Simulation(region = "Midwest", dat = dat24)
east <- Region.Simulation(region = "East", dat = dat24)
west <- Region.Simulation(region = "West", dat = dat24)
south <- Region.Simulation(region = "South", dat = dat24)
reg1 <- sample(x = c("east", "west"), size = 1,
prob = c(east$Regional_Final$rd6_win,
west$Regional_Final$rd6_win))
National_Semi1 <- get(reg1)$Regional_Final
reg2 <- sample(x = c("midwest", "south"), size = 1,
prob = c(midwest$Regional_Final$rd6_win,
south$Regional_Final$rd6_win))
National_Semi2 <- get(reg2)$Regional_Final
National_Champ <- sample(x = c(National_Semi1$team_name,
National_Semi2$team_name),
size = 1,
prob = c(National_Semi1$rd7_win,
National_Semi2$rd7_win))
midwest$Regional_Final <- midwest$Regional_Final$team_name
east$Regional_Final <- east$Regional_Final$team_name
west$Regional_Final <- west$Regional_Final$team_name
south$Regional_Final <- south$Regional_Final$team_name
list(Midwest = midwest, East = east, West = west, South = south,
National_Semi = c(National_Semi1$team_name, National_Semi2$team_name),
National_Champ = National_Champ)
}
```
Now the fun!!
Create an empty vector and empty matrix of any replication size you want.(Lines 144-146)
For Loop runs the simulation with 1:ANY AMOUNT YOU DESIRE of replicates. (Lines 149-152)
From matrix to data frame. (Line 153)
```{r}
nat.champ.vec <- unlist(March.Madness.Simulation(dat = dat24))
nat.champ.mat <- matrix("", nrow = 10000, ncol = length(nat.champ.vec))
colnames(nat.champ.mat) <- names(nat.champ.vec)
for(i in 1:10000){
set.seed(i)
nat.champ.mat[i, ] <- unlist(March.Madness.Simulation(dat = dat24))
}
nat.champ.mat <- as.data.frame(nat.champ.mat)
```
Sort the data frame from most National Championships to least. (Line 160)
Most Common from Simulation (Line 163)
```{r}
tbl <- sort(table(nat.champ.mat$National_Champ), decreasing = TRUE)
tbl
t(t(apply(nat.champ.mat, 2, function(x){names(sort(table(x), decreasing = TRUE))[1]})))
```
Now visualize for more fun and make some good picks.
National Champions
```{r}
barplot(tbl/10000, col="cyan3", las=2, ylab="Probability", main="Win The Ship")
```
Final four probability by region
```{r}
south.tbl <- sort(table(nat.champ.mat$South.Regional_Final), decreasing = TRUE)
west.tbl <- sort(table(nat.champ.mat$West.Regional_Final), decreasing = TRUE)
east.tbl <- sort(table(nat.champ.mat$East.Regional_Final), decreasing = TRUE)
midwest.tbl <- sort(table(nat.champ.mat$Midwest.Regional_Final), decreasing = TRUE)
par(mfrow=c(2,2))
par(mar=c(8,4,4,4))
barplot(south.tbl/10000, col="blue", las=2, ylab="Probability", main="South Region")
barplot(west.tbl/10000, col="green", las=2, ylab="Probability", main="West Region")
barplot(midwest.tbl/10000, col="red", las=2, ylab="Probability", main="Midwest Region")
barplot(east.tbl/10000, col="purple", las=2, ylab="Probability", main="East Region")
```
2 vs 15
```{r}
iowast.sdak.tbl <- sort(table(nat.champ.mat$East.First_Round2), decreasing = TRUE)
zona.lbsu.tbl <- sort(table(nat.champ.mat$West.First_Round2 ), decreasing = TRUE)
marq.wky.tbl <- sort(table(nat.champ.mat$South.First_Round2 ), decreasing = TRUE)
tenn.spu.tbl <- sort(table(nat.champ.mat$Midwest.First_Round2 ), decreasing = TRUE)
barplot(iowast.sdak.tbl/10000, col=c("firebrick", "blue"), ylab="Probability")
barplot(zona.lbsu.tbl/10000, col=c("red","gold"), ylab="Probability")
barplot(marq.wky.tbl/10000, col=c("darkblue", "red"), ylab="Probability")
barplot(tenn.spu.tbl/10000, col=c("orange","deepskyblue4"), ylab="Probability")
```
3 vs 14
```{r}
ill.more.tbl <- sort(table(nat.champ.mat$East.First_Round3), decreasing = TRUE)
bay.colg.tbl <- sort(table(nat.champ.mat$West.First_Round3 ), decreasing = TRUE)
uk.oak.tbl <- sort(table(nat.champ.mat$South.First_Round3 ), decreasing = TRUE)
crei.akro.tbl <- sort(table(nat.champ.mat$Midwest.First_Round3 ), decreasing = TRUE)
barplot(ill.more.tbl/10000, col=c("chocolate1","royalblue1"), ylab="Probability")
barplot(bay.colg.tbl/10000, col=c("forestgreen","firebrick2"), ylab="Probability")
barplot(uk.oak.tbl/10000, col=c("blue","gold3"), ylab="Probability")
barplot(crei.akro.tbl/10000, col=c("blue1","darkblue"), ylab="Probability")
```
4 vs 13
```{r}
aub.yale.tbl <- sort(table(nat.champ.mat$East.First_Round4), decreasing = TRUE)
bama.char.tbl <- sort(table(nat.champ.mat$West.First_Round4 ), decreasing = TRUE)
duke.vrm.tbl <- sort(table(nat.champ.mat$South.First_Round4), decreasing = TRUE)
ku.sam.tbl <- sort(table(nat.champ.mat$Midwest.First_Round4), decreasing = TRUE)
barplot(aub.yale.tbl/10000, col=c("tomato2","royalblue4"), ylab="Probability")
barplot(bama.char.tbl/10000, col=c("red4","indianred4"), ylab="Probability")
barplot(duke.vrm.tbl/10000, col=c("blue","gold"), ylab="Probability")
barplot(ku.sam.tbl/10000, col=c("blue","red"), ylab="Probability")
```
5 vs 12
```{r}
sdsu.uab.tbl <- sort(table(nat.champ.mat$East.First_Round5 ), decreasing = TRUE)
mary.gcu.tbl <- sort(table(nat.champ.mat$West.First_Round5 ), decreasing = TRUE)
wis.jmu.tbl <- sort(table(nat.champ.mat$South.First_Round5 ), decreasing = TRUE)
zaga.mcns.tbl <- sort(table(nat.champ.mat$Midwest.First_Round5 ), decreasing = TRUE)
barplot(sdsu.uab.tbl/10000, col=c("darkred","darkgreen"), ylab="Probability")
barplot(mary.gcu.tbl/10000, col=c("red3","purple"), ylab="Probability")
barplot(wis.jmu.tbl/10000, col=c("red","darkblue"), ylab="Probability")
barplot(zaga.mcns.tbl/10000, col=c("grey30","gold"), ylab="Probability")
```
6 vs 11
```{r}
byu.dnq.tbl <- sort(table(nat.champ.mat$East.First_Round6 ), decreasing = TRUE)
clem.nmu.tbl <- sort(table(nat.champ.mat$West.First_Round6 ), decreasing = TRUE)
tt.ncst.tbl <- sort(table(nat.champ.mat$South.First_Round6 ), decreasing = TRUE)
ore.scu.tbl <- sort(table(nat.champ.mat$Midwest.First_Round6 ), decreasing = TRUE)
barplot(byu.dnq.tbl/10000, col=c("blue","darkblue"), ylab="Probability")
barplot(clem.nmu.tbl/10000, col=c("orange2","darkred"), ylab="Probability")
barplot(tt.ncst.tbl/10000, col=c("red","red3"), ylab="Probability")
barplot(ore.scu.tbl/10000, col=c("darkred","green"), ylab="Probability")
```
7 vs 10
```{r}
wazzu.drk.tbl <- sort(table(nat.champ.mat$East.First_Round7 ), decreasing = TRUE)
day.nev.tbl <- sort(table(nat.champ.mat$West.First_Round7 ), decreasing = TRUE)
fla.colo.bsu.tbl <- sort(table(nat.champ.mat$South.First_Round7 ), decreasing = TRUE)
tex.csu.uva.tbl <- sort(table(nat.champ.mat$Midwest.First_Round7 ), decreasing = TRUE)
barplot(wazzu.drk.tbl/10000, col=c("blue","grey"), ylab="Probability")
barplot(day.nev.tbl/10000, col=c("darkblue","red"), ylab="Probability")
barplot(fla.colo.bsu.tbl/10000, col=c("blue","gold","orange"), ylab="Probability")
barplot(tex.csu.uva.tbl/10000, col=c("orange3","darkblue", "darkgreen"), ylab="Probability")
```
8 vs 9
```{r}
fau.nw.tbl <- sort(table(nat.champ.mat$East.First_Round8), decreasing = TRUE)
misst.msu.tbl <- sort(table(nat.champ.mat$West.First_Round8 ), decreasing = TRUE)
neb.tam.tbl <- sort(table(nat.champ.mat$South.First_Round8 ), decreasing = TRUE)
usu.tcu.tbl <- sort(table(nat.champ.mat$Midwest.First_Round8 ), decreasing = TRUE)
barplot(fau.nw.tbl/10000, col=c("royalblue","purple3"), ylab="Probability")
barplot(misst.msu.tbl/10000, col=c("green4","red4"), ylab="Probability")
barplot(neb.tam.tbl/10000, col=c("darkred","red"), ylab="Probability")
barplot(usu.tcu.tbl/10000, col=c("purple","royalblue3"), ylab="Probability")
```
25 Bracket Simulations
```{r}
sim1 <- March.Madness.Simulation(dat=dat24)
sim2 <- March.Madness.Simulation(dat=dat24)
sim3 <- March.Madness.Simulation(dat=dat24)
sim4 <- March.Madness.Simulation(dat=dat24)
sim5 <- March.Madness.Simulation(dat=dat24)
sim6 <- March.Madness.Simulation(dat=dat24)
sim7 <- March.Madness.Simulation(dat=dat24)
sim8 <- March.Madness.Simulation(dat=dat24)
sim9 <- March.Madness.Simulation(dat=dat24)
sim10 <- March.Madness.Simulation(dat=dat24)
sim11 <- March.Madness.Simulation(dat=dat24)
sim12 <- March.Madness.Simulation(dat=dat24)
sim13 <- March.Madness.Simulation(dat=dat24)
sim14 <- March.Madness.Simulation(dat=dat24)
sim15 <- March.Madness.Simulation(dat=dat24)
sim16 <- March.Madness.Simulation(dat=dat24)
sim17 <- March.Madness.Simulation(dat=dat24)
sim18 <- March.Madness.Simulation(dat=dat24)
sim19 <- March.Madness.Simulation(dat=dat24)
sim20 <- March.Madness.Simulation(dat=dat24)
sim21 <- March.Madness.Simulation(dat=dat24)
sim22 <- March.Madness.Simulation(dat=dat24)
sim23 <- March.Madness.Simulation(dat=dat24)
sim24 <- March.Madness.Simulation(dat=dat24)
sim25 <- March.Madness.Simulation(dat=dat24)
```
```{r}
sim26 <- March.Madness.Simulation(dat=dat24)
sim27 <- March.Madness.Simulation(dat=dat24)
sim28 <- March.Madness.Simulation(dat=dat24)
sim29 <- March.Madness.Simulation(dat=dat24)
sim30 <- March.Madness.Simulation(dat=dat24)
sim31 <- March.Madness.Simulation(dat=dat24)
sim32 <- March.Madness.Simulation(dat=dat24)
sim33 <- March.Madness.Simulation(dat=dat24)
sim34 <- March.Madness.Simulation(dat=dat24)
sim35 <- March.Madness.Simulation(dat=dat24)
sim36 <- March.Madness.Simulation(dat=dat24)
sim37 <- March.Madness.Simulation(dat=dat24)
sim38 <- March.Madness.Simulation(dat=dat24)
sim39 <- March.Madness.Simulation(dat=dat24)
sim40 <- March.Madness.Simulation(dat=dat24)
sim41 <- March.Madness.Simulation(dat=dat24)
sim42 <- March.Madness.Simulation(dat=dat24)
sim43 <- March.Madness.Simulation(dat=dat24)
sim44 <- March.Madness.Simulation(dat=dat24)
sim45 <- March.Madness.Simulation(dat=dat24)
sim46 <- March.Madness.Simulation(dat=dat24)
sim47 <- March.Madness.Simulation(dat=dat24)
sim48 <- March.Madness.Simulation(dat=dat24)
sim49 <- March.Madness.Simulation(dat=dat24)
sim50<- March.Madness.Simulation(dat=dat24)
sim51 <- March.Madness.Simulation(dat=dat24)
sim52 <- March.Madness.Simulation(dat=dat24)
sim53 <- March.Madness.Simulation(dat=dat24)
sim54 <- March.Madness.Simulation(dat=dat24)
sim55 <- March.Madness.Simulation(dat=dat24)
sim56 <- March.Madness.Simulation(dat=dat24)
sim57 <- March.Madness.Simulation(dat=dat24)
sim58 <- March.Madness.Simulation(dat=dat24)
sim59 <- March.Madness.Simulation(dat=dat24)
sim60 <- March.Madness.Simulation(dat=dat24)
sim61 <- March.Madness.Simulation(dat=dat24)
sim62 <- March.Madness.Simulation(dat=dat24)
sim63 <- March.Madness.Simulation(dat=dat24)
sim64 <- March.Madness.Simulation(dat=dat24)
sim65 <- March.Madness.Simulation(dat=dat24)
sim66 <- March.Madness.Simulation(dat=dat24)
sim67 <- March.Madness.Simulation(dat=dat24)
sim68 <- March.Madness.Simulation(dat=dat24)
sim69 <- March.Madness.Simulation(dat=dat24)
sim70 <- March.Madness.Simulation(dat=dat24)
sim71 <- March.Madness.Simulation(dat=dat24)
sim72 <- March.Madness.Simulation(dat=dat24)
sim73 <- March.Madness.Simulation(dat=dat24)
sim74 <- March.Madness.Simulation(dat=dat24)
sim75 <- March.Madness.Simulation(dat=dat24)
sim76 <- March.Madness.Simulation(dat=dat24)
sim77 <- March.Madness.Simulation(dat=dat24)
sim78 <- March.Madness.Simulation(dat=dat24)
sim79 <- March.Madness.Simulation(dat=dat24)
sim80 <- March.Madness.Simulation(dat=dat24)
sim81 <- March.Madness.Simulation(dat=dat24)
sim82 <- March.Madness.Simulation(dat=dat24)
sim83 <- March.Madness.Simulation(dat=dat24)
sim84 <- March.Madness.Simulation(dat=dat24)
sim85 <- March.Madness.Simulation(dat=dat24)
sim86 <- March.Madness.Simulation(dat=dat24)
```
```{r}
sim87 <- March.Madness.Simulation(dat=dat24)
sim88 <- March.Madness.Simulation(dat=dat24)
sim89 <- March.Madness.Simulation(dat=dat24)
sim90 <- March.Madness.Simulation(dat=dat24)
sim91 <- March.Madness.Simulation(dat=dat24)
sim92 <- March.Madness.Simulation(dat=dat24)
sim93 <- March.Madness.Simulation(dat=dat24)
sim94 <- March.Madness.Simulation(dat=dat24)
sim95 <- March.Madness.Simulation(dat=dat24)
sim96 <- March.Madness.Simulation(dat=dat24)
sim97 <- March.Madness.Simulation(dat=dat24)
sim98 <- March.Madness.Simulation(dat=dat24)
sim99 <- March.Madness.Simulation(dat=dat24)
```