-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimulate_Function.R
455 lines (329 loc) · 13.7 KB
/
Simulate_Function.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
library (rvest)
library (dplyr)
#Scrape Team Data
source("NCAA_Data_Scraper.R")
#Scrape Upcoming Game Data
#Insert URL ------
URL <- 'https://www.sports-reference.com/cbb/'
#Read the HTML code from the website
webpage <- read_html(URL)
#Home Team Names
home_teams_html <- html_nodes(webpage, '#scores thead+ tbody tr:nth-child(1) a')
home_teams <- html_text(home_teams_html)
noquote(home_teams)
home_teams <- as.list(head(home_teams, -1))
#Away Team Names
away_teams_html <- html_nodes(webpage, '.expanded thead+ tbody tr+ tr a')
away_teams <- html_text(away_teams_html)
noquote(away_teams)
away_teams <- as.list(head(away_teams, -1))
#Compile Stats for Home Teams
game_count <- length(home_teams)
#Append home and away team stats into a DF Function
home_stats <- vector("double", ncol(ncaa_team_stats_2020))
for (i in seq_along(home_teams)) {
home_temp <- ncaa_team_stats_2020 %>%
filter(team_name == home_teams[[i]])
home_stats <- rbind(home_temp, home_stats)
}
away_stats <- vector("double", ncol(ncaa_team_stats_2020))
for (i in seq_along(away_teams)) {
away_temp <- ncaa_team_stats_2020 %>%
filter(team_name == away_teams[[i]])
away_stats <- rbind(away_temp, away_stats)
}
#Basketball Simulation function
simulate_bg <- function(home_team, away_team, sim_count = 500, adjust_sos = FALSE, adjust_hfa = TRUE) {
#Set Home Team
home_team <- home_stats
#Set Away Team
away_team <- away_stats
# #Set Adjustment
# home_adj <- switch(as.character(home_team$sos_bins),
# "Very Weak" = -.075,
# "Weak" = -.05,
# "Moderately Weak" = -.025,
# "Average" = 0,
# "Moderately Strong" = .025,
# "Strong" = .05,
# "Very Strong" = .075)
#
# away_adj <- switch(as.character(away_team$sos_bins),
# "Very Weak" = -.075,
# "Weak" = -.05,
# "Moderately Weak" = -.025,
# "Average" = 0,
# "Moderately Strong" = .025,
# "Strong" = .05,
# "Very Strong" = .075)
#Establish Variables
home_team_total_score <- 0
away_team_total_score <- 0
home_team_ft_att <- 0
home_team_ft_points <- 0
away_team_ft_att <- 0
away_team_ft_points <- 0
spread <- 0
#Establish Home Field Advantage
home_win_pct <- home_team$home_wins/(home_team$home_wins + home_team$home_losses)
if (adjust_hfa == TRUE) {
home_adv <- home_win_pct/20
} else {
home_adv <- 0
}
if (adjust_sos == TRUE) {
#Create Adjustment Functions
home_stat_adjust <- function(stat) {
new_home_stat <- stat + (stat*home_adj)
return(new_home_stat)
}
away_stat_adjust <- function(stat) {
new_away_stat <- stat + (stat*away_adj)
return(new_away_stat)
}
#Adjust Home
home_team$fg_pct <- home_stat_adjust(home_team$fg_pct)
home_team$three_pct <- home_stat_adjust(home_team$three_pct)
home_team$ts_pct <- home_stat_adjust(home_team$ts_pct)
home_team$trb_pct <- home_stat_adjust(home_team$trb_pct)
home_team$steal_pct <- home_stat_adjust(home_team$steal_pct)
home_team$block_pct <- home_stat_adjust(home_team$block_pct)
home_team$orb_pct <- home_stat_adjust(home_team$orb_pct)
home_team$tov_pct <- home_team$tov_pct - (home_team$tov_pct*home_adj)
#Adjust Away
away_team$fg_pct <- away_stat_adjust(away_team$fg_pct)
away_team$three_pct <- away_stat_adjust(away_team$three_pct)
away_team$ts_pct <- away_stat_adjust(away_team$ts_pct)
away_team$trb_pct <- away_stat_adjust(away_team$trb_pct)
away_team$steal_pct <- away_stat_adjust(away_team$steal_pct)
away_team$block_pct <- away_stat_adjust(away_team$block_pct)
away_team$orb_pct <- away_stat_adjust(away_team$orb_pct)
away_team$tov_pct <- away_team$tov_pct - (away_team$tov_pct*away_adj)
}
#Reset Simulation Summary
sim_sum <- 0
x <- as.integer(1) #Game Count for Data Frame Entry
#Establish Max Shot Clock
sc_max <- 30
sc_min <- 10
#Percentage of Shots, Field Goals and Three Pointers
home_team_pct_fg <- home_team$fg_count / (home_team$fg_count + home_team$three_count)
home_team_pct_th <- home_team$three_count / (home_team$fg_count + home_team$three_count)
away_team_pct_fg <- away_team$fg_count / (away_team$fg_count + away_team$three_count)
away_team_pct_th <- away_team$three_count / (away_team$fg_count + away_team$three_count)
#Number of Simulations
i <- sim_count
loop <- i
while (i > 0)
{
#Set Scores for game simulation
home_team_score <- 0
away_team_score <- 0
#Set clock for game simulation
clock <- 2400
#Set counters for fg attempts during game simulation
home_team_fg_att <- 0
away_team_fg_att <- 0
#Tipoff
if (runif(1) >= .51) {
home_team_pos <- 1
away_team_pos <- 0
} else {
home_team_pos <- 0
away_team_pos <- 1
}
while (clock > 0) {
#Home Team Posession
if (home_team_pos == 1) {
if(runif(1) > (home_team$tov_pct - (home_team$tov_pct*home_adv))) { #Testing for Turnover
if (runif(1) <= home_team_pct_fg) { #Testing for Shot Type - FG
shot_type <- 2 #FG Shot Type
home_team_fg_att <- home_team_fg_att + 1
if (runif(1) > away_team$block_pct) { #Testing for Blocked Shot
if (runif(1) <= (home_team$fg_pct + (home_team$fg_pct*home_adv))) { #Testing if Shot is Made
home_team_score <- home_team_score + 2 #Shot is Made
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
} else { #Shot is Missed
if ( runif(1) <= (home_team$orb_pct + (home_team$orb_pct*home_adv))) { #Testing for Offensive Rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
} else { #Defense grabs the rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
}
}
} else { #Shot it Blocked
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
}
} else { #Shot Type - Three Point
shot_type <- 3 #Three Point Shot
if (runif(1) <= (home_team$three_pct + (home_team$three_pct*home_adv))) { #Testing if Shot is Made
home_team_score <- home_team_score + 3 #Shot is Made
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
} else { #Shot is Missed
if ( runif(1) <= (home_team$orb_pct + (home_team$orb_pct*home_adv))) { #Testing for Offensive Rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
} else { #Defense grabs the rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
}
}
}
} else { #Forced Turnover
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 0
away_team_pos <- 1
}
} else { #Away Team Posession
away_team_pos == 1
if(runif(1) > away_team$tov_pct) { #Testing for Turnover
if (runif(1) <= away_team_pct_fg) { #Testing for Shot Type - FG
shot_type <- 2 #FG Shot Type
away_team_fg_att <- away_team_fg_att + 1
if (runif(1) > home_team$block_pct) { #Testing for Blocked Shot
if (runif(1) <= away_team$fg_pct) { #Testing if Shot is Made
away_team_score <- away_team_score + 2 #Shot is Made
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
} else { #Shot is Missed
if ( runif(1) <= away_team$orb_pct) { #Testing for Offensive Rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
} else { #Defense grabs the rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
}
}
} else { #Shot it Blocked
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
}
} else { #Shot Type - Three Point
shot_type <- 3 #Three Point Shot
if (runif(1) <= away_team$fg_pct) { #Testing if Shot is Made
away_team_score <- away_team_score + 3 #Shot is Made
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
} else { #Shot is Missed
if ( runif(1) <= away_team$orb_pct) { #Testing for Offensive Rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
} else { #Defense grabs the rebound
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
}
}
}
} else { #Forced Turnover
#Calculating Time of Posession
top <- runif(1, min=sc_min, max=sc_max)
#Adjusting Clock Time
clock <- clock - top
#Posession Change
home_team_pos <- 1
away_team_pos <- 0
}
}
}
#Add in Free Throws
#Home Team
home_team_ft_att <- home_team_fg_att*home_team$ft_pct #FT Attempted
home_team_ft_points <- home_team_ft_att*home_team$ft_pct #FT Made
#Away Team
away_team_ft_att <- away_team_fg_att*away_team$ft_pct #FT Attempted
away_team_ft_points <- away_team_ft_att*away_team$ft_pct #FT Made
#Add to total score & Convert to type Int
home_team_score <- as.integer(home_team_score + home_team_ft_points)
away_team_score <- as.integer(away_team_score + away_team_ft_points)
if (home_team_score > away_team_score) { #Determine Game Winner
home_team_wins <- 1
away_team_wins <- 0
} else {
home_team_wins <- 0
away_team_wins <- 1
}
spread <- (home_team_score - away_team_score)
#Compile Game Summary Data
game_sum <- c(Game = x,
Home = home_team_score,
Away = away_team_score,
Spread = spread,
Home.Win = home_team_wins,
Away.Win = away_team_wins)
sim_sum <- data.frame(rbind(sim_sum, game_sum))
i <- i - 1 #Reduce Loop Count (Total Simulations Left to Run)
x <- x + 1
}
overall_sum <- colSums(sim_sum)/loop
overall_sum <- overall_sum[-1]
overall_sum <- t(type.convert(data.frame(overall_sum)))
return(overall_sum)
}
vectorized_sbg <- Vectorize(simulate_bg, vectorize.args = c("home_team", "away_team"))
simulate_bg(home_team = home_stats[1,], away_team = away_stats[1,])