Last active
December 28, 2023 05:18
-
-
Save andreweatherman/a79d12e3e29b4b60145f8773a6f9c5ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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 THE LIBRARIES | |
| needed_packages <- c('tidyverse', 'rvest', 'gt', 'gtExtras', 'withr') | |
| installed_packages <- needed_packages %in% rownames(installed.packages()) | |
| if (any(installed_packages == FALSE)) { | |
| install.packages(packages[!installed_packages]) | |
| } | |
| invisible(lapply(needed_packages, library, character.only = TRUE)) | |
| ### PART 1: Data Extraction ==== | |
| # read in season schedule | |
| # withr::local_options(HTTPUserAgent='Buckets & Bytes') for windows machines | |
| schedule <- read_csv("https://barttorvik.com/2024_master_sked.csv", col_names = FALSE) %>% | |
| setNames(c('game_id', 'game_date', 'game_type', 'neutral', 'away', 'home')) %>% | |
| select(neutral, home, away) | |
| # scrape ratings | |
| # withr::local_options(HTTPUserAgent='Buckets & Bytes') for windows machines | |
| ratings <- read_html('https://barttorvik.com/trankpre.php') %>% | |
| html_table() %>% | |
| pluck(1) %>% | |
| janitor::clean_names() %>% | |
| # convert columns to numeric | |
| mutate(across(c(adj_oe, adj_de), ~ as.numeric(.x))) %>% | |
| select(team, adj_oe, adj_de) | |
| # IF YOU RUN INTO CONNECTION ERORRS: | |
| # ratings <- read_csv('https://gist.github.com/andreweatherman/f7c5e850a88f22b0577599fbc9d26da9/raw/928588a1afbddf1c49266cf3b56892be6682793f/bytes_ratings_connection_error.csv') %>% | |
| # convert columns to numeric | |
| # mutate(across(c(adj_oe, adj_de), ~ as.numeric(.x))) %>% | |
| # select(team, adj_oe, adj_de) | |
| # manually encode T-25 | |
| ap_top_25 <- tibble( | |
| rank = 1:25, | |
| team = c('Kansas', 'Duke', 'Purdue', 'Michigan St.', 'Marquette', 'Connecticut', 'Houston', 'Creighton', 'Tennessee', | |
| 'Florida Atlantic', 'Gonzaga', 'Arizona', 'Miami FL', 'Arkansas', 'Texas A&M', 'Kentucky', 'San Diego St.', 'Texas', | |
| 'North Carolina', 'Baylor', 'USC', 'Villanova', "Saint Mary's", 'Alabama', 'Illinois') | |
| ) %>% left_join(ratings, by = 'team') | |
| ### PART 2: DATA ANALYSIS ===== | |
| # get team logos for plotting | |
| teams <- read_csv('https://gist.github.com/andreweatherman/cd2a258b7a75dc75cd86940e29f28af9/raw/12a2cd5f919a681ab4711c4b5082385bacd78c2e/teams.csv') %>% | |
| select(common_team, logo) | |
| parse_wp <- function(team) { | |
| win_percentages <- schedule %>% | |
| filter(home == team | away == team) %>% | |
| # add game location and average ratings | |
| mutate( | |
| game_location = case_when( | |
| home == team & neutral == 0 ~ 'home', | |
| away == team & neutral == 0 ~ 'away', | |
| .default = 'neutral' | |
| ), | |
| team = team, | |
| opponent = if_else(team == home, away, home), | |
| avg_ap_adj_oe = mean(ap_top_25$adj_oe), | |
| avg_ap_adj_de = mean(ap_top_25$adj_de)) %>% | |
| # join on ratings | |
| left_join(ratings %>% select(team, opp_adj_oe = adj_oe, opp_adj_de = adj_de), join_by('opponent' == 'team')) %>% | |
| left_join(ap_top_25 %>% select(-rank), by = 'team') %>% | |
| # adjust ratings | |
| adjust_efficiency() %>% | |
| # calculate pythag and wp | |
| mutate( | |
| team_pythag = (adj_oe^11.5) / (adj_oe^11.5 + adj_de^11.5), | |
| avg_ap_pythag = (avg_ap_adj_oe^11.5) / (avg_ap_adj_oe^11.5 + avg_ap_adj_de^11.5), | |
| opp_pythag = (opp_adj_oe^11.5) / (opp_adj_oe^11.5 + opp_adj_de^11.5), | |
| team_wp = (team_pythag - team_pythag * opp_pythag) / (team_pythag + opp_pythag - 2 * team_pythag * opp_pythag), | |
| ap_wp = (avg_ap_pythag - avg_ap_pythag * opp_pythag) / (avg_ap_pythag + opp_pythag - 2 * avg_ap_pythag * opp_pythag) | |
| ) %>% | |
| select(team, team_wp, ap_wp) | |
| return(win_percentages) | |
| } | |
| adjust_efficiency <- function(df) { | |
| adjusted <- df %>% | |
| mutate( | |
| # off. ratings for team and AP average (NOT opponent) | |
| across(ends_with("oe") & !starts_with("opp"), | |
| ~ case_when( | |
| game_location == "home" ~ . * 1.013, | |
| game_location == "away" ~ . * 0.987, | |
| .default = . # no change for neutral | |
| )), | |
| # def. ratings for team and AP average (NOT opponent) | |
| across(ends_with("de") & !starts_with("opp"), | |
| ~ case_when( | |
| game_location == "home" ~ . * 0.987, | |
| game_location == "away" ~ . * 1.013, | |
| .default = . | |
| )), | |
| # off. ratings for opponent (game location is switched!) | |
| across(opp_adj_oe, | |
| ~ case_when( | |
| game_location == "home" ~ . * 0.987, | |
| game_location == "away" ~ . * 1.013, | |
| .default = . | |
| )), | |
| # def. ratings for opponent (game location is switched!) | |
| across(opp_adj_de, | |
| ~ case_when( | |
| game_location == "home" ~ . * 1.013, | |
| game_location == "away" ~ . * 0.987, | |
| .default = . | |
| )) | |
| ) | |
| return(adjusted) | |
| } | |
| # loop over parse_wp() function with every ranked team | |
| game_preds <- map_dfr(ap_top_25$team, \(team) parse_wp(team)) | |
| # get expected wins to plot | |
| game_preds_summarized <- game_preds %>% | |
| # throw out non-D1 games | |
| filter(!is.na(team_wp)) %>% | |
| # calculate expected wins (sum) | |
| summarize( | |
| team_wins = sum(team_wp), | |
| ap_wins = sum(ap_wp), | |
| diff = team_wins - ap_wins, | |
| .by = team | |
| ) %>% | |
| # join on team logos | |
| left_join(teams, join_by('team' == 'common_team')) %>% | |
| mutate(ap_rank = row_number(), .before = team) %>% | |
| # css to display logo -> team in table | |
| mutate(team_logo = glue::glue("<img src='{logo}' style='height: 25px; width: auto; vertical-align: middle;'> {team}")) | |
| ### PART 2: DATA VIZ. ===== | |
| game_preds_summarized %>% | |
| # init. table and use ID for custom css | |
| gt(id='ap') %>% | |
| # base theme | |
| gt_theme_excel() %>% | |
| # render html columns | |
| fmt_markdown(team_logo) %>% | |
| # hide, move, align cols. | |
| cols_hide(c(team, logo)) %>% | |
| cols_move(team_logo, after = ap_rank) %>% | |
| cols_align(columns = c(team_wins, ap_wins, diff, ap_rank), align = 'center') %>% | |
| cols_align(columns = team_logo, align = 'left') %>% | |
| # relabel cols. | |
| cols_label( | |
| ap_rank = 'Rank', | |
| team_logo = 'Team', | |
| team_wins = 'TM Wins', | |
| ap_wins = 'AP Wins', | |
| diff = 'Diff.') %>% | |
| # numeric cols. to 2 digits and conditional fill | |
| fmt_number(columns = c(team_wins, ap_wins, diff), decimals = 2) %>% | |
| gt_hulk_col_numeric(diff) %>% | |
| # table title, subtitle, and caption | |
| tab_header(title = md('How Teams Stack Up to Avg. AP Top 25 Expectation'), subtitle = md('Venue-adjused team wins vs. expected wins by the average top 25 team')) %>% | |
| tab_source_note(source_note = md('Data by Barttorvik<br>Analysis + Viz. by @andreweatherman')) %>% | |
| # bold diff. column text | |
| tab_style(style = cell_text(weight = 'bold'), location = cells_body(columns = diff)) %>% | |
| tab_options(heading.padding = 3, table.font.size = 15) %>% | |
| # adjust font, weight, and size of title, subtitle, and caption | |
| tab_style( | |
| locations = cells_title('title'), | |
| style = cell_text( | |
| font = google_font('Fira Sans'), | |
| weight = 600, | |
| size = px(19) | |
| ) | |
| ) %>% | |
| tab_style( | |
| locations = cells_title('subtitle'), | |
| style = cell_text( | |
| font = google_font('Fira Sans'), | |
| weight = 400, | |
| size = px(14) | |
| ) | |
| ) %>% | |
| tab_style( | |
| locations = cells_source_notes(), | |
| style = cell_text( | |
| font = google_font('Fira Sans'), | |
| weight = 400, | |
| size = px(12) | |
| ) | |
| ) %>% | |
| # adjust font, weight, size, alignment and force col. label to uppercase | |
| tab_style( | |
| locations = cells_column_labels(columns = everything()), | |
| style = cell_text( | |
| font = google_font('Fira Sans'), | |
| weight = 600, | |
| size = px(14), | |
| transform = 'uppercase', | |
| align = 'center' | |
| ) | |
| ) %>% | |
| # table font + weight | |
| opt_table_font( | |
| font = google_font('Fira Sans'), | |
| weight = 450 | |
| ) %>% | |
| # css to adjust padding | |
| opt_css( | |
| css = " | |
| #ap .gt_heading { | |
| padding-bottom: 0px; | |
| padding-top: 6px | |
| } | |
| #ap .gt_subtitle { | |
| padding-top: 2px; | |
| padding-bottom: 6px; | |
| } | |
| " | |
| ) %>% | |
| # save table | |
| gtsave_extra('ap_25_expected.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment