Last active
January 27, 2021 16:13
-
-
Save friscojosh/f59e3c5b71da541a9a34ea27a53bafef to your computer and use it in GitHub Desktop.
WOPR year over year stability analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################ | |
#### Grab airyards data from airyards.com and test the year-to-year | |
#### stability of WOPR (Weighted Opportunity Rating) | |
############################################################################ | |
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://api.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 WOPR for each season, and add a column called season2 | |
## to join on in the next step | |
wopr_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)) %>% | |
mutate(wopr = round(0.7 * (air_yards / team_air) + 1.4 * (targets / team_att), 2), | |
season2 = season + 1) %>% | |
select(player_id, full_name, wopr, season, season2) %>% | |
arrange(-wopr) | |
## join the dataframe on itself using the season + 1 column we just made | |
wopr_joined <- wopr_data_seasons %>% | |
left_join(wopr_data_seasons, by = c("player_id", 'season2' = 'season')) %>% | |
na.omit() | |
## check the correlation by creating a simple linear model | |
model <- lm(data = wopr_joined, wopr.y ~ wopr.x) | |
summary(model) | |
## draw a quick scatter plot to see the relationship year over year | |
plot(wopr_joined$wopr.x, wopr_joined$wopr.y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment