Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / two_astros.R
Created February 4, 2023 21:41
R script for comparing two Astros pitchers post
# load in required packages
library(dplyr)
library(ggplot2)
library(CalledStrike)
library(janitor)
library(ShinyBaseball)
library(readr)
# collect mlb ids from two pitchers
@bayesball
bayesball / RunMe.R
Created November 13, 2022 22:58
R code to fit nonnested multilevel model to compare the roles of offense and defense in baseball run scoring
# load in required packages
library(purrr)
library(ggplot2)
library(tidyr)
library(dplyr)
# read in modeling functions
source("fit_model.R")
@bayesball
bayesball / app.R
Last active November 8, 2022 21:08
Shiny app to compare career trajectories of HOF candidates with contemporary players already in the HOF.
library(dplyr)
library(ggplot2)
library(geomtextpath)
library(readr)
# datasets are read from a Github respository
fg_batting <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/fgbatting_complete.csv")
hof <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata.csv")
hof_candidates <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata_candidates.csv")
hof_cand_batting <- filter(hof_candidates,
@bayesball
bayesball / estimate_batting.R
Last active November 5, 2022 19:43
R work for multinomial post -- main file is multinomial_setup.R
estimate_batting <- function(retro_final_PA_1990_2020d,
season,
s_woba = 0.5){
require(dplyr)
require(LearnBayes)
retro_final_PA_1990_2020d %>%
filter(YEAR == season) -> retroseason
retroseason %>%
group_by(BAT_ID) %>%
summarize(PA = n(),
@bayesball
bayesball / get_hr_data.R
Last active September 26, 2022 00:43
R work for the Predicting Home Runs using a Multilevel Model post
get_hr_data <- function(pred_season,
retro_data,
n_prev_seasons = 4,
mPA = 1000,
mPA_season = 200){
# n_prev_seasons is number of previous seasons
# mPA is the minimum number of cumulative PA
# retrodata - Retrosheet data for current season
# mPA_season - minimum number of PA in both
@bayesball
bayesball / prediction_work.R
Created September 16, 2022 12:29
R function to compare five methods in predicting future home run rates
prediction_work <- function(seasons,
mPA = 1000,
retrodata,
mPA_season = 200){
# seasons is a vector of previous seasons
# mPA is the minimum number of cumulative PA
# retrodata - Retrosheet data for current season
# mPA_season - minimum number of PA in both
# halves of current season
@bayesball
bayesball / sac_fly_work.R
Created August 19, 2022 20:04
R function to implement computations for sacrifice flies blog post
sac_fly_work <- function(sc, season){
# load required packages
require(dplyr)
require(ggplot2)
require(metR)
require(mgcv)
require(CalledStrike)
# define location, distance, and spray angle vars
@bayesball
bayesball / trout_ofer_work.R
Created June 6, 2022 13:48
R script to compute and graph lengths of ofers for Mike Trout
# load in Retrosheet pbp data frames
# for seasons 2011 through 2021
library(Lahman)
library(dplyr)
library(ggplot2)
# get Mike Trout's retro id from the People
# data frame in Lahman package
@bayesball
bayesball / data_work.R
Last active May 7, 2022 01:53
R code to measure the change in wOBA weights from one season to a second season
data_work <- function(){
require(readr)
require(dplyr)
require(lubridate)
sc_2021 <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/statcast2021.csv")
sc_2022 <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/statcast_2022.csv")
sc_old <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/SC_BB_mini.csv")
names(sc_old)[2] <- "Game_Date"
@bayesball
bayesball / barrels_script.R
Last active April 29, 2022 17:29
Graph of barrel region and GAM fits using 2021 Statcast data
# using Statcast data for the 2021 season
# two functions plot_xb_contour_work() and
# plot_woba_contour_work() implement the
# logistic and ordinal fitting algorithms
# ggplot2 code is used to show the barrel
# region over the (launch speed, launch angle)
# space