For Assignment 3 we were focusing on a certain play, regarding a punt return. That play was an 84- Yard punt return by Isiah Mckenzie of the Buffalo Bills. The animation had something called a traffic meter which represents the amount of traffic Isiah McKenzie must move around during the play. The initial metric calculated the distance from each defender to McKenzie and created an average which is ultimately the traffic meter number.Even though that measurement was accurate when a defender is behind or front of you matters more rather than how far theyโre. That why we calculated the remainder of the returner location divided by the opponents location. The reason behind using remainder is it gives us an idea of how much ground the opponent would have to make up in order to tackle the return. SO from that if the remainder is less than 1 the impact level is a 1 and if it is greater than 1 it is -1. The reason for this is to have a clear distinction whether or not a defender has an impact.
#Buffalo (home) vs Miami Dolphins (away)
suppressWarnings(suppressMessages(library(data.table)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(gganimate)))
suppressWarnings(suppressMessages(library(gifski)))
setwd("U:/")
#read in the necessary files for tracking and plays
player_tracking_df<- fread("tracking2020.csv")
plays_df <- fread("plays.csv")
player_stats <- function(pt_df,p_df,game_ID,play_ID,jersey){
# I am interested, for demo purposes, in play 1586 in game 2021010300
#game_ID <- c("2021010300")
#play_ID <- c("1586", "395", "1232", "3930")
df <- player_tracking_df %>%
filter(gameId %in% game_ID & playId %in% play_ID) %>%
left_join(plays_df, by = c("gameId" = "gameId", "playId" = "playId")) %>%
select(gameId, playId, time, x, y, dis, nflId, jerseyNumber, team,
frameId, playDescription,s,playDirection) %>%
data.frame()
#Removes data
#rm(player_tracking_df)
#rm(plays_df)
df19 <- df %>%
select(gameId, playId, time, jerseyNumber, x, y, frameId, dis, s) %>%
filter(jerseyNumber == 19) %>%
data.frame()
df2 <- left_join(df, df19, by = c("gameId" = "gameId",
"playId" = "playId",
"time" = "time",
"frameId" = "frameId")) %>%
select(gameId, playId, time, x.x, y.x, nflId, jerseyNumber.x,
frameId, x.y, y.y,team, playDescription, dis.y, s.y,playDirection) %>%
# compute the distance from player 19 to all opposing players, otherwise NA
mutate(dist = ifelse(team == "away",
#returner is x.x and y.x
#opposition is x.y and y.y
sqrt((x.x - x.y)^2 + (y.x - y.y)^2),
NA ),
impact = ifelse(!is.na(dist),
ifelse(playDirection == "right",
ifelse(x.x %% x.y <1, 1, 1),
ifelse(x.x %% x.y >1, -1, 0)),
NA))%>%
# clean up or rename variables
select(-c("x.y","y.y")) %>%
rename(x = x.x,
y = y.x,
s = s.y,
jerseyNumber = jerseyNumber.x) %>%
# find the average distance from player 19 to opposing players
#113.33 = dist on field from lower left corner to upper right corner
#100 yards long and 53.3333 wide or 120 feet wide
group_by(gameId, playId, frameId) %>%
mutate(mean_dist_to_19 = mean(dist, na.rm = TRUE),
impact_level= mean(dist * impact, na.rm = TRUE),
alpha = 1 - (impact_level/113.33) ) %>%
ungroup() %>%
# compute the total distance traveled by player 19
arrange(jerseyNumber, time, frameId) %>%
group_by(gameId, playId, jerseyNumber) %>%
mutate(dist_running_total = cumsum(dis.y)) %>%
# create a width for a moving bar
mutate(width = round( 20*(1-(mean_dist_to_19/113.33)) ,0) ) %>%
# create each play level stats for the receiver
group_by(gameId, playId) %>%
mutate(play_stat = mean(unique(mean_dist_to_19))) %>%
ungroup() %>%
# create each game level stats for the receiver
group_by(gameId) %>%
mutate(game_stat = mean(unique(play_stat))) %>%
ungroup() %>%
# create each player stat over all plays and all games
mutate(player_stat = mean(unique(game_stat))) %>%
# create a new data frame
data.frame()
#print out play, game, and player stats
result1<- df2%>% select(gameId, playId, play_stat,playDirection) %>% distinct %>% data.frame()
result2 <- df2%>% select(gameId, game_stat) %>% distinct %>% data.frame()
result3 <- df2%>% select(player_stat) %>% distinct %>% data.frame()
return(list(result1,result2,result3,df2))
}
out <- player_stats(player_tracking_df,
plays_df,
c("2021010300"),
c("1586", "395", "1232", "3930"),
19)
View(out)