Analyzing Baseball Data with R

Some information about the book Analyzing Baseball Data With R, 2nd edition by Max Marchi, Jim Albert, and Ben Baumer:

Some useful links for the book.

 

Advertisements

27 responses

  1. […] my foray into R with baseball is a neat graphic based on a recent post from the authors of Analyzing Baseball With R.  They use the R statistical programming language to go through the copious amount of baseball […]

  2. Question 7 Chapter 3, p. 85 asks you to pull Pete Rose’s info, but from what I can tell, the function “getinfo” doesn’t work for two players with the exact same name (junior), or am I wrong?

  3. Yeah, I’ll try to fix this and then make the new function available — thanks.

  4. Errata link seems to be broken

    1. Aaron — thanks for noticing that — it should work now.

  5. Sergio Marrero Marrero | Reply

    Hello ! In regard to Chapter 9. I would like to have more information (further reading) about these topics:

    1) How to build a transition matrix to simulate a complete game between two teams, taking into account all offensive(batters/runners) and defensive (pitchers/fielding) strength.
    2) The method used in section 9.2.6 to estimate the transition matrix. In the book is written: “The description of this methodology is beyond the level of this book…” but no further reading or reference is given

    I feel lot of interest on these topics and I appreciate to have some references to continue my research.

    Thanks in advance. Sergio.

  6. Hello !! In regard to the Bradley-Terry model (chapter 9).

    1) Section 9.4: Further reading. The reference of “Chapter 9 of Albert and Bennet (2003)” seems to be wrong as in my book copy the Bradle Terry model is developed in the “Chapter 12, Did the best team win?”. Maybe I have a different edition.

    2) After examine the “Chapter 9 of Anaylzing Baseball Data With R” I jumped to the “Chapter 12, Did the best team win?. Curve Ball” with the hope of finding how to calculate the “Talent(t)” of teams. I did not find anyhing about it. The only way I know is “log5 model by Bill James”. I have thought on maximize the likelihood to find the “teams talent (t)”, but I would like to ask for some reading before jump on my own developing.

    So, is there any other approach to calculate the talents? Can anyone helpy me with further readings about it?

    Lot of thanks in advance !

    Sergio.

  7. Hello !!

    In regard to the Bradley-Terry model (chapter 9).

    1) Section 9.4: Further reading. The reference of “Chapter 9 of Albert and Bennet (2003)” seems to be wrong as in my book copy the Bradle Terry model is developed in the “Chapter 12, Did the best team win?”. Maybe I have a different edition.

    2) After examine the “Chapter 9 of Anaylzing Baseball Data With R” I jumped to the “Chapter 12, Did the best team win?. Curve Ball” with the hope of finding how to calculate the “Talent(t)” of teams. I did not find anyhing about it. The only way I know is “log5 model by Bill James”. I have thought on maximize the likelihood to find the “teams talent (t)”, but I would like to ask for some reading before jump on my own developing.

    So, is there any other approach to calculate the talents? Can anyone helpy me with further readings about it?

    Lot of thanks in advance !

    Sergio.

    1. Sergio: As I recall, I used a value of the standard deviation of the Bradley Terry talent distribution so that predicted w/l records of the simulated data resembled the observed w/l records. One could formally fit this B-T model and estimate the standard deviation, but I believe I used this empirical approach to estimate the standard deviation. This type of B-T model is typically used in paired comparison models and there are some Bayesian papers on this.

  8. Jim –

    In chapter 1, you guys state that “In 2011, hitters compiled a .253 batting average on plate appearances where they fell behind 0-2. Conversely they hit .479 after going ahead 2-0.” I’m trying to replicate those numbers and even using the pbp11rc.csv file, I can’t even come close. Instead of batter average, did you mean OBP?

  9. I’m trying to add an “Age” column in the Lahman batting.csv file. My idea is that I can use a combination of getinfo and the sapply function. I’m comfortable using the getinfo function for individual players. I’ve attempted to adapt the function to do this but I’m struggling. Any suggestions?

    Much appreciated. I’m really enjoying this book so far!

  10. Just got the new version of the book. In section 2.7 (p52), I’m getting an error with “object ‘crcblue’ not found.” Is that a color for the chart? I’ve tried uninstalling and reinstalling tidyverse and retyping the code and I can’t get through it. Any advice?

    1. Sorry, we neglected to define that color code in our code — just add the code
      crcblue <- "#2905a1".

      Thanks.

  11. I hate that I’m stumped on the first question I came across, and it seems straightforward, but for the average number of home runs per game recorded in each decade the answer says that the first two decades had 0.3 HRs per game. In the 1870 I get 356 HRs over 4,062 games for 0.09 per game, and in the 1880s 3,773 HRs over 17,484 games for 0.22 per game. What am I missing?

    1. It seems that you double-counted games. When you sum the games variable, it will be twice as large as the actual number of games since 2 teams are in each game. With this correction, you will get reasonable looking values.

      1. Dumb mistake on my part, I should have realized that error. Thank you very much for the response. I am enjoying the book a great deal!

  12. Hi Jim,

    In Chapter 7, when modeling called strike percentage, the book limits the PITCHf/x data to type == “S”, but from the description variable in the first column, it looks like “S” includes strikes of any kind. Rather than “S” and “X” being “called strikes” and “strikes” respectively as noted in the book, I’m pretty sure they stand for “strikes” and “balls in play.”

    Thanks

    1. Judah, I think you’re right — I’ll check with Ben. Thanks for mentioning the issue.

  13. On page 84 the code goes as follows:
    HRdata %
    mutate(Age = yearID – birthyear) %>%
    select(Player, Age, HR) %>%
    mutate(CHR = cumsum(HR))

    The graph however seems to perform the cumulative Sums for every players HR totals. Is there a way to unpack the Cumsum function for the individual players? Thank you for the awesome book.

    1. Christian:

      To do this for the individual plays, you just add a group_by(Player) line after the first mutate() line.

    2. To get these summaries for individual players, you just insert a group_by(Player) before the mutate operation.

  14. Hi guys. What a great book. Not sure if this is a great forum for questions but I’ll give it a shot. In chapter 6 it uses a “Cabrera” data set that has PitchFx data plus some batted ball location data. For these pre-loaded data sets, I’ve found it fun to see if I can recreate the same data from the source. I was able to do this for the PitchFx data, but I can’t find the batted ball data anywhere? Is that publicly available? It looks like Baseball Savant might have this data, but just in summary form and not in a database you can download. Thanks again for the great book. This is from the 1st edition if it matters

    1. Tim, you can load pitch-by-pitch data from Statcast using the baseballr package. This gives you all of the pitch information together with the batted ball measurements.

      1. Neat, can’t wait to look at that. But I am having trouble installing. I seem to be having the same issue described here which was never answered:
        https://stackoverflow.com/questions/55938608/cant-install-baseballr-package

        Although for me the error is “cannot remove prior installation of package ‘rlang’”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: