Overview

Analysis of Retrosheet’s Major League Baseball Game Log Data on ejections.

Questions:

  1. Which years and teams have the highest and lowest ejection rates?

  2. Are there certain umpires who eject more frequently?

  3. Are there patterns in the number of ejections based on the “Eras” of baseball?

  4. What factors have the strongest correlation with ejections?

Quick History

Why do umpires eject players, coaches, and managers from baseball games?

When did ejectionsbecome a norm in MLB?

Source: (Imber, 2024)

Data Clean Up

Retrosheet Ejections Data https://www.retrosheet.org/eject.htm

Import Data Sets from Retrosheet

ejections <- read.csv("C:/CS 134/Final Project 1/alldata/ejections.csv")
View(ejections)
ballparks <- read.csv("C:/CS 134/Final Project 1/alldata/ballparks.csv")
View(ballparks)
teams <- read.csv("C:/CS 134/Final Project 1/alldata/teams.csv")
View(teams)

Create Data Set for Ejections

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
ejections_teams <- left_join(ejections, teams, by = "TEAM") %>%
select(TEAM, NICKNAME, DATE, GAMEID, EJECTEENAME, JOB, UMPIRENAME, INNING, REASON)

Create Data Set for Ejections By Year

library(dplyr)
ejections_years <- ejections_teams %>%
  mutate(DATE = as.Date(DATE, format = "%m/%d/%Y")) %>%
  group_by(Year = format(DATE, "%Y")) %>%
  summarize(TOTAL = n())
colnames(ejections_years)[colnames(ejections_years) == "NICKNAME"] <- "TEAM"
colnames(ejections_years)[colnames(ejections_years) == "Year"] <- "YEAR"

Plot Ejections By Year

library(ggplot2)
ejections_years$YEAR <- as.numeric(ejections_years$YEAR)
ggplot(data = ejections_years,
       aes(x = YEAR, y = TOTAL, color = YEAR)) +
  geom_point(size = 3) + 
  labs(title = "Ejections By Year",
       subtitle = "1914 had the most ejections (339). 1892 had the least ejections (8). By comparison, 2023 had 247 ejections.") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_x_continuous(breaks = seq(1880, 2030, by = 10), labels = scales::number_format())

Create Data Set for Ejections By Team

ejections_totals <- ejections_teams %>%
  group_by(NICKNAME) %>%
  summarize(TOTAL = n()) %>%
  mutate(PROPORTION = TOTAL / sum(TOTAL))
colnames(ejections_totals)[colnames(ejections_totals) == "NICKNAME"] <- "TEAM"

Plot Ejections By Team

library(ggplot2)
ggplot(data = ejections_totals,
       mapping = aes(x = TEAM, y = TOTAL, color = TEAM)) +
  geom_point(size = 3) + 
  labs(title = "Ejections By Team",
       subtitle = "Of current MLB teams, the Dodgers have the most ejections (1097) and the Nationals have the least ejections (128).") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Create Data Set for Ejections By Umpire

library(dplyr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
ejections_umpires <- ejections_teams %>%
  group_by(UMPIRENAME) %>%
  summarize(TOTAL = n(),
            `DATE_START` = min(year(as.Date(DATE, format = "%m/%d/%Y"))),
            `DATE_END` = max(year(as.Date(DATE, format = "%m/%d/%Y"))),
            .groups = "drop")
colnames(ejections_umpires)[colnames(ejections_umpires) == "UMPIRENAME"] <- "UMPIRE"

Plot Total Ejections By Umpire

ggplot(ejections_umpires, aes(x = UMPIRE, y = TOTAL)) +
geom_col(fill = "004d71") +
labs(title = "Total Ejections By Umpire Name", subtitle = "Bill Klem made the most ejections (364 from 1905-1940)", x = "Umpire", y = "Total") + theme(axis.text.x = element_text(angle = 90, hjust = 1))

Ejections By Eras

Why Look At Eras?

Create Data Set of Ejections With Totals By Eras By Years

library(dplyr)

eras <- c("Dead Ball Era", "Live Ball Era", "Integration Era", "Expansion Era", "Free Agency Era", "Steroid Era", "Modern Era")
era_ranges <- c(1901, 1920, 1942, 1961, 1977, 1994, 2005, Inf)

ejections_years_eras <- ejections_teams %>%
  mutate(DATE = as.Date(DATE, format = "%m/%d/%Y")) %>%
  group_by(Year = format(DATE, "%Y")) %>%
  summarize(TOTAL = n())

colnames(ejections_years_eras)[colnames(ejections_years_eras) == "NICKNAME"] <- "TEAM"
colnames(ejections_years_eras)[colnames(ejections_years_eras) == "Year"] <- "YEAR"

ejections_years_eras <- ejections_years_eras %>%
  mutate(ERA = case_when(
    YEAR >= era_ranges[1] & YEAR < era_ranges[2] ~ eras[1],
    YEAR >= era_ranges[2] & YEAR < era_ranges[3] ~ eras[2],
    YEAR >= era_ranges[3] & YEAR < era_ranges[4] ~ eras[3],
    YEAR >= era_ranges[4] & YEAR < era_ranges[5] ~ eras[4],
    YEAR >= era_ranges[5] & YEAR < era_ranges[6] ~ eras[5],
    YEAR >= era_ranges[6] & YEAR < era_ranges[7] ~ eras[6],
    YEAR >= era_ranges[7] & YEAR < era_ranges[8] ~ eras[7],
    YEAR >= era_ranges[4] ~ eras[4],
    TRUE ~ "Unknown"
  )) %>%
  group_by(YEAR, ERA) %>%
  summarize(TOTAL = sum(TOTAL))
## `summarise()` has grouped output by 'YEAR'. You can override using the
## `.groups` argument.

Create Data Set of Ejections With Totals By Eras Only

library(dplyr)

eras <- c("Dead Ball Era", "Live Ball Era", "Integration Era", "Expansion Era", "Free Agency Era", "Steroid Era", "Modern Era")
era_ranges <- c(1901, 1920, 1942, 1961, 1977, 1994, 2005, Inf)

ejections_eras <- ejections_years %>%
  mutate(ERA = case_when(
    YEAR >= era_ranges[1] & YEAR < era_ranges[2] ~ eras[1],
    YEAR >= era_ranges[2] & YEAR < era_ranges[3] ~ eras[2],
    YEAR >= era_ranges[3] & YEAR < era_ranges[4] ~ eras[3],
    YEAR >= era_ranges[4] & YEAR < era_ranges[5] ~ eras[4],
    YEAR >= era_ranges[5] & YEAR < era_ranges[6] ~ eras[5],
    YEAR >= era_ranges[6] & YEAR < era_ranges[7] ~ eras[6],
    YEAR >= era_ranges[7] & YEAR < era_ranges[8] ~ eras[7],
    YEAR >= era_ranges[4] ~ eras[4],
    TRUE ~ "Unknown"
  )) %>%
  group_by(ERA) %>%
  summarize(TOTAL = sum(TOTAL)) 

Plot Ejections by Eras

library(ggplot2)

ejections_years_eras$YEAR <- as.numeric(as.character(ejections_years_eras$YEAR))

ggplot(ejections_years_eras, aes(x = YEAR, y = TOTAL, fill = ERA)) +
  geom_col() + 
  scale_x_continuous(breaks = seq(1889, 2023, by = 25))

# Observations

What factors have the strongest correlation with ejections? - By looking at the eras of baseball and seeing the spread of ejections amongst those years, I have made the following observations: - Umpires were influenced by the new ejection rule (see Dead Ball Era) and frequently gave ejections. - Increased ejections were influenced by the Steroid Era, assuming increased scoring lead to increased rules broken.

More Fun!

# Making interactive plots for Ejections by Eras

options(repos = c(CRAN = "https://cran.rstudio.com/"))
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ stringr 1.5.1
## ✔ purrr   1.0.2     ✔ tibble  3.2.1
## ✔ readr   2.1.4     ✔ tidyr   1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
static_plot <- ggplot(ejections_years_eras, aes(x = YEAR, y = TOTAL, fill = ERA)) +
  geom_col() + 
  scale_x_continuous(breaks = seq(1889, 2023, by = 25))

ggplotly(static_plot)
install.packages("flexdashboard")
## Installing package into 'C:/Users/cngil/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'flexdashboard' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\cngil\AppData\Local\Temp\RtmpOYrcWF\downloaded_packages