-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd1_data_acquisition.R
256 lines (191 loc) · 8.08 KB
/
d1_data_acquisition.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
################################################################################
# Modified from Ben Baldwin's Code: https://github.com/nflverse/nflverse-pbp/blob/master/models/model_data.R
# Purpose: Prepare data for nflfastR models for EP and WP
# This takes a long time (especially finding next score half)
# Save a pre-prepared df
################################################################################
library(tidyverse)
########################################################################
#### helper function for building dataset to estimate EPA model ########
########################################################################
#original code
#https://github.com/ryurko/nflscrapR-models/blob/master/R/init_models/init_ep_fg_models.R
find_game_next_score_half <- function(pbp_dataset) {
# Which rows are the scoring plays:
score_plays <- which(pbp_dataset$sp == 1 & pbp_dataset$play_type != "no_play")
# Define a helper function that takes in the current play index,
# a vector of the scoring play indices, play-by-play data,
# and returns the score type and drive number for the next score:
find_next_score <- function(play_i, score_plays_i,pbp_df) {
# Find the next score index for the current play
# based on being the first next score index:
next_score_i <- score_plays_i[which(score_plays_i >= play_i)[1]]
# If next_score_i is NA (no more scores after current play)
# or if the next score is in another half,
# then return No_Score and the current drive number
if (is.na(next_score_i) |
(pbp_df$qtr[play_i] %in% c(1, 2) & pbp_df$qtr[next_score_i] %in% c(3, 4, 5)) |
(pbp_df$qtr[play_i] %in% c(3, 4) & pbp_df$qtr[next_score_i] == 5)) {
score_type <- "No_Score"
# Make it the current play index
score_drive <- pbp_df$drive[play_i]
# Else return the observed next score type and drive number:
} else {
# Store the score_drive number
score_drive <- pbp_df$drive[next_score_i]
# Then check the play types to decide what to return
# based on several types of cases for the next score:
# 1: Return TD
if (pbp_df$touchdown[next_score_i] == 1 & (pbp_df$td_team[next_score_i] != pbp_df$posteam[next_score_i])) {
# For return touchdowns the current posteam would not have
# possession at the time of return, so it's flipped:
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Opp_Touchdown"
} else {
score_type <- "Touchdown"
}
} else if (identical(pbp_df$field_goal_result[next_score_i], "made")) {
# 2: Field Goal
# Current posteam made FG
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Field_Goal"
# Opponent made FG
} else {
score_type <- "Opp_Field_Goal"
}
# 3: Touchdown (returns already counted for)
} else if (pbp_df$touchdown[next_score_i] == 1) {
# Current posteam TD
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Touchdown"
# Opponent TD
} else {
score_type <- "Opp_Touchdown"
}
# 4: Safety (similar to returns)
} else if (pbp_df$safety[next_score_i] == 1) {
if (identical(pbp_df$posteam[play_i],pbp_df$posteam[next_score_i])) {
score_type <- "Opp_Safety"
} else {
score_type <- "Safety"
}
# 5: Extra Points
} else if (identical(pbp_df$extra_point_result[next_score_i], "good")) {
# Current posteam Extra Point
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Extra_Point"
# Opponent Extra Point
} else {
score_type <- "Opp_Extra_Point"
}
# 6: Two Point Conversions
} else if (identical(pbp_df$two_point_conv_result[next_score_i], "success")) {
# Current posteam Two Point Conversion
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Two_Point_Conversion"
# Opponent Two Point Conversion
} else {
score_type <- "Opp_Two_Point_Conversion"
}
# 7: Defensive Two Point (like returns)
} else if (identical(pbp_df$defensive_two_point_conv[next_score_i], 1)) {
if (identical(pbp_df$posteam[play_i], pbp_df$posteam[next_score_i])) {
score_type <- "Opp_Defensive_Two_Point"
} else {
score_type <- "Defensive_Two_Point"
}
# 8: Errors of some sort so return NA (but shouldn't take place)
} else {
score_type <- NA
}
}
return(data.frame(Next_Score_Half = score_type,
Drive_Score_Half = score_drive))
}
# Using lapply and then bind_rows is much faster than
# using map_dfr() here:
lapply(c(1:nrow(pbp_dataset)), find_next_score,
score_plays_i = score_plays, pbp_df = pbp_dataset) %>%
bind_rows() %>%
return
}
################################################################################
# DATA PREP
################################################################################
#read in data from data repo (takes ~ 3 mins)
pbp_data <- nflfastR::load_pbp(1999:2022) %>%
filter(season_type == 'REG') %>%
mutate(
Winner = if_else(home_score > away_score, home_team,
if_else(home_score < away_score, away_team, "TIE"))
)
#get next score half using the provided function (takes ~ 12 mins)
pbp_next_score_half <- map_dfr(unique(pbp_data$game_id),
function(x) {
pbp_data %>%
filter(game_id == x) %>%
find_game_next_score_half()
})
#bind to original df
pbp_data <- bind_cols(pbp_data, pbp_next_score_half)
# # too many GB to save ...
# write_csv(pbp_data, 'data_nflFastR_pbp0_1999_2021.csv')
###########################################################################################
###########################################################################################
#for estimating the models, apply some filters
pbp_data_1 <- pbp_data %>%
filter(Next_Score_Half %in% c("Opp_Field_Goal", "Opp_Safety", "Opp_Touchdown",
"Field_Goal", "No_Score", "Safety", "Touchdown") &
play_type %in% c("field_goal", "no_play", "pass", "punt", "run",
"qb_spike") & is.na(two_point_conv_result) & is.na(extra_point_result) &
!is.na(down) & !is.na(game_seconds_remaining)) %>%
#to keep file size manageable
select(
game_id,
play_id,
Next_Score_Half,
Drive_Score_Half,
play_type,
desc,
game_seconds_remaining,
half_seconds_remaining,
yardline_100,
roof,
posteam,
defteam,
home_team,
away_team,
ydstogo,
season,
qtr,
down,
week,
drive,
ep,
score_differential,
posteam_score,
defteam_score,
posteam_timeouts_remaining,
defteam_timeouts_remaining,
desc,
receiver_player_name,
pass_location,
air_yards,
yards_after_catch,
complete_pass, incomplete_pass, interception,
qb_hit,
extra_point_result,
field_goal_result,
sp,
Winner,
result,
spread_line,
total_line,
### other features
passer_player_name, rusher_player_name, pass_attempt, rush_attempt, yards_gained,
kicker_player_name, punter_player_name, home_coach, away_coach,
### fake punt, fake FG, kneel
qb_kneel,
)
#save pbp data
write_csv(pbp_data_1, 'data_nflFastR_pbp_1999_2022.csv')