# This script helps examine the timing of events extracted from play-by-play library(lattice) times <- read.csv("times.csv",header=T) if (1) { # shot events fits <- vector("list",0) maxes <- c() colors <- c("red","green","blue","orange","yellow","pink","purple","black") pstarts <- list( c("2fg_make","3fg_make"), c("dreb_2fg_live","dreb_3fg_live"), c("oreb_2fg_live","oreb_3fg_live"), c("period_start"), c("timeout"), c("inbounds_after_foul"), c("tov_dead"),c("tov_steal")) sevents <- c("shot_2fg_block","shot_2fg_make","shot_2fg_make_sf","shot_2fg_miss","shot_2fg_miss_sf","shot_3fg_block","shot_3fg_make","shot_3fg_make_sf","shot_3fg_miss","shot_3fg_miss_sf") x <- seq(0,30,1) for (i in 1:NROW(pstarts)) { # setup empty data vector data <- 0 for (j in 1:NROW(sevents)) { indexes <- times$event == sevents[j] for (k in 1:NROW(pstarts[[i]])) { indexes <- indexes + (times$pstart == pstarts[[i]][k]) } if (NCOL(data) <= 1) { data <- times[ indexes == 2, 5:NCOL(times) ] } else { data <- data + times[ indexes == 2, 5:NCOL(times) ] } } data <- colSums(data) fit <- density(x,weights=data/sum(data),from=0,to=30,adjust=0.5) fits[[i]] <- fit maxes <- c(maxes,max(fit$y)) } x <- seq(0,30,1) y <- rep(-1,31) #plot(x,y,ylim=c(0,max(maxes))) # #for (i in 1:NROW(fits)) { # lines(fits[[i]],lwd=2,col=colors[i]) #} # period start vs timeout vs inbounds after foul (4/5/6) panel.first <- function (x, y, ...) { llines(fits[[4]],lwd=2,col="black") llines(fits[[5]],lwd=2,col="red") llines(fits[[6]],lwd=2,col="blue") panel.xyplot(x, y, ...) } # opp shot make vs dreb live vs oreb live (1/2/3) panel.second <- function (x, y, ...) { llines(fits[[1]],lwd=2,col="black") llines(fits[[2]],lwd=2,col="red") llines(fits[[3]],lwd=2,col="blue") panel.xyplot(x, y, ...) } # tov dead vs tov steal (7/8) panel.third <- function (x, y, ...) { llines(fits[[7]],lwd=2,col="black") llines(fits[[8]],lwd=2,col="red") panel.xyplot(x, y, ...) } fig1 <- xyplot(y ~ x, main="Time to Shot Event vs Density", xlab="Time to Shot Event (seconds)", ylab="Density", key = list(lines = list(lwd=2, span=0.5, col="black"), text = list("Period Start"), lines = list(lwd=2, span=0.5, col="red"), text = list("Timeout"), lines = list(lwd=2, span=0.5, col="blue"), text = list("Inbounds After Foul")), ylim=c(0,max(maxes)+0.005), panel=panel.first) fig2 <- xyplot(y ~ x, main="Time to Shot Event vs Density", xlab="Time to Shot Event (seconds)", ylab="Density", key = list(lines = list(lwd=2, span=0.5, col="black"), text = list("Opp Shot Made"), lines = list(lwd=2, span=0.5, col="red"), text = list("Live Def Reb"), lines = list(lwd=2, span=0.5, col="blue"), text = list("Live Off Reb")), ylim=c(0,max(maxes)+0.005), panel=panel.second) fig3 <- xyplot(y ~ x, main="Time to Shot Event vs Density", xlab="Time to Shot Event (seconds)", ylab="Density", key = list(lines = list(lwd=2, span=0.5, col="black"), text = list("Dead Ball Turnover"), lines = list(lwd=2, span=0.5, col="red"), text = list("Steal")), ylim=c(0,max(maxes)+0.005), panel=panel.third) # save graphics trellis.device(dev=pdf, file="time2shot_event1.pdf"); #, width="5", height="3"); # default width=height=7 print(fig1); graphics.off(); trellis.device(dev=pdf, file="time2shot_event2.pdf"); #, width="5", height="3"); # default width=height=7 print(fig2); graphics.off(); trellis.device(dev=pdf, file="time2shot_event3.pdf"); #, width="5", height="3"); # default width=height=7 print(fig3); graphics.off(); #print(fig1) #print(fig2) #print(fig3) } # end if (0) { # personal foul events fits <- vector("list",0) maxes <- c() colors <- c("red","green","blue","orange","yellow","pink","purple","black") pstarts <- list( c("2fg_make","3fg_make"), c("dreb_2fg_live","dreb_3fg_live"), c("oreb_2fg_live","oreb_3fg_live"), c("period_start"), c("timeout"), c("inbounds_after_foul"), c("tov_dead"), c("tov_steal")) sevents <- c("foul_personal") for (i in 1:NROW(pstarts)) { # setup empty data vector data <- 0 for (j in 1:NROW(sevents)) { indexes <- times$event == sevents[j] for (k in 1:NROW(pstarts[[i]])) { indexes <- indexes + (times$pstart == pstarts[[i]][k]) } if (NCOL(data) <= 1) { data <- times[ indexes == 2, 5:NCOL(times) ] } else { data <- data + times[ indexes == 2, 5:NCOL(times) ] } } data <- colSums(data) fit <- density(x,weights=data/sum(data),from=0,to=30,adjust=0.5) fits[[i]] <- fit maxes <- c(maxes,max(fit$y)) } x <- seq(0,30,1) y <- rep(-1,31) #plot(x,y,ylim=c(0,max(maxes))) # #for (i in 1:NROW(fits)) { # lines(fits[[i]],lwd=2,col=colors[i]) #} panel.q <- function (x, y, ...) { for (i in 1:NROW(fits)) { llines(fits[[i]],lwd=2,col=colors[i]) } panel.xyplot(x, y, ...) } fig <- xyplot(y ~ x, ylim=c(0,max(maxes)+0.005), panel=panel.q) print(fig) } # end if (0) { # turnover events fits <- vector("list",0) maxes <- c() colors <- c("red","green","blue","orange","yellow","pink","purple","black") pstarts <- list( c("2fg_make","3fg_make"), c("dreb_2fg_live","dreb_3fg_live"), c("oreb_2fg_live","oreb_3fg_live"), c("period_start"), c("timeout"), c("inbounds_after_foul"), c("tov_dead"), c("tov_steal")) sevents <- c("tov_steal") for (i in 1:NROW(pstarts)) { # setup empty data vector data <- 0 for (j in 1:NROW(sevents)) { indexes <- times$event == sevents[j] for (k in 1:NROW(pstarts[[i]])) { indexes <- indexes + (times$pstart == pstarts[[i]][k]) } if (NCOL(data) <= 1) { data <- times[ indexes == 2, 5:NCOL(times) ] } else { data <- data + times[ indexes == 2, 5:NCOL(times) ] } } data <- colSums(data) fit <- density(x,weights=data/sum(data),from=0,to=30,adjust=0.5) fits[[i]] <- fit maxes <- c(maxes,max(fit$y)) } x <- seq(0,30,1) y <- rep(-1,31) #plot(x,y,ylim=c(0,max(maxes))) # #for (i in 1:NROW(fits)) { # lines(fits[[i]],lwd=2,col=colors[i]) #} panel.q <- function (x, y, ...) { for (i in 1:NROW(fits)) { llines(fits[[i]],lwd=2,col=colors[i]) } panel.xyplot(x, y, ...) } fig <- xyplot(y ~ x, ylim=c(0,max(maxes)+0.005), panel=panel.q) print(fig) } # end