Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / bin_FF_locations.R
Created May 7, 2021 12:50
Functions that bin four-seam pitch measurements and graph rates over the zone.
bin_FF_locations <- function(sc, plateX, plateZ){
# inputs:
# ------------------------------------
# sc - statcast data with variables plate_x,
# plate_z, description, stand, type, events
# plateX = c(plateX_lo, plateX_hi, width_X)
# plateZ = c(plateZ_lo, plateZ_hi, width_Z)
require(dplyr)
# compute number of FF pitches thrown to each side
@bayesball
bayesball / radial_plot.R
Last active April 11, 2021 11:27
R function to construct a Baseball Savant radial chart
radial_plot <- function(sc, title = ""){
# NOTE: the graphics file player.png should be
# in the same folder as this function
# But function will still work without an
# error if the graphics file is missing
# load in packages and turn off warnings
require(latex2exp)
require(ggplot2)
require(patchwork)
@bayesball
bayesball / TTTOwork.Rnd
Created March 23, 2021 18:16
Files for blog post Times Through the Order Effects
---
title: "TTTO"
author: "Jim Albert"
date: "3/23/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
@bayesball
bayesball / re_offspeed.R
Last active February 26, 2021 13:27
Slider Whiff Rates Work (two files)
re_offspeed <- function(sc, pside, PT, Nmin){
require(CalledStrike)
require(mgcv)
require(dplyr)
require(ggplot2)
require(gammit)
# collect number of offspeed pitches for each
# pitcher of that particular side
sc %>%
filter(p_throws == pside) %>%
@bayesball
bayesball / app.R
Last active February 5, 2021 12:51
Shiny app to illustrate brushing to obtain in-play batting averages
library(shiny)
library(ggplot2)
library(dplyr)
sc2019_ip <- read.table("https://raw.githubusercontent.com/bayesball/CalledStrike/master/data/sc2019_ip.txt",
header = TRUE)
ui <- fluidPage(
h2("Brushing In-Play Batting Averages"),
textInput("name", "Batter Name:", value = ""),
@bayesball
bayesball / pitch_value_work.R
Last active January 17, 2021 13:11
Compute pitch values for all pitches in a Statcast dataset and graph the smoothed values of pitch value using a filled contour graph
---
title: "Computing Pitch Values"
author: "Jim Albert"
date: "1/15/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
@bayesball
bayesball / setup_work.R
Created December 27, 2020 19:33
R function that provides new variables indicating if plate appearance goes through each possible count.
setup_work <- function(d2015){
# d2015 is a Retrosheet play by play dataset
# removes all non-pitches from PITCH_SEQ_TX
d2015$pseq <- gsub("[.>123N+*]", "", d2015$PITCH_SEQ_TX)
# create a b and s sequence
d2015$pseq <- gsub("[BIPV]", "b", d2015$pseq)
d2015$pseq <- gsub("[CFKLMOQRST]", "s", d2015$pseq)
one.string3 <- function(ex, count){
@bayesball
bayesball / called_strike_demo.R
Created November 27, 2020 15:35
R code for illustration of Called Strike package
# here are the packages I'm using
# CalledStrike depends on other packages
library(CalledStrike)
library(readr)
library(dplyr)
library(ggplot2)
# read in Statcast data for the 2019 season
@bayesball
bayesball / density_estimates.Rmd
Created November 20, 2020 23:36
Illustrate of bivariate density estimates on 2019 Statcast data
---
title: "Some Density Estimates"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
message = FALSE,
@bayesball
bayesball / pitch_selection.Rmd
Created October 31, 2020 12:19
Using tabyls() function to explore pitch selection in the 2020 World Series
---
title: "tabyls"
author: "Jim Albert"
date: "10/27/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,