{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Clear environment\nrm(list = ls())\n`%notin%` <- Negate(`%in%`)\n\n\n# Setting the random number generator seed so that our results are reproducible\nset.seed(1)\n\nlibrary(dplyr)\nlibrary(GGally)\nlibrary(data.table)\nlibrary(ggplot2)\nlibrary(devtools)\nlibrary(ggpubr)\n\n#library(weatherData)\n#library(rnoaa)\n\n## gg_field function - set up as a list of annotations\n## lots of thanks to mfurman for the field layout graphic. makes the visuals great\ngg_field <- function(yardmin=0, yardmax=120, buffer=5, direction=\"horiz\",\n                     field_color=\"forestgreen\",line_color=\"white\",\n                     sideline_color=field_color, endzone_color=\"darkgreen\"){\n  \n  ## field dimensions (units=yards)\n  xmin <- 0\n  xmax <- 120\n  \n  ymin <- 0\n  ymax <- 53.33\n  \n  \n  ## distance from sideline to hash marks in middle (70 feet, 9 inches)\n  hash_dist <- (70*12+9)/36\n  \n  ## yard lines locations (every 5 yards) \n  yd_lines <- seq(15,105,by=5)\n  \n  ## hash mark locations (left 1 yard line to right 1 yard line)\n  yd_hash <- 11:109\n  \n  ## field number size\n  num_size <- 5\n  \n  ## rotate field numbers with field direction\n  ## first element is for right-side up numbers, second for upside-down\n  angle_vec <- switch(direction, \"horiz\" = c(0, 180), \"vert\" = c(270, 90))\n  num_adj <- switch(direction, \"horiz\" = c(-1, 1), \"vert\" = c(1, -1))\n  \n  ## list of annotated geoms\n  p <- list(\n    \n    ## add field background \n    annotate(\"rect\", xmin=xmin, xmax=xmax, ymin=ymin-buffer, ymax=ymax+buffer, \n             fill=field_color),\n    \n    ## add end zones\n    annotate(\"rect\", xmin=xmin, xmax=xmin+10, ymin=ymin, ymax=ymax, fill=endzone_color),\n    annotate(\"rect\", xmin=xmax-10, xmax=xmax, ymin=ymin, ymax=ymax, fill=endzone_color),\n    \n    ## add yardlines every 5 yards\n    annotate(\"segment\", x=yd_lines, y=ymin, xend=yd_lines, yend=ymax,\n             col=line_color),\n    \n    ## add thicker lines for endzones, midfield, and sidelines\n    annotate(\"segment\",x=c(0,10,60,110,120), y=ymin, xend=c(0,10,60,110,120), yend=ymax,\n             lwd=1.3, col=line_color),\n    annotate(\"segment\",x=0, y=c(ymin, ymax), xend=120, yend=c(ymin, ymax),\n             lwd=1.3, col=line_color) ,\n    \n    ## add field numbers (every 10 yards)\n    ## field numbers are split up into digits and zeros to avoid being covered by yard lines\n    ## numbers are added separately to allow for flexible ggplot stuff like facetting\n    \n    ## 0\n    annotate(\"text\",x=seq(20,100,by=10) + num_adj[2], y=ymin+12, label=0, angle=angle_vec[1],\n             col=line_color, size=num_size),\n    \n    ## 1\n    annotate(\"text\",label=1,x=c(20,100) + num_adj[1], y=ymin+12, angle=angle_vec[1],\n             colour=line_color, size=num_size),\n    ## 2\n    annotate(\"text\",label=2,x=c(30,90) + num_adj[1], y=ymin+12, angle=angle_vec[1],\n             colour=line_color, size=num_size),\n    ## 3\n    annotate(\"text\",label=3,x=c(40,80) + num_adj[1], y=ymin+12, angle=angle_vec[1],\n             colour=line_color, size=num_size),\n    ## 4\n    annotate(\"text\",label=4,x=c(50,70) + num_adj[1], y=ymin+12, angle=angle_vec[1],\n             colour=line_color, size=num_size),\n    ## 5\n    annotate(\"text\",label=5,x=60 + num_adj[1], y=ymin+12, angle=angle_vec[1],\n             colour=line_color, size=num_size),\n    \n    \n    ## upside-down numbers for top of field\n    \n    ## 0\n    annotate(\"text\",x=seq(20,100,by=10) + num_adj[1], y=ymax-12, angle=angle_vec[2],\n             label=0, col=line_color, size=num_size),\n    ## 1\n    annotate(\"text\",label=1,x=c(20,100) + num_adj[2], y=ymax-12, angle=angle_vec[2],\n             colour=line_color, size=num_size),\n    ## 2\n    annotate(\"text\",label=2,x=c(30,90) + num_adj[2], y=ymax-12, angle=angle_vec[2],\n             colour=line_color, size=num_size),\n    ## 3\n    annotate(\"text\",label=3,x=c(40,80) + num_adj[2], y=ymax-12, angle=angle_vec[2],\n             colour=line_color, size=num_size),\n    ## 4\n    annotate(\"text\",label=4,x=c(50,70) + num_adj[2], y=ymax-12, angle=angle_vec[2],\n             colour=line_color, size=num_size),\n    ## 5\n    annotate(\"text\",label=5,x=60 + num_adj[2], y=ymax-12, angle=angle_vec[2],\n             colour=line_color, size=num_size),\n    \n    \n    ## add hash marks - middle of field\n    annotate(\"segment\", x=yd_hash, y=hash_dist - 0.5, xend=yd_hash, yend=hash_dist + 0.5,\n             color=line_color),\n    annotate(\"segment\", x=yd_hash, y=ymax - hash_dist - 0.5, \n             xend=yd_hash, yend=ymax - hash_dist + 0.5,color=line_color),\n    \n    ## add hash marks - sidelines\n    annotate(\"segment\", x=yd_hash, y=ymax, xend=yd_hash, yend=ymax-1, color=line_color),\n    annotate(\"segment\", x=yd_hash, y=ymin, xend=yd_hash, yend=ymin+1, color=line_color),\n    \n    ## add conversion lines at 2-yard line\n    annotate(\"segment\",x=12, y=(ymax-1)/2, xend=12, yend=(ymax+1)/2, color=line_color),\n    annotate(\"segment\",x=108, y=(ymax-1)/2, xend=108, yend=(ymax+1)/2, color=line_color),\n    \n    ## cover up lines outside of field with sideline_color\n    annotate(\"rect\", xmin=0, xmax=xmax, ymin=ymax, ymax=ymax+buffer, fill=sideline_color),\n    annotate(\"rect\",xmin=0, xmax=xmax, ymin=ymin-buffer, ymax=ymin, fill=sideline_color),\n    \n    ## remove axis labels and tick marks\n    labs(x=\"\", y=\"\"),\n    theme(axis.text.x = element_blank(),axis.text.y = element_blank(),\n          axis.ticks = element_blank()),\n    \n    ## clip axes to view of field\n    if(direction==\"horiz\"){\n      coord_cartesian(xlim=c(yardmin, yardmax), ylim = c(ymin-buffer,ymax+buffer), \n                      expand = FALSE)\n      \n    } else if (direction==\"vert\"){\n      ## flip entire plot to vertical orientation\n      coord_flip(xlim=c(yardmin, yardmax), ylim = c(ymin-buffer,ymax+buffer), expand = FALSE)\n      \n    }\n  )\n  \n  return(p)\n\n}\nggplot() + gg_field()\n\ngames <- read.csv('../input/nfl-big-data-bowl-2022/games.csv', header=TRUE)\nglimpse(games)\n\ntrak18 <- fread('../input/nfl-big-data-bowl-2022/tracking2018.csv')\n\nglimpse(trak18)\n\nunique(trak18$event)\n\n# This is only for kickoffs and returns\ntrak18kickplays <- trak18 %>%\n  filter(event == 'kickoff') %>%\n  mutate(game_play = paste0(gameId, '_', playId))\nglimpse(trak18kickplays)\n\nunique(trak18kickplays$event)\n\ntrak18kick_id_list <- unique(trak18kickplays$game_play)\ntrak18kick_id_list[1:40]\n\ntrak18 <- trak18 %>%\n  mutate(game_play = paste0(gameId, '_', playId)) %>%\n  filter(game_play %in% trak18kick_id_list) %>%\n  filter(displayName == 'football')\n\nglimpse(trak18)\n\n#which(trak18$event == 'field_goal_missed')\n\nunique(trak18$displayName)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T23:37:22.976286Z","iopub.execute_input":"2022-01-06T23:37:22.977991Z","iopub.status.idle":"2022-01-06T23:38:03.437506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kickdf1 <- data.frame()\nreturndf1 <- data.frame()\nplayID_list <-unique(trak18$game_play)[1:100]\n# testtab <- trak18[trak18$game_play=='2018123000_36']\nfor(i in playID_list){\n  print(i)\n  \n  gp <- trak18 %>%\n  filter(game_play == i)\n  \n  kick2 <- gp[gp$event == 'kickoff']\n  \n\n  initcatch2 <- gp[gp$event == 'kick_received']\n \n  \n  finalpos2 <- gp[gp$event == 'tackle']\n  \n  \n  returndf2 <- rbind(initcatch2, finalpos2) %>%\n    select(x, y, playDirection) %>%\n    mutate(game_play = i) %>%\n    as.data.frame()\n\n  kickdf2 <- rbind(kick2, initcatch2) %>%\n    select(x, y, playDirection) %>%\n    mutate(game_play = i) %>%\n    as.data.frame()\n\n \n  \n  returndf1 <- rbind(returndf1, returndf2)\n  \n  kickdf1 <- rbind(kickdf1, kickdf2)\n}\n\n\n\np_return_big <-  ggplot(data = returndf1, aes(x, y)) +\n  gg_field() + \n  geom_point(col='gold', cex=2) +\n  geom_line(aes(group = game_play, col=playDirection)) +\n  theme(legend.position=\"none\") +\n  labs(title = \"Return Plays\", y = \"\", x = \"\")\n\n  \n\np_return_big\n\np_kick_big <-  ggplot(data = kickdf1, aes(x, y)) +\n  gg_field() + \n  geom_point(col='gold', cex=2) +\n  geom_line(aes(group = game_play, col=playDirection)) +\n  theme(legend.position=\"none\")+\n  labs(title = \"Kickoff Plays\", y = \"\", x = \"\")\n  \n\np_kick_big\n\n\n\n# The above was used for figures, now Im going to make a wide dataframe for calculating distances easily.\n\nkickdf3 <- data.frame()\nreturndf3 <- data.frame()\nplayID_list <-unique(trak18$game_play)[1:100]\n# testtab <- trak18[trak18$game_play=='2018123000_36']\nfor(i in playID_list){\n  print(i)\n  \n  gp <- trak18 %>%\n  filter(game_play == i)\n  \n  kick5 <- gp[gp$event == 'kickoff']\n\n  initcatch5 <- gp[gp$event == 'kick_received']\n  colnames(initcatch5)[which(names(initcatch5) == 'x')] <- 'x2'\n  colnames(initcatch5)[which(names(initcatch5) == 'y')] <- 'y2'\n  \n  finalpos5 <- gp[gp$event == 'tackle']\n  colnames(finalpos5)[which(names(finalpos5) == 'x')] <- 'x3'\n  colnames(finalpos5)[which(names(finalpos5) == 'y')] <- 'y3'\n  \n  # returndf2 <- rbind(initcatch2, finalpos2) %>%\n  #   select(x, y) %>%\n  #   mutate(playid = i) %>%\n  #   as.data.frame()\n  # \n  # kickdf2 <- rbind(kick2, initcatch2) %>%\n  #   select(x, y) %>%\n  #   mutate(playid = i) %>%\n  #   as.data.frame()\n  \n  returndf10 <- cbind(initcatch5, finalpos5) %>%\n    select(x2, y2, x3, y3) %>%\n    mutate(game_play = i) %>%\n    as.data.frame()\n\n  kickdf10 <- cbind(kick5, initcatch5) %>%\n    select(x, y, x2, y2) %>%\n    mutate(game_play = i) %>%\n    as.data.frame()\n  \n  returndf3 <- rbind(returndf3, returndf10)\n  \n  kickdf3 <- rbind(kickdf3, kickdf10)\n}\n\nreturndf3 <- returndf3 %>%\n  mutate(distance = abs(x3-x2))\nglimpse(returndf3)\n\nkickdf3 <- kickdf3 %>%\n  mutate(distance = abs(x2-x))\nglimpse(kickdf3)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T23:38:17.763172Z","iopub.execute_input":"2022-01-06T23:38:17.765595Z","iopub.status.idle":"2022-01-06T23:38:23.262779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays <- fread('../input/nfl-big-data-bowl-2022/plays.csv')\n\nplays <- plays %>%\n  mutate(game_play = paste0(gameId, '_', playId)) %>%\n  select(game_play, gameId, kickLength, kickReturnYardage, specialTeamsPlayType)\n\npffscout <- fread('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\n\npffscout <- pffscout %>%\n  mutate(game_play = paste0(gameId, '_', playId)) %>%\n  select(game_play, hangTime, kickContactType, kickType)\n\ndata <- left_join(plays, pffscout, by = 'game_play')\n\ngames <- games %>%\n  select(gameId, gameDate, gameTimeEastern, homeTeamAbbr)\n\ndata <- left_join(data, games, by = 'gameId')\n\nglimpse(data)\n\nstadiums_list <- unique(data$homeTeamAbbr)\nstadiums_list\n\ncovereddf <- data.frame(homeTeamAbbr = stadiums_list, covered = c(0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1))\n\ndata <- left_join(data, covereddf, by = 'homeTeamAbbr')\ndata$covered <- as.factor(data$covered)\nkickdat <- data %>%\n  filter(specialTeamsPlayType == 'Kickoff')\n\npuntdat <- data %>%\n  filter(specialTeamsPlayType == 'Punt')\n\np_cov_kick_vio <- ggplot(kickdat, aes(x=as.factor(covered), y=kickLength)) +\n  geom_violin() +\n  stat_compare_means(method = \"t.test\") +\n  labs(title = \"Kickoff Plays\", y = \"Kick Length\", x = \"Covered - 1 vs Uncovered - 0 Stadiums\")\n\np_cov_kick_vio\n\np_cov_kick_jitter <- ggplot(data, aes(x=as.factor(covered), y=kickLength, color = kickType, alpha=0.2)) +\n  geom_jitter() +\n  stat_compare_means(method = \"t.test\") +\n  labs(title = \"Kickoff Plays\", y = \"Kick Length\", x = \"Covered - 1 vs Uncovered - 0 Stadiums\")\n\np_cov_kick_jitter\n\n\np_cov_punt_vio <- ggplot(puntdat, aes(x=as.factor(covered), y=kickLength)) +\n  geom_violin() +\n  stat_compare_means(method = \"t.test\") +\n  labs(title = \"Punt Plays\", y = \"Kick Length\", x = \"Covered - 1 vs Uncovered - 0 Stadiums\")\n\n\np_cov_punt_vio\n\np_cov_punt_jitter <- ggplot(data, aes(x=as.factor(covered), y=kickLength, color = kickType, alpha=0.2)) +\n  geom_jitter() +\n  stat_compare_means(method = \"t.test\") +\n  labs(title = \"Punt Plays\", y = \"Kick Length\", x = \"Covered - 1 vs Uncovered - 0 Stadiums\")\n\n\np_cov_punt_jitter\n\n\nkickcors <- ggpairs(kickdat,\n                   columns=c(6, 3, 4),\n                   mapping = aes(color = covered),\n                   switch = \"both\",\n                   upper = list(continuous = wrap(\"cor\")),\n                   lower=list(continuous = wrap(\"points\",size= 2)),\n                   diag = list(continuous = wrap(\"densityDiag\",alpha = 0.5))) +\n  theme_grey(base_size=8)\nkickcors\n\n\npuntcors <- ggpairs(puntdat,\n                   columns=c(6, 3, 4),\n                   mapping = aes(color = covered),\n                   switch = \"both\",\n                   upper = list(continuous = wrap(\"cor\")),\n                   lower=list(continuous = wrap(\"points\",size= 2)),\n                   diag = list(continuous = wrap(\"densityDiag\",alpha = 0.5))) +\n  theme_grey(base_size=8)\npuntcors\n\nkick_scat_type <- ggplot(kickdat, aes(x=kickLength, y=kickReturnYardage, color=kickType, alpha=0.5)) +\n  geom_point() +\n  labs(title = \"Kickoff Plays\", x = \"Kick Length\", y = \"Return Yards\")\n\n\nkick_scat_type\n\npunt_scat_type <- ggplot(puntdat, aes(x=kickLength, y=kickReturnYardage, color=kickType, alpha=0.2)) +\n  geom_point() +\n  labs(title = \"Punt Plays\", x = \"Kick Length\", y = \"Return Yards\")\n\n\npunt_scat_type\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T23:38:48.266446Z","iopub.execute_input":"2022-01-06T23:38:48.268053Z","iopub.status.idle":"2022-01-06T23:38:58.733479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}