In a previous post, I described how to download and clean data for understanding how likely a baseball player is to hit into an error given that they hit the ball into play.
This analysis will statistically demonstrate that some players are more likely to hit into errors than others.
Errors are uncommon, so players hit into errors very infrequently. Estimating the likelihood of an infrequent event is hard and requires lots of data. To acquire as much data as possible, I wrote a bash script that will download data for all players between 1970 and 2018.
This data enables me to use data from multiple years for each player, giving me more data when estimating how likely a particular player is to hit into an error.
1 2 3 4 5 6 7 8 9 10 11 12
The data has 5 columns: playerid, playername, errors hit into, balls hit into play (BIP), and year. The file does not have a header.
aaroh101, Hank Aaron, 8, 453, 1970 aarot101, Tommie Aaron, 0, 53, 1970 abert101, Ted Abernathy, 0, 10, 1970 adaij101, Jerry Adair, 0, 24, 1970 ageet101, Tommie Agee, 12, 480, 1970 akerj102, Jack Aker, 0, 10, 1970 alcal101, Luis Alcaraz, 1, 107, 1970 alleb105, Bernie Allen, 1, 240, 1970 alled101, Dick Allen, 4, 341, 1970 alleg101, Gene Alley, 6, 356, 1970
I can load the data into pandas using the following command.
1 2 3 4 5
I have almost 39,000 year, player combinations…. a good amount of data to play with.
While exploring the data, I noticed that players hit into errors less frequently now than they used to. Let’s see how the probability that a player hits into an error has changed across the years.
1 2 3 4 5 6 7 8 9 10 11 12
Interestingly, the proportion of errors per BIP has been dropping over time. I am not sure if this is a conscious effort by MLB score keepers, a change in how hitters hit, or improved fielding (but I suspect it’s the score keepers). It looks like this drop in errors per BIP leveled off around 2015. Zooming in.
I explore this statistically in a jupyter notebook on my github.
Because I don’t want year to confound the analysis, I remove all data before 2015.
This leaves me with 3500 year, player combinations.
Next I combine players’ data across years.
I want an idea for how likely players are to hit into errors.
1 2 3
Again, errors are very rare, so I want know how many “trials” (BIP) I need for a reasonable estimate of how likely each player is to hit into an error.
I’d like the majority of players to have at least 5 errors. I can estimate how many BIP that would require.
Looks like I should require at least 425 BIP for each player. I round this to 500.
I’ve identified 354 players who have enough BIP for me to estimate how frequently they hit into errors.
Below, I plot how the likelihood of hitting into errors is distributed.
1 2 3 4
The question is whether someone who has hit into errors in 2% of their BIP is more likely to hit into an error than someone who has hit into errors in 0.5% of their BIP (or is this all just random variation).
To try and estimate this, I treat each BIP as a Bernoulli trial. Hitting into an error is a “success”. I use a Binomial distribution to model the number of “successes”. I would like to know if different players are more or less likely to hit into errors. To do this, I model each player as having their own Binomial distribution and ask whether p (the probability of success) differs across players.
To answer this question, I could use a chi square contingency test but this would only tell me whether players differ at all and not which players differ.
The traditional way to identify which players differ is to do pairwise comparisons, but this would result in TONS of comparisons making false positives all but certain.
Another option is to harness Bayesian statistics and build a Hierarchical Beta-Binomial model. The intuition is that each player’s probability of hitting into an error is drawn from a Beta distribution. I want to know whether these Beta distributions are different. I then assume I can best estimate a player’s Beta distribution by using that particular player’s data AND data from all players together.
The model is built so that as I accrue data about a particular player, I will trust that data more and more, relying less and less on data from all players. This is called partial pooling. Here’s a useful explanation.
I largely based my analysis on this tutorial. Reference the tutorial for an explanation of how I choose my priors. I ended up using a greater lambda value (because the model sampled better) in the Exponential prior, and while this did lead to more extreme estimates of error likelihood, it didn’t change the basic story.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (2 chains in 2 jobs) NUTS: [rates, kappa_log, phi] Sampling 2 chains: 100%|██████████| 6000/6000 [01:47<00:00, 28.06draws/s]
Check whether the model converged.
1 2 3
The most challenging parameter to fit is kappa which modulates for the variance in the likelihood to hit into an error. I take a look at it to make sure things look as expected.
I can also look at phi, the estimated global likelihood to hit into an error.
Finally, I can look at how all players vary in their likelihood to hit into an error.
Obviously, the above plot is a lot to look at it, so let’s order players by how likely the model believes they are to hit in an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Now, the ten players who are most likely to hit into an error.
1 2 3
And the 10 players who are least likely to hit in an error.
1 2 3
It looks to me like players who hit more ground balls are more likely to hit into an error than players who predominately hits fly balls and line-drives. This makes sense since infielders make more errors than outfielders.
Using the posterior distribution of estimated likelihoods to hit into an error, I can assign a probability to whether Carlos Correa is more likely to hit into an error than Daniel Murphy.
The model believes Correa is much more likely to hit into an error than Murphy!
I can also plot these players’ posterior distributions.
1 2 3 4 5
Finally, I can look exclusively at how the posterior distributions of the ten most likely and 10 least likely players to hit into an error compare.
All in all, this analysis makes it obvious that some players are more likely to hit into errors than other players. This is probably driven by how often players hit ground balls.