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
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") %>% |
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
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( |
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
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() %>% |
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
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()) |
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
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) |
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
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") |> |
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
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)) |> |
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
#' 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`. |
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_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, |
OlderNewer