Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created February 4, 2023 21:41
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/cb92678f740bb1d543ba46fa611792cf to your computer and use it in GitHub Desktop.
Save bayesball/cb92678f740bb1d543ba46fa611792cf to your computer and use it in GitHub Desktop.
R script for comparing two Astros pitchers post
# load in required packages
library(dplyr)
library(ggplot2)
library(CalledStrike)
library(janitor)
library(ShinyBaseball)
library(readr)
# collect mlb ids from two pitchers
chadwick %>%
filter(name_first == "Cristian",
name_last == "Javier") %>%
pull(key_mlbam) -> cj_id
chadwick %>%
filter(name_first == "Framber",
name_last == "Valdez") %>%
pull(key_mlbam) -> fv_id
# read in 2022 Statcast data
statcast2022 <- read_csv("~/Dropbox/STATCAST/data/statcast2022.csv")
# collect data from two pitchers
statcast2022 %>%
filter(pitcher %in% c(cj_id, fv_id)) -> two_pitchers
two_pitchers %>%
mutate(Name = ifelse(pitcher == 664285,
"Valdez", "Javier")) -> two_pitchers
# table of pitch type and incidence of ground ball outs
two_pitchers %>%
mutate(GroundBall = ifelse(launch_angle <= 10 &
type == "X",
"Yes", "No")) ->
two_pitchers
two_pitchers %>%
filter(Name == "Valdez", type == "X",
is.na(GroundBall) == FALSE) %>%
tabyl(pitch_type, GroundBall)
two_pitchers %>%
filter(Name == "Javier", type == "X") %>%
tabyl(pitch_type, GroundBall)
# compute pitch values for all pitches
two_pitchers <- compute_pitch_values(two_pitchers)
# compare pitch locations
df1 <- filter(two_pitchers,
Name == "Javier",
pitch_type == "FF",
stand == "R")
df2 <- filter(two_pitchers,
Name == "Valdez",
pitch_type == "SI",
stand == "R")
df <- list(df1, df2)
names(df) <- c("Javier FF", "Valdez SI")
location_compare(df)
# compare contact rates
contact_swing_plot(df)
# compare launch angles and launch speeds of two pitches
df_new <- rbind(df1, df2) %>%
mutate(Type = paste(Name, pitch_type))
df_new %>%
mutate(GroundBall = ifelse(launch_angle <= 10,
"Yes", "No")) -> df_new
ggplot(filter(df_new, type == "X",
is.na(GroundBall) == FALSE),
aes(launch_angle, launch_speed,
color = GroundBall)) +
geom_point() +
facet_wrap(~ Type, ncol = 1) +
increasefont()
# compare expected wOBA
woba_plot(df)
# pitch value comparison
pitch_value_contour(df,
title = "Pitch Value to Right-Handers")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment