-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanData.Rmd
246 lines (195 loc) · 6.79 KB
/
cleanData.Rmd
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
---
title: "cleanData"
author: "Montgomery Inman"
date: "2024-08-27"
output: html_document
---
```{r}
library(cfbfastR)
library(cfbplotR)
library(tidyverse)
library(gt)
library(gtExtras)
```
Pull Data:
```{r}
team = "Iowa State"
year = 2024
week = 1
#Getting PBP data
pbpForEpa = cfbd_pbp_data(year = year, team = team, week = week, epa_wpa = T) %>%
filter(down > 0,
(rush == 1 | pass == 1),
!str_detect(play_text, "Kneel"))
#Getting gameid for the GAME
gameid = pbpForEpa$game_id[1]
#Getting advanced game log data for the GAME
advancedBox = cfbd_game_box_advanced(gameid, long = F)
#Getting team stats for the GAME
teamStats = cfbd_game_team_stats(year = year, game_id = gameid)
#Separating teamStats completions and attempts
teamStats = teamStats %>% separate_wider_delim(completion_attempts, delim = "-", names = c("completions", "attempts"))
#Separating teamStats third down efficiency attempts
teamStats = teamStats %>% separate_wider_delim(third_down_eff, delim = "-", names = c("thirdDownCompleted", "thirdDownAttempts"))
```
pbpForEpa: gameid, home & away teams
advancedBox:
teamStats: points
```{r}
#Seperating the team stats df with home and away
homeTeamStats = teamStats %>% filter(home_away == "home")
awayTeamStats = teamStats %>% filter(home_away == "away")
#Seperating the advanced box df with home and away
homeAdvBox = advancedBox %>% filter(team == pbpForEpa$home[1])
awayAdvBox = advancedBox %>% filter(team == pbpForEpa$away[1])
#Seperating the pbpForEpa df with home and away
homePBP = pbpForEpa %>%
filter(pos_team == pbpForEpa$home[1])
awayPBP = pbpForEpa %>%
filter(pos_team == pbpForEpa$away[1])
#Creating home Pbp for passes
homePBPpass = homePBP %>%
filter(pass == 1)
#Creating away Pbp for passes
awayPBPpass = awayPBP %>%
filter(pass == 1)
#Creating home Pbp for rushes
homePBPrush = homePBP %>%
filter(rush == 1)
#Creating away Pbp for rushes
awayPBPrush = awayPBP %>%
filter(rush == 1)
#Calculating EPA per PLAY for home & away
homeEPAperPlay = sum(homePBP$EPA, na.rm = T) / nrow(homePBP)
awayEPAperPlay = sum(awayPBP$EPA, na.rm = T) / nrow(awayPBP)
#Calculating EPA per PASS for home & away
homeEPAperPass = sum(homePBPpass$EPA, na.rm = T) / nrow(homePBPpass)
awayEPAperPass = sum(awayPBPpass$EPA, na.rm = T) / nrow(awayPBPpass)
#Calculating EPA per RUSH for home & away
homeEPAperRush = sum(homePBPrush$EPA, na.rm = T) / nrow(homePBPrush)
awayEPAperRush = sum(awayPBPrush$EPA, na.rm = T) / nrow(awayPBPrush)
```
```{r}
#Creating the data frame for the gt table
dfForGt = data.frame(
"Stat" = c("Score",
"EPA / Play",
"Yards / Play",
"Success Rate",
"EPA / Pass",
"Yards / Completion",
"Completion %",
"Attempts",
"Yards",
"EPA / Rush",
"Yards / Rush",
"Stuff Rate",
"Attempts",
"Yards",
"Third Down %"
),
home = c(homeTeamStats$points,
homeEPAperPlay,
as.numeric(homeTeamStats$total_yards) / homeAdvBox$ppa_plays,
homeAdvBox$success_rates_overall_total,
homeEPAperPass,
as.numeric(homeTeamStats$net_passing_yards) / as.numeric(homeTeamStats$completions),
as.numeric(homeTeamStats$completions) / as.numeric(homeTeamStats$attempts),
homeTeamStats$attempts,
homeTeamStats$net_passing_yards,
homeEPAperRush,
homeTeamStats$yards_per_rush_attempt,
homeAdvBox$rushing_stuff_rate,
homeTeamStats$rushing_attempts,
homeTeamStats$rushing_yards,
as.numeric(homeTeamStats$thirdDownCompleted) / as.numeric(homeTeamStats$thirdDownAttempts)),
away = c(awayTeamStats$points,
awayEPAperPlay,
as.numeric(awayTeamStats$total_yards) / awayAdvBox$ppa_plays,
awayAdvBox$success_rates_overall_total,
awayEPAperPass,
as.numeric(awayTeamStats$net_passing_yards) / as.numeric(awayTeamStats$completions),
as.numeric(awayTeamStats$completions) / as.numeric(awayTeamStats$attempts),
awayTeamStats$attempts,
awayTeamStats$net_passing_yards,
awayEPAperRush,
awayTeamStats$yards_per_rush_attempt,
awayAdvBox$rushing_stuff_rate,
awayTeamStats$rushing_attempts,
awayTeamStats$rushing_yards,
as.numeric(awayTeamStats$thirdDownCompleted) / as.numeric(awayTeamStats$thirdDownAttempts))
)
#Replacing the columns with the home and away teams
colnames(dfForGt)[2] <- pbpForEpa$home[1]
colnames(dfForGt)[3] <- pbpForEpa$away[1]
#Setting each column to be numeric
dfForGt[,2] = as.numeric(dfForGt[,2])
dfForGt[,3] = as.numeric(dfForGt[,3])
```
```{r}
#Getting data for ALL team stats for 2023 season
combined_data_team_stats <- data.frame()
weeksForPercentiles = c(1:15)
for (weeks in weeksForPercentiles) {
temp = cfbd_game_team_stats(
year = 2023,
week = weeks,
season_type = "regular",
team = NULL,
conference = NULL,
game_id = NULL,
rows_per_team = 1
)
combined_data_team_stats <- rbind(combined_data_team_stats, temp)
}
#Making combined_data_team_stats into a data frame
combined_data_team_stats = as.data.frame(combined_data_team_stats)
#Getting data for ALL adv stats for 2023 season
combined_data_adv_stats <- data.frame()
weeksForPercentiles = c(1:15)
weekData = data.frame()
#Have to get ALL the game ids
for (weeks in weeksForPercentiles) {
temp = cfbd_game_info(
year = year,
week = weeks,
season_type = "regular",
division = "fbs",
)
weekData = rbind(weekData, temp)
gameIds = weekData$game_id
}
#for all the gameids, get ALL the advanced stats for the 2023 season
combined_data_adv_stats <- do.call(rbind, lapply(gameIds, function(game) {
cfbd_game_box_advanced(game_id = game)
}))
#Getting ALL play by play data for the 2023 season
pbp2023 = cfbd_pbp_data(year = year, epa_wpa = T) %>%
filter(down > 0,
!str_detect(play_text, "Kneel"),
(rush == 1 | pass == 1))
```
```{r}
combined_data_team_stats = combined_data_team_stats %>% separate_wider_delim(completion_attempts, delim = "-", names = c("completions", "attempts"))
combined_data_team_stats = combined_data_team_stats %>% separate_wider_delim(third_down_eff, delim = "-", names = c("thirdDownCompleted", "thirdDownAttempts"))
combined_data_team_stats$completionPercent = as.numeric(combined_data_team_stats$completions) / as.numeric(combined_data_team_stats$attempts)
```
```{r}
#EFFICIENCY
#"EPA / Play", pbp2023
#"Yards / Play",
#"Success Rate",
#"Third Down %"
#PASSING
#"EPA / Pass",
#"Yards / Completion",
#"Completion %",
#"Attempts",
#"Yards",
#RUSHING
#"EPA / Rush",
#"Yards / Rush", combined_data_team_stats
#"Stuff Rate", combined_data_adv_stats
#"Attempts",
#"Yards",
```