# Kaggle Competition
# NFL punt rule suggestion
# by Sandra Sexton and Daniel Matutat
# January 8, 2019

# Set working directory
# Need packages: ggplot2, dplyr
library(ggplot2)
library(dplyr)
library(reshape2)
library(utils)

#setwd("/kaggle/input")
getwd()

# Import datasets
video_review <- read.csv("../input/video_review.csv", header=TRUE)
video_footage.injury <- read.csv("../input/video_footage-injury.csv", header=TRUE)
video_footage.control <- read.csv("../input/video_footage-control.csv", header=TRUE)
player.punt.data <- read.csv("../input/player_punt_data.csv", header=TRUE)
player.role.data <- read.csv("../input/play_player_role_data.csv", header=TRUE)

# Rename column names to match amoungst datasets and differentiate injury
colnames(video_review)[colnames(video_review)=="PlayID"] <- "playid"
colnames(video_review)[colnames(video_review)=="GameKey"] <- "gamekey"
colnames(video_review)[colnames(video_review)=="Season_Year"] <- "season"
colnames(video_footage.control)[colnames(video_footage.control)=="Season_Type"] <- "Type"
colnames(video_footage.injury)[colnames(video_footage.injury)=="PREVIEW.LINK..5000K."] <- "Preview.Link"
colnames(player.role.data)[colnames(player.role.data)=="Season_Year"] <- "season"
colnames(player.role.data)[colnames(player.role.data)=="GameKey"] <- "gamekey"
colnames(player.role.data)[colnames(player.role.data)=="PlayID"] <- "playid"
video_footage.control$Injury= "no injury"
video_footage.injury$Injury= "injury"

# Combine datasets
file1 <- rbind(video_footage.control, video_footage.injury)
file2 <- merge(video_review, file1, by=c("playid", "gamekey", "season"), all.y=TRUE)

#---------------------------------------------------------

# Get numbers of injured players using player_punt_data
file3 <- merge(file2, player.punt.data, by=c("GSISID"), all.x=TRUE)

# Upon examination of merged datasets, there are 21 more rows than there should be. 
# Some players have multiple numbers. Must look in video footage and internet search to identify correct number and delete incorrect.
file4 <- file3[-c(14, 15, 17, 21, 23, 25, 26, 29, 31, 33, 34, 37, 39, 42, 44, 45, 47, 51, 53, 56, 57),]

# Get numbers of injury causing players using player_punt_data
# For multiple numbers, look in video footage to identify correct number and delete incorrect.
file5 <- merge(file4, player.punt.data, by.x=c("Primary_Partner_GSISID"), by.y=c("GSISID"), all.x=TRUE)

# Upon examination of merged datasets, there are 24 more rows than there should be. 
# Some players have multiple numbers. Must look in video footage and internet search to identify correct number and delete incorrect.
file6 <- file5[-c(9, 10, 11, 13, 15, 16, 18, 19, 23, 24, 27, 31, 35, 37, 39, 40, 42, 43, 49, 50, 52, 53, 54, 58),]

#---------------------------------------------------------

# Get role of injured person in the game
file7 <- merge(player.role.data, file6, by=c("season", "gamekey", "playid", "GSISID"), all.y=TRUE)

# Get role of injuring person in the game
colnames(player.role.data)[colnames(player.role.data)=="GSISID"] <- "Primary_Partner_GSISID"
file8 <- merge(player.role.data, file7, by=c("season", "gamekey", "playid", "Primary_Partner_GSISID"), all.y=TRUE)

# Rename some columns for more clarity
colnames(file8)[colnames(file8)=="season"] <- "Season"
colnames(file8)[colnames(file8)=="gamekey"] <- "Game_Key"
colnames(file8)[colnames(file8)=="playid"] <- "Play_Id"
colnames(file8)[colnames(file8)=="Primary_Partner_GSISID"] <- "Injuring_GSISID"
colnames(file8)[colnames(file8)=="Role.x"] <- "Injuring_Role"
colnames(file8)[colnames(file8)=="Role.y"] <- "Injured_Role"
colnames(file8)[colnames(file8)=="Player_Activity_Derived"] <- "Injured_Activity"
colnames(file8)[colnames(file8)=="Primary_Partner_Activity_Derived"] <- "Injuring_Activity"
colnames(file8)[colnames(file8)=="Preview.Link"] <- "Preview_Link"
colnames(file8)[colnames(file8)=="Number.x"] <- "Injured_Number"
colnames(file8)[colnames(file8)=="Position.x"] <- "Injured_Position"
colnames(file8)[colnames(file8)=="Number.y"] <- "Injuring_Number"
colnames(file8)[colnames(file8)=="Position.y"] <- "Injuring_Position"
colnames(file8)[colnames(file8)=="GSISID"] <- "Injured_GSISID"

# Rearranged columns to make easier to read
file8 <- file8[c(1,2,3,14,13,15,16,17,9,20,10,12,6,4,7,5,8,11,22,24,21,23,18,19)]

#---------------------------------------------
# Extract nature of play - faircatch, outofbounds, tackle
extractPlay <- function(PlayDescription) {
  PlayDescription <- as.character(PlayDescription)
  
  if(length(grep("fair", PlayDescription)) >0) {
    return("faircatch")
  } else if(length(grep(", out of bounds", PlayDescription)) >0) {
    return("out of bounds")
  } else {
    return("tackle")
  }
}

Play <- NULL
for(i in 1:nrow(file8)){
  Play <- c(Play, extractPlay(file8[i,"PlayDescription"]))
}
file8$Play <- as.factor(Play)
#--------------------------------------------

# Usage of outside data is allowed.
# Since not available in datasets, use videos to determine seconds between punt and injury, injured offense or defense,
# injuring offense or defense, injured hit above or below waist, injuring hit above or below waist, injured hit front/ side/ back,
# who had ball when injury happened, punt returner involved in injury event.
video.data = data.frame(
  Game_Key=c(5,21,29,45,54,60,144,149,189,218,231,234,266,274,280,280,281,289,296,357,364,364,384,392,397,399,414,448,473,506,553,567,585,585,601,607,618),
  Play_Id=c(3129,2587,538,1212,1045,905,2342,3663,3509,3468,1976,3278,2902,3609,2918,3746,1526,2341,2667,3630,2489,2764,183,1088,1526,3312,1262,2792,2072,1988,1683,1407,733,2208,602,978,2792),
  Sec_Punt_Injury=c(9,4,5,12,NA,0,NA,8,10,NA,10,6,4,NA,0,9,7,9,5,8,6,7,6,12,9,6,10,9,8,9,8,12,10,10,7,0,12),
  Injured_Off_Def=c("defense","defense","defense","defense","unsure","defense","offense","offense","offense","unsure","defense","offense","offense","defense","defense","defense","defense","defense","defense","defense","defense","offense","defense","defense","defense","offense","defense","defense","defense","offense","defense","offense","defense","defense","defense","defense","defense"),
  Injuring_Off_Def=c("offense","offense","offense","offense","unsure","offense","defense","defense","defense","unsure","offense","defense","defense","offense","offense","offense","defense","offense","defense","offense","offense","defense","defense","offense","offense","defense","na","offense","defense","defense","offense","defense","offense","offense","offense","offense","offense"),
  Injured_Waist=c("above","above","above","above","unsure","above","unsure","above","above","unsure","above","above","below","above","above","above","above","above","above","above","above","above","above","above","above","above","na","above","above","above","above","above","above","above","above","above","above"),
  Injuring_Waist=c("below","above","below","above","unsure","above","unsure","above","above","unsure","above","above","above","above","above","above","above","above","above","above","above","above","below","above","above","above","na","above","below","above","above","above","above","above","above","above","above"),  
  Injured_Direction=c("front","front","front","front","unsure","front","unsure","front","front","unsure","front","front","front","side","front","front","front","side","front","side","side","front","front","front","front","front","na","side","front","front","front","front","side","side","front","front","side"),  
  Ball_Holder=c("PR","noone","PR","PR","unsure","kicker","unsure","PR","PR","unsure","PR","defense","PR","kicker","kicker","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","PR","kicker","PR"),
  PR_Secondary=c("no","no","no","no","no","no","no","no","no","no","no","no","no","no","no","no","no","no","PR","no","no","no","PR","no","no","no","no","no","PR","no","no","no","no","no","no","no","no")
)

# Add to file8 dataset
file8 <- merge(file8, video.data, by=c("Game_Key", "Play_Id"), all.x=TRUE)

#--------------------------------------------
#Use given datasets to do some exploratory charts
# Is regular or pre-season significant?
file8.typetable <- table(factor(file8$Type))
file8.type <- barplot(file8.typetable, main="Injuries Regular vs Pre-Season")

# Is quarter significant?
file8.qtrtable <- table(factor(file8$Qtr))
file8.qtr <- barplot(file8.qtrtable, main="Quarter Injuries Occurred in", xlab="quarter")

# Is week significant?
file8.weektable <- table(factor(file8$Week))
file8.week <- barplot(file8.weektable, main="Week that Injuries Occurred in", xlab="week")

# What is primary impact type?
file8.impacttable <- table(factor(file8$Primary_Impact_Type))
file8.impact <- barplot(file8.impacttable, main="Primary Impact Type for Injuries")

# Was it friendly fire?
file8.friendlytable <- table(factor(file8$Friendly_Fire))
# file8.friendlytable %>% filter(is.na(file8.friendlytable) == FALSE) %>%
file8.friendly <- barplot(file8.friendlytable, main="Friendly Fire Occurrences for Injuries")

# In injury gorup, how many were tackles, fair catch or out of bounds play?
file8.naturepre <- droplevels(subset(file8, Injury=="injury"))
file8.naturetable <- table(factor(file8.naturepre$Play))
file8.nature <- barplot(file8.naturetable, main="Nature of Play that Caused the Injury")

# In non-injury group, how many were tackles, fair catch or out of bounds play?
file8.naturepre2 <- droplevels(subset(file8, Injury=="no injury"))
file8.naturetable2 <- table(factor(file8.naturepre2$Play))
file8.nature2 <- barplot(file8.naturetable2, main="Nature of Play within Non-Injury Group")

# Chart Play according to injury and non-injury datasets
ggplot(file8, aes(x=Play, fill=Injury)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727','#ff7676')) +
  ggtitle("Nature of Play for Injured vs Injuring Event")


#-----------------------------------------------------------
#Use data retrieved from the videos
# How many seconds between the punt and injury event?
file8.sectable <- table(factor(file8$Sec_Punt_Injury))
file8.sec <- barplot(file8.sectable, main="What is the Number of Seconds Between Punt and Injury", xlab="seconds")

# Is the injured player offense or defense?
file8.table1 <- table(factor(file8$Injured_Off_Def))
file8.injured.off <- barplot(file8.table1, main="Is Injured Player Offense or Defense?")

# Is the injuring player offense or defense?
file8.table2 <- table(factor(file8$Injuring_Off_Def))
file8.injuring.off <- barplot(file8.table2, main="Is Injuring Player Offense or Defense?")

# Is the injured player hit above or below the waist?
file8.table3 <- table(factor(file8$Injuring_Waist))
file8.hit <- barplot(file8.table3, main="Was the Injured Player Hit Above or Below the Waist?")

# Is the injured player hit from the front, side or back?
file8.table4 <- table(factor(file8$Injured_Direction))
file8.front <- barplot(file8.table4, main="Was the Injured Player Hit From the Front, Side or Back?")

# Who had the ball when the injury happened?
file8.table5 <- table(factor(file8$Ball_Holder))
file8.ball <- barplot(file8.table5, main="Who Had the Ball When the Injury Happened?")

#---------------------------------------------------
# What was the position of injured person?
file8.subset1 <- droplevels(subset(file8, Injury=='injury'))
file8.subset2 <- subset(file8.subset1, select=c("Injured_Position", "Injuring_Position", "Injured_GSISID"))
file8.subset3 <- melt(file8.subset2, id.vars="Injured_GSISID", na.rm=TRUE)
colnames(file8.subset3)[3] <- "Position"
colnames(file8.subset3)[2] <- "Injury"
file8.subset4 <- subset(file8.subset3, Injury=='Injured_Position')
ggplot(file8.subset4, aes(x=Position)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Position of Injured Person")
# Seems mostly evenly distributed across 6 different positions

# What was the position of the injuring person?
file8.subset5 <- subset(file8.subset3, Injury=='Injuring_Position')
ggplot(file8.subset5, aes(x=Position)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Position of Injuring Person")
# OLB is clearly the most prominent injuring person. By their very nature, they are...

# What was the combination of positions of injured person playing & injuring position 
ggplot(file8.subset3, aes(x=Position, fill=Injury)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727','#ff7676', '#4dff4d')) +
  ggtitle("Number of Times Certain Position Involved in Injured vs Injuring Event")

#-------------------------------------------------------------
# What was the activity of injured person?
file8.subset1 <- droplevels(subset(file8, Injury=='injury'))
file8.subset5 <- subset(file8.subset1, select=c("Injured_Activity", "Injuring_Activity", "Injured_GSISID"))
file8.subset6 <- melt(file8.subset5, id.vars="Injured_GSISID", na.rm=TRUE)
colnames(file8.subset6)[3] <- "Activity"
colnames(file8.subset6)[2] <- "Injury"
file8.subset7 <- subset(file8.subset6, Injury=='Injured_Activity')
ggplot(file8.subset7, aes(x=Activity)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Activity of Injured Person")
# Seems somewhat divided across 5 roles

# What was the activity of the injuring person?
file8.subset8 <- subset(file8.subset6, Injury=='Injuring_Activity')
ggplot(file8.subset8, na.rm = TRUE, aes(x=Activity)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Activity of Injuring Person")
# PR has almost double the number of the next closest one

# What was the combination of activities of injured person playing & injuring position 
ggplot(file8.subset6, aes(x=Activity, fill=Injury)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727','#ff7676', '#4dff4d')) +
  ggtitle("Number of Times Certain Activity Involved in Injured vs Injuring Event")

#------------------------------------------------
# What was the role of injured person?
file8.subset1 <- droplevels(subset(file8, Injury=='injury'))
file8.subset5 <- subset(file8.subset1, select=c("Injured_Role", "Injuring_Role", "Injured_GSISID"))
file8.subset9 <- melt(file8.subset5, id.vars="Injured_GSISID", na.rm=TRUE)
colnames(file8.subset9)[3] <- "Role"
colnames(file8.subset9)[2] <- "Injury"
file8.subset10 <- subset(file8.subset9, Injury=='Injured_Role')
ggplot(file8.subset10, aes(x=Role)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Role of Injured Person")

# What was the role of the injuring person?
file8.subset11 <- subset(file8.subset9, Injury=='Injuring_Role')
ggplot(file8.subset11, na.rm = TRUE, aes(x=Role)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727')) +
  ggtitle("Role of Injuring Person")

# What was the combination of roles of injured person playing & injuring position 
ggplot(file8.subset9, aes(x=Role, fill=Injury)) + 
  geom_bar(stat="count", width=0.2) +
  scale_fill_manual(values=c('#ff2727','#ff7676', '#4dff4d')) +
  ggtitle("Number of Times Certain Role Involved in Injured vs Injuring Event")