-
Notifications
You must be signed in to change notification settings - Fork 3
/
2-simulate-brackets.R
250 lines (195 loc) · 13.9 KB
/
2-simulate-brackets.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
year<-2023
numBrackets<-1000
load("data/game-data.RData");
load(paste0(year,"/TourneySims_2000sims.Rda")) #tourneysims file
name<-paste0(year,"/BracketResults_FullTournament_",numBrackets ,"sims.Rda") #name to save this file as
###organize ownership data, check names********************
whoPicked<-whoPicked[whoPicked$Season==year, !colnames(whoPicked)%in% "Season"]
#comment out after getting kaggle data:
#can delete this once kaggle data released
#TourneySeeds<-TourneySeeds_temp
# TourneyRounds2<-TourneyRounds[TourneyRounds$Season==2021,]
# TourneyRounds2$Season<-2022
# TourneyRounds<-rbind.fill(TourneyRounds, TourneyRounds2)
# table(TourneyRounds$Season)
# TourneySlots2<-TourneySlots[TourneySlots$Season==2021,]
# TourneySlots2$Season<-2022
# TourneySlots<-rbind.fill(TourneySlots, TourneySlots2)
# table(TourneySlots$Season)
# setdiff(TourneySeeds$Seed[TourneySeeds$Season==2022], TourneySlots$StrongSeed[TourneySlots$Season==2022])
TourneySlots[, c("Slot", "StrongSeed", "WeakSeed")]<-sapply(TourneySlots[, c("Slot", "StrongSeed", "WeakSeed")], as.character)
#prepare dataframe to be used for ownership-create-breackets
bracket.data<-TourneyRounds[grepl("R", TourneyRounds$Slot) & TourneyRounds$Season==year,]
bracket.data$Team<-TourneySeeds$Team[TourneySeeds$Season==year][match(bracket.data$Seed,TourneySeeds$Seed[TourneySeeds$Season==year] )]
bracket.data$Team_Full<-Teams$Team_Full[match(bracket.data$Team, Teams$TeamID)]
bracket.data$Round<-substring(bracket.data$Slot, 1 ,2)
#handle play-in games, if playINTBD=T then combine play-in games into one
bracket.data<-bracket.data[bracket.data$Team%in% c(tourneySims$Team[grepl("R1", tourneySims$Slot) ],
tourneySims$Loser[grepl("R1", tourneySims$Slot) ]), ]
if( year==2018){
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Arizona State", "Syracuse")]<-"Asu/sy"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("St Bonaventure", "Ucla")]<-"Bon/la"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Long Island", "Radford")]<-"Liu/rad"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("North Carolina Central", "Texas Southern")]<-"Ncc/ts"
whoPicked$Team[whoPicked$Team%in% c("Arizona State", "Syracuse")]<-"Asu/sy"
whoPicked$Team[whoPicked$Team%in% c("St Bonaventure", "Ucla")]<-"Bon/la"
whoPicked$Team[whoPicked$Team%in% c("Long Island", "Radford")]<-"Liu/rad"
whoPicked$Team[whoPicked$Team%in% c("North Carolina Central", "Texas Southern")]<-"Ncc/ts"
} else if (year==2019){
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Arizona State", "St Johns")]<-"Asu/sju"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Belmont", "Temple")]<-"Bel/tem"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Fairleigh Dickinson", "Prairie View A&m")]<-"Fdu/pv"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("North Dakota State", "North Carolina Central")]<-"Nds/ncc"
whoPicked$Team[whoPicked$Team%in% c("Arizona State", "St Johns")]<-"Asu/sju"
whoPicked$Team[whoPicked$Team%in% c("Belmont", "Temple")]<-"Bel/tem"
whoPicked$Team[whoPicked$Team%in% c("Fairleigh Dickinson", "Prairie View A&m")]<-"Fdu/pv"
whoPicked$Team[whoPicked$Team%in% c("North Dakota State", "North Carolina Central")]<-"Nds/ncc"
}else if (year==2021){
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Michigan State", "Ucla")]<-"Msu/ucla"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Norfolk State", "Appalachian State")]<-"Norf/app"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Wichita State", "Drake")]<-"Wich/drke"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Mount St Marys", "Texas Southern")]<-"Msm/txso"
whoPicked$Team[whoPicked$Team%in% c("Michigan State", "Ucla")]<-"Msu/ucla"
whoPicked$Team[whoPicked$Team%in% c("Norfolk State", "Appalachian State")]<-"Norf/app"
whoPicked$Team[whoPicked$Team%in% c("Wichita State", "Drake")]<-"Wich/drke"
whoPicked$Team[whoPicked$Team%in% c("Mount St Marys", "Texas Southern")]<-"Msm/txso"
}else if (year==2022){
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Texas Southern", "Texas A&m Corpus Christi")]<-"Txso/tcc"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Wright State", "Bryant")]<-"Wrst/bry"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Rutgers", "Notre Dame")]<-"Rutg/nd"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Wyoming", "Indiana")]<-"Wyo/iu"
whoPicked$Team[whoPicked$Team%in% c("Texas Southern", "Texas A&m Corpus Christi")]<-"Txso/tcc"
whoPicked$Team[whoPicked$Team%in% c("Wright State", "Bryant")]<-"Wrst/bry"
whoPicked$Team[whoPicked$Team%in% c("Rutgers", "Notre Dame")]<-"Rutg/nd"
whoPicked$Team[whoPicked$Team%in% c("Wyoming", "Indiana")]<-"Wyo/iu"
}else if (year==2023){
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Southeast Missouri State", "Texas A&m Corpus Christi")]<-"Amcc/smo"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Texas Southern", "Fairleigh Dickinson")]<-"Txso/fdu"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Arizona State", "Nevada")]<-"Asu/nev"
bracket.data$Team_Full[bracket.data$Team_Full%in% c("Mississippi State", "Pittsburgh")]<-"Msst/pitt"
whoPicked$Team[whoPicked$Team%in% c("Southeast Missouri State", "Texas A&m Corpus Christi")]<-"Amcc/smo"
whoPicked$Team[whoPicked$Team%in% c("Texas Southern", "Fairleigh Dickinson")]<-"Txso/fdu"
whoPicked$Team[whoPicked$Team%in% c("Arizona State", "Nevada")]<-"Asu/nev"
whoPicked$Team[whoPicked$Team%in% c("Mississippi State", "Pittsburgh")]<-"Msst/pitt"
}
setdiff( whoPicked$Team, bracket.data$Team_Full)
bracket.data<-merge(bracket.data, whoPicked, by.x=c( "Team_Full","Round"), by.y=c("Team", "Round"), all.x=T)
####SIMULATE BRACKETS#####
whoPicked<-merge(whoPicked, unique(bracket.data[, c("Team_Full", "Seed")]), by.x=c("Team"), by.y=c("Team_Full"), all.x=T)
whoPicked[is.na(whoPicked$Seed),]
whoPicked<-whoPicked[!duplicated(whoPicked[, c("Team", "Round")]), ] ###play-in games get duplicated
whoPicked[whoPicked$Round=="R6", ][order(whoPicked$Ownership[whoPicked$Round=="R6"], decreasing = T), ]
setdiff( whoPicked$Team, bracket.data$Team_Full)
setdiff( bracket.data$Team_Full, whoPicked$Team)
bracket.data<-bracket.data[bracket.data$Seed%in% whoPicked$Seed, ]
setdiff( bracket.data$Team_Full, whoPicked$Team)
####CREATE BRACKETS######
r2_df<-data.frame(Slot=c("R2W1", "R2W4", "R2W3","R2W2", "R2X1", "R2X4",
"R2X3", "R2X2", "R2Y1", "R2Y4", "R2Y3", "R2Y2","R2Z1", "R2Z4", "R2Z3", "R2Z2" ), TeamPicked=NA)
r1_df<-data.frame(Slot=c(paste0("R1W", c(1, 8, 4,5, 3, 6, 2, 7)), paste0("R1X", c(1, 8, 4,5, 3, 6, 2, 7)),
paste0("R1Y", c(1, 8, 4,5, 3, 6, 2, 7)), paste0("R1Z",c(1, 8, 4,5, 3, 6, 2, 7))), TeamPicked=NA)
table(bracket.data$Team_Full, bracket.data$Round)
# unique(bracket.data[bracket.data$Team_Full=="Vermont",1:4])
# unique(whoPicked[whoPicked$Team=="Vermont",1:4])
brackets<-list();length(brackets)<-numBrackets
for(j in 1:length(brackets)){
#the bracket simulation procedure starts with the finals,
#then goes to earlier rounds, rejecting ecah 2-team sample if the winner of the next round is not in the 2 teams sampled
#i cant figure out a better way to do this and the brackets get pretty close except for slightly under-sampling the underdog teams
#sample 1 champion using pick-percentages as probabilities
R6<-sample(whoPicked$Team[whoPicked$Round=="R6"] , 1, prob=c(whoPicked$Ownership[ whoPicked$Round=="R6"]))
#sample 2-finalists using pick percentages, reject if champion not in selected pair
R5_1<-c()
R5_2<-c()
while(!R6%in%c(R5_1, R5_2)) {
R5_1<-sample(whoPicked$Team[grepl("W|X", whoPicked$Seed) & whoPicked$Round=="R5"], 1,
prob=whoPicked$Ownership[grepl("W|X", whoPicked$Seed) & whoPicked$Round=="R5"])
R5_2<-sample(whoPicked$Team[grepl("Y|Z", whoPicked$Seed) & whoPicked$Round=="R5"], 1,
prob=whoPicked$Ownership[grepl("Y|Z", whoPicked$Seed) & whoPicked$Round=="R5"])
}
#sample final 4, make sure 2 finalists are in final 4
R4_1<-c()
R4_2<-c()
R4_3<-c()
R4_4<-c()
while(!R5_1%in%c(R4_1, R4_2)) {
R4_1<-sample(whoPicked$Team[grepl("W", whoPicked$Seed) & whoPicked$Round=="R4"], 1,
prob=whoPicked$Ownership[grepl("W", whoPicked$Seed) & whoPicked$Round=="R4"])
R4_2<-sample(whoPicked$Team[grepl("X", whoPicked$Seed) & whoPicked$Round=="R4"], 1,
prob=whoPicked$Ownership[grepl("X", whoPicked$Seed) & whoPicked$Round=="R4"])
}
while(!R5_2%in%c(R4_3, R4_4)) {
R4_3<-sample(whoPicked$Team[grepl("Y", whoPicked$Seed) & whoPicked$Round=="R4"], 1,
prob=whoPicked$Ownership[grepl("Y", whoPicked$Seed) & whoPicked$Round=="R4"])
R4_4<-sample(whoPicked$Team[grepl("Z", whoPicked$Seed) & whoPicked$Round=="R4"], 1,
prob=whoPicked$Ownership[grepl("Z", whoPicked$Seed) & whoPicked$Round=="R4"])
}
#sample elite 8, make sure final 4 is in the selection
R3_1<-c()
R3_2<-c()
R3_3<-c()
R3_4<-c()
R3_5<-c()
R3_6<-c()
R3_7<-c()
R3_8<-c()
while(!R4_1%in%c(R3_1, R3_2)) {
R3_1<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3W1"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3W1"]) & whoPicked$Round=="R3"])
R3_2<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3W2"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3W2"]) & whoPicked$Round=="R3"])
}
while(!R4_2%in%c(R3_3, R3_4)) {
R3_3<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3X1"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3X1"]) & whoPicked$Round=="R3"])
R3_4<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3X2"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3X2"]) & whoPicked$Round=="R3"])
}
while(!R4_3%in%c(R3_5, R3_6)) {
R3_5<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Y1"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Y1"]) & whoPicked$Round=="R3"])
R3_6<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Y2"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Y2"]) & whoPicked$Round=="R3"])
}
while(!R4_4%in%c(R3_7, R3_8)) {
R3_7<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Z1"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Z1"]) & whoPicked$Round=="R3"])
R3_8<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Z2"]) & whoPicked$Round=="R3"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot=="R3Z2"]) & whoPicked$Round=="R3"])
}
#sample sweet 16
r2_df$TeamPicked<-NA
r2_df$NextRoundTeam<-rep(c(R3_1, R3_2, R3_3, R3_4, R3_5, R3_6, R3_7, R3_8), each=2)
for(i in seq(1, nrow(r2_df), by=2)) {
while(!r2_df$NextRoundTeam[i] %in% r2_df$TeamPicked[i:(i+1)]){
r2_df$TeamPicked[i]<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot==r2_df$Slot[i]]) & whoPicked$Round=="R2"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in%unique(bracket.data$Seed[bracket.data$Slot==r2_df$Slot[i]]) & whoPicked$Round=="R2"])
r2_df$TeamPicked[i+1]<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot==r2_df$Slot[i+1]]) & whoPicked$Round=="R2"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in%unique(bracket.data$Seed[bracket.data$Slot==r2_df$Slot[i+1]]) & whoPicked$Round=="R2"])
}
}
#sample round of 32
r1_df$TeamPicked<-NA
r1_df$NextRoundTeam<-rep(r2_df$TeamPicked, each=2)
for(i in seq(1, nrow(r1_df), by=2)) {
while(!r1_df$NextRoundTeam[i] %in% r1_df$TeamPicked[i:(i+1)]){
r1_df$TeamPicked[i]<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot==r1_df$Slot[i]]) & whoPicked$Round=="R1"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in%unique(bracket.data$Seed[bracket.data$Slot==r1_df$Slot[i]]) & whoPicked$Round=="R1"])
r1_df$TeamPicked[i+1]<-sample(whoPicked$Team[whoPicked$Seed%in% unique(bracket.data$Seed[bracket.data$Slot==r1_df$Slot[i+1]]) & whoPicked$Round=="R1"], 1,
prob=whoPicked$Ownership[whoPicked$Seed%in%unique(bracket.data$Seed[bracket.data$Slot==r1_df$Slot[i+1]]) & whoPicked$Round=="R1"])
}
}
#save full sampled bracket
brackets[[j]]<-c(r1_df$TeamPicked,r2_df$TeamPicked,
R3_1, R3_2, R3_3, R3_4, R3_5, R3_6, R3_7, R3_8,R4_1, R4_2, R4_3, R4_4, R5_1, R5_2, R6)
print(j)
}
#combine and save
brackets<-data.frame(t(sapply(brackets, `[`)))
r2_df<-data.frame(Slot=c("R2W1", "R2W4", "R2W3","R2W2", "R2X1", "R2X4",
"R2X3", "R2X2", "R2Y1", "R2Y4", "R2Y3", "R2Y2","R2Z1", "R2Z4", "R2Z3", "R2Z2" ), TeamPicked=NA)
r1_df<-data.frame(Slot=c(paste0("R1W", c(1, 8, 4,5, 3, 6, 2, 7)), paste0("R1X", c(1, 8, 4,5, 3, 6, 2, 7)),
paste0("R1Y", c(1, 8, 4,5, 3, 6, 2, 7)), paste0("R1Z",c(1, 8, 4,5, 3, 6, 2, 7))), TeamPicked=NA)
colnames(brackets)<- c(r1_df$Slot, r2_df$Slot,
"R3W1", "R3W2", "R3X1", "R3X2", "R3Y1", "R3Y2" , "R3Z1", "R3Z2", "R4W1", "R4X1", "R4Y1", "R4Z1", "R5WX", "R5YZ", "R6CH")
save(brackets, file=name)