###########################################################
#                                                         # 
# Matthew Landes                                          #
#                                                         #
# Kaggle NFL Punt Competition                             #
#                                                         #
# January 2019                                            #
#                                                         #
###########################################################

###########################################################
# Packages
library(DataExplorer)
library(dplyr)
###########################################################


###########################################################
# Read in data
#setwd("input/NFL Punt Analytics Competition")

#2016
post2016 = read.csv("../input/NGS-2016-post.csv",
                    header = TRUE)
pre2016 = read.csv("../input/NGS-2016-pre.csv",
                    header = TRUE)
week1to6_2016 = read.csv("../input/NGS-2016-reg-wk1-6.csv",
                    header = TRUE)
week7to12_2016 = read.csv("../input/NGS-2016-reg-wk7-12.csv",
                         header = TRUE)
week13to17_2016 = read.csv("../input/NGS-2016-reg-wk13-17.csv",
                         header = TRUE)
season2016 = week1to6_2016 %>%
  union(week7to12_2016) %>%
  union(week13to17_2016)
rm(week1to6_2016, week7to12_2016, week13to17_2016)

#2017
post2017 = read.csv("../input/NGS-2017-post.csv",
                    header = TRUE)
pre2017 = read.csv("../input/NGS-2017-pre.csv",
                   header = TRUE)
week1to6_2017 = read.csv("../input/NGS-2017-reg-wk1-6.csv",
                         header = TRUE)
week7to12_2017 = read.csv("../input/NGS-2017-reg-wk7-12.csv",
                          header = TRUE)
week13to17_2017 = read.csv("../input/NGS-2017-reg-wk13-17.csv",
                           header = TRUE)
season2017 = week1to6_2017 %>%
  union(week7to12_2017) %>%
  union(week13to17_2017)
rm(week1to6_2017, week7to12_2017, week13to17_2017)


# Additional Datasets
game_data = read.csv("../input/game_data.csv",
                     header = TRUE)
playInfo = read.csv("../input/play_information.csv",
                    header = TRUE)
playerRole = read.csv("../input/play_player_role_data.csv",
                    header = TRUE)
puntData = read.csv("../input/player_punt_data.csv",
                    header = TRUE)

# Video
control = read.csv("../input/video_footage-control.csv",
                   header = TRUE)
injury = read.csv("../input/video_footage-injury.csv",
                   header = TRUE)
review = read.csv("../input/video_review.csv",
                  header = TRUE)

###########################################################

###########################################################
# Added Data
role1 = c("GL", "GLi", "GLo", "PLT", "PLG", "PLS", "PRG", "PRT", 
          "GR", "GRi", "GRo", "PLW", "PRW", "PC", "PPR", "P")
team1 = c(rep(1, length(role1)))
m1 = as.data.frame(as.matrix(cbind(role1, team1)))
names(m1) = c("Role", "SpecialTeam")
role2 = c("VL", "VLi", "VLo", "PDL1", "PDL2", "PDL3", "PDR3", "PDR2", "PDR1", 
          "VR", "VRi", "VRo", "PLL", "PLL1", "PLM", "PLR", "PFB", "PR")
team2 = c(rep(4, length(role2)))
m2 = as.data.frame(as.matrix.data.frame(cbind(role2, team2)))
names(m2) = c("Role", "SpecialTeam")
roles = m1 %>% 
  union(m2) %>% 
  mutate(SpecialTeam = as.numeric(SpecialTeam))
###########################################################


###########################################################
# Combine Data
combine = injury %>%
  select(-PlayDescription,
         -PREVIEW.LINK..5000K.) %>%
  left_join(review, by=c("season" = "Season_Year", 
                         "gamekey" = "GameKey", 
                         "playid" = "PlayID"))
###########################################################

###########################################################
#2016 Preseason Games
# Concussed Players
inj_pre2016_1 = combine %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "red") %>%
  filter(season == 2016, Type == "Pre") %>%
  left_join(pre2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# Partner Players
inj_pre2016_2 = combine %>%
  mutate(GSISID = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "blue") %>%
  filter(season == 2016, Type == "Pre") %>%
  left_join(pre2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# All other players
inj_pre2016_3 = combine %>%
  mutate(GSISID1 = GSISID,
         GSISID2 = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived, -GSISID) %>%
  mutate(Concussion = "lightgrey") %>%
  filter(season == 2016, Type == "Pre") %>%
  left_join(pre2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID")) %>%
  filter(GSISID != GSISID1 & GSISID != GSISID2 | is.na(GSISID2)) %>%
  select(-GSISID1, -GSISID2)

inj_pre2016 = inj_pre2016_1 %>% 
  union(inj_pre2016_2) %>%
  union(inj_pre2016_3) %>%
  left_join(playerRole, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID")) %>%
  mutate(Role = as.character(Role)) %>%
  left_join(roles, by="Role") %>%
  mutate(Concussion = if_else(Role == "PR" & Concussion == "lightgrey", "darkgoldenrod2", Concussion))
rm(inj_pre2016_1, inj_pre2016_2, inj_pre2016_3)

###########################################################


###########################################################
#2016 Season Games
# Concussed Players
s2016_1 = combine %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "red") %>%
  filter(season == 2016, Type == "Reg") %>%
  left_join(season2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# Partner Players
s2016_2 = combine %>%
  mutate(GSISID = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "blue") %>%
  filter(season == 2016, Type == "Reg") %>%
  left_join(season2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# All other players
s2016_3 = combine %>%
  mutate(GSISID1 = GSISID,
         GSISID2 = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived, -GSISID) %>%
  mutate(Concussion = "lightgrey") %>%
  filter(season == 2016, Type == "Reg") %>%
  left_join(season2016, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID")) %>%
  filter(GSISID != GSISID1 & GSISID != GSISID2 | is.na(GSISID2))  %>%
  select(-GSISID1, -GSISID2)

inj_season2016 = s2016_1 %>% 
  union(s2016_2) %>%
  union(s2016_3) %>%
  left_join(playerRole, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID")) %>%
  mutate(Role = as.character(Role)) %>%
  left_join(roles, by="Role") %>%
  mutate(Concussion = if_else(Role == "PR" & Concussion == "lightgrey", "darkgoldenrod2", Concussion))
rm(s2016_1, s2016_2, s2016_3)

###########################################################


###########################################################
#2017 Preseason Games
# Concussed Players
inj_pre2017_1 = combine %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "red") %>%
  filter(season == 2017, Type == "Pre") %>%
  left_join(pre2017, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# Partner Players
inj_pre2017_2 = combine %>%
  mutate(GSISID = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "blue") %>%
  filter(season == 2017, Type == "Pre") %>%
  left_join(pre2017, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID",
                          "GSISID" = "GSISID"))
# All other players
inj_pre2017_3 = combine %>%
  mutate(GSISID1 = GSISID,
         GSISID2 = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived, -GSISID) %>%
  mutate(Concussion = "lightgrey") %>%
  filter(season == 2017, Type == "Pre") %>%
  left_join(pre2017, by=c("gamekey" = "GameKey",
                          "playid" = "PlayID")) %>%
  filter(GSISID != GSISID1 & GSISID != GSISID2 | is.na(GSISID2)) %>%
  select(-GSISID1, -GSISID2)

inj_pre2017 = inj_pre2017_1 %>% 
  union(inj_pre2017_2) %>%
  union(inj_pre2017_3) %>%
  left_join(playerRole, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID")) %>%
  mutate(Role = as.character(Role)) %>%
  left_join(roles, by="Role") %>%
  mutate(Concussion = if_else(Role == "PR" & Concussion == "lightgrey", "darkgoldenrod2", Concussion))
rm(inj_pre2017_1, inj_pre2017_2, inj_pre2017_3)

###########################################################


###########################################################
#2017 Season Games
# Concussed Players
s2017_1 = combine %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "red") %>%
  filter(season == 2017, Type == "Reg") %>%
  left_join(season2017, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID"))
# Partner Players
s2017_2 = combine %>%
  mutate(GSISID = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived) %>%
  mutate(Concussion = "blue") %>%
  filter(season == 2017, Type == "Reg") %>%
  left_join(season2017, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID"))
# All other players
s2017_3 = combine %>%
  mutate(GSISID1 = GSISID,
         GSISID2 = as.numeric(as.character(Primary_Partner_GSISID))) %>%
  select(-Primary_Partner_GSISID, -Primary_Partner_Activity_Derived, -GSISID) %>%
  mutate(Concussion = "lightgrey") %>%
  filter(season == 2017, Type == "Reg") %>%
  left_join(season2017, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID")) %>%
  filter(GSISID != GSISID1 & GSISID != GSISID2 | is.na(GSISID2)) %>%
  select(-GSISID1, -GSISID2)

inj_season2017 = s2017_1 %>% 
  union(s2017_2) %>%
  union(s2017_3) %>%
  left_join(playerRole, by=c("gamekey" = "GameKey",
                             "playid" = "PlayID",
                             "GSISID" = "GSISID")) %>%
  mutate(Role = as.character(Role)) %>%
  left_join(roles, by="Role") %>%
  mutate(Concussion = if_else(Role == "PR" & Concussion == "lightgrey", "darkgoldenrod2", Concussion))
rm(s2017_1, s2017_2, s2017_3)

###########################################################



################### 2016 Preseason Injuries ###############

p16 = combine %>%
  filter(season == 2016 & Type == "Pre")

for (i in 1:length(p16[,1])) {
  game = inj_pre2016 %>% filter(gamekey == p16[i,7], playid == p16[i,8])
  
  par(bg = "darkgreen")
  par(mai = c(0, 0, 1, 0))
  par(col.main = "white")
  plot(game$x, 
       game$y, 
       col = game$Concussion,
       pch = game$SpecialTeam,
       xlim = c(0, 120),
       ylim = c(0,54),
       main = c(paste(game[1, 1], " ", as.character(game[1,2]), " Week ", game[1, 3]),
                paste(as.character(game[1, 5]), " vs. ", as.character(game[1, 4])),
                paste("GameKey: ", game[1, 7], " PlayID: ", game[1, 8]),
                paste("Activity: ", game[1, 10], " Impact: ", game[1, 12])))
  abline(v=0, col = "white", lwd=5)
  abline(v=10, col = "white", lwd=5)
  abline(v=c(20, 30, 40, 50, 70, 80, 90, 100), col = "white")
  abline(v=60, col = "white", lwd=4)
  abline(v=110, col = "white", lwd=5)
  abline(v=120, col = "white", lwd=5)
  abline(h=0, col = "white", lwd=5)
  abline(h=53.3, col = "white", lwd=5)
  abline(v=-4, col = "darkgreen", lwd=45)
  abline(v=124, col = "darkgreen", lwd=45)
  abline(h=-2, col = "darkgreen", lwd=30)
  abline(h=55.1, col = "darkgreen", lwd=30)
}

################### 2016 Season Injuries ###############

s16 = combine %>%
  filter(season == 2016 & Type == "Reg")

for (i in 1:length(s16[,1])) {
  game = inj_season2016 %>% filter(gamekey == s16[i,7], playid == s16[i,8])
  
  par(bg = "darkgreen")
  par(mai = c(0, 0, 1, 0))
  par(col.main = "white")
  plot(game$x, 
       game$y, 
       col = game$Concussion,
       pch = game$SpecialTeam,
       xlim = c(0, 120),
       ylim = c(0,54),
       main = c(paste(game[1, 1], " ", as.character(game[1,2]), " Week ", game[1, 3]),
                paste(as.character(game[1, 5]), " vs. ", as.character(game[1, 4])),
                paste("GameKey: ", game[1, 7], " PlayID: ", game[1, 8]),
                paste("Activity: ", game[1, 10], " Impact: ", game[1, 12])))
  abline(v=0, col = "white", lwd=5)
  abline(v=10, col = "white", lwd=5)
  abline(v=c(20, 30, 40, 50, 70, 80, 90, 100), col = "white")
  abline(v=60, col = "white", lwd=4)
  abline(v=110, col = "white", lwd=5)
  abline(v=120, col = "white", lwd=5)
  abline(h=0, col = "white", lwd=5)
  abline(h=53.3, col = "white", lwd=5)
  abline(v=-4, col = "darkgreen", lwd=45)
  abline(v=124, col = "darkgreen", lwd=45)
  abline(h=-2, col = "darkgreen", lwd=30)
  abline(h=55.1, col = "darkgreen", lwd=30)
}


################### 2017 Preseason Injuries ###############

p17 = combine %>%
  filter(season == 2017 & Type == "Pre")

for (i in 1:length(p17[,1])) {
  game = inj_pre2017 %>% filter(gamekey == p17[i,7], playid == p17[i,8])
  
  par(bg = "darkgreen")
  par(mai = c(0, 0, 1, 0))
  par(col.main = "white")
  plot(game$x, 
       game$y, 
       col = game$Concussion,
       pch = game$SpecialTeam,
       xlim = c(0, 120),
       ylim = c(0,54),
       main = c(paste(game[1, 1], " ", as.character(game[1,2]), " Week ", game[1, 3]),
                paste(as.character(game[1, 5]), " vs. ", as.character(game[1, 4])),
                paste("GameKey: ", game[1, 7], " PlayID: ", game[1, 8]),
                paste("Activity: ", game[1, 10], " Impact: ", game[1, 12])))
  abline(v=0, col = "white", lwd=5)
  abline(v=10, col = "white", lwd=5)
  abline(v=c(20, 30, 40, 50, 70, 80, 90, 100), col = "white")
  abline(v=60, col = "white", lwd=4)
  abline(v=110, col = "white", lwd=5)
  abline(v=120, col = "white", lwd=5)
  abline(h=0, col = "white", lwd=5)
  abline(h=53.3, col = "white", lwd=5)
  abline(v=-4, col = "darkgreen", lwd=45)
  abline(v=124, col = "darkgreen", lwd=45)
  abline(h=-2, col = "darkgreen", lwd=30)
  abline(h=55.1, col = "darkgreen", lwd=30)
}

################### 2017 Season Injuries ###############

s17 = combine %>%
  filter(season == 2017 & Type == "Reg")

for (i in 1:length(s17[,1])) {
  game = inj_season2017 %>% filter(gamekey == s17[i,7], playid == s17[i,8])
  
  par(bg = "darkgreen")
  par(mai = c(0, 0, 1, 0))
  par(col.main = "white")
  plot(game$x, 
       game$y, 
       col = game$Concussion,
       pch = game$SpecialTeam,
       xlim = c(0, 120),
       ylim = c(0,54),
       main = c(paste(game[1, 1], " ", as.character(game[1,2]), " Week ", game[1, 3]),
                paste(as.character(game[1, 5]), " vs. ", as.character(game[1, 4])),
                paste("GameKey: ", game[1, 7], " PlayID: ", game[1, 8]),
                paste("Activity: ", game[1, 10], " Impact: ", game[1, 12])))
  abline(v=0, col = "white", lwd=5)
  abline(v=10, col = "white", lwd=5)
  abline(v=c(20, 30, 40, 50, 70, 80, 90, 100), col = "white")
  abline(v=60, col = "white", lwd=4)
  abline(v=110, col = "white", lwd=5)
  abline(v=120, col = "white", lwd=5)
  abline(h=0, col = "white", lwd=5)
  abline(h=53.3, col = "white", lwd=5)
  abline(v=-4, col = "darkgreen", lwd=45)
  abline(v=124, col = "darkgreen", lwd=45)
  abline(h=-2, col = "darkgreen", lwd=30)
  abline(h=55.1, col = "darkgreen", lwd=30)
}