Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created November 27, 2020 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/247da8335883130e22c471d6f981b6ee to your computer and use it in GitHub Desktop.
Save bayesball/247da8335883130e22c471d6f981b6ee to your computer and use it in GitHub Desktop.
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
statcast2019 <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/statcast2019.csv")
# DJ LeMahieu example
dj <- filter(statcast2019,
player_name == "DJ LeMahieu")
# scatterplot of in-play locations with hits
# indicated by color variable
dj %>%
setup_inplay() %>%
ggplot(aes(plate_x, plate_z,
color = H)) +
geom_point() +
add_zone() +
increasefont() +
coord_fixed()
# smoothed hit probabilities over zone
hit_contour(dj,
L = seq(0, 1, by = 0.02),
title = "2019 LeMahieu Probability of Hit")
# Swing and contact part
statcast2019 <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/statcast2019.csv")
# pick up FanGraph batting leaders for 2019 season
fg <- fg_bat_leaders(2019, 2019)
# identify low and high swingers
ggplot(fg, aes(Swing_pct_pi, Contact_pct_pi,
label = Name)) +
geom_point(size = 3, color = "blue") +
geom_text(data = filter(fg,
Swing_pct_pi < 37 |
Swing_pct_pi > 57),
nudge_y = -0.6, color = "red") +
geom_point(data = filter(fg,
Swing_pct_pi < 37 |
Swing_pct_pi > 57),
color = "red", size = 3) +
xlim(30, 62) +
ggtitle("2019 Swing and Contact Fractions") +
centertitle() +
increasefont()
low_swingers <- filter(fg, Swing_pct_pi < 37) %>%
pull(Name)
high_swingers <- filter(fg, Swing_pct_pi > 57) %>%
pull(Name)
swing <- c(low_swingers, high_swingers)
# compare nine players on probability of swing
sc2 <- filter(statcast2019,
player_name %in% swing)
swing_contour(split(sc2,
sc2$player_name),
L = seq(0, 1, by = 0.02),
NCOL = 3)
# look at Mike Trout
# break down by side of pitcher
statcast2019 %>%
filter(player_name == "Mike Trout") %>%
split_LR() %>%
swing_contour(L = seq(0, 1, by = 0.05),
title="Mike Trout Swing Rate")
# break down by type of pitch
statcast2019 %>%
filter(player_name == "Mike Trout") %>%
split_pitchtype() %>%
swing_contour(L = seq(0, 1, by = 0.05),
title="Mike Trout Swing Rate")
# break down by pitch count
statcast2019 %>%
filter(player_name == "Mike Trout") %>%
split_count() %>%
swing_contour(L = seq(0, 1, by = 0.05),
title="Mike Trout Swing Rate")
# compare with Jeff McNeil
statcast2019 %>%
filter(player_name == "Jeff McNeil") %>%
split_count() %>%
swing_contour(L = seq(0, 1, by = 0.05),
title="Jeff McNeil Swing Rate")
# look at contact rates
ggplot(fg, aes(Swing_pct_pi, Contact_pct_pi,
label = Name)) +
geom_point(size = 3, color = "blue") +
geom_text(data = filter(fg,
Contact_pct_pi < 67.5 |
Contact_pct_pi > 86.9),
nudge_y = -0.6, color = "red") +
geom_point(data = filter(fg,
Contact_pct_pi < 67.5 |
Contact_pct_pi > 86.9),
color = "red", size = 3) +
xlim(30, 62) +
ggtitle("2019 Swing and Contact Fractions") +
centertitle() +
increasefont()
low_connect <- filter(fg, Contact_pct_pi < 67.5) %>%
pull(Name)
high_connect <- filter(fg, Contact_pct_pi > 86.9) %>% pull(Name)
connect <- c(low_connect, high_connect)
# plots of smoothed contact probabilities
sc1 <- filter(statcast2019,
player_name %in% connect)
contact_swing_contour(split(sc1,
sc1$player_name),
L = seq(0, 1, by = 0.02),
NCOL = 3)
# Luke Voit -- how does eontact prob depend on pitch
# type?
filter(statcast2019,
player_name == "Luke Voit") %>%
split_pitchtype() %>%
contact_swing_contour(L = seq(0, 1, by = 0.05),
title =
"Luke Voit - Prob(Contact)")
# depend on pitcher side?
filter(statcast2019,
player_name == "Luke Voit") %>%
split_LR() %>%
contact_swing_contour(L = seq(0, 1, by = 0.05),
title = "Luke Voit - Prob(Contact)")
# depend on both pitcher side and pitch type?
filter(statcast2019,
player_name == "Luke Voit") %>%
split_LR_pitchtype() %>%
contact_swing_contour(L = seq(0, 1, by = 0.05),
title =
"Luke Voit - Prob(Contact)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment