---
title: "Player Movement Patterns"
df_print: paged
output:
    html_document:
        df_print: paged
        theme: journal
        code_folding: hide
        toc: yes
        toc_depth: 3
        numbered_sections: yes
---
``` {r, include = FALSE}
library(ggplot2)
library(tidyverse)
library(rmarkdown)
library(dplyr)
```

# Other Notebooks

My analysis is split between four different notebooks. The rest of the notebooks can be found at the links below:

[Exploratory Analysis of NFL Non-Contact Injuries](https://www.kaggle.com/erinpsajdl/exploratory-analysis-of-nfl-non-contact-injuries) /
This notebook takes an in-depth look at the variables provided in the given datasets and how they intereact with the others.

[Injury Risk Model for NFL Non-Contact Injuries](https://www.kaggle.com/erinpsajdl/injury-risk-model-for-nfl-non-contact-injuries) /
The notebook builds out the injury risk model for non-contact lower limb injuries.                                                 
                                                 
[Player Movement Effects](https://www.kaggle.com/erinpsajdl/player-movement-effects) /                                                 
This notebook takes a look at the effect that various game scenarios have on overall player movement.

``` {r, include = FALSE}
# import data
InjuryRecord <- data.table::fread("../input/nfl-playing-surface-analytics/InjuryRecord.csv", stringsAsFactors = F)
PlayerTrackData <- data.table::fread("../input/nfl-playing-surface-analytics/PlayerTrackData.csv", stringsAsFactors = F)
PlayList <- data.table::fread("../input/nfl-playing-surface-analytics/PlayList.csv", stringsAsFactors = F)
```

# Movement Patterns With Injury

The following graphs show the player movement for each body part, separated by field type. From these graphs, I am looking for any patterns in the path of the injured players, specifically field locations or sharp directional changes. Also, the speed of the player at each point in their path is noted by the size of the given point - the larger the point, the faster the speed at that time.

``` {r, include = FALSE}
# Add Field Type To Player Track Data
PlayerTrackData <- PlayerTrackData %>% left_join(PlayList %>% select(PlayKey, FieldType),by="PlayKey")
```

## Knee Injuries
``` {r}
KneeInjuries <- filter(InjuryRecord, PlayKey!= ' ' & BodyPart=='Knee')
KneeInjuryKeys <- as.character(KneeInjuries$PlayKey)
KneeTrackDataSyn <- filter(PlayerTrackData, PlayKey %in% KneeInjuryKeys, FieldType == "Synthetic")
ggplot(KneeTrackDataSyn, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.3) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Knee Injuries on Synthetic Turf")

KneeTrackDataNat <- filter(PlayerTrackData, PlayKey %in% KneeInjuryKeys, FieldType == "Natural")
ggplot(KneeTrackDataNat, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.3) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Knee Injuries on Natural Turf")
```

With knee injuries on synthetic turf, there is a trend of farther distances ran over the course of the play. The injuries on natural turf appear to have a sharper change of direction than they do on synthetic turf. 

## Ankle Injuries
``` {r}
AnkleInjuries <- filter(InjuryRecord, PlayKey!= ' ' & BodyPart=='Ankle')
AnkleInjuryKeys <- as.character(AnkleInjuries$PlayKey)

AnkleTrackDataSyn <- filter(PlayerTrackData, PlayKey %in% AnkleInjuryKeys, FieldType == "Synthetic")
ggplot(AnkleTrackDataSyn, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.2) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Ankle Injuries on Synthetic Turf")  + xlab("Sideline (X Coordinates)") + ylab("Endzone (Y Coordinates")

AnkleTrackDataNat <- filter(PlayerTrackData, PlayKey %in% AnkleInjuryKeys, FieldType == "Natural")
ggplot(AnkleTrackDataNat, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.2) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Ankle Injuries on Natural Turf")  + xlab("Sideline (X Coordinates)") + ylab("Endzone (Y Coordinates")
```

For ankle injuries on natural turf, we tend to see more sharp directional changes; while we still see that on synthetic turf, not every path has a sharp turn. The ankle injuries, though, do not have the players running the length of the field as much as the knee injuries do. 

## Foot Injuries

``` {r}
FootInjuries <- filter(InjuryRecord, PlayKey!= ' ' & BodyPart=='Foot')
FootInjuryKeys <- as.character(FootInjuries$PlayKey)
rm(InjuryRecord)

FootTrackDataSyn <- filter(PlayerTrackData, PlayKey %in% FootInjuryKeys, FieldType == "Synthetic")
ggplot(FootTrackDataSyn, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.2) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Foot Injuries on Synthetic Turf")  + xlab("Sideline (X Coordinates)") + ylab("Endzone (Y Coordinates")

FootTrackDataNat <- filter(PlayerTrackData, PlayKey %in% FootInjuryKeys, FieldType == "Natural")
ggplot(FootTrackDataNat, aes(x=x, y=y, size=s, col=PlayKey)) + geom_point(alpha=0.2) + scale_size(range = c(0.1,5)) + theme(legend.position = "none") + ggtitle("Tracking Plays with Foot Injuries on Natural Turf")  + xlab("Sideline (X Coordinates)") + ylab("Endzone (Y Coordinates")
```

Foot injuries do not necessarily have a clear pattern, either, but there are so few records for this, that any pattern at all might have been mere coincedence.

## Toe & Heel Injuries

For the injuries to the toes and the heel, there are none with specific play keys, so I am unable to map them. 

# Speed Through Time Of Game

In my Exploratory Analysis of Non-Contact Injuries, I highlighted that injuries occur earlier in games on both types of playing surfaces, and hypothesized it was due to players having more energy and making more intense movements earlier in the game. To determine if this is true, I am going to look at average speed by player game play.
First, I am going to run a correlation between Player Game Play and Average Speed on the play.
``` {r average and max speed for playlist, include = FALSE}
PlayerTrackData <- 
    PlayerTrackData %>% group_by(PlayKey) %>%
    mutate(Average_Speed = mean(s, na.rm = T)) %>%  
    mutate(Max_Speed = max(s, na.rm = T)) %>% ungroup()
PlayList <- PlayList %>% left_join(PlayerTrackData %>% select(PlayKey, Average_Speed, Max_Speed), by = "PlayKey")
rm(PlayerTrackData)
```
``` {r average speed correlation}
cor(PlayList$PlayerGamePlay,PlayList$Average_Speed, use="complete.obs")
```
The correlation for average speed returned a value of -0.1364463, meaning as the Player Game Play value (the play number for a given player in a given game) increased, the speed decreased. This fits my theory that more energy is exerted at the beginning of the game. Now, I will have to run an ANOVA to determine whether it is significant. First, I will test the homogeniety of the data.
                                                                                                         
``` {r, include = FALSE}
library(car)
```
``` {r, warning = FALSE}
PlayListSample <- sample_n(PlayList, 5000)
leveneTest(PlayListSample$Average_Speed, PlayListSample$PlayerGamePlay)
```

Equal variance is assumed, from the P-Value greater than 0.05, so I will run a one-way ANOVA to determine if it is statistically significant.                                                                                                        
``` {r}
AvgSpeedAOV <- aov(Average_Speed ~ PlayerGamePlay, data = PlayListSample)
summary(AvgSpeedAOV)                                                                                                         
```
                                                                                                    