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
## HOW TO USE THIS ## | |
# Go to https://github.com/statsbomb/amf-open-data/tree/main#getting-started | |
# and download the zipped json files containing the individual seasons, available via AWS S3 | |
# | |
# Alternatively click the below links directly | |
# https://statsbomb-amf-open-data.s3.eu-west-2.amazonaws.com/tracking/SB_tracking_TB12DB_2021.zip | |
# https://statsbomb-amf-open-data.s3.eu-west-2.amazonaws.com/tracking/SB_tracking_TB12DB_2022.zip | |
# | |
# Unzip those files to a local directory. That directory will include deeply nested json files |
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
# Scrape nflmockdraftdatabase consensus board from | |
# https://www.nflmockdraftdatabase.com | |
load_nflmockdraftdatabase_consensus_board <- function(year){ | |
cli::cli_progress_step("Loading {.val {year}}. Please be patient, the parser takes a while.") | |
raw <- glue::glue("https://www.nflmockdraftdatabase.com/big-boards/{year}/consensus-big-board-{year}") |> | |
rvest::read_html() | |
mock_list <- raw |> | |
rvest::html_elements(xpath = "//*[@class='mock-list-item']") |
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, |
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
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
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) | |
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) | |
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(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(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( |
NewerOlder