Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@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 / 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 / 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,
@bayesball
bayesball / perfect.R
Created October 26, 2021 19:18
Function for finding all perfect-k games from Retrosheet data for a particular season
perfect <- function(d, n_innings){
# inputs are Retrosheet play-by-play data frame d and
# number of innings n_innings
# output is a data frame containing all Perfect-k
# games for that season
require(dplyr)
d %>%
mutate(PIT_ID = as.character(PIT_ID),
@bayesball
bayesball / home_run_update.R
Created August 30, 2021 22:48
R script to produce graphs for blog post "Home Run Update: August 31"
# required packages
library(dplyr)
library(ggplot2)
library(readr)
library(lubridate)
library(stringr)
# some helper functions
@bayesball
bayesball / retro_streakwork.R
Last active July 10, 2021 13:38
R function to extract streaks from Retrosheet data for a particular season
retro_streakwork <- function(retrodata){
find_streaks_retro <- function(playerid, retrodata){
require(lubridate)
require(dplyr)
require(purrr)
retrodata %>%
mutate(Date = ymd(substr(GAME_ID, 4, 11)),
Game = as.numeric(substr(GAME_ID, 12, 12))) %>%
filter(BAT_ID == playerid,
AB_FL == TRUE) %>%
@bayesball
bayesball / app.R
Created May 14, 2021 13:17
sample shiny app
library(shiny)
library(readr)
library(dplyr)
library(ggplot2)
sc_pitcher_2019 <- read_delim(
"https://raw.githubusercontent.com/bayesball/ShinyBaseball/main/data/sc_pitcher_2019.txt",
delim = " ")
ui <- fluidPage(