-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicEstimation.R
More file actions
71 lines (70 loc) · 2.96 KB
/
DynamicEstimation.R
File metadata and controls
71 lines (70 loc) · 2.96 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
rm(list = ls())
#-------------------------------------------------------------------------------
# Libraries and utils
#-------------------------------------------------------------------------------
library(dplyr)
library(tidyverse)
library(rstan)
library(ggplot2)
library(patchwork)
#-------------------------------------------------------------------------------
# Global settings and variables
#-------------------------------------------------------------------------------
options(mc.cores = parallel::detectCores())
#rstan_options(auto_write = TRUE)
rstan_options(threads_per_chain = 2)
N_CHAINS=4
N_ITERS=11000
N_WARMUP=1000
DATA_DIR= "data/"
STAN_DIR= "stan/"
SEASON="2122"
OFFLINE_OFFLINE_MODELS_DIR= paste0("estimated_models/season_",SEASON,"/offline_models/")
#-------------------------------------------------------------------------------
# Data import and preparation
#-------------------------------------------------------------------------------
SerieA_data<- read.csv(file= paste0(DATA_DIR,"season_",SEASON,"/SerieA_",SEASON,".csv"))
SerieA_data<- SerieA_data[,c("HomeTeam","AwayTeam","FTHG","FTAG")]
teams<- unique(SerieA_data$HomeTeam)
n_games<- nrow(SerieA_data)
n_teams<- length(teams)
n_matchdays= ceiling(n_games/((n_teams)/2))
ht= unlist(sapply(1:n_games,function (g) which(teams==SerieA_data$HomeTeam[g])))
at= unlist(sapply(1:n_games,function (g) which(teams==SerieA_data$AwayTeam[g])))
teams<- str_replace_all(teams, " ", "")
#-------------------------------------------------------------------------------
# Estimation of the models over time
#-------------------------------------------------------------------------------
if(!file.exists(OFFLINE_OFFLINE_MODELS_DIR)){
dir.create(OFFLINE_OFFLINE_MODELS_DIR,recursive = T)
}
for(m in 19:n_matchdays){
cat("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
cat("Parameters estimation after matchday n.",m,"...\n")
cat("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
#---------------------------------------------------------------------------
# (1) Training and test set for the current matchday
cat("...Preparing the training set...\n")
training<- SerieA_data[1:(10*m),c("HomeTeam","AwayTeam","FTHG","FTAG")]
training<- na.omit(training)
# (2) Prepare the parameters for stan
stan_parameters = list(
n_teams=n_teams,
n_games=nrow(training),
home_team= ht[1:nrow(training)],
away_team= at[1:nrow(training)],
goal_difference = SerieA_data$FTHG[1:nrow(training)]-SerieA_data$FTAG[1:nrow(training)]
)
# (3) Fit the model
cat("...Fitting the model...\n")
KN_model <- stan(file = paste0(STAN_DIR,"karlis-ntzoufras.stan"),
data = stan_parameters,
chains = N_CHAINS,
iter = N_ITERS,
warmup = N_WARMUP,
seed = 16
)
# (4) Save the model
dir.create(paste0(OFFLINE_OFFLINE_MODELS_DIR,"matchday",m))
save(KN_model,file=paste0(OFFLINE_OFFLINE_MODELS_DIR,"matchday",m,"/KN_matchday",m,".rds"))
}