knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
Access pre-formatted NHL shot location data and implement spatially explicit models of NHL goal scoring probability. Advanced team, player, and goaltender statistics can be automatically calculated.
devtools::install_github("jedalong/nhlmodelr")
Load in some data (2011-12 season) and take a look at the structure.
library(nhlmodelr) data(shots20112012) head(shots20112012) summary(shots20112012)
Next, format the shots for the spatial model by spatially pooling all the shots to the 5525 unique locations in the offensive zone.
modshots <- shots2model(shots20112012) head(modshots)
Set up parameters for the spatial model. First, we need to set the number of Gibbs samples for the MCMC chain and the burn-in. We also need to set-up initial values for each location (here we use random values between 0 and 1). The parameter p.max is the tuning parameter, which is the maximum probability of scoring a goal (in the centre of the goal). Finally, the output filename for storing output *.RData file needs to be specified (the model produces a single *.RData file with 8 objects, see ?NHLmodelr).
ngibbs <- 100 nburnin <- 50 p.max <- 0.29 inits <- runif(dim(modshots)[1],0,1) outfile <- 'C:/Workspace/NHLModelOutput.RData' #Model Run - Takes a couple of minutes df <- NHLmodel(modshots,ngibbs,nburnin,p.max,inits,outfile) head(df)
Their are a number of plotting functions available. We can plot the raw shots data, only the goals, the posterior mean, and the posterior variance. Note: The posterior mean and variance here are unrealistic as they are based on only 100 Gibbs samples from the MCMC run.
NHLmodelplot(df,'shots') NHLmodelplot(df,'goals') NHLmodelplot(df,'postmean') NHLmodelplot(df,'postvar')
We can also compute advanced team, player, and goaltender spatially adjusted scoring (and save percentage) statistics.
#==== Not Run ==== # teamdb <- teams(df,shots20112012) # playerdb <- players(df,shots20112012) # goaliedb <- goalies(df,shots20112012) #====
--- End ---
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.