Created
February 2, 2023 17:19
-
-
Save Henryjean/60951a6166b85a7bf823bb2701dd61de to your computer and use it in GitHub Desktop.
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
# Load packages | |
library(tidyverse) | |
library(vroom) | |
library(gt) | |
library(gtExtras) | |
# read in full l2m data | |
dat <- vroom("https://raw.githubusercontent.com/atlhawksfanatic/L2M/master/1-tidy/L2M/L2M.csv") | |
# count total games requiring an L2M for each team in 2023 -- we'll use this later | |
total_games <- dat %>% | |
filter(season == 2023) %>% | |
select(bkref_id, home_team, away_team) %>% | |
distinct() %>% | |
pivot_longer(-bkref_id, values_to = "team") %>% | |
count(team, sort = T) | |
# select relevant columns | |
df <- dat %>% | |
select(game_details, game_date, period, time, comments, call_type, committing, committing_team, disadvantaged_team, decision, season) %>% | |
# remove duplicates %>% | |
distinct() %>% | |
# limit data to 2023 | |
filter(season == 2023) | |
# hard code some ICs/INCs that were labeled as NA for some reason | |
df <- df %>% | |
mutate( | |
disadvantaged_team = case_when( | |
# klay steps out of bounds. wasnt called. disadvantages ORL, favors GSW | |
game_details == "Warriors(129) @ Magic(130)" & decision %in% c("INC", "IC") & (is.na(committing_team) | is.na(disadvantaged_team)) ~ "ORL", | |
# shot clock was reset. shouldnt have been. disadvantages PHI, favors HOU | |
game_details == "76ers(123) @ Rockets(132)" & decision %in% c("INC", "IC") & (is.na(committing_team) | is.na(disadvantaged_team))~ "PHI", | |
# mobley travels. wasnt called. disadvantages IND, favors CLE | |
game_details == "Pacers(112) @ Cavaliers(118)" & decision %in% c("INC", "IC") & (is.na(committing_team) | is.na(disadvantaged_team)) ~ "IND", | |
# tre jones interferes with the ball and tosses it the official. wasnt called. disadvantages MEM, favors HOU | |
game_details == "Spurs(129) @ Grizzlies(135)" & decision %in% c("INC", "IC") & (is.na(committing_team) | is.na(disadvantaged_team)) ~ "MEM", | |
TRUE ~ disadvantaged_team), | |
committing_team = case_when( | |
# shot clock was reset. shouldnt have been. disadvantages PHI, favors HOU | |
game_details == "76ers(123) @ Rockets(132)" & decision %in% c("INC", "IC") & (is.na(committing_team) | is.na(disadvantaged_team)) ~ "HOU", | |
TRUE ~ committing_team) | |
) | |
# count times an INC (incorrect no call) or IC (incorrect call) went in a teams favor | |
favor <- df %>% | |
mutate(team_favored = case_when( | |
decision == "INC" ~ committing_team, | |
decision == "IC" ~ disadvantaged_team | |
)) %>% | |
group_by(team_favored) %>% | |
summarize(count_favored = n()) %>% | |
filter(!is.na(team_favored)) %>% | |
arrange(desc(count_favored)) | |
# count times INC or IC went in other team's favor | |
not_favor <- df %>% | |
mutate(team_not_favor = case_when( | |
decision == "INC" ~ disadvantaged_team, | |
decision == "IC" ~ committing_team | |
)) %>% | |
group_by(team_not_favor) %>% | |
summarize(count_not_favored = n()) %>% | |
filter(!is.na(team_not_favor)) %>% | |
arrange(desc(count_not_favored)) | |
# combine two bad call data frames into one | |
bad_calls_df <- full_join(favor, not_favor, by = c("team_favored" = "team_not_favor")) %>% | |
rename("team" = "team_favored") %>% | |
left_join(total_games, by = "team") %>% | |
transmute(Team = team, | |
`L2Ms` = n, | |
`Errors` = count_favored + count_not_favored, | |
`For` = count_favored, | |
`Against` = count_not_favored, | |
`Diff.` = count_favored - count_not_favored) %>% | |
arrange(desc(`Diff.`)) %>% | |
print(n = 30) | |
# get the min and max of the differences | |
min_max <- max(c(abs(min(bad_calls_df$`Diff.`)), abs(max(bad_calls_df$`Diff.`)))) | |
# create table | |
bad_calls_df %>% | |
gt() %>% | |
tab_header( | |
title = md("**L2M Reports**"), | |
subtitle = paste0("2022-23 Regular Season | Updated ", format(Sys.Date(), format="%B %d, %Y")) | |
) %>% | |
gt_theme_538() %>% | |
gt_hulk_col_numeric("Diff.", trim = TRUE, domain = c(min_max, min_max*-1)) %>% | |
tab_options( | |
heading.title.font.size = 16, | |
heading.title.font.weight = 'bold', | |
heading.subtitle.font.size = 11, | |
column_labels.font.size = 10.5, | |
table.font.size = 10 | |
) %>% | |
cols_width( | |
Team ~ px(35), | |
everything() ~ px(55) | |
) %>% | |
tab_footnote( | |
footnote = "ICs on opponent or INCs on team", | |
locations = cells_column_labels( | |
columns = For | |
) | |
) %>% | |
tab_footnote( | |
footnote = "INCs on opponent or ICs on team", | |
locations = cells_column_labels( | |
columns = Against | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment