-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPace.R
443 lines (364 loc) · 14.3 KB
/
Pace.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
###############################################
# PACE
# Session Info:
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19042)
###############################################
# Load packages and set things up-------
library(tidyverse)
library(nbastatR)
library(extrafont)
library(ballr)
library(rvest)
library(httr)
library(jsonlite)
library(teamcolors)
library(readxl)
library(geomtextpath)
library(tidybayes)
library(rstanarm)
library(bayesplot)
# set seed
set.seed(20483789)
# for downloads
Sys.setenv(VROOM_CONNECTION_SIZE = 131072*3)
# headers for scraping
headers = c(
`Connection` = 'keep-alive',
`Accept` = 'application/json, text/plain, */*',
`x-nba-stats-token` = 'true',
`X-NewRelic-ID` = 'VQECWF5UChAHUlNTBwgBVw==',
`User-Agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
`x-nba-stats-origin` = 'stats',
`Sec-Fetch-Site` = 'same-origin',
`Sec-Fetch-Mode` = 'cors',
`Referer` = 'https://stats.nba.com/players/leaguedashplayerbiostats/',
`Accept-Encoding` = 'gzip, deflate, br',
`Accept-Language` = 'en-US,en;q=0.9'
)
# Download initial data----
# get nba team ids
tms <- nba_teams()
tms <- tms %>%
filter(isNonNBATeam == 0) %>%
select(nameTeam, slugTeam, idTeam)
# game logs
dat_team <- game_logs(seasons = 2022, result_types = "team")
# get box scores
dat_team2 <- box_scores(game_ids = dat_team$idGame, league = "NBA", result_types = "team")
# data for players
dat_player <- game_logs(seasons = c(2022), season_types = "Regular Season", result_types = c("player"))
# filter for just the wizards
dat_player_wiz <- dat_player %>% filter(nameTeam == "Washington Wizards")
# get games with Dinwiddie
dat_player_din <- dat_player_wiz %>% filter(namePlayer == "Spencer Dinwiddie") %>%
select(numberGameTeamSeason)
# games with Beal
dat_player_beal <- dat_player_wiz %>% filter(namePlayer == "Bradley Beal") %>%
select(numberGameTeamSeason)
# games with Dinwiddie and no Beal
spence_games <- dat_player_din %>% filter(!numberGameTeamSeason %in% dat_player_beal$numberGameTeamSeason)
# Scrape NBA.com------
# get just Wiz in-game pace
res_wiz <- GET(url = "https://stats.nba.com/stats/teamdashboardbygamesplits?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=ingame&TeamID=1610612764&VsConference=&VsDivision=", add_headers(.headers=headers))
json_resp <- fromJSON(content(res_wiz, "text"))
headings <- data.frame(json_resp$resultSets$headers[3])
dat1 <- data.frame(json_resp$resultSets$rowSet[3])
names(dat1) <- headings$c..GROUP_SET....GROUP_VALUE....GP....W....L....W_PCT....MIN...
# take a look
dat1 %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)) %>%
filter(Quarter <5) %>%
ggplot(aes(x = Quarter, y = Pace, group = GROUP_SET)) +
geom_step( size = 3) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20))
# Scrape the whole NBA-----
# now lets do that for every team
id <- tms$idTeam
team_pace <- NULL
for(i in 1:length(id)){
url <- paste0("https://stats.nba.com/stats/teamdashboardbygamesplits?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=ingame&TeamID="
, id[i], "&VsConference=&VsDivision=")
res <- GET(url = url, add_headers(.headers=headers))
json_res2p <- fromJSON(content(res, "text"))
tmp_dat <- data.frame(json_res2p$resultSets$rowSet[3]) %>%
full_join(data.frame(json_res2p$resultSets$rowSet[3]))
tmp_dat$team <- id[i]
team_pace[[i]] <- tmp_dat
}
pace_df <- bind_rows(team_pace, .id = "column_label")
names_pace <- data.frame(headers = json_res2p$resultSets$headers[3])
names_pace2 <- c(names_pace$c..GROUP_SET....GROUP_VALUE....GP....W....L....W_PCT....MIN...)
names(pace_df)[2:48] <- names_pace2
pace_df <- pace_df %>% left_join(tms, by = c("team" = "idTeam"))
# get team colors
tm.colors <- teamcolors
tm.colors <- tm.colors %>%
filter(league == "nba") %>%
select("nameTeam" = name, primary) %>%
mutate(primary = case_when(
nameTeam == "Golden State Warriors" ~ "#1D428A",
nameTeam == "Indiana Pacers" ~ "#002D62",
nameTeam == "Los Angeles Lakers" ~ "#552583",
nameTeam == "San Antonio Spurs" ~ "#000000",
nameTeam == "Oklahoma City Thunder" ~ "#EF3B24",
nameTeam == "Charlotte Hornets" ~ "#00788C",
nameTeam == "Utah Jazz" ~ "#00471B",
nameTeam == "New Orleans Pelicans" ~ "#0C2340",
TRUE ~ primary
)) %>%
bind_rows(tibble(nameTeam = "League Average", primary = "#666666"))
cols <- tm.colors %>% arrange(nameTeam) %>% select(primary)
# let's just look at league average vs. Wiz-----
cols_wiz_avg <- tm.colors %>%
filter(nameTeam %in% c("League Average", "Washington Wizards")) %>%
arrange(nameTeam) %>%
select(primary) # %>%
# bind_rows(tibble(primary = "#ed174c"))
last_game <- tibble(Quarter = c(1:4)
, Pace = c(96, 92, 84, 90)
, Team = "Wizards win vs. Sixers, Feb. 2")
p1 <- pace_df %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
) %>%
filter(Quarter <5) %>%
group_by(Quarter) %>%
summarize(Pace = mean(Pace, na.rm=T)) %>%
mutate(Team = "League Average") %>%
bind_rows(dat1 %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
, Team = "Wizards"
) %>%
filter(Quarter <5) %>% select(Quarter, Pace, Team)
) %>%
# bind_rows(last_game) %>%
ggplot(aes(x = Quarter, y = Pace, group = Team
, col = Team
)
) +
geom_step( size = 2) +
scale_color_manual(values = cols_wiz_avg$primary) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
facet_grid(~Team) +
labs(title = "NBA pace and Wizards pace by quarter for the season so far"
, x = "Quarter", y = "Pace"
, caption = "wizardspoints.substack.com\ndata: nba.com")
ggsave("Wiz pace.png", p1, width = 10, height = 8, dpi = 300, type = 'cairo')
# true pace----
pace_df %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
) %>%
filter(Quarter <5) %>%
group_by(Quarter) %>%
summarize(Pace = mean(Pace, na.rm=T)) %>%
mutate(Team = "League Average") %>%
bind_rows(dat1 %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
, Team = "Wizards"
) %>%
filter(Quarter <5) %>% select(Quarter, Pace, Team)
) %>% pivot_wider(names_from = Team, values_from = Pace) %>%
mutate(pctdiff = ( Wizards-`League Average`)/((`League Average` + Wizards)/2)*100)
# pace across the league-----
p2 <- pace_df %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
) %>%
filter(Quarter <5) %>%
group_by(Quarter) %>%
summarize(Pace = mean(Pace, na.rm=T)) %>%
mutate(Team = "League Average") %>%
bind_rows(pace_df %>%
rename(Quarter = GROUP_VALUE
, Pace = PACE) %>%
mutate(Quarter = as.numeric(Quarter)
, Pace = as.numeric(Pace)
, Team = nameTeam
) %>%
filter(Quarter <5) %>% select(Quarter, Pace, Team)
) %>%
left_join(tm.colors, by = c("Team" = "nameTeam")) %>%
ggplot(aes(x = Quarter, y = Pace, group = Team
, col = Team
)
) +
geom_step( size = 2) +
scale_color_manual(values = cols$primary) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
facet_wrap(~Team, labeller = label_wrap_gen(width=20)) +
labs(title = "Pace across the NBA by quarter for the season so far"
, x = "Quarter", y = "Pace"
, caption = "wizardspoints.substack.com\ndata: nba.com")
ggsave("NBA pace.png", p2, width = 14, height = 16, dpi = 300, type = 'cairo')
# last wizards game that was good (i.e., against Philly)----
p3 <- last_game %>%
ggplot(aes(x = Quarter, y = Pace)
) +
geom_step( size = 2
, col = "#ed174c"
) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
labs(title = "Pace for Wizards vs. Sixers on Feb. 2"
, x = "Quarter", y = "Pace"
, caption = "wizardspoints.substack.com\ndata: nba.com")
ggsave("sixers pace.png", p3, width = 14, height = 12, dpi = 300, type = 'cairo')
# Wiz pace and other stats----
# data from https://www.basketball-reference.com/teams/WAS/2022/gamelog-advanced/
games <- read_excel("sportsref_download.xls.xlsx")
games2 <- games %>%
select(-Rk, -G, -Date, -at, -Opp...5, -`W/L`) %>%
rename("Opp_Points" = Opp...8
, "Wiz_Points" = Tm
, "Opponents_RB%" = `ORB%`
, "Opponents_eFG%" = `OeFG%`
, "Opponents_TOV%" = `OTOV%`
, "Opponents_DRB%" = `ODRB%`
, "Opponents_FT/FGA" = `OFT/FGA`
) %>%
mutate("NetRtg" = ORtg-DRtg)
p4 <- games %>%
select(-Rk, -G, -Date, -at, -Opp...5, -`W/L`) %>%
rename("Points (Opponents)" = Opp...8
, "Points (Wizards)" = Tm
, "3pt Attempt Rate" = `3PAr`
, "Opponents RB%" = `ORB%`
, "Opponents eFG%" = `OeFG%`
, "Opponents TOV%" = `OTOV%`
, "Opponents DRB%" = `ODRB%`
, "Opponents FT/FGA" = `OFT/FGA`
, "Free Throw Attempt Rate" = FTr
) %>%
pivot_longer(cols = -Pace
, names_to = "Stat"
, values_to = "Values") %>%
# filter(Stat %in% c("TOV%"
# ,"Opponents TOV%"
# , "FT/FGA"
# , "Opponents FT/FGA"
# )
# ) %>%
mutate(Values = ifelse(Stat %in% c("3pt Attempt Rate"
, "FT/FGA"
, "Free Throw Attempt Rate"
, "eFG%"
, "Opponents eFG%"
, "Opponents FT/FGA"
, "TS%"
), Values*100, Values)) %>%
ggplot(aes(x = Pace, y = Values)) +
geom_point(col = "#002b5c") +
geom_smooth(method = "lm", se = F, col = "#666666") +
facet_wrap(~Stat, scales = "free_y", labeller = label_wrap_gen(width=20)) +
theme_minimal() +
theme(legend.position = "NA"
, text = element_text(size = 20)) +
# scale_y_continuous(labels = percent_format(accuracy = 1)) +
labs(x = "Pace", y = ""
, title = "Pace and everything else"
, caption = "wizardspoints.substack.com\ndata: basketball-reference.com"
)
ggsave("other stats and pace.png", p4, width = 14, height = 12, dpi = 300, type = 'cairo')
# pace over time
# create a vector just with games that didn't have Beal in them
dates <- games %>%
mutate(Dinwiddie = ifelse(G %in% spence_games$numberGameTeamSeason, "Only Dinwiddie, no Beal", " ")
) %>% filter(Dinwiddie == "Only Dinwiddie, no Beal") %>%
select(Date)
p5 <- games %>%
mutate(Dinwiddie = ifelse(G %in% spence_games$numberGameTeamSeason, "Only Dinwiddie, no Beal", " ")
) %>%
select(G, Dinwiddie, Date, Pace) %>%
ggplot(aes(Date, Pace)) +
geom_rect(aes(xmin = dates$Date[1], xmax = dates$Date[1]+1
, ymin = -Inf
, ymax = Inf
), fill = "dark grey"
, alpha = 1) +
geom_rect(aes(xmin = dates$Date[2], xmax = dates$Date[3]
, ymin = -Inf
, ymax = Inf
), fill = "dark grey"
, alpha = 0.02) +
geom_rect(aes(xmin = dates$Date[4], xmax = dates$Date[6]
, ymin = -Inf
, ymax = Inf
), fill = "dark grey"
, alpha = 0.02) +
geom_rect(aes(xmin = dates$Date[7], xmax = dates$Date[9]
, ymin = -Inf
, ymax = Inf
), fill = "dark grey"
, alpha = 0.02) +
geom_rect(aes(xmin = dates$Date[10], xmax = dates$Date[13]
, ymin = -Inf
, ymax = Inf
), fill = "dark grey"
, alpha = 0.02) +
geom_step(size = 2, col = "#002b5c") +
theme_minimal() +
theme(text = element_text(size = 20)) +
labs(x = "", y = "Pace"
, title = "Wizards pace by game over the season so far"
, subtitle = "Grey areas denote games without Beal"
, caption = "wizardspoints.substack.com\ndata: basketball-reference.com"
)
ggsave("Pace for the season.png", p5, width = 14, height = 7, dpi = 300, type = 'cairo')
# Models----
prior_dist <- rstanarm::cauchy(location = 20)
m1 <- stan_glm(Pace ~ `Opponents_TOV%`, data = games2
, prior_intercept = prior_dist,
prior = prior_dist
)
plot(m1, pars = "`Opponents_TOV%`", "hist") + labs(x = "Opponents TOV %")
# Ridgelines version of the areas plot
plot(m1, "hist", regex_pars = "Wiz_Points")
set.seed(123)
# make the parsnip model
bayes_mod <-
linear_reg() %>%
set_engine("stan",
prior_intercept = prior_dist,
prior = prior_dist)
# train the model
bayes_fit <-
bayes_mod %>%
fit(Pace ~ `TOV%`, data = games2)
print(bayes_fit, digits = 5)
new_points <- expand.grid(Wiz_Points = 106.7547
)
new_points
bayes_plot_data <-
new_points %>%
bind_cols(predict(bayes_fit, new_data = new_points)) %>%
bind_cols(predict(bayes_fit, new_data = new_points, type = "conf_int"))
ggplot(bayes_plot_data, aes(x = Wiz_Points)) +
geom_point(aes(y = .pred)) +
geom_errorbar(aes(ymin = .pred_lower, ymax = .pred_upper), width = .2) +
labs(y = "Pace") +
ggtitle("Bayesian model with t(1) prior distribution")