knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

In this vignette we will show how to use this package to shrink baseball averages. This is similar to beta-binomail shrinkage, but with a more flexible prior. We use the same setup as shown in David Robinson's Post.

Data Prepation

We first prepare the data

library(gest)
library(Lahman)
library(tidyverse)
career <- Batting %>%
  filter(AB > 0) %>%
  anti_join(Pitching, by = "playerID") %>%
  group_by(playerID) %>%
  summarize(H = sum(H), AB = sum(AB)) %>%
  mutate(average = H / AB)

# use names along with the player IDs
career <- Master %>%
  tbl_df() %>%
  select(playerID, nameFirst, nameLast) %>%
  unite(name, nameFirst, nameLast, sep = " ") %>%
  inner_join(career, by = "playerID") %>%
  select(-playerID)

head(career)

Add posterior estimates

We are now in a position to add posterior estimates. You will note that this operation takes a few seconds. This can be made faster by deceasing the grain of the parameter space (theta_by) which is set to 0.01 by default.

df_augmented <- career %>% 
  mutate(x = H, n = AB) %>% 
  add_binom_gest()

head(df_augmented)


alexhallam/gest documentation built on March 24, 2020, 1:22 a.m.