-
Notifications
You must be signed in to change notification settings - Fork 5
/
Final Project Team 2 - Approach 2.R
327 lines (257 loc) · 12.3 KB
/
Final Project Team 2 - Approach 2.R
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
############################################################################################################
# Setup
############################################################################################################
library(coda)
library(rjags)
library(mcmcplots)
library(stringr)
library(plyr)
library(xtable)
library(ggplot2)
source("plotPost.R")
set.seed(12345)
#-----------------------------------------------------------------------------------------------------------
# Away: -1, Draw: 0 , Home: 1
fmel <- df
fmel$match_result <- sign(fmel$homeGoals - fmel$awayGoals)
fmel$home=as.character(fmel$home)
fmel$away=as.character(fmel$away)
fmel$year=as.numeric(fmel$year)
# Dataframe fmel with only the completly matched results
fmel <- na.omit(fmel)
teams <- unique(c(fmel$home, fmel$away))
year <- unique(fmel$year)
# JAGS list with the data from fmel in integer strings
DATAlist <- list(homeGoals = fmel$homeGoals, awayGoals = fmel$awayGoals,
home = as.numeric(factor(fmel$home, levels=teams)),
away = as.numeric(factor(fmel$away, levels=teams)),
year = as.numeric(factor(fmel$year, levels=year)),
n_teams = length(teams), n_games = nrow(fmel),
n_year = length(year))
# Generator for the type of column names Jags outputs.
column_name <- function(name, ...) {
paste0(name, "[", paste(..., sep=",") , "]")
}
############################################################################################################
# Check the actual distribution of the number of scored goals and a Poisson distribution
############################################################################################################
oldParam <- par(mfcol=c(2,1), mar=rep(2.2, 4))
hist(c(fmel$awayGoals, fmel$homeGoals), xlim=c(-0.5, 8), breaks = -1:20 + 0.5,
main = "Distribution of the Goals")
Goal_Mean <- mean(c(fmel$awayGoals, fmel$homeGoals))
hist(rpois(9999, Goal_Mean), xlim=c(-0.5, 8), breaks = -1:9 + 0.5,
main = "Poisson Distribution with the Same Mean")
par(oldParam)
############################################################################################################
# Model 1
############################################################################################################
model1_string <- "model {
for(i in 1:n_games) {
homeGoals[i] ~ dpois(lambda_home[home[i],away[i]])
awayGoals[i] ~ dpois(lambda_away[home[i],away[i]])
}
for(home_i in 1:n_teams) {
for(away_i in 1:n_teams) {
lambda_home[home_i, away_i] <- exp(base_line + skill[home_i] - skill[away_i])
lambda_away[home_i, away_i] <- exp(base_line + skill[away_i] - skill[home_i])
}
}
skill[1] <- 0
for(j in 2:n_teams) {
skill[j] ~ dnorm(group_skill, group_tau)
}
group_skill ~ dnorm(0, 0.0625)
group_tau <- 1 / pow(group_sigma, 2)
group_sigma ~ dunif(0, 3)
base_line ~ dnorm(0, 0.0625)
}"
#-----------------------------------------------------------------------------------------------------------
# Compiling model 1
model1 <- jags.model(textConnection(model1_string), data=DATAlist, n.chains=3, n.adapt=1000)
# Burning some samples on the altar of the MCMC god
update(model1, 1000)
# Generating MCMC samples
s1 <- coda.samples(model1, variable.names=c("base_line", "skill", "group_skill", "group_sigma"), n.iter=1000, thin=2)
# Merging the three MCMC chains into one matrix
ms1 <- as.matrix(s1)
# Skill plot of Valencia
plot(s1[,column_name("skill", which(teams == "Valencia"))])
# Skill plot of Sevilla
plot(s1[,column_name("skill", which(teams == "Sevilla"))])
#-----------------------------------------------------------------------------------------------------------
# Plots histograms over Goals_H, Goals_A, the difference in goals and a barplot over the credible match results.
plot_goals <- function(Goals_H, Goals_A) {
n_matches <- length(Goals_H)
Goal_difference <- Goals_H - Goals_A
match_result <- ifelse(Goal_difference < 0, "away_win", ifelse(Goal_difference > 0, "home_win", "equal"))
hist(Goals_H, xlim=c(-0.5, 10), breaks=(0:100) - 0.5)
hist(Goals_A, xlim=c(-0.5, 10), breaks=(0:100) - 0.5)
hist(Goal_difference, xlim=c(-6, 6), breaks=(-100:100) - 0.5 )
barplot(table(match_result) / n_matches , ylim=c(0, 1))
}
# Simulates game goals scores using the MCMC samples from the model1 model.
plotPrediction1 <- function(home_team, away_team, ms) {
oldParam <- par(mfrow = c(2, 4))
base_line <- ms[, "base_line"]
skill_H <- ms[, column_name("skill", which(teams == home_team))]
skill_A <- ms[, column_name("skill", which(teams == away_team))]
Goals_H <- rpois(nrow(ms), exp(base_line + skill_H - skill_A))
Goals_A <- rpois(nrow(ms), exp(base_line + skill_A - skill_H))
plot_goals(Goals_H, Goals_A)
Goals_H <- fmel$homeGoals[ fmel$home == home_team & fmel$away == away_team]
Goals_A <- fmel$awayGoals[ fmel$home == home_team & fmel$away == away_team]
plot_goals(Goals_H, Goals_A)
par(oldParam)
}
plotPrediction1("Valencia", "Sevilla", ms1)
plotPrediction1("Sevilla", "Valencia",ms1)
############################################################################################################
# Model 2 with Home Advantages
############################################################################################################
model2_string <- "model {
for(i in 1:n_games) {
homeGoals[i] ~ dpois(lambda_home[home[i],away[i]])
awayGoals[i] ~ dpois(lambda_away[home[i],away[i]])
}
for(home_i in 1:n_teams) {
for(away_i in 1:n_teams) {
lambda_home[home_i, away_i] <- exp( home_base_line + skill[home_i] - skill[away_i])
lambda_away[home_i, away_i] <- exp( away_base_line + skill[away_i] - skill[home_i])
}
}
skill[1] <- 0
for(j in 2:n_teams) {
skill[j] ~ dnorm(group_skill, group_tau)
}
group_skill ~ dnorm(0, 0.0625)
group_tau <- 1/pow(group_sigma, 2)
group_sigma ~ dunif(0, 3)
home_base_line ~ dnorm(0, 0.0625)
away_base_line ~ dnorm(0, 0.0625)
}"
model2 <- jags.model(textConnection(model2_string), data=DATAlist, n.chains=3, n.adapt=5000)
update(model2, 5000)
#s2 <- coda.samples(model2, variable.names=c("home_base_line", "away_base_line","skill", "group_sigma", "group_skill"), n.iter=10000, thin=2)
s2 <- coda.samples(model2, variable.names=c("home_base_line", "away_base_line","skill", "group_sigma", "group_skill"), n.iter=5000, thin=2)
ms2 <- as.matrix(s2)
plot(s2[,"home_base_line"])
plot(s2[,"away_base_line"])
#-----------------------------------------------------------------------------------------------------------
plotPost(exp(ms2[,"home_base_line"]) - exp(ms2[,"away_base_line"]), compVal=0,
xlab="Extra Goals in Home Advantage")
## @knitr
#dic_model1 <- dic.samples(model1, 10000, "pD")
#dic_model2 <- dic.samples(model2, 10000, "pD")
dic_model1 <- dic.samples(model1, 1000, "pD")
dic_model2 <- dic.samples(model2, 1000, "pD")
diffdic(dic_model1, dic_model2)
plotPrediction2 <- function(home_team, away_team, ms) {
oldParam <- par(mfrow = c(2, 4))
home_base_line <- ms[, "home_base_line"]
away_base_line <- ms[, "away_base_line"]
skill_H <- ms[, column_name("skill", which(teams == home_team))]
skill_A <- ms[, column_name("skill", which(teams == away_team))]
Goals_H <- rpois(nrow(ms), exp(home_base_line + skill_H - skill_A))
Goals_A <- rpois(nrow(ms), exp(away_base_line + skill_A - skill_H))
plot_goals(Goals_H, Goals_A)
Goals_H <- fmel$homeGoals[ fmel$home == home_team & fmel$away == away_team]
Goals_A <- fmel$awayGoals[ fmel$home == home_team & fmel$away == away_team]
plot_goals(Goals_H, Goals_A)
par(oldParam)
}
plotPrediction2("Valencia", "Sevilla", ms2)
plotPrediction2("Sevilla", "Valencia",ms2)
############################################################################################################
# Model 3 with Skill Cariability
############################################################################################################
model3_string <- "model {
for(i in 1:n_games) {
homeGoals[i] ~ dpois(lambda_home[year[i], home[i],away[i]])
awayGoals[i] ~ dpois(lambda_away[year[i], home[i],away[i]])
}
for(year_i in 1:n_year) {
for(home_i in 1:n_teams) {
for(away_i in 1:n_teams) {
lambda_home[year_i, home_i, away_i] <- exp( home_base_line[year_i] + skill[year_i, home_i] - skill[year_i, away_i])
lambda_away[year_i, home_i, away_i] <- exp( away_base_line[year_i] + skill[year_i, away_i] - skill[year_i, home_i])
}
}
}
skill[1, 1] <- 0
for(j in 2:n_teams) {
skill[1, j] ~ dnorm(group_skill, group_tau)
}
group_skill ~ dnorm(0, 0.0625)
group_tau <- 1/pow(group_sigma, 2)
group_sigma ~ dunif(0, 3)
home_base_line[1] ~ dnorm(0, 0.0625)
away_base_line[1] ~ dnorm(0, 0.0625)
for(year_i in 2:n_year) {
skill[year_i, 1] <- 0
for(j in 2:n_teams) {
skill[year_i, j] ~ dnorm(skill[year_i - 1, j], year_tau)
}
home_base_line[year_i] ~ dnorm(home_base_line[year_i - 1], year_tau)
away_base_line[year_i] ~ dnorm(away_base_line[year_i - 1], year_tau)
}
year_tau <- 1/pow(year_sigma, 2)
year_sigma ~ dunif(0, 3)
}"
#model3 <- jags.model(textConnection(model3_string), data=DATAlist, n.chains=3, n.adapt=10000)
#update(model3, 10000)
#s3 <- coda.samples(model3, variable.names=c("home_base_line", "away_base_line","skill", "year_sigma", "group_sigma", "group_skill"), n.iter=10000, thin=8)
#ms3 <- as.matrix(s3)
model3 <- jags.model(textConnection(model3_string), data=DATAlist, n.chains=3, n.adapt=3500)
update(model3, 3500)
s3 <- coda.samples(model3, variable.names=c("home_base_line", "away_base_line","skill", "year_sigma", "group_sigma", "group_skill"), n.iter=10000, thin=8)
ms3 <- as.matrix(s3)
plot(s3[,"year_sigma"])
## Ranking Plot
team_skill <- ms3[, str_detect(string=colnames(ms3), "skill\\[5,")]
team_skill <- (team_skill - rowMeans(team_skill)) + ms3[, "home_base_line[5]"]
team_skill <- exp(team_skill)
colnames(team_skill) <- teams
team_skill <- team_skill[,order(colMeans(team_skill), decreasing=T)]
oldParam <- par(mar=c(2,0.7,0.7,0.7), xaxs='i')
caterplot(team_skill, labels.loc="above", val.lim=c(0.7, 3.8))
par(oldParam)
plotPost(team_skill[, "Real Madrid"] - team_skill[, "Barcelona"], compVal = 0, xlab = "← Barcelona vs Real Madrid →")
############################################################################################################
# Prediction
############################################################################################################
n <- nrow(ms3)
model3_pred <- sapply(1:nrow(fmel), function(i) {
home_team <- which(teams == fmel$home[i])
away_team <- which(teams == fmel$away[i])
year <- which(year == fmel$year[i])
skill_H <- ms3[, column_name("skill", year, home_team)]
skill_A <- ms3[, column_name("skill", year, away_team)]
home_base_line <- ms3[, column_name("home_base_line", year)]
away_base_line <- ms3[, column_name("away_base_line", year)]
Goals_H <- rpois(n, exp(home_base_line + skill_H - skill_A))
Goals_A <- rpois(n, exp(away_base_line + skill_A - skill_H))
Goals_H_table <- table(Goals_H)
Goals_A_table <- table(Goals_A)
match_results <- sign(Goals_H - Goals_A)
match_results_table <- table(match_results)
mode_home_goal <- as.numeric(names(Goals_H_table)[ which.max(Goals_H_table)])
mode_away_goal <- as.numeric(names(Goals_A_table)[ which.max(Goals_A_table)])
match_result <- as.numeric(names(match_results_table)[which.max(match_results_table)])
rand_i <- sample(seq_along(Goals_H), 1)
c(mode_home_goal = mode_home_goal, mode_away_goal = mode_away_goal, match_result = match_result,
mean_home_goal = mean(Goals_H), mean_away_goal = mean(Goals_A),
rand_home_goal = Goals_H[rand_i], rand_away_goal = Goals_A[rand_i],
rand_match_result = match_results[rand_i])
})
model3_pred <- t(model3_pred)
hist(fmel$homeGoals, breaks= (-1:10) + 0.5, xlim=c(-0.5, 10))
hist(model3_pred[ , "mode_home_goal"], breaks= (-1:10) + 0.5, xlim=c(-0.5, 10))
hist(model3_pred[ , "mean_home_goal"], breaks= (-1:10) + 0.5, xlim=c(-0.5, 10))
hist(model3_pred[ , "rand_home_goal"], breaks= (-1:12) + 0.5, xlim=c(-0.5, 10))
mean(fmel$homeGoals == model3_pred[ , "mode_home_goal"], na.rm=T)
mean((fmel$homeGoals - model3_pred[ , "mean_home_goal"])^2, na.rm=T)
hist(fmel$match_result, breaks= (-2:1) + 0.5)
hist(model3_pred[ , "match_result"], breaks= (-2:1) + 0.5)
hist(model3_pred[ , "rand_match_result"], breaks= (-2:1) + 0.5)
mean(fmel$match_result == model3_pred[ , "match_result"], na.rm=T)
############################################################################################################