MLB Batting Statistics

In this problem set, you will practice using the pipe %>% and grouped calculations with batting statistics taken from the Lahman dataset. We will compute the same statistics as we did in Problem Set 3 and also standardize these statistics within year, year and league, and also historical era.

To load the batting data into R, we can do the following

Unfortunately, some statistics like hit-by-pitch (HBP) were not recorded in the earlier decades of baseball. In the tbl, these missing values are designated NA. To see some of these, we can run the following code:

A common convention for dealing with the missing values when computing \(\text{PA}\) is to replace the NA with a 0. To do this, we can use the function replace_na() within a pipe as follows.

The syntax for replace_na() is a bit involved but the basic idea is you have to specify the value you want to replace each NA. When using replace_na() it is very important to remember to include the list(...) bit.

  1. Load the Lahman data and run the above code to create the tbl Batting, which has replaced all of the NA’ in the columns IBB, HBP, SH, SF and GIDP`.

  2. Using the pipe %>%, mutate(), filter(), and select(), create a tbl batting by:

  1. Standardize each of BA, OBP, OPS, and wOBA using data from all of the years. Who were the best and worst batters according to these four metrics? Name the columns containing these new standardizedvalues zBA_all, zOPB_all, etc. Remember, you must re-define the standardize() function we wrote in Module 4
  1. Group batting by year and compute the standardized BA, OBP, OPS, and wOBA within each year. Now who are the best and worst batters according to the four measures? Name the columns containing these new standardized values zBA_year, zOBP_year, etc.
  1. Remove the grouping by year and instead group by year and league. Once again, standardize OBP, OPS, and wOBA within each league-year combination. Are the best and worst batters still the same? Name the columns containing these new standardized valuse zBA_year_lg, zOBP_year_lg, etc.
  1. Remove the grouping you created in Problem 4. Bill James divided baseball history into several eras as follows:

Use mutate() and case_when() (just like we did in Module 3) to add a column called Hist_era to batting that records the historical era.

  1. Group batting by Hist_era and standardize BA, OBP, OPS, and wOBA within historical era. Who are the best and worst batters now? Name the columns containing these new stanardized values zBA_hist, zOBP_hist, etc.

  2. Remove the grouping you added in Problem 6.

MLB Payroll and Winnings

Recall from Problem Set 2, we plotted the relative payroll of MLB teams against their winning percentage. In that problem set, we read in a file that had included the relative payroll for each team as a separate column. To get some additional practice with dplyr, we will read in a different dataset and re-compute these relative payrolls.

  1. Remember the function we wrote in Module 4 to standardize various statistics? It is reproduced below

We need to write another function in order to compute “relative payroll”. This function will take in a vector x, compute its median, and then divides every element of x by the median.

  1. Read in the MLB Payroll Data and load it into a tibble called mlb_payrolls.
Parsed with column specification:
cols(
  Team = col_character(),
  GM = col_character(),
  Team_Payroll = col_double(),
  Winning_Percentage = col_double(),
  Year = col_double()
)
  1. Using the pipe %>%, group_by(), and mutate(), add a column to mlb_payrolls that contains the relative payroll for each team.

  2. Make a scatterplot of winning percentage against relative payrolls. Comment on the relationship. Your scatterplot should be identical to one you made in Problem Set 2.

  3. Using the summarize() function, compute the average team payroll and relative payroll for each year. Save these results in a new tbl called payroll_avg.

  4. Make a scatterplot that shows how team payrolls have evolved over the year. Similar to what we did in Module 4, add a line to this scatterplot that shows the average team payroll. Do the same thing for relative payroll. What do you notice about the average team payroll and relative payroll?

  5. As you will see in coming lectures, correlation is a measure of the strength of the linear relationship between two variables. The closer to +1 or -1 the correlation between two variables is, the more predictable they are of each other. We can compute it using the cor() function. Using summary() and cor(), compute the correlation between relative payroll and winning percentage within each year. What do you notice about how the relationship between winning percentage and relative payroll changes year to year?

# A tibble: 18 x 2
    Year   cor
   <dbl> <dbl>
 1  1998 0.764
 2  1999 0.699
 3  2000 0.327
 4  2001 0.338
 5  2002 0.443
 6  2003 0.415
 7  2004 0.515
 8  2005 0.497
 9  2006 0.538
10  2007 0.495
11  2008 0.322
12  2009 0.504
13  2010 0.347
14  2011 0.408
15  2012 0.195
16  2013 0.330
17  2014 0.297
18  2015 0.281

A Challenge Question

Without running the code, work with your teammates to see if you can figure out what the code below is doing.

  1. Run the code above and save the tbl batting_2014_2015.RData to the file “data/batting_2014_2015.RData”. We will return to this dataset in Lecture 4.