Instantly share code, notes, and snippets.

Embed
What would you like to do?
############################################################################
#### Grab airyards data from airyards.com and test the year-to-year
#### stability of depth of target for all receivers
############################################################################
library(tidyverse)
library(jsonlite)
## define a function that uses jsonlite to call the airyards.com
## API and returns the processed JSON as a dataframe.
get_air_yards <- function(year){
uri <- paste0("http://airyards.com/", year, "/weeks")
df_air <- fromJSON(uri) %>%
mutate(season = year)
return(df_air)
}
## initialize an empty data frame so we have something to work with
wopr_data <- data.frame()
## for loops are generally frowned upon in R, but this is fine and fuck the haters.
## we take each year of data and bind all the rows to create one large dataframe.
for (year in 2009:2017) {
print(paste("Grabbing air yards data for", year))
df_air <- get_air_yards(year)
wopr_data <- rbind(wopr_data, df_air)
}
## group all the weekly data into player-seasons,
## re-calculate aDOT for each season, and add a column called season2
## to join on in the next step
adot_data_seasons <- wopr_data %>%
filter(tar >= 1) %>%
group_by(player_id, full_name, season) %>%
summarize(team_att = sum(tm_att),
team_air = sum(team_air),
targets = sum(tar),
air_yards = sum(air_yards),
adot = sum(air_yards) / sum(tar)) %>%
mutate(wopr = round(0.7 * (air_yards / team_air) + 1.4 * (targets / team_att), 2),
season2 = season + 1) %>%
select(player_id, full_name, adot, season, season2)
## join the dataframe on itself using the season + 1 column we just made
adot_joined <- adot_data_seasons %>%
left_join(adot_data_seasons, by = c("player_id", 'season2' = 'season')) %>%
na.omit()
## check the correlation by creating a simple linear model
model <- lm(data = adot_joined, adot.y ~ adot.x)
summary(model)
## draw a quick scatter plot to see the relationship year over year
qplot(adot_joined$adot.x, adot_joined$adot.y, xlim = c(0, 20), ylim = c(0, 20), xlab = "Year Y Receiver aDOT", ylab = "Year Y+1 Receiver aDOT" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment