-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRest.nhl.nba.R
More file actions
107 lines (76 loc) · 3.95 KB
/
Copy pathRest.nhl.nba.R
File metadata and controls
107 lines (76 loc) · 3.95 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
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
source("config.R")
load("~/Desktop/bigfour_public.rda")
library(dplyr); library(tidyr); library(ggplot2)
### This uses the public .rda available at
## https://github.com/bigfour/competitiveness/blob/master/data/bigfour_public.rda
winter <- filter(bigfour_public, sport == "nhl"|sport == "nba")
winter.h <- winter %>%
mutate(sdiff = home_score - visitor_score, gameDate = as.Date(gameDate), type = "home") %>%
select(gameDate, type, home_team, sdiff, sport) %>%
rename(team = home_team)
winter.v <- winter %>%
mutate(sdiff = visitor_score - home_score, gameDate = as.Date(gameDate), type = "vis") %>%
select(gameDate, type, visitor_team, sdiff, sport) %>%
rename(team = visitor_team)
winter.all <- rbind(winter.h, winter.v) %>%
arrange(team, gameDate) %>%
group_by(team) %>%
mutate(date.lag = gameDate - lag(gameDate, 1),
date.lag = ifelse(date.lag > 3, 3, date.lag),
date.lag2 = gameDate - lag(gameDate, 2),
date.lag3 = gameDate - lag(gameDate, 3),
date.lag4 = gameDate - lag(gameDate, 4),
date.lag5 = gameDate - lag(gameDate, 5),
five.in.seven = date.lag5 <=7,
four.in.six = date.lag4 <=6,
three.in.four = date.lag3 <=4,
back.to.back = date.lag <=1) %>%
ungroup()
winter.sum <- winter.all %>% filter(type == "home") %>%
na.omit() %>%
group_by(date.lag, sport) %>%
summarise(ave.prob = mean(sdiff > 0), n.ex = n(), me = 1/sqrt(n.ex))
limits <- aes(ymin= ave.prob - me, ymax = ave.prob + me)
ggplot(filter(winter.sum, date.lag > 0), aes(date.lag, ave.prob, colour = sport)) +
geom_errorbar(limits, position = "dodge", width=0.25) +
geom_point(position = position_dodge(width = 0.25))+
geom_line(position = position_dodge(width = 0.25)) +
scale_x_continuous(labels = c("1 day", "2 days", "3 days+"),
breaks = c(1,2,3), "Days Rest") +
ggtitle("Win percentage by days rest, home team") +
scale_y_continuous(labels = scales::percent, "")
winter.sum <- winter.all %>% filter(type == "vis") %>%
na.omit() %>%
group_by(date.lag, sport) %>%
summarise(ave.prob = mean(sdiff > 0), n.ex = n(), me = 1/sqrt(n.ex))
limits <- aes(ymin= ave.prob - me, ymax = ave.prob + me)
ggplot(filter(winter.sum, date.lag > 0), aes(date.lag, ave.prob, colour = sport)) +
geom_errorbar(limits, position = "dodge", width=0.25) +
geom_point(position = position_dodge(width = 0.25))+
geom_line(position = position_dodge(width = 0.25)) +
scale_x_continuous(labels = c("1 day", "2 days", "3 days+"),
breaks = c(1,2,3), "Days Rest") +
ggtitle("Win percentage by days rest, visiting team") +
scale_y_continuous(labels = scales::percent, "")
winter.new <- gather(na.omit(winter.all), RestType,
RestVar, five.in.seven:back.to.back, factor_key=TRUE)
rates.sport <- winter.new %>% group_by(sport, RestType) %>%
summarise(n.games.season = sum(RestVar)/(11*30))
rates.sport$RestType <- factor(rates.sport$RestType,
levels = c("back.to.back", "three.in.four",
"four.in.six", "five.in.seven"))
ggplot(rates.sport, aes(RestType, n.games.season, colour = sport, fill = sport)) +
geom_bar(stat = "identity", position="dodge") +
scale_x_discrete(labels = c("2 in 2", "3 in 4", "4 in 6", "5 in 7")) +
ylab("") + ggtitle("Number of low-rest games per team per season")
rates.sport <- winter.new %>% filter(RestVar) %>%
group_by(sport, RestType) %>%
summarise(n.games.season = sum(sdiff > 0)/n(), n.games = n())
rates.sport$RestType <- factor(rates.sport$RestType,
levels = c("back.to.back", "three.in.four",
"four.in.six", "five.in.seven"))
ggplot(rates.sport, aes(RestType, n.games.season, colour = sport, fill = sport, size = n.games)) +
geom_point(stat = "identity", position="dodge") +
scale_x_discrete(labels = c("2 in 2", "3 in 4", "4 in 6", "5 in 7")) +
ylab("") + ggtitle("Win percentage in low-rest games") +
scale_y_continuous(labels = scales::percent, "")