---
title: 'EDA Project: NHL 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 [__NHL shot data__](http://www.stat.cmu.edu/cmsac/sure/2021/materials/data/eda_projects/nhl_playoffs_shots_2021.csv). This dataset contains all shot attempts from the 2021 NHL playoffs (through June 5th), courtesy of [MoneyPuck.com](http://moneypuck.com/data.htm). The code chunk at the end shows how this dataset was processed in `R` (note the dataset was originally downloaded from the MoneyPuck site.)

Each row of the dataset corresponds to a shot attempt and has the following columns:

* `shooterPlayerId`: The NHL player id of the skater taking the shot
* `shooterName`: The First and Last name of the player taking the shot
* `team`: The team taking the shot
* `shooterLeftRight`: Whether the shooter is a left or right shot
* `shooterTimeOnIce`: playing time in seconds that have passed since the shooter started their shift
* `shooterTimeOnIceSinceFaceoff`: minimum of the  playing time in seconds since the last faceoff and the playing time that has passed since the shooter started their shift
* `event`: Whether the shot was a shot on goal (SHOT), goal, (GOAL), or missed the net (MISS)
* `location`: The zone the shot took place in
* `shotType`: Type of the shot
* `shotAngle`: The angle of the shot in degrees - is a positive number if the shot is from the left side of the ice. 
* `shotAnglePlusRebound`: The difference in angle between the previous shot and this shot if this shot is a rebound, is otherwise set to 0
* `shotDistance`: The distance from the net of the shot in feet, net is defined as being at the (89,0) coordinates
* `shotOnEmptyNet`: Indicator if the shot was on an empty net
* `shotRebound`: Indicator if the shot is a rebound, i.e., if the last event was a shot and within 3 seconds of this shot
* `shotRush`: Indicator if the shot was on a rush, i.e., ff the last event was in another zone and within 4 seconds
* `shotWasOnGoal`: Indicator if the shot was on net - either a goal or a goalie save,
* `shotGeneratedRebound`: Indicator if the shot generated a rebound shot within 3 seconds of the this shot,
* `shotGoalieFroze`: Indicator if the goalie froze the puck within 1 second of the shot,
* `arenaAdjustedShotDistance`: shot distance adjusted for arena recording bias - uses the same methodology as [War On Ice](http://blog.war-on-ice.com/index.html%3Fp=512.html) proposed by [Schuckers and Curro](https://www.statsportsconsulting.com/wp-content/uploads/Schuckers_Curro_MIT_Sloan_THoR.pdf)
* `arenaAdjustedXCord`: x coordinate of the arena adjusted shot location - always a positive number,
* `arenaAdjustedYCord`: y coordinate of the arena adjusted shot location,
* `goalieIdForShot`: The NHL player id for the goalie the shot is on,
* `goalieNameForShot`: The First and Last name of the goalie the shot is on,
* `teamCode`: The team code of the shooting team,
* `isHomeTeam`: Indicator if the shooting team is the home team,
* `homeSkatersOnIce`: The number of skaters on the ice for the home team (does not count the goalie)
* `awaySkatersOnIce`: The number of skaters on the ice for the away team (does not count the goalie)
* `game_id`: The NHL Game_id of the game the shot took place in
* `homeTeamCode`: home team in the game
* `awayTeamCode`: away team in the game
* `homeTeamGoals`: Home team goals before the shot took place
* `awayTeamGoals`: Away team goals before the shot took place
* `time`: Seconds into the game of the shot
* `period`: period of the game

Note that a full glossary of the features available for NHL shot data can be found [here](http://peter-tanner.com/moneypuck/downloads/MoneyPuck_Shot_Data_Dictionary.csv).


## Code to build dataset

```{r, eval = FALSE}
# Accessed NHL shots from 2020-2021 season from MoneyPuck.com, but will
# simplify the dataset to be easier to work with. Load the original data, and
# then just filter to the playoff game shots (as of June 5th)
playoff_shot_data <- read_csv("http://www.stat.cmu.edu/cmsac/sure/2021/materials/data/xy_examples/moneypuck_shots_2020.csv") %>%
  dplyr::filter(isPlayoffGame == 1)

# Now only select columns to work with for this task:
playoff_shot_data <- playoff_shot_data %>%
  dplyr::select(# Player info attempting the shot
                shooterPlayerId, shooterName, team, shooterLeftRight, 
                shooterTimeOnIce, shooterTimeOnIceSinceFaceoff,
                # Info about the shot:
                event, location, shotType, shotAngle, shotAnglePlusRebound, 
                shotDistance, shotOnEmptyNet, shotRebound, shotRush, 
                shotWasOnGoal, shotGeneratedRebound, shotGoalieFroze,
                # Adjusted for arena locations
                arenaAdjustedShotDistance, 
                arenaAdjustedXCord, arenaAdjustedYCord,
                # Goalie info:
                goalieIdForShot, goalieNameForShot,
                # Team context
                teamCode, isHomeTeam, homeSkatersOnIce, awaySkatersOnIce,
                # Game context
                game_id, homeTeamCode, awayTeamCode, homeTeamGoals,
                awayTeamGoals, time, period)
```


