-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.R
249 lines (161 loc) · 6.97 KB
/
functions.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
# convex hull area ---------------------------------------------------
chull_area <- function(X,Y) {
#Calculates the convex hull area
df_hull <- data.frame(X = X, Y = Y)
c.hull <- chull(df_hull)
#You need five points to draw four line segments, so we add the first set of points at the end
c.hull <- c(c.hull, c.hull[1])
chull.coords <- df_hull[c.hull ,]
chull.poly <- Polygon(chull.coords, hole = F)
chull.area <- chull.poly@area
return (chull.area)
}
get_chull <- function(play_df, teamid) {
end_of_play <- min(play_df$game_clock)
end_minus_2 <- play_df$game_clock[which.min(abs(end_of_play + 2 - play_df$game_clock) )]
clipped_teamid <- teamid %>%
as.character() %>%
str_sub(-2) %>%
as.integer()
chull_moment <- play_df %>%
filter(game_clock == end_minus_2,
team_id == clipped_teamid) %>%
.[1:5,]
chull_area(chull_moment$x_loc, chull_moment$y_loc)
}
# ball distance ------------------------------------------------------
dist_traveled <- function(xloc, yloc) {
dx <- diff(xloc) %>% abs()
dy <- diff(yloc) %>% abs()
sqrt(dx^2 + dy^2) %>% sum()
}
stagnation <- function(play_df, o_team) {
elapsed_time <- play_df$game_clock %>%
{ max(.) - min(.) }
o_team_short <- substr(o_team, 9, 10) %>%
as.numeric()
team_distance <- play_df %>%
filter(team_id == o_team_short) %>%
group_by(player_id) %>%
summarize(dist_traveled = dist_traveled(x_loc, y_loc)) %>%
.$dist_traveled %>%
sum()
team_distance / elapsed_time
}
ax_dist <- function(loc) {
diff(loc) %>%
abs() %>%
sum()
}
ax_spread <- function(loc) {
max(loc) - min(loc)
}
# ball in paint ------------------------------------------------------
in_paint <- function(x, y) {
y_bound <- y > 19 & y < 31
x_bound <- (x > 0 & x < 19) | (x > 75 & x < 94)
x_bound & y_bound
}
# posession ----------------------------------------------------------
quarter_side <- function(my_chull, q) {
# this function takes a quarter number and a dataframe with chull areas
# for each moment of a game by team. it returns a dataframe that labels
# offensive side for each team
# calculate avg chull over the course of each event
pos <- my_chull %>%
group_by(event.id, team_id, quarter) %>%
summarize(avg_chull = mean(chull),
med_chull = median(chull),
court_side = most_frequent(court_side) ) %>%
# determine the "bigger team" during each event for the specified quarter
# on a given side of the court this indicates possession for that side
filter(quarter == q) %>%
group_by(event.id) %>%
summarize(bigger_team = team_id[which.max(med_chull)],
court_side = court_side[which(is.na(team_id))[1]] ) %>%
mutate(bigger_team = factor(bigger_team))
# compute the frequency of plays on each court side for the bigger team
table(pos$bigger_team, pos$court_side) %>%
data.frame() %>%
rename(o_team_id = Var1) %>%
group_by(o_team_id) %>%
summarize(side = Var2[which.max(Freq)])
}
# most frequent ------------------------------------------------------
mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
most_frequent <- function(x) {
mf_wrapper <- function(y) {
most_f <- y %>%
.[!is.na(.)] %>%
table() %>%
which.max() %>%
names()
if (is.null(most_f)){
most_f <- 'none'
}
return(most_f)
}
tryCatch( mf_wrapper(x),
error = function(e) return('none'),
warning = function(e) return('none'))
}
# last non na --------------------------------------------------------
last_non_na <- function(x) {
last_wrapper <- function(y) {
vec_index <- which(!is.na(y) ) %>%
max()
last_nna <- y[vec_index]
if (is.null(last_nna)){
last_nna <- 'none'
}
return(last_nna)
}
tryCatch( last_wrapper(x),
error = function(e) return('none'),
warning = function(e) return('none'))
}
# join player stats to play by play-----------------------------------
get_player_stats <- function(pbp_file, player_file) {
pbp <- read.csv(pbp_file)
players_df <- read.csv(player_file)
player_link <- bind_rows(data.frame(id = pbp$PLAYER1_ID,
name = pbp$PLAYER1_NAME,
team = pbp$PLAYER1_TEAM_ID),
data.frame(id = pbp$PLAYER2_ID,
name = pbp$PLAYER2_NAME,
team = pbp$PLAYER2_TEAM_ID) ) %>%
distinct()
player_link %>%
mutate(name = tolower(name)) %>%
left_join(players_df, by = c('name' = 'full_name'))
}
# distance matrix ----------------------------------------------------
player_dist <- function(df, player_id_A, player_id_B) {
#Functions finds the distance of the player, assumes you have a dataframe all.movements with player info
df <- df[which((df$player_id == player_id_A | df$player_id == player_id_B) ), ]
dfA <- df %>% filter (player_id == player_id_A) %>% select (x_loc,y_loc)
dfB <- df %>% filter (player_id == player_id_B) %>% select (x_loc,y_loc)
df.l <- 1:nrow(dfA)
distsdf <- unlist(lapply(df.l,function(x) {dist(rbind(dfA[x,], dfB[x,]))}))
return(distsdf)
}
# data prep ----------------------------------------------------------
prep_data_from_frame <- function(frame, game) {
frame %>%
# filter to game of interest
filter(gameid == game) %>%
mutate(shot_clock = ifelse(is.na(shot_clock),0,shot_clock)) %>%
arrange(quarter, desc(game_clock), shot_clock, x_loc) %>%
# label ball-side
mutate(ballside = ifelse(player_id == -1 & x_loc < 47, 'L', NA ),
ballside = ifelse(player_id == -1 & x_loc >= 47, 'R', ballside) ) %>%
# label court-side
mutate(courtside = ifelse(x_loc < 47, 'L', 'R')) %>%
# is player / ball in the paint
mutate(inPaint = in_paint(x_loc, y_loc) ) %>%
# take only distinct quarter-game_clock-player combinations
distinct(player_id, quarter, game_clock, .keep_all = T)
}