This package is a player-level baseball statistics calculator. Similar to using sum(2,2)
to perform the operation 2 + 2
, mlbstats
allows for simple computation of a host of player-level metrics based on raw inputs. Common metrics from major league baseball (MLB) include batting, pitching, fielding, baserunning, and player-summary statistics. The goal of this package, then, is to provide an easy-to-use calculator to efficiently calculate player metrics based on raw, easily obtainable data. For some other metrics such as WAR, UZR, PWA, etc., see https://www.baseball-reference.com/ or Bill James' sabermetrics guides.
Take a look at the package, along with many other excellent related packages, in the Sports Analytics CRAN Task View.
From the simplest calculations (e.g., batting average = number of hits / at bats) to complicated ones (e.g., a pitcher's component earned run average, calculated as, 9 * ((h + bb + hbp) * (0.89 * (1.255 * (h - hr) + 4 * hr) + 0.56 * (bb + hbp - ibb)) / (bfp * ip)) - 0.56)
), unique functions in mlbstats
take unique values from player-level data, and return the desired metric.
install.packages("mlbstats")
library(mlbstats)
obp(
h = 204, # hits
bb = 58, # walks
hbp = 9, # hits by pitch
ab = 590, # at bats
sf = 4 # sacrifice flys
)
[1] 0.4099849
Corroborate with the official MLB statistics, recording Altuve's 2017 regular season OBP as 0.410
.
pfr(
k = 308, # strikeouts
bb = 43, # walks
ip = 214.1 # innings pitched
)
[1] 1.639421
Load the package (0.1.0) from CRAN. If you have any questions, find any bugs requiring fixing, or find metrics I left out, either open an issue ticket or feel free to reach out directly. If you use the package for any published work, I would love to know!