Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / compute_expected_runs.R
Created October 10, 2024 01:41
R function to compute expected runs for different bases/outs states from event table from statsapi.mlb.com
compute_expected_runs <- function(events){
# function computes expected runs for event table
# obtained via statsapi.mlb.com
require(dplyr)
# find the total runs scored and number of outs each half-inning
events |>
group_by(game_id, inning, half_inning) |>
summarize(max_runs = sum(runs_on_event),
total_outs = sum(post_outs - pre_outs),
@bayesball
bayesball / get_standings.R
Created October 3, 2024 11:13
R function to obtain MLB standings for a particular season between two dates
get_standings <- function(s_date, e_date){
require(sabRmetrics)
require(dplyr)
Teams <- c("Chicago White Sox", "Cleveland Guardians",
"Detroit Tigers", "Kansas City Royals",
"Minnesota Twins", "Baltimore Orioles",
"Boston Red Sox", "New York Yankees",
"Tampa Bay Rays", "Toronto Blue Jays",
"Houston Astros", "Los Angeles Angels",
@bayesball
bayesball / fit_norm_mix_model.R
Created July 28, 2024 14:55
R function for fitting a normal mixture model to exit velocities for balls in play for a specific baseball hitter.
fit_norm_mix_model <- function(pname, statcast){
require(dplyr)
require(LearnBayes)
library(magrittr)
logpost <- function(theta, y){
mu1 <- theta[1]
sigma1 <- exp(theta[2])
mu2 <- theta[3]
sigma2 <- exp(theta[4])
p <- exp(theta[5]) / (1 + exp(theta[5]))
@bayesball
bayesball / prediction_normal.R
Last active July 18, 2024 14:44
R function for implementing posterior predictive simulation of individual home run counts
prediction_normal <- function(S, games, f_games){
# data frame S contains batter, BIP, HR for
# first part of season
# games - average number of games already played
# f_games - number of future games played
# need to have the JAGS software installed
# https://mcmc-jags.sourceforge.io/
library(readr)
@bayesball
bayesball / Count_Effect_JAGS_final.qmd
Last active July 10, 2024 15:19
Quarto file for Bayesian fitting of a random effects model for balls in play data with a bias component for the count.
---
title: "Count Effects Model"
format: html
editor: visual
---
Illustration of Bayesian fitting of a multilevel model for situational data using JAGS software.
Read in Statcast data from the first half of the 2023 season.
Reference: Albert, Jim (2024). Bayesian Workflow of a Situational Random Effects Model
@bayesball
bayesball / predicting_hr_glm_gam.R
Created April 6, 2024 15:29
Illustration of two algorithms for predicting home runs from distance and spray angle
# load some packages
library(dplyr)
library(ggplot2)
library(mgcv)
library(janitor)
library(metR)
# have Statcast data through games of April 5, 2024
@bayesball
bayesball / count_two_seasons.qmd
Created March 26, 2024 19:43
Quarto file that collects retrosheet data and compares count rates for two seasons
---
title: "Retrosheet Package - Comparing Count Rates for Two Seasons"
format: html
editor: visual
---
Load packages for this particular run.
```{r}
#| message: FALSE
library(abdwr3edata)
@bayesball
bayesball / app.R
Created November 15, 2023 12:47
Shiny app that uses the mlbplotR package to produce scatterplots with team logo points
# live version of Shiny app
# https://bayesball.shinyapps.io/fg2023_discipline/
library(shiny)
library(mlbplotR)
library(dplyr)
library(ggplot2)
library(readr)
# data work
@bayesball
bayesball / Wheeler_Work.qmd
Created October 23, 2023 14:58
Quarto document showing work for Zack Wheeler 2023 NLCS work
---
title: "Zach Wheeler 2023 NCS"
format: html
editor: visual
---
Load in necessary packages:
```{r}
library(baseballr)
@bayesball
bayesball / player_function_lb.R
Last active May 20, 2023 18:28
R function to fit a multilevel quadratic smoothing model to season-to-season AVG data for any player of interest.
player_function_lb <- function(player_id){
# uses LearnBayes package to simulate from
# multilevel model
library(dplyr)
library(ggplot2)
library(Lahman)
library(LearnBayes)