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 / xgot.md
Last active March 29, 2022 00:39
Difference in xGOT
library(readr)
library(ggplot2)
library(broom)

df <- 'penalty.csv' %>%
  read_csv() %>% 
  transmute(
    xgot = expectedGoalsOnTarget,
@tonyelhabr
tonyelhabr / example.md
Created March 29, 2022 14:21
Standardized Opta Goal Mouth Coordinates
library(readr)
library(dplyr)
library(devtools)
shots <- read_csv('shots_1549727.csv')
glimpse(shots)
#> Rows: 22
#> Columns: 14
#> $ season_id         <dbl> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021~
@tonyelhabr
tonyelhabr / qs_from_url.R
Last active June 3, 2022 01:02
Read .qs file from a URL
qs_from_url <- function(url){
load <- try(curl::curl_fetch_memory(url), silent = TRUE)
if (inherits(load, "try-error")) {
cli::cli_warn("Failed to retrieve data from {.url {url}}")
return(data.frame())
}
content <- try(qs::qdeserialize(load$content), silent = TRUE)
@tonyelhabr
tonyelhabr / big5_fotmob_match_details.csv
Created July 10, 2022 22:30
Big 5 2020/21 - 2021/22 Fotmob match details
We can't make this file beautiful and searchable because it's too large.
match_id,match_round,league_id,league_name,league_round_name,parent_league_id,parent_league_season,match_time_utc,home_team_id,home_team,home_team_color,away_team_id,away_team,away_team_color,id,event_type,team_id,player_id,player_name,x,y,min,min_added,is_blocked,is_on_target,goal_crossed_y,expected_goals,expected_goals_on_target,shot_type,situation,period,is_own_goal,on_goal_shot_x,on_goal_shot_y,on_goal_shot_zoom_ratio,first_name,last_name,team_color,short_name,blocked_x,blocked_y,goal_crossed_z
3411352,1,47,Premier League,Premier League Round 1,47,2022/2023,"Sat, Sep 12, 2020, 11:30 UTC",9879,Fulham,#000000,9825,Arsenal,#bd0510,2210246139,AttemptSaved,9879,149150,Denis Odoi,84.7373786408,18.254012473628215,7,,FALSE,TRUE,31.559999999999995,0.0274,0.115,LeftFoot,RegularPlay,FirstHalf,FALSE,1.6455026455026467,0.29047618904761907,1,Denis,Odoi,#000000,,,,
3411352,1,47,Premier League,Premier League Round 1,47,2022/2023,"Sat, Sep 12, 2020, 11:30 UTC",9879,Fulham,#000000,9825,Arsenal,#bd0510,2210246547,Miss,9825,
@tonyelhabr
tonyelhabr / t_test.md
Last active August 15, 2022 13:14
Example nationality resids difference in means calculation
library(tidyverse)
library(infer)

## Generate some fake data that is similar to your actual data
set.seed(1)
resids <- tibble(
  nationality = c(rep('English', 60), rep('non-English', 100), rep('African', 40)),
  .resid = c(
    rnorm(60, sd = 2) + runif(60, 0, 0.5),
@tonyelhabr
tonyelhabr / match_and_model.md
Last active August 23, 2022 12:16
Nationality bias analysis
library(tidyverse)
library(infer) ## for t-tests
library(ranger) ## for random forest

combined <- read_csv('combined.csv') |> 
  ## drop the unnamed column
  select(-1)
@tonyelhabr
tonyelhabr / ucl-2022.R
Created September 8, 2022 22:28
2022 UCL Final from Fotmob
library(dplyr)
library(worldfootballR)
match <- fotmob_get_matches_by_date('2022-05-28') |> filter(name == 'Champions League Final Stage')
match_details <- fotmob_get_match_details(match$match_id)
match_details |> select(id, home_team, home_team_id, away_team, away_team_id, team_id, min, min_added, xg = expected_goals)
@tonyelhabr
tonyelhabr / corners.md
Created September 20, 2022 14:29
get corners for a match with worldfootballR

hey jon 👋

library(dplyr)
library(worldfootballR)

match_team_stats <- fotmob_get_match_team_stats(3609994)
match_team_stats |> 
  filter(stats_title == 'Corners') |> 
  glimpse()
#> Rows: 1
@tonyelhabr
tonyelhabr / fotmob_shots_by_season.md
Created October 17, 2022 16:59
Fotmob shots for top scorers in the 2022/23 EPL season
suppressPackageStartupMessages({
  library(worldfootballR)
  library(dplyr)
  library(lubridate)
})

## Need the very most recent version of the package for `load_fotmob_match_details()` to work.
packageVersion("worldfootballR")
@tonyelhabr
tonyelhabr / cdc-scraper.md
Last active November 19, 2022 23:38
cdc get rekt

Heart Disease and Stroke

For example, this.

library(httr)
library(tibble)
library(tidyr)
library(dplyr)