---
title: 'EDA Project: MLB data'
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Overview

This project will begin on Monday June 7th, and __conclude with a 10-15 minute presentation one week later on Thursday, June 17th during lab from 2:30 to 4 PM EDT__. The goal of this project is to practice understanding the structure of a dataset, and to practice generating and evaluating hypotheses using fundamental EDA and data visualization techniques.

## Deliverables

Your team is expected to produce `R Markdown` slides (an example template will be provided shortly) to accompany your 10-15 minute presentation with the following information:

* Explanation of the data structure of the dataset,
* __Three hypotheses__ you are interested in exploring,
* __Three data visualizations__ exploring the hypotheses, at least two of which must be multivariate.  __Each visualization must be in a different format__ from the other two, and you must have at least one categorical and one continuous visualization.
* __One clustering example__,
* Conclusions reached for the hypotheses based on your EDA and data visualizations,

## Timeline

There will be two submission deadlines:

**Friday, June 11th @ 4:00pm EST** - Each student will push their individual code for the project thus far to their GitHub accounts for review. We will then provide feedback on the code submitted.

**Thursday, June 17 @ 2:00pm EST** - Slides and full code must be completed and ready for presentation. Send your slides to Ron's email ([ryurko@andrew.cmu.edu](mailto:ryurko@andrew.cmu.edu)).  All code, visualizations, and presentations must be made in `R`. Take advantage of examples from lecture and the presentation template, but also feel free to explore material online that may be relevant!


## Data

Your team is assigned the [__MLB batted balls data__](http://www.stat.cmu.edu/cmsac/sure/2021/materials/data/eda_projects/mlb_batted_balls_2021.csv). This dataset contains all batted balls from the current 2021 MLB season, through June 5th, courtesy of [baseballsavant.com](https://baseballsavant.mlb.com/) and accessed using the [`baseballr`](http://billpetti.github.io/baseballr/) package. The code chunk at the end shows how this dataset was constructed in `R`.

Each row of the dataset corresponds to a batted ball and has the following columns:

* `player_name`: Name of the batter in Last, First format
* `batter`: Unique identifier for the batter
* `stand`: handedness of hitter, either `L` (left) or `R` (right), note that switch hitters are in this table but they'll switch depending on the pitcher,
* `events`: categorical event denoting the outcome of the batted ball,
* `hc_x`: horizontal coordinate of where a batted ball is first touched by a fielder,
* `hc_y`: vertical coordinate of where a batted ball is first touched by a fielder (note you should take the multiply this by -1 when plotting),
* `hit_distance_sc`: distance of the batted ball in feet according to Statcast,
* `launch_speed`: exit velocity of the ball off the bat (mph),
* `launch_angle`: the vertical angle of the ball off the bat measured from a line parallel to the ground,
* `hit_location`: positional number of the player who fielded the ball, possible values are 1-9 for each player (see [here](https://en.wikipedia.org/wiki/Baseball_positions)),
* `bb_type`: batted ball type,
* `barrel`: indicator if the batted ball was a ["barrel"](https://www.mlb.com/glossary/statcast/barrel),
* `pitch_type`: type of pitch thrown according to MLB's algorithm,
* `release_speed`: speed of the pitch measured when the ball is released (mph),
* `effective_speed`: perceived velocity of the ball, i.e., the velocity of the pitch is adjusted for how close it is to home when it is released (mph)
* `if_fielding_alignment`: type of infield shift by defense
* `of_fielding_alignment`: type of outfield shift by defense
* `game_date`: date of the game (mm/dd/yyyy)
* `balls`: number of balls in the count,
* `strikes`: number of strikes in the count,
* `outs_when_up`: number of outs when the batter is up,
* `on_1b`: unique identifier for a runner on first base (if there is one),
* `on_2b`: unique identifier for a runner on second base (if there is one),
* `on_3b`: unique identifier for a runner on third base (if there is one),
* `inning`: the inning number,
* `inning_topbot`: top or bottom of the inning,
* `home_score`: home team score before batted ball,
* `away_score`: away team score before batted ball,  
* `post_home_score`: home team score after batted ball,
* `post_away_score`: away team score after batted ball,  
* `des`: description of the batted ball and play.

Note that a full glossary of the features available from MLB's Statcast data can be found [here](https://app.box.com/v/statcast-pitchfx-glossary-pett).


## Code to build dataset

```{r, eval = FALSE}
library(baseballr)
library(tidyverse)

# Scrape all data for this season:
mlb_batted_balls_2021 <- 
  scrape_statcast_savant_batter_all(start_date = "2021-01-01",
                                    end_date = "2021-06-04") %>%
  dplyr::filter(type == "X")

mlb_batted_balls_2021 <- mlb_batted_balls_2021 %>%
  # Only select columns regarding the batted ball with discrete pitch type
  # information (except for the speed) for now: 
  dplyr::select(# Batter info:
                player_name, batter, stand, 
                # Batted ball info:
                events, hc_x, hc_y, hit_distance_sc, launch_speed, launch_angle,
                hit_location, bb_type, barrel,
                # Pitch info:
                pitch_type, release_speed, effective_speed, 
                # Shift info:
                if_fielding_alignment, of_fielding_alignment,
                # Game level context:
                game_date, balls, strikes, outs_when_up, on_1b, on_2b, on_3b, 
                inning, inning_topbot, home_score, away_score, post_home_score,
                post_away_score,
                # Description of play:
                des)
```

