knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7.2,
  fig.height = 7.2
)
library(tidyverse)
library(mlbgm)

Using the right colors

mlb_pal()
mlb_pal(2)
mlb_pal(names = "lahman_name")

Using the right names

lkup_teams()
forbes %>%
  pull(Team) %>%
  unique() %>%
  standardize_team_name()

Cross-referencing IDs

lkup_players
lkup_players %>%
  summarize(
    N = n(),
    mlbam = n_distinct(mlbam_id),
    lahman = n_distinct(lahman_id),
    retro = n_distinct(retro_id),
    espn = n_distinct(espn_id)
  )

The value of the franchise

ggplot(forbes, aes(x = Year, y = Value, color = Team)) + 
  geom_point() + 
  geom_line() + 
  geom_smooth(se = FALSE) + 
  scale_color_mlb(names = "teamcolors_name") + 
  scale_y_continuous("Francise Value (Millions of USD)") + 
  scale_x_continuous(NULL) + 
  coord_trans(y = "log10") + 
  labs(title = "MLB Franchise Valuations",
       caption = "Source: Forbes")

Major League Service and contracts

contracts
mls

WAR

rwar

Las Vegas futures

futures <- read_ws_futures() %>%
  summarize_futures()
futures

ggplot(futures, aes(x = reorder(teamID, ws_prob_normalized), 
                    y = ws_prob_normalized, 
                    color = teamID, fill = teamID)) +
  geom_col() + 
  scale_fill_mlb() + 
  scale_color_mlb(2) + 
  scale_x_discrete(NULL) +
  scale_y_continuous("Mean Probability of Winning the World Series") + 
  labs(caption = "Source: many different betting markets")

Comparable players

some_comps <- comps("beltrca01", 2005)
summary(some_comps)
plot_rwar(some_comps, 7)

Player profile

beltran <- rwar %>%
  filter(playerId == "beltrca01") %>%
  mutate(teamID = standardize_team_ids(teamId)) %>%
  select(playerId, yearId, stintId, teamID, PA, rWAR) %>%
  left_join(Lahman::Salaries, 
            by = c("playerId" = "playerID", "yearId" = "yearID", "teamID")) 

# %>%
#   group_by(playerId, yearId) %>%
#   summarize(num_teams = n(), 
#             rwar = sum(rWAR), 
#             earnings = sum(salary, na.rm = TRUE))

ggplot(beltran, aes(x = yearId, color = teamID, fill = teamID)) + 
  geom_col(aes(y = -salary/4e6)) + 
  geom_col(aes(y = rWAR)) + 
  geom_smooth(aes(y = rWAR), se = FALSE) + 
  geom_smooth(aes(y = -salary/4e6), se = FALSE) + 
  scale_y_continuous("Wins above Replacement") + 
  scale_color_mlb() + 
  scale_fill_mlb(2)


beanumber/mlbgm documentation built on Jan. 21, 2020, 8:33 p.m.