R Code Snippets
Load a pitcherโs Statcast data
library(baseballr)
# Look up player ID
gerrit <- playerid_lookup('Cole', 'Gerrit')
mlbam <- gerrit$key_mlbam[1]
# Pull pitch-level Statcast data
sc <- statcast_search_pitchers(
start_date = '2024-03-01',
end_date = '2024-09-30',
pitcherid = mlbam
)
library(tidyverse)
arsenal <- sc |>
filter(!is.na(pitch_type)) |>
group_by(pitch_type) |>
summarise(
n = n(),
usage = n() / nrow(sc),
avg_velo = mean(release_speed, na.rm=TRUE),
avg_spin = mean(release_spin_rate, na.rm=TRUE),
ivb = mean(pfx_z * 12, na.rm=TRUE), # inches, pitcher POV
hb = mean(pfx_x * 12, na.rm=TRUE), # inches, pitcher POV
avg_ext = mean(release_extension, na.rm=TRUE),
whiff_pct = sum(description %in% c('swinging_strike','swinging_strike_blocked')) /
sum(description %in% c('swinging_strike','swinging_strike_blocked',
'foul','hit_into_play')),
csw_pct = sum(description %in% c('called_strike','swinging_strike',
'swinging_strike_blocked')) / n()
)
Pull FanGraphs Stuff+ (type=36)
stuff_lb <- fg_pitcher_leaders(
startseason = 2024, endseason = 2024,
type = 36, # <-- key: Stuff+ / Location+ / Pitching+ tab
qual = 20 # min 20 IP
)
stuff_lb |> select(PlayerName, Team, IP, `Stuff+`, `Location+`, `Pitching+`) |> head()
Run CPS model
source('R/utils_calc.R')
weights <- readRDS('data/weights_default.rds')
stuff_data <- stuff_lb |> filter(playerid == gerrit$key_fangraphs[1])
cps <- calc_cps(sc, stuff_data, weights$cps)
cat('CPS:', cps$cps_raw, '|', cps$grade)