Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Dilday bdilday

View GitHub Profile
@bdilday
bdilday / grstand1.R
Created Aug 24, 2020
graphical mlb standings in R
View grstand1.R
library(dplyr)
library(ggplot2)
library(ggrepel)
library(rvest)
library(stringr)
fg_current_standings <- function() {
url = "https://www.fangraphs.com/depthcharts.aspx?position=Standings"
@bdilday
bdilday / ncaa_mbb_current_results_2019.csv
Last active Mar 30, 2019
ncaa march madness results 2019
View ncaa_mbb_current_results_2019.csv
game result
2019_1242_1318 1
2019_1266_1293 0
2019_1233_1314 0
2019_1330_1345 0
2019_1199_1436 1
2019_1276_1285 1
2019_1124_1393 1
2019_1279_1328 0
2019_1125_1396 1
@bdilday
bdilday / war_data.csv
Last active Sep 2, 2018
war_data.csv
View war_data.csv
We can't make this file beautiful and searchable because it's too large.
"playerID","yearID","WAA","WAR","WAR_def","WAR_off","POS"
"aaronha01",1954,-0.61,1.27,-0.71,1.98,"OF"
"aaronha01",1955,3.77,6.25,-0.01,6.26,"OF"
"aaronha01",1956,4.63,7.18,0.65,6.53,"OF"
"aaronha01",1957,5.5,7.97,-0.13,8.1,"OF"
"aaronha01",1958,4.84,7.32,0.21,7.11,"OF"
"aaronha01",1959,6.13,8.64,-1.08,9.72,"OF"
"aaronha01",1960,5.47,7.98,0.77,7.21,"OF"
"aaronha01",1961,6.86,9.4,2.02,7.38,"OF"
@bdilday
bdilday / catcher_defense_data.csv
Last active Sep 1, 2018
catcher_defense_data.csv
View catcher_defense_data.csv
We can't make this file beautiful and searchable because it's too large.
"game_id","year_id","bat_home_id","off_score","def_pitcher","def_catcher","off_team","def_team","park_id","park"
"WS2196109210",1961,0,3,"danib102_1961","retzk101_1961","MIN_1961","WS2_1961","WS2_1961","WS2"
"WS2196109190",1961,0,2,"mcclj104_1961","retzk101_1961","MIN_1961","WS2_1961","WS2_1961","WS2"
"WS2196109150",1961,0,1,"danib102_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
"WS2196109130",1961,0,1,"hobae101_1961","retzk101_1961","BOS_1961","WS2_1961","WS2_1961","WS2"
"WS2196109120",1961,0,0,"burnp102_1961","retzk101_1961","BOS_1961","WS2_1961","WS2_1961","WS2"
"WS2196109032",1961,0,2,"gablg102_1961","dalep101_1961","CHA_1961","WS2_1961","WS2_1961","WS2"
"WS2196109170",1961,0,2,"maesh101_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
"WS2196109160",1961,0,0,"burnp102_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
View mlb_stadia_paths.csv
team x y segment
angels 26.27 104.81 outfield_outer
angels 25.932060362453754 98.80779459065957 outfield_outer
angels 26.551757956401122 92.8655601538784 outfield_outer
angels 29.069970572128522 87.4573503249933 outfield_outer
angels 32.986155173212474 82.91330959433427 outfield_outer
angels 37.08065184258191 78.5115488314632 outfield_outer
angels 41.202800272760506 74.13565416464868 outfield_outer
angels 45.356441744254404 69.7896729842234 outfield_outer
angels 49.49117542835149 65.42575404594743 outfield_outer
View statcast_impute_derivation.R
library(baseballr)
library(dplyr)
obtain_data = function(type="postgres",
start_date="2017-03-29",
end_date="2017-10-03", infile=NULL) {
if (type=="postgres") {
# postgres db connection here
@bdilday
bdilday / scrape_statcast_expected_stats.R
Last active May 17, 2018
scrape statcast expected stats
View scrape_statcast_expected_stats.R
library(dplyr)
library(ggplot2)
library(rvest)
library(jsonlite)
scrape_statcast_expected_stats = function(year=2018, min_pa=25) {
url = sprintf("https://baseballsavant.mlb.com/expected_statistics?type=batter&year=%s&position=&team=&min=%d", year, min_pa)
h = xml2::read_html(url)
s = html_nodes(h, "script")[[10]]
@bdilday
bdilday / top10warfranch.R
Last active Apr 15, 2018
R graph - top10 war by franchise
View top10warfranch.R
library(readr)
library(ggplot2)
library(dplyr)
library(Lahman)
br_war = read_csv("https://www.baseball-reference.com/data/war_daily_bat.txt")
m = Lahman::Teams
ndf = br_war %>% merge(m %>% select(teamIDBR, yearID, name, franchID), by.x=c("team_ID", "year_ID"), by.y=c("teamIDBR", "yearID")) %>% group_by(franchID, name, player_ID) %>% summarise(w=sum(as.numeric(WAR), na.rm=TRUE))
xx = ndf %>% arrange(franchID, -w) %>% mutate(i=row_number()) %>% group_by(franchID, name) %>% summarise(m=max(i)) %>% arrange(franchID,-m) %>% mutate(ir=row_number()) %>% group_by(franchID) %>% mutate(m2=max(m), m=sum(m)) %>% filter(ir==1) %>% select(franchID, m=m2)
p = ndf %>% merge(xx, by="franchID") %>% group_by(franchID) %>% arrange(-w) %>% mutate(i=row_number()) %>% filter(i<=10) %>% filter(m>=110) %>% ggplot(aes(x=i, y=w)) + geom_bar(stat='identity') + facet_wrap(~franchID) + theme_minimal() + labs(x='franchise rank', y='career WAR')
@bdilday
bdilday / trajectory_calculator1.R
Last active Feb 20, 2018
trajectory calculator
View trajectory_calculator1.R
trajectory_pars <- list(
# constants
mass = 5.125, # oz,
circumference = 9.125, # in
beta = 1.217e-4, # 1 / meter
cd0 = 0.3008,
cdspin = 0.0292,
cl0 = 0.583,
cl1 = 2.333,
@bdilday
bdilday / retrosheet_events_2016.csv
Last active May 28, 2017
sample retrosheet data
View retrosheet_events_2016.csv
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 1.
"GAME_ID","YEAR_ID","AWAY_TEAM_ID","INN_CT","BAT_HOME_ID","OUTS_CT","BALLS_CT","STRIKES_CT","PITCH_SEQ_TX","AWAY_SCORE_CT","HOME_SCORE_CT","BAT_ID","BAT_HAND_CD","RESP_BAT_ID","BAT_ON_DECK_ID","BAT_IN_HOLD_ID","RESP_BAT_HAND_CD","PIT_ID","PIT_HAND_CD","RESP_PIT_ID","RESP_PIT_HAND_CD","POS2_FLD_ID","POS3_FLD_ID","POS4_FLD_ID","POS5_FLD_ID","POS6_FLD_ID","POS7_FLD_ID","POS8_FLD_ID","POS9_FLD_ID","BASE1_RUN_ID","BASE2_RUN_ID","BASE3_RUN_ID","EVENT_TX","LEADOFF_FL","PH_FL","BAT_FLD_CD","BAT_LINEUP_ID","EVENT_CD","BAT_EVENT_FL","AB_FL","H_CD","SH_FL","SF_FL","EVENT_OUTS_CT","DP_FL","TP_FL","RBI_CT","WP_FL","PB_FL","FLD_CD","BATTEDBALL_CD","BUNT_FL","FOUL_FL","BATTEDBALL_LOC_TX","ERR_CT","ERR1_FLD_CD","ERR1_CD","ERR2_FLD_CD","ERR2_CD","ERR3_FLD_CD","ERR3_CD","BAT_DEST_ID","RUN1_DEST_ID","RUN2_DEST_ID","RUN3_DEST_ID","BAT_PLAY_TX","RUN1_PLAY_TX","RUN2_PLAY_TX","RUN3_PLAY_TX","RUN1_SB_FL","RUN2_SB_FL","RUN3_SB_FL","RUN1_CS_FL","RUN2_CS_FL","RUN3_CS_FL","RUN1_PK_FL","RUN2_PK_FL","RUN3_PK_FL","RUN1_RESP_PIT_ID","RUN2_R