Skip to content

Instantly share code, notes, and snippets.

View tonyelhabr's full-sized avatar
🏠
Working from home

Tony ElHabr tonyelhabr

🏠
Working from home
View GitHub Profile
@tonyelhabr
tonyelhabr / gamestate-xgd.md
Last active June 16, 2024 04:04
The correct way to calculate xG difference by gamestate

Here's some fake data showing how shot logs might look for a soccer match.

library(tibble)
library(dplyr)

df <- tibble::tibble(
  shot_id = seq.int(1, 8),
  minute = c(7, 13, 25, 30, 41, 44, 58, 78), ## doesn't matter, just for illustrative purposes
@tonyelhabr
tonyelhabr / print-for-chunks.md
Last active April 6, 2024 14:34
Replicate reprex printing for arbitrary variables

This is useful if you simply want to copy-paste some code output without having to run code (or render/knit a whole document).

Method 1

## Based on internals of reprex:::reprex_impl
print_df_for_chunk <- function(code_string) {
  reprex_document_options <- list(
    venue = 'gh', 
@tonyelhabr
tonyelhabr / scrape-places.md
Last active March 11, 2024 13:14
Scraping Google Places API

Setup.

library(googleway)
library(dplyr)
library(tibble)

DATA_DIR <- "path/to/dir"
dir.create(DATA_DIR, showWarnings = FALSE, recursive = TRUE)
@tonyelhabr
tonyelhabr / scrape-big5-team-logos.md
Created January 26, 2024 13:59
Get Big 5 team logos from FBref
library(rvest)
library(tibble)

url <- 'https://fbref.com/en/comps/Big5/Big-5-European-Leagues-Stats'
page <- read_html(url)

team_elements <- page |> 
  html_elements('table') |> 
@tonyelhabr
tonyelhabr / delete-gha-runs.md
Last active July 9, 2024 00:14
Delete GitHub action run logs
library(gh)
library(purrr)
library(dplyr)

token <- Sys.getenv("GITHUB_PAT")
REPO <- "my-repo"
OWNER <- "me"

runs &lt;- gh::gh(
@tonyelhabr
tonyelhabr / shots.md
Created September 14, 2023 11:30
shot x-y data

Full set of fotmob shot releases here: https://github.com/JaseZiv/worldfootballR_data/releases/tag/fotmob_match_details

raw_shots <- readr::read_csv('https://github.com/JaseZiv/worldfootballR_data/releases/download/fotmob_match_details/47_match_details.csv')
#> Rows: 28667 Columns: 43
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (18): league_name, league_round_name, parent_league_season, match_time_u...
#> dbl (22): match_id, match_round, league_id, parent_league_id, home_team_id, ...
@tonyelhabr
tonyelhabr / pull-2023-fifa-womens-world-cup-xg.md
Created August 27, 2023 22:20
Pull xG data from StatsBomb and Opta (via FBRef) for the 2023 FIFA Women's World Cup

Raw data pull

library(StatsBombR)
library(worldfootballR)
library(dplyr)
library(janitor)
library(tibble)
@tonyelhabr
tonyelhabr / scrape-2023-fifa-womens-world-cup.md
Last active July 21, 2023 19:02
Scrape player stats for the 2023 FIFA Women's World Cup
library(httr)
library(tibble)
library(tidyr)
library(dplyr)
library(purrr)
library(janitor)
library(cli)
library(stringr)
@tonyelhabr
tonyelhabr / ff_scores.csv
Last active July 2, 2023 22:34
Scrape ESPN fantasy football league scores (2018/19-2022/23)
season week user_name opponent_user_name user_score opponent_score result
2018 1 Juan Avalos Manuel Espinosa 137.84 86.26 W
2018 1 Manuel Espinosa Juan Avalos 86.26 137.84 L
2018 1 Enrique Rodriguez Tracy Krohn 123.14 121.78 W
2018 1 Steven Valencia Juan Pineda 134.54 86.2 W
2018 1 Andrew ElHabr Andrew Lara 106.26 102.06 W
2018 1 Drake Hernandez Tony ElHabr 131.04 123.42 W
2018 1 Tony ElHabr Drake Hernandez 123.42 131.04 L
2018 1 Andrew Lara Andrew ElHabr 102.06 106.26 L
2018 1 Tracy Krohn Enrique Rodriguez 121.78 123.14 L
@tonyelhabr
tonyelhabr / overperformance.md
Last active May 30, 2023 12:21
Emperical bayes estimation of xG overperformance

This is my attempt to replicate the analysis by Laurie Shaw here.

Creating some fake data.

library(tibble)
library(purrr)
library(tidyr)
library(MASS)
library(ggplot2)