Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@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
@bayesball
bayesball / 3000hitclub.R
Created April 24, 2022 17:44
R script for 3000 hitting club exploration
# read in three packages
library(readr)
library(dplyr)
library(ggplot2)
# helper ggplot2 function
helper_ggplot <- function(){
theme(text=element_text(size=18)) +
@bayesball
bayesball / swingmiss2018.R
Created October 25, 2018 01:51
Exploring 2018 swing and miss rates
# Load in the necessary packages
library(tidyverse)
library(mgcv)
library(ggrepel)
library(gridExtra)
# Read in the statcast data for the 2018 season
d <- read_csv("../Statcastdata/statcast2018new.csv")
@bayesball
bayesball / nonnested_fit_lme4.R
Created March 16, 2022 15:38
Fit of a nonnested multilevel model using the lme4 package
# load in packages
library(readr)
library(dplyr)
library(lme4)
library(ggplot2)
library(posterior)
library(gridExtra)
library(Lahman)
@bayesball
bayesball / nonnested_fit.R
Last active March 16, 2022 15:03
Fit of a nonnested multilevel model to wOBA data for the 2021 baseball season
# load in packages
library(readr)
library(dplyr)
library(brms)
library(ggplot2)
library(posterior)
library(gridExtra)
library(Lahman)
@bayesball
bayesball / parse.retrosheet2.pbp.R
Created February 9, 2014 01:50
R function for downloading, upzipping, and appending Retrosheet play-by-play data
parse.retrosheet2.pbp = function(season){
# ADJUSTED FOR MAC -- function will work for WINDOWS and MAC
# download, unzip, append retrosheet data
# assume current directory has a folder download.folder
# download.folder has two subfolders unzipped and zipped
# program cwevent.exe is in unzipped folder (for windows)
download.retrosheet <- function(season){
# get zip file from retrosheet website
download.file(
@bayesball
bayesball / fg_data_prep.R
Created January 16, 2022 15:52
R script to add variables to FanGraphs leaderboard data
# preparation work for FanGraphs leaderboard data
# collected FanGraphs Leaderboard batting data
# choose Multiple Seasons (all), Split Seasons
# minimum 100 PA in each season
# download data and saved as "fgbatting.csv"
library(dplyr)
library(readr)
library(Lahman)
@bayesball
bayesball / app.R
Created December 5, 2021 18:11
Shiny app to display in-play and hit rate data over Retrosheet field locations
library(shiny)
library(ggplot2)
library(dplyr)
library(readr)
# read in Retrosheet data for two seasons
d <- read_csv("retro_2seasons_ip.csv")
# main drawing function
@bayesball
bayesball / AVG_ground_balls.R
Created November 2, 2021 16:11
Markdown file to compute random effects estimates of team defenses on ground balls.
---
title: "AVG on Ground Balls"
author: "Jim Albert"
date: "11/2/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
message = FALSE,