-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNHL_penalties_minute.R
More file actions
71 lines (56 loc) · 2.03 KB
/
Copy pathNHL_penalties_minute.R
File metadata and controls
71 lines (56 loc) · 2.03 KB
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
####First, read in the data (it's a large data set!)
rm(list = ls())
library(MASS)
library(stringr)
library(tidyverse)
library(patchwork)
## old data
pens <- read_csv("~/Dropbox/NHL code/allpens.csv")
pens <- pens %>%
mutate(gid = paste0(season, gcode)) %>%
filter(seconds <=3600) %>%
mutate(minutes = floor((seconds-1)/60) + 1,
minutes = pmax(1, minutes),
ps = ifelse(gcode > 29999, "Postseason", "Regular season"),
period.cat = ifelse(period == 1, "1st Period",
ifelse(period == 2, "2nd period", "3rd period")))
n.games <- length(unique(pens$gid))
n.psgames <- length(unique(pens[pens$gcode > 29999,]$gid))
n.reggames <- n.games - n.psgames
function.penalty <- function(ptype){
pen.type <- ptype
pens.type <- subset(pens, ptype == pen.type)
penalty.sum <- pens.type %>%
group_by(ps, minutes, period.cat) %>%
count() %>%
filter(minutes <= 60) %>%
mutate(penalties.rate = ifelse(ps == "Postseason", n/n.psgames, n/n.reggames)) %>%
ungroup()
rs <- penalty.sum %>%
filter(ps == "Regular season") %>%
ggplot(aes(minutes, penalties.rate)) +
geom_point() +
geom_smooth() +
facet_wrap(~period.cat, scales = "free_x", nrow = 1) +
theme(panel.spacing = unit(0, "lines")) +
#scale_y_continuous(labels = scales::percent) +
theme_bw(14)+ ylab("") + xlab("Minute") +
labs(title = paste("Regular season", pen.type, "penalties per minute"))
ps <- penalty.sum %>%
filter(ps == "Postseason") %>%
ggplot(aes(minutes, penalties.rate)) +
geom_point() +
geom_smooth() +
facet_wrap(~period.cat, scales = "free_x", nrow = 1) +
theme(panel.spacing = unit(0, "lines")) +
#scale_y_continuous(labels = scales::percent) +
theme_bw(14) + ylab("") + xlab("Minute") +
labs(title = paste("Postseason", pen.type, "penalties by minute"))
return(rs + ps)
}
## Less judgement
function.penalty("Holding")
function.penalty("Tripping")
function.penalty("Roughing")
function.penalty("Interference")
pens %>% group_by(ptype) %>% count() %>% arrange(-n) %>% head(20)