-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsoccer_games.R
360 lines (263 loc) · 11.8 KB
/
soccer_games.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
library(tidyverse)
library(Metrics)
library(ggplot2)
library(ggthemr)
library(tidymodels)
library(parsnip)
library(poissonreg)
library(caret)
#read data
#https://www.kaggle.com/martj42/international-football-results-from-1872-to-2017
data <- read_csv("/results.csv")
#list of competing countries
euro2020_teams <- c("Austria", "Germany", "Poland", "Netherlands",
"Italy", "England", "Scotland", "Portugal",
"France", "Belgium", "Czech Republic", "Slovakia",
"Hungary", "Wales", "Sweden", "Northern Ireland",
"Turkey", "Switzerland", "Russia", "Finland",
"Croatia", "Spain","Ukraine", "Denmark")
#https://www.kaggle.com/cashncarry/fifaworldranking?select=fifa_ranking-2021-05-27.csv
rankings_data <- read_csv("fifa_ranking-2021-05-27.csv")
rankings_data <- rankings_data %>% filter(rank_date == '2021-05-27') %>% select(country_full, total_points)
rankings_data$country_full[rankings_data$country_full == "Côte d'Ivoire"] <- 'Ivory Coast'
rankings_data$country_full[rankings_data$country_full == "USA"] <- 'United States'
#filtering out all matches before 2019 and with non-competing countries
data <- data %>%
filter(date > "2019-01-01") %>%
filter(home_team %in% euro2020_teams | away_team %in% euro2020_teams) %>%
filter(!is.na(home_score)) %>%
arrange(date, desc=TRUE)
#add rankings to data
data <- data %>%
left_join(rankings_data, by = c("home_team" = "country_full")) %>% rename(home_team_ranking=total_points) %>%
left_join(rankings_data, by = c("away_team" = "country_full")) %>% rename(away_team_ranking=total_points)
#add fifa ranking difference variable
data <- data %>% mutate(ranking_diff = home_team_ranking-away_team_ranking)
#EDA -------
#home score
ggthemr('fresh')
ggplot(data, aes(home_score)) +
geom_histogram(binwidth=1, color="white") +
ggtitle("Goals scored by the home team") +
scale_x_continuous(name="Goals", breaks=0:10) +
geom_vline(aes(xintercept = mean(home_score)), color="red") +
geom_text(aes(label = round(mean(home_score),1), x= mean(home_score)+0.3, y=100), size=4, color="red")
#home score
ggplot(data, aes(away_score)) +
geom_histogram(binwidth=1, color="white") +
ggtitle("Goals scored by the away team") +
scale_x_continuous(name="Goals", breaks=0:7) +
geom_vline(aes(xintercept = mean(away_score)), color="red") +
geom_text(aes(label = round(mean(away_score),1), x= mean(away_score)+0.3, y=140), size=4, color="red")
#all score
ggplot(data, aes(away_score+home_score)) +
geom_histogram(binwidth=1, color="white") +
ggtitle("Goals scored per match") +
scale_x_continuous(name="Goals", breaks=0:10) +
geom_vline(aes(xintercept = mean(away_score+home_score)), color="red")
data %>%
ggplot(aes(ranking_diff, home_score)) +
geom_point() +
xlab("FIFA Ranking Difference between home and away team") +
ylab("Goals scored by home team") +
ggtitle("FIFA Ranking Difference of teams vs. home team goals")
data %>%
ggplot(aes(ranking_diff, away_score)) +
geom_point() +
xlab("FIFA Ranking Difference between home and away team") +
ylab("Goals scored by away team") +
ggtitle("FIFA Ranking Difference of teams vs. away team goals")
euro_data <- data %>% filter(tournament == "UEFA Euro")
data <- data %>% filter(tournament != "UEFA Euro")
#Machine Learning ------
train_test_split <- initial_split(data, prop = 0.8)
data_train <- training(train_test_split)
data_test <- testing(train_test_split)
#set up models
pois_mod <- poisson_reg() %>% set_engine("glm")
lin_mod <- linear_reg() %>% set_engine("lm")
rf_mod <- rand_forest(trees=100) %>% set_engine("randomForest") %>% set_mode("regression")
#set up cv
folds <- vfold_cv(data_train, v = 30)
#set up model formulas
hs_formula_single <- formula("home_score ~ ranking_diff")
as_formula_single <- formula("away_score ~ ranking_diff")
#poisson model home
pois_mod_hs_wf <-
workflow() %>%
add_model(pois_mod) %>%
add_formula(hs_formula_single)
#poisson model away
pois_mod_as_wf <-
workflow() %>%
add_model(pois_mod) %>%
add_formula(as_formula_single)
#lin model home
lin_mod_hs_wf <-
workflow() %>%
add_model(lin_mod) %>%
add_formula(hs_formula_single)
#lin model away
lin_mod_as_wf <-
workflow() %>%
add_model(lin_mod) %>%
add_formula(as_formula_single)
#rf model away
rf_mod_as_wf <-
workflow() %>%
add_model(rf_mod) %>%
add_formula(hs_formula_single)
#rf model away
rf_mod_hs_wf <-
workflow() %>%
add_model(rf_mod) %>%
add_formula(as_formula_single)
set.seed(23)
#perform cross validation for each model
pois_mod_hs_rs <-
pois_mod_hs_wf %>%
fit_resamples(folds)
pois_mod_as_rs <-
pois_mod_as_wf %>%
fit_resamples(folds)
lin_mod_hs_rs <-
lin_mod_hs_wf %>%
fit_resamples(folds)
lin_mod_as_rs <-
lin_mod_as_wf %>%
fit_resamples(folds)
rf_mod_as_rs <-
rf_mod_as_wf %>%
fit_resamples(folds)
rf_mod_hs_rs <-
rf_mod_hs_wf %>%
fit_resamples(folds)
#compare cv results ---
collect_metrics(pois_mod_hs_rs)
collect_metrics(pois_mod_as_rs)
collect_metrics(lin_mod_hs_rs)
collect_metrics(lin_mod_as_rs)
collect_metrics(rf_mod_hs_rs)
collect_metrics(rf_mod_as_rs)
#fit final models ----
rf_mod_hs <- rf_mod %>% fit(hs_formula_single, data = data)
rf_mod_as <- rf_mod %>% fit(as_formula_single, data = data)
lin_mod_hs <- lin_mod %>% fit(hs_formula_single, data = data)
lin_mod_as <- lin_mod %>% fit(as_formula_single, data = data)
pois_mod_hs <- pois_mod %>% fit(hs_formula_single, data = data)
pois_mod_as <- pois_mod %>% fit(as_formula_single, data = data)
#predict scores ----
pois_hs_res <- unname(unlist(predict(pois_mod_hs, data %>% select(ranking_diff))))
pois_as_res <- unname(unlist(predict(pois_mod_as, data %>% select(ranking_diff))))
lm_hs_res <- unname(unlist(predict(lin_mod_hs, data %>% select(ranking_diff))))
rf_hs_res <- unname(unlist(predict(rf_mod_hs, data %>% select(ranking_diff))))
#compare models visually
ggplot(data %>% select(ranking_diff, home_score), aes(ranking_diff,home_score)) +
geom_point() +
geom_line(aes(ranking_diff, pois_hs_res), color="red", size=1) +
geom_line(aes(ranking_diff, lm_hs_res), color="green", size=1) +
geom_line(aes(ranking_diff, rf_hs_res), color="blue", size=1) +
xlab("FIFA Ranking Difference of teams vs. away team goals") +
ylab("Goals scored by home team") +
ggtitle("Comparison of Models")
#add results column (reclassify match outcomes into win,lose,draw)
data <- data %>% mutate(result=case_when(home_score > away_score ~ 0,
home_score < away_score ~ 1,
home_score == away_score ~ 2))
#reclassify predicted goals into win,lose,draw by rounding and comparison
predicted_result <- rep(0, length(pois_hs_res))
predicted_result[round(pois_hs_res) > round(pois_as_res)] <- 0
predicted_result[round(pois_hs_res) < round(pois_as_res)] <- 1
predicted_result[round(pois_hs_res) == round(pois_as_res)] <- 2
#analyse performance (accuracy and f1)
data = data %>% mutate(predicted_result = as.factor(predicted_result))
data = data %>% mutate(result = as.factor(result))
performance <- confusionMatrix(
data=as.factor(predicted_result),
reference=as.factor(data$result))
cm <- conf_mat(data, result, predicted_result)
autoplot(cm, type = "heatmap") +
scale_fill_gradient(low="#D6EAF8",high = "#2E86C1") +
ggtitle("Confusion Matrix of match outomes (with rounding)") +
scale_x_discrete(labels=c("Home Team Win","Away Team Win","Draw")) +
scale_y_discrete(labels=c("Draw","Away Team Win","Home Team Win"))
#macro f1
sum(performance[["byClass"]][ , "F1"])/3
#weighted f1
sum(performance[["byClass"]][ , "F1"]*colSums(performance$table))/sum(colSums(performance$table))
#accuracy
performance$overall["Accuracy"]
#calculate f1 scores for different thresholds
diffs <- seq(0.01,2,0.01)
weighted_f1_scores <- rep(NA,length(diffs))
for(i in 1:length(diffs)){
predicted_result_enh <- rep(NA, length(pois_hs_res))
predicted_result_enh[pois_hs_res > pois_as_res+diffs[i]] <- 0
predicted_result_enh[pois_hs_res < pois_as_res-diffs[i]] <- 1
predicted_result_enh[!(pois_hs_res > pois_as_res+diffs[i]) & !(pois_hs_res < pois_as_res-diffs[i])] <- 2
performance <- confusionMatrix(
as.factor(predicted_result_enh),
as.factor(data$result))
weighted_f1 <- sum(performance[["byClass"]][ , "F1"]*colSums(performance$table))/sum(colSums(performance$table))
weighted_f1_scores[i] <- weighted_f1
}
ggplot(as_tibble(diffs, weighted_f1_scores), aes(diffs,weighted_f1_scores)) +
geom_point() +
geom_line() +
geom_vline(aes(xintercept=diffs[which.max(weighted_f1_scores)]), color="red")+
geom_text(aes(label = diffs[which.max(weighted_f1_scores)], x= diffs[which.max(weighted_f1_scores)]+0.1, y=0.7), size=4, color="red") +
ggtitle("F1-Scores for goal difference thesholds") +
ylab("Weighted F1-Scores") +
xlab("Goal Difference Thresholds")
predicted_result_enh <- rep(NA, length(pois_hs_res))
diff <- 0.41
predicted_result_enh[pois_hs_res > pois_as_res+diff] <- 0
predicted_result_enh[pois_hs_res < pois_as_res-diff] <- 1
predicted_result_enh[!(pois_hs_res > pois_as_res+diff) & !(pois_hs_res < pois_as_res-diff)] <- 2
data = data %>% mutate(predicted_result_enh = as.factor(predicted_result_enh))
cm <- conf_mat(data, result, predicted_result_enh)
autoplot(cm, type = "heatmap") +
scale_fill_gradient(low="#D6EAF8",high = "#2E86C1") +
ggtitle("Confusion Matrix of match outomes (with difference threshold)") +
scale_x_discrete(labels=c("Home Team Win","Away Team Win","Draw")) +
scale_y_discrete(labels=c("Draw","Away Team Win","Home Team Win"))
performance <- confusionMatrix(
data=as.factor(predicted_result_enh),
reference=as.factor(data$result))
#weighted f1
sum(performance[["byClass"]][ , "F1"]*colSums(performance$table))/sum(colSums(performance$table))
#------------
#EXPORE EURO 2020 MATCHES
#-----------
euro_data <- euro_data %>% mutate(result=case_when(home_score > away_score ~ 0,
home_score < away_score ~ 1,
home_score == away_score ~ 2))
euro_data_short <- euro_data %>% select(home_team, away_team, home_score, away_score, result)
#predict euro 2020
pois_hs_res_euro <- unname(unlist(predict(pois_mod_hs, euro_data %>% select(ranking_diff))))
pois_as_res_euro <- unname(unlist(predict(pois_mod_as, euro_data %>% select(ranking_diff))))
predicted_result_euro <- rep(0, length(pois_hs_res_euro))
#reclassify predicted goals into win,lose,draw by rounding
predicted_result_euro[round(pois_hs_res_euro) > round(pois_as_res_euro)] <- 0
predicted_result_euro[round(pois_hs_res_euro) < round(pois_as_res_euro)] <- 1
predicted_result_euro[round(pois_hs_res_euro) == round(pois_as_res_euro)] <- 2
#check performance
performance <- confusionMatrix(as.factor(predicted_result_euro),
as.factor(euro_data_short$result))
sum(performance[["byClass"]][ , "F1"]*colSums(performance$table))/sum(colSums(performance$table))
#f1 with different threshold (from above)
predicted_result_enh <- rep(NA, length(pois_hs_res_euro))
diff <- 0.41
predicted_result_enh[pois_hs_res_euro > pois_as_res_euro+diff] <- 0
predicted_result_enh[pois_hs_res_euro < pois_as_res_euro-diff] <- 1
predicted_result_enh[!(pois_hs_res_euro > pois_as_res_euro+diff) & !(pois_hs_res_euro < pois_as_res_euro-diff)] <- 2
performance <- confusionMatrix(
as.factor(predicted_result_enh),
as.factor(euro_data_short$result))
sum(performance[["byClass"]][ , "F1"]*colSums(performance$table))/sum(colSums(performance$table))
euro_data_short <-
euro_data_short %>%
mutate(home_score_predicted = pois_hs_res_euro) %>%
mutate(away_score_predicted = pois_as_res_euro) %>%
mutate(result_predicted=predicted_result_enh)
write_csv(euro_data_short, "euro_res.csv")