Skip to content

Instantly share code, notes, and snippets.

View mrcaseb's full-sized avatar

mrcaseb

  • Munich, Germany
  • 09:53 (UTC +01:00)
  • X @mrcaseb
View GitHub Profile
@mrcaseb
mrcaseb / function_get_espn_wp_college.R
Created April 15, 2020 18:47
Function for scraping win probability data from the ESPN API. Outputs game_id, play_id and home_wp and should therefore be joinable to cfbscrapR data.
library(dplyr)
get_espn_wp_college <- function(espn_game_id) {
espn_wp <- data.frame()
tryCatch(
expr = {
espn_wp <-
httr::GET(url = glue::glue("http://site.api.espn.com/apis/site/v2/sports/football/college-football/summary?event={espn_game_id}")) %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON(flatten = TRUE) %>%
purrr::pluck("winprobability") %>%
@mrcaseb
mrcaseb / weekly_dakota.R
Created March 2, 2021 16:03
Code to create a plot of weekly dakota smoothed for selected QBs by @mrcaseb
library(tidyverse)
# Lee Sharpe's brightness function ----------------------------------------
brightness <- function(hex) {
result <- rep(0, length(hex))
for (i in 2:7)
{
ch <- substr(hex, i, i)
result <- result + ifelse(i %% 2 == 0, 16, 1) * case_when(
@mrcaseb
mrcaseb / gt_table_align_decimal_separator.R
Last active November 11, 2022 20:55
A suggestion how to align a column in a gt table to the decimal separator without monospace fonts
library(magrittr)
df <- tibble::tibble(
var_a = c("a", "b", "c", "d"),
var_b = c(1.234, 12.34, 123.4, 1234)
)
num_to_html <- function(num, separator = ".") {
stringr::str_split_fixed(num, stringr::fixed(separator), n = 2) %>%
as.data.frame() %>%
@mrcaseb
mrcaseb / special_teams_epa.R
Created November 22, 2021 15:52
How to compute special teams epa split by kicking and receiving
library(dplyr)
st_plays <- nflreadr::load_pbp(2021) |>
filter(!is.na(epa) & touchback != 1 & special == 1)
epa_kicking <- st_plays |>
group_by(team = posteam) |>
mutate(epa = dplyr::if_else(play_type == "kickoff", -epa, epa)) |>
summarize(epa_kick = mean(epa), plays_kick = n())
library(tidyverse)
library(nflverse)
#> ── Attaching packages ─────────────────────────────────── nflverse 1.0.1.9000 ──
#> ✓ nflfastR 4.3.0.9008 ✓ nflreadr 1.1.3
#> ✓ nflseedR 1.0.2.9001 ✓ nflplotR 1.0.0.9000
#> ✓ nfl4th 1.0.1.9000
#> ──────────────────────────────────────────────────────────────── Ready to go! ──
options(dplyr.summarise.inform = FALSE)
options(nflreadr.verbose = FALSE)
s <- nflreadr::load_schedules(TRUE)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
preds <- nflreadr::csv_from_url("https://raw.githubusercontent.com/nflverse/nfldata/master/data/predictions.csv")
g <- nflreadr::load_schedules(2021)
points <- preds |>
filter(prediction != 50) |>
left_join(g |> select(game_id, week, result), by = "game_id") |>
library(tidyverse)
drafts <- nflreadr::load_draft_picks()
draft_order <- rvest::read_html("https://en.wikipedia.org/wiki/2022_NFL_Draft") |>
rvest::html_table() |>
purrr::pluck(5) |>
janitor::clean_names() |>
select(round = rnd, pick = pick_no, team = nfl_team, notes) |>
mutate(across(c(round, pick), as.numeric)) |>
#' Remove Bookmaker’s Vig from a Vector of Probablities
#' @description This function implements the iterative power method to adjust
#' implied probabilities suggested by Vovk and Zhadanov (2009) and Clarke (2016),
#' where bookmakers’ implied probabilities are raised to a fixed power, which
#' never produces bookmaker or fair probabilities outside the 0-1 range
#' (upper limit can be changed) and allows for the favorite long-shot bias.
#' @param probs A vector of probabilities from which to remove the bookmaker’s
#' vig. The probabilities will be adjusted to sum up to `sum_to`.
@mrcaseb
mrcaseb / load_538_games.R
Created October 4, 2022 18:28
Load historical game data from 538
load_538_games <- function(seasons = "SB_ERA"){
s <- nflreadr::csv_from_url("https://projects.fivethirtyeight.com/nfl-api/nfl_elo.csv") |>
tibble::as_tibble() |>
dplyr::na_if("") |>
dplyr::select(
season,
season_type = playoff,
away_team = team2,
away_score = score2,
home_score = score1,