Skip to content

Instantly share code, notes, and snippets.

Jim Albert bayesball

Block or report user

Report or block bayesball

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bayesball
bayesball / home_run_prediction.R
Created Jun 28, 2019
Prediction of 2019 MLB home run total at midseason (through games of June 27)
View home_run_prediction.R
# 2019 statcast data is in data frame sc2019
# collect number of home runs in each game
library(tidyverse)
sc2019 %>%
group_by(game_pk) %>%
summarize(HR = sum(events == "home_run",
na.rm = TRUE)) -> S
@bayesball
bayesball / clean_statcast.R
Created Jun 26, 2019
R code for "cleaning" Statcast data
View clean_statcast.R
# read in SC data from 2019 seasons
# through games of June 23, 2019
library(tidyverse)
library(CalledStrike)
sc2019 <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/statcast2019.csv")
sc2019_ip <- filter(sc2019, type == "X")
# focus on Statcast 2019 in-play data
@bayesball
bayesball / calledstrikework.R
Created Feb 8, 2019
R study of called balls and strikes -- uses CalledStrike package
View calledstrikework.R
# load some packages
library(baseballr)
library(tidyverse)
library(CalledStrike)
library(gridExtra)
# scrape data for four pitchers
Aaron <- scrape_statcast_savant(start_date = "2018-03-15",
@bayesball
bayesball / homerunvalue.R
Created Feb 3, 2019
Exploring values of home runs
View homerunvalue.R
# load in two packages
library(tidyverse)
library(WinProbability)
# assume dataset all2018.csv is in current working
# directory -- these functions compute the runs
# expectancies and WPA values
d2018 <- compute.runs.expectancy(2018)
@bayesball
bayesball / compute.win.probs.R
Last active Apr 22, 2019
Updated win probability functions
View compute.win.probs.R
compute.win.probs <- function(d, S){
# adds variables P.OLD, P.NEW, and WPA
# to retrosheet data with run expectancies
invlogit <- function(x) exp(x) / (1 + exp(x))
d %>%
mutate(half.inning.row = 2 * INN_CT + BAT_HOME_ID,
runs0 = ifelse(BAT_HOME_ID == 1,
HOME_SCORE_CT - AWAY_SCORE_CT + RUNS.STATE,
HOME_SCORE_CT - AWAY_SCORE_CT - RUNS.STATE)) -> d
@bayesball
bayesball / pitch_length.R
Created Jan 1, 2019
Explores impact of length of plate appearance using 2018 Retrosheet data
View pitch_length.R
# Load tidyverse packages and read in the Retrosheet data
library(tidyverse)
load("~/Dropbox/Google Drive/Retrosheet/pbp.2018.Rdata")
# if you have trouble getting the 2018 Retrosheet data, you can
# use several complete Retrosheet datasets from previous seasons
# see http://www-math.bgsu.edu/~albert/retrosheet/
# Create new variables: pseq containing only the pitch
@bayesball
bayesball / erode_work.R
Created Dec 15, 2018
Does Plate Discipline Erode
View erode_work.R
# read in Statcast data for two seasons
library(tidyverse)
sc <- read_csv("../StatcastData/statcast2018new.csv")
sc17 <- read_csv("../StatcastData/statcast2017.csv")
# erode function will complete individual regression estimates for all
# players who have seen 1000 called pitches
erode <- function(sc){
@bayesball
bayesball / distances2.R
Created Dec 2, 2018
Comparing distances on balls put into play for 2017 and 2018 seasons
View distances2.R
# read in packages and data
library(tidyverse)
library(gridExtra)
sc2018 <- read_csv("../StatcastData/statcast2018new.csv")
sc2017 <- read_csv("../StatcastData/statcast2017.csv")
sc2017ip <- filter(sc2017, type == "X") # 127555 rows
sc2018ip <- filter(sc2018, type == "X") # 126282 rows
@bayesball
bayesball / app.R
Created Nov 2, 2018
Shiny R code to compare home run pitch locations for two batters
View app.R
library(shiny)
library(tidyverse)
sc2018 <- read_csv("sc2018_ip.csv") %>%
mutate(Ptype = ifelse(pitch_type %in% c("FC", "FF", "FO", "FS", "FT", "SI"),
"fb", "os"))
sc2018 %>% group_by(player_name) %>% summarize(HR = sum(HR)) -> S
plist <- filter(S, HR >= 30) %>% select(player_name) %>% pluck()
ui = fluidPage(
titlePanel("Locations of 2018 Home Runs: Compare Two 30+ Players"),
@bayesball
bayesball / app.R
Created Oct 31, 2018
Shiny app for displaying baseball history
View app.R
library(shiny)
# Define UI for application that draws a histogram
ui <- shinyUI(pageWithSidebar(
# Application title
headerPanel("Offensive Production in Baseball History"),
# Sidebar with controls to select the variable to plot against year
You can’t perform that action at this time.