In this assignment what we tried to do is to track how many player are in within 15 yards of the the person who is in possession of the ball.
suppressWarnings(suppressMessages(library(data.table)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(gganimate)))
suppressWarnings(suppressMessages(library(gifski)))
setwd("~/Desktop/Data")
player_tracking_df <- fread("tracking2020.csv")
## Avoidable 12.092 seconds. This file is very unusual: it ends abruptly without a final newline, and also its size is a multiple of 4096 bytes. Please properly end the last row with a newline using for example 'echo >> file' to avoid this time to copy.
plays_df <- fread("plays.csv")
player_stats <- function(pt_df, p_df, game_ID, play_ID, jersey) {
df <- pt_df %>%
filter(gameId %in% game_ID & playId %in% play_ID) %>%
left_join(p_df, by = c("gameId" = "gameId", "playId" = "playId")) %>%
select(gameId, playId, time, x, y, dis, nflId, jerseyNumber, team,
frameId, playDescription,s, playDirection) %>%
data.frame()
df19 <- df %>% # sub setting from df for jersey number 19
select(gameId, playId, time, jerseyNumber, x, y, frameId, dis, s) %>%
filter(jerseyNumber == jersey) %>%
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) %>%
mutate(x.y = x.y <= 15) %>%
mutate(y.y = y.y <= 15) %>%
mutate(dist = ifelse(team == "away",
sqrt((x.x - x.y)^2 + (y.x - y.y)^2), NA )) %>%
select(-c("x.y","y.y")) %>%
rename(x = x.x,
y = y.x,
s = s.y,
jerseyNumber = jerseyNumber.x) %>%
group_by(gameId, playId, frameId) %>%
mutate(mean_dist_to_19 = mean(dist, na.rm = TRUE),
alpha = 1 - (mean_dist_to_19/113.33) ) %>%
ungroup() %>%
arrange(jerseyNumber, time, frameId) %>%
group_by(gameId, playId, jerseyNumber) %>%
mutate(dist_running_total = cumsum(dis.y)) %>%
mutate(width = round( 20*(1-(mean_dist_to_19/113.33)) ,0) ) %>%
group_by(gameId, playId) %>%
mutate(play_stat = mean(unique(mean_dist_to_19))) %>%
ungroup() %>%
group_by(gameId) %>%
mutate(game_stat = mean(unique(play_stat))) %>%
ungroup() %>%
mutate(player_stat = mean(unique(game_stat))) %>%
data.frame()
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))
}
game_ID<- c("2021010300")
play_ID <- c("1586", "395", "1232", "3930")
out <- player_stats(player_tracking_df, plays_df, game_ID, play_ID, 19 )
This function will let us input any game and player we want to and we will get the stats of this player for each game. The main this that we did was we set x.y and y.y (the oppesing players) to 15 so that only the players 15 yards away from the player we can see how well he did.
out[[1]]
## gameId playId play_stat playDirection
## 1 2021010300 1232 76.19110 right
## 2 2021010300 1586 59.09702 right
## 3 2021010300 395 53.87263 left
## 4 2021010300 3930 61.05378 left
##Chart In this chart we can see the chooses players stats for the chooses play and game.
my_df <- out[[4]]
my_df$playId <- as.factor(my_df$playId)
ggplot(my_df %>% filter(team=="away"), aes(x = dist, group=playId, color=playId, fill=playId)) +
geom_histogram(binwidth = 2, aes(y=..density..), color="black", fill="white") +
geom_density(alpha=0.6) +
facet_wrap(~playId, scales = "free") +
transition_time(frameId)
##
Graph We can see in the graph How the players trackfic of the play rose
and fell as the play goes on.