Skip to content

Instantly share code, notes, and snippets.

Avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / app.R
Created May 14, 2021
sample shiny app
View app.R
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(
@bayesball
bayesball / bin_FF_locations.R
Created May 7, 2021
Functions that bin four-seam pitch measurements and graph rates over the zone.
View bin_FF_locations.R
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 Apr 11, 2021
R function to construct a Baseball Savant radial chart
View radial_plot.R
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 Mar 23, 2021
Files for blog post Times Through the Order Effects
View TTTOwork.Rnd
---
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 Feb 26, 2021
Slider Whiff Rates Work (two files)
View re_offspeed.R
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 Feb 5, 2021
Shiny app to illustrate brushing to obtain in-play batting averages
View app.R
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 Jan 17, 2021
Compute pitch values for all pitches in a Statcast dataset and graph the smoothed values of pitch value using a filled contour graph
View pitch_value_work.R
---
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 Dec 27, 2020
R function that provides new variables indicating if plate appearance goes through each possible count.
View setup_work.R
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 Nov 27, 2020
R code for illustration of Called Strike package
View called_strike_demo.R
# 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 Nov 20, 2020
Illustrate of bivariate density estimates on 2019 Statcast data
View density_estimates.Rmd
---
title: "Some Density Estimates"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
message = FALSE,