-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.Rmd
171 lines (141 loc) · 5.55 KB
/
process.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
title: "NHL Stanley Cup Playoff Predictions"
author: "Ilari Scheinin"
output: html_document
---
Previous: [1. Scrape raw data](scrape.html)
Next: [3. Train models](model.html)
2. Process data
---------------
In order to train models on the outcomes of past playoffs, I am calculating
summary statistics on each teams performance in the regular season. This script
reads the data files produced by
[nhlscrapr](https://github.com/acthomasca/nhlscrapr) and generates these
summaries for every team and for every season. It also generates separate
statistics for away and home games, as well as an overall metric.
First, load the [dplyr](https://github.com/hadley/dplyr)
[package](http://cran.r-project.org/web/packages/dplyr/) for data manipulation:
```{r, echo=FALSE}
options(width=120)
```
```{r}
suppressMessages({
library(dplyr)
})
```
Next, define a function to calculate the summary statistics for each season. First for each game, calculate for both the away and home teams:
* the proportion of goals scored (*e.g.* for away team:
`away goals / (away goals + home goals)`)
* the proportion of shots
* the proportion of faceoffs won
* the proportion of penalties
* power play (*e.g.* `power play goals for away team / penalties for home team`)
* penalty kill (*e.g.*
`power play goals for home team / penalties for away team`)
Then, calculate the average for the entire regular season. Separate averages
are calculated for each team when they are playing away, when they are playing
at home, and also the overall for both.
```{r}
process_season <- function(theseason) {
message("Processing season ", substring(theseason, 1, 4), "-",
substring(theseason, 5, 8), "...", appendLF=FALSE)
load(file.path("source-data", paste0("nhlscrapr-", theseason, ".RData")))
grand.data <- tbl_df(grand.data)
grand.data$season <- as.character(grand.data$season)
gamestats <- grand.data %>%
filter(ev.team %in% unique(grand.data$hometeam)) %>%
filter(substring(gcode, 1, 1) == "2") %>%
mutate(season==season) %>%
group_by(season, gcode) %>%
summarise(
awayteam=first(awayteam),
hometeam=first(hometeam),
totalgoals=sum(etype == "GOAL"),
awaygoals=sum(ev.team == awayteam & etype == "GOAL"),
homegoals=sum(ev.team == hometeam & etype == "GOAL"),
totalshots=sum(etype %in% c("SHOT", "GOAL")),
awayshots=sum(ev.team == awayteam & etype %in% c("SHOT", "GOAL")),
homeshots=sum(ev.team == hometeam & etype %in% c("SHOT", "GOAL")),
totalfaceoffs=sum(etype == "FAC"),
awayfaceoffs=sum(ev.team == awayteam & etype == "FAC"),
homefaceoffs=sum(ev.team == hometeam & etype == "FAC"),
totalpenalties=sum(etype == "PENL"),
awaypenalties=sum(ev.team == awayteam & etype == "PENL"),
homepenalties=sum(ev.team == hometeam & etype == "PENL"),
awaypp=sum(ev.team == awayteam & etype == "GOAL" &
away.skaters > home.skaters),
homepp=sum(ev.team == hometeam & etype == "GOAL" &
away.skaters < home.skaters),
awaysh=sum(ev.team == awayteam & etype == "GOAL" &
away.skaters < home.skaters),
homesh=sum(ev.team == hometeam & etype == "GOAL" &
away.skaters > home.skaters))
awaygames <- gamestats %>%
ungroup() %>%
transmute(
season=season,
team=awayteam,
goals=awaygoals / totalgoals,
shots=awayshots / totalshots,
faceoffs=awayfaceoffs / totalfaceoffs,
penalties=ifelse(totalpenalties==0, NA, awaypenalties / totalpenalties),
pp=ifelse(homepenalties==0, NA, awaypp / homepenalties),
pk=ifelse(awaypenalties==0, NA, homepp / awaypenalties))
homegames <- gamestats %>%
ungroup() %>%
transmute(
season=season,
team=hometeam,
goals=homegoals / totalgoals,
shots=homeshots / totalshots,
faceoffs=homefaceoffs / totalfaceoffs,
penalties=ifelse(totalpenalties==0, NA, homepenalties / totalpenalties),
pp=ifelse(awaypenalties==0, NA, homepp / awaypenalties),
pk=ifelse(homepenalties==0, NA, awaypp / homepenalties))
awaystats <- awaygames %>%
group_by(season, team) %>%
summarise_each(funs(mean(., na.rm=TRUE)))
homestats <- homegames %>%
group_by(season, team) %>%
summarise_each(funs(mean(., na.rm=TRUE)))
overallstats <- bind_rows(awaystats, homestats) %>%
group_by(season, team) %>%
summarise_each(funs(mean(., na.rm=TRUE)))
message()
list(away=awaystats, home=homestats, overall=overallstats)
}
```
Finally, load all available seasons and run the function defined above on each one, combine results together, and save the processed data.
```{r}
load(file.path("source-data", "nhlscrapr-core.RData"))
seasons <- unique(games$season)
rm(list=c("games", "roster.master", "roster.unique"))
seasonstats <- lapply(seasons, process_season)
gamestats <- list(
away=bind_rows(lapply(seasonstats, "[[", "away")),
home=bind_rows(lapply(seasonstats, "[[", "home")),
overall=bind_rows(lapply(seasonstats, "[[", "overall")))
rm(list=c("seasons", "seasonstats"))
saveRDS(gamestats, "processed.rds")
```
This is what the generated summary statistics look like:
```{r, eval=FALSE}
head(gamestats[["away"]])
```
```{r, echo=FALSE, results='asis'}
knitr::kable(head(gamestats[["away"]]), digits=3)
```
```{r, eval=FALSE}
head(gamestats[["home"]])
```
```{r, echo=FALSE, results='asis'}
knitr::kable(head(gamestats[["home"]]), digits=3)
```
```{r, eval=FALSE}
head(gamestats[["overall"]])
```
```{r, echo=FALSE, results='asis'}
knitr::kable(head(gamestats[["overall"]]), digits=3)
```
Next: [3. Train models](model.html)
Previous: [1. Scrape raw data](scrape.html)