-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstan_foot.R
4331 lines (3830 loc) · 150 KB
/
stan_foot.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Fit football models with Stan
#'
#' Stan football modelling for the most famous models:
#' double Poisson, bivariate Poisson, Skellam, student t, diagonal-inflated bivariate Poisson and zero-inflated Skellam.
#'
#'@param data A data frame, or a matrix containing the following mandatory items: season, home team, away team,
#'home goals, away goals.
#'@param model The type of Stan model used to fit the data.
#' One among the following: \code{"double_pois"},
#' \code{"biv_pois"}, \code{"skellam"}, \code{"student_t"}, \code{"diag_infl_biv_pois"}, \code{"zero_infl_skellam"}.
#'@param predict The number of out-of-sample matches. If missing, the function returns
#'the fit for the training set only.
#'@param ranking Eventual numeric ranking provided for the teams in the dataset (e.g., the \href{https://www.fifa.com/fifa-world-ranking}{Coca-Cola Fifa ranking})
#'@param dynamic_type One among \code{"weekly"} or \code{"seasonal"} for weekly dynamic parameters or seasonal
#'dynamic parameters.
#'@param prior The prior distribution for the team-specific abilities.
#'Possible choices: \code{normal}, \code{student_t}, \code{cauchy}, \code{laplace}.
#'See the \pkg{rstanarm} for a deep overview and read the vignette \href{http://mc-stan.org/rstanarm/articles/priors.html}{\emph{Prior
#' Distributions for rstanarm Models}}
#'@param prior_sd The prior distribution for the team-specific standard deviations. See the \code{prior} argument for more details.
#'@param ... Optional parameters passed to the function
#' in the \bold{rstan} package. It is possibly to specify \code{iter}, \code{chains}, \code{cores}, \code{refresh}, etc.
#'@return
#'
#'An object of S4 class, \code{\link[rstan]{stanfit-class}}.
#'
#'@details
#'Let \eqn{(y^{H}_{n}, y^{A}_{n})} denote the
#'observed number of goals scored by the home
#'and the away team in the \eqn{n}-th game,
#'respectively. A general bivariate Poisson model
#'allowing for goals' correlation
#'(Karlis & Ntzoufras, 2003) is the following:
#'
#'\deqn{ Y^H_n, Y^A_n| \lambda_{1n}, \lambda_{2n}, \lambda_{3n} \sim \mathsf{BivPoisson}(\lambda_{1n}, \lambda_{2n}, \lambda_{3n})}
#'\deqn{\log(\lambda_{1n}) = \mu+att_{h_n} + def_{a_n}}
#'\deqn{\log(\lambda_{2n}) = att_{a_n} + def_{h_n}}
#'\deqn{\log(\lambda_{3n}) =\beta_0,}
#'
#' where the case \eqn{\lambda_{3n}=0} reduces to
#' the double Poisson model (Baio & Blangiardo, 2010).
#' \eqn{\lambda_{1n}, \lambda_{2n}} represent the
#' scoring rates for the home and the away team,
#' respectively, where: \eqn{\mu} is the home effect;
#' the parameters \eqn{att_T} and
#' \eqn{def_T} represent the attack and the
#' defence abilities,
#' respectively, for each team \eqn{T}, \eqn{T=1,\ldots,N_T};
#' the nested indexes \eqn{h_{n}, a_{n}=1,\ldots,N_T}
#' denote the home and the away team playing in the \eqn{n}-th game,
#' respectively. Attack/defence parameters are imposed a
#' sum-to-zero constraint to achieve identifiability and
#' assigned some weakly-informative prior distributions:
#'
#' \deqn{att_T \sim \mathcal{N}(\mu_{att}, \sigma_{att})}
#' \deqn{def_T \sim \mathcal{N}(\mu_{def}, \sigma_{def}),}
#'
#' with hyperparameters \eqn{\mu_{att}, \sigma_{att}, \mu_{def}, \sigma_{def}}.
#'
#' Instead of using the marginal number of goals,
#' another alternative is to modelling directly
#' the score difference \eqn{(y^{H}_{n}- y^{A}_{n})}.
#' We can use the Poisson-difference distribution
#' (or Skellam distribution) to model goal
#' difference in the \eqn{n}-th match (Karlis & Ntzoufras, 2009):
#'
#' \deqn{y^{H}_{n}- y^{A}_{n}| \lambda_{1n}, \lambda_{2n} \sim PD(\lambda_{1n}, \lambda_{2n}),}
#'
#' and the scoring rates \eqn{\lambda_{1n}, \lambda_{2n}} are
#' unchanged with respect to the bivariate/double Poisson model.
#' If we want to use a continue distribution, we can
#' use a student t distribution with 7 degrees of
#' freedom (Gelman, 2014):
#'
#' \deqn{y^{H}_{n}- y^{A}_{n} \sim t(7, ab_{h_{n}}-ab_{a(n)}, \sigma_y)}
#' \deqn{ab_t \sim \mathcal{N}(\mu + b \times {prior\_score}_t, sigma_{ab}),}
#'
#' where \eqn{ab_t} is the overall ability for
#' the \eqn{t}-th team, whereas \eqn{prior\_score_t}
#' is a prior measure of team's strength (for instance a
#' ranking).
#'
#' These model rely on the assumption of static parameters.
#' However, we could assume dynamics in the attach/defence
#' abilities (Owen, 2011; Egidi et al., 2018) in terms of weeks or seasons through the argument
#' \code{dynamic_type}. In such a framework, for a given
#' number of times \eqn{1, \ldots, \mathcal{T}}, the models
#' above would be unchanged, but the priors for the abilities
#' parameters at each time \eqn{\tau, \tau=2,\ldots, \mathcal{T},} would be:
#'
#' \deqn{att_{T, \tau} \sim \mathcal{N}({att}_{T, \tau-1}, \sigma_{att})}
#' \deqn{def_{T, \tau} \sim \mathcal{N}({def}_{T, \tau-1}, \sigma_{def}),}
#'
#' whereas for \eqn{\tau=1} we have:
#'
#' \deqn{att_{T, 1} \sim \mathcal{N}(\mu_{att}, \sigma_{att})}
#' \deqn{def_{T, 1} \sim \mathcal{N}(\mu_{def}, \sigma_{def}).}
#'
#' Of course, the identifiability constraint must be imposed for
#' each time \eqn{\tau}.
#'
#' The current version of the package allows for the fit of a
#' diagonal-inflated bivariate Poisson and a zero-inflated Skellam model in the
#' spirit of (Karlis & Ntzoufras, 2003) to better capture draw occurrences. See the vignette for further details.
#'
#'@author Leonardo Egidi \email{[email protected]}, Vasilis Palaskas \email{[email protected]}.
#'
#'@references
#' Baio, G. and Blangiardo, M. (2010). Bayesian hierarchical model for the prediction of football
#' results. Journal of Applied Statistics 37(2), 253-264.
#'
#' Egidi, L., Pauli, F., and Torelli, N. (2018). Combining historical data
#' and bookmakers' odds in modelling football scores. Statistical Modelling, 18(5-6), 436-459.
#'
#' Gelman, A. (2014). Stan goes to the World Cup. From
#' "Statistical Modeling, Causal Inference, and Social Science" blog.
#'
#' Karlis, D. and Ntzoufras, I. (2003). Analysis of sports data by using bivariate poisson models.
#' Journal of the Royal Statistical Society: Series D (The Statistician) 52(3), 381-393.
#'
#' Karlis, D. and Ntzoufras,I. (2009). Bayesian modelling of football outcomes: Using
#' the Skellam's distribution for the goal difference. IMA Journal of Management Mathematics 20(2), 133-145.
#'
#' Owen, A. (2011). Dynamic Bayesian forecasting models
#' of football match outcomes with estimation of the
#' evolution variance parameter. IMA Journal of Management Mathematics, 22(2), 99-113.
#'
#'
#'@examples
#'\donttest{
#'if(requireNamespace("engsoccerdata")){
#'require(engsoccerdata)
#'require(tidyverse)
#'require(dplyr)
#'
#'### Use Italian Serie A from 2000 to 2002
#'
#'italy <- as_tibble(italy)
#'italy_2000_2002<- italy %>%
#' dplyr::select(Season, home, visitor, hgoal,vgoal) %>%
#' dplyr::filter(Season=="2000" | Season=="2001"| Season=="2002")
#'
#'
#' ### Fit Stan models
#' ## no dynamics, no predictions
#'
#' fit1 <- stan_foot(data = italy_2000_2002,
#' model="double_pois") # double poisson
#' print(fit1, pars =c("home", "sigma_att",
#' "sigma_def"))
#'
#' fit2 <- stan_foot(data = italy_2000_2002,
#' model="biv_pois") # bivariate poisson
#' print(fit2, pars =c("home", "rho",
#' "sigma_att", "sigma_def"))
#'
#' fit3 <- stan_foot(data = italy_2000_2002,
#' model="skellam") # skellam
#' print(fit3, pars =c("home", "sigma_att",
#' "sigma_def"))
#'
#' fit4 <- stan_foot(data = italy_2000_2002,
#' model="student_t") # student_t
#' print(fit4, pars =c("home", "beta"))
#'
#' ## seasonal dynamics, no prediction
#'
#' fit5 <- stan_foot(data = italy_2000_2002,
#' model="double_pois",
#' dynamic_type ="seasonal") # double poisson
#' print(fit5, pars =c("home", "Sigma_att",
#' "Sigma_def"))
#'
#' ## seasonal dynamics, prediction for the last season
#'
#' fit6 <- stan_foot(data = italy_2000_2002,
#' model="double_pois",
#' dynamic_type ="seasonal",
#' predict = 306) # double poisson
#' print(fit6, pars =c("home", "Sigma_att",
#' "Sigma_def"))
#'
#' ## other priors' options
#'
#' fit_p <- stan_foot(data = italy_2000_2002,
#' model="double_pois",
#' priors = student_t (4, 0, NULL),
#' prior_sd = laplace(0,1)) # double poisson with
#' # student_t priors for teams abilities
#' # and laplace prior for the hyper sds
#' print(fit_p, pars = c("home", "sigma_att",
#' "sigma_def"))
#' }
#'}
#'@import rstan
#'@import bayesplot
#'@import matrixStats
#'@import reshape2
#'@import ggplot2
#'@export
stan_foot <- function(data,
model,
predict,
ranking,
dynamic_type,
prior,
prior_sd,
ind_home = "TRUE",
...){
## DATA CHECKS
if (!is.matrix(data) & !is.data.frame(data)){
stop("Data are not stored in matrix/data frame
structure. Pleasy, provide data correctly.")
}
if (dim(data)[2]<5){
stop("Data dimensions are wrong! Please,
supply a matrix/data frame containing
the following mandatory column items:
season, home team, away team,
home goals, away goals.")
}
#if (dim(data)[2]==5){
colnames(data) <- c("season", "home", "away",
"homegoals", "awaygoals")
#}
# checks sui formati
if ( !is.numeric(data$homegoals) |!is.numeric(data$awaygoals)){
stop("Goals are not numeric! Please, provide
numeric values for the goals")
}
# check about columns
if (dim(data)[2]>5){
warning("Your dataset seems too large!
The function will evaluate the first
five columns as follows:
season, home team, away team, home goals,
away goals")
# stop("Wrong number of columns! Please,
# supply a matrix/data frame containing
# the following mandatory column items:
# season, home team, away team,
# home goals, away goals.")
}
## MODEL'S NAME CHECKS
good_names <- c("double_pois",
"biv_pois",
"skellam",
"student_t",
"diag_infl_biv_pois",
"zero_infl_skellam")
model <- match.arg(model, good_names)
nteams<- length(unique(data$home))
user_dots <- list(chains = 4, iter = 2000,
#warmup = floor(iter/2),
thin = 1,
init = "random", seed = sample.int(.Machine$integer.max, 1),
algorithm = c("NUTS", "HMC", "Fixed_param"),
control = NULL, sample_file = NULL, diagnostic_file = NULL,
save_dso = TRUE, verbose = FALSE, include = TRUE,
cores = getOption("mc.cores", 1L),
open_progress = interactive() && !isatty(stdout()) &&
!identical(Sys.getenv("RSTUDIO"), "1"),
boost_lib = NULL, eigen_lib = NULL,
nu = 7)
## OPTIONAL ARGUMENTS CHECKS
if (missing(...)){
user_dots <- user_dots
}else{
user_dots_prel <- list(...)
names_prel <- names(user_dots_prel)
names_dots<- names(user_dots)
for (u in 1:length(names_prel)){
user_dots[names_prel[u] == names_dots]<- user_dots_prel[u]
}
}
## PREDICT CHECKS
#predict <- round(predict)
if (missing(predict)){ # check on predict
predict <- 0
N <- dim(data)[1]# rows of the dataset
N_prev <- 0
type <- "fit"
}else if(predict ==0){
predict <- 0
N <- dim(data)[1]
N_prev <- 0
type <- "fit"
}else if (is.numeric(predict)){
if (predict%%1 !=0){
warning("Please, use integer numbers for the argument 'predict'!
The input has been rounded to the closes integer number.")
predict <- round(predict)
}
N <- dim(data)[1]-predict
N_prev <- predict
type <- "prev"
}else if (!is.numeric(predict)){
stop("The number of out-of-sample matches is ill posed!
Pick up an integer number.")
}
if (predict >= dim(data)[1]){
stop("The training set size is zero!
Please, select a lower value for the
out-of-sample matches, through the
argument predict.")
}
## DYNAMICS CHECKS
# names conditions
if (!missing(dynamic_type)){
dynamic_names <- c("weekly", "seasonal")
dynamic_type <- match.arg(dynamic_type, dynamic_names)
}
if (missing(dynamic_type)){
dyn <-""
}else if (dynamic_type == "weekly" ){
dyn <- "dynamic_"
if (length(unique(data$season))!=1){
stop("When using weekly dynamics,
please consider one season only.")
}else{
weak_count <- ((N+predict)*2)/(nteams)
if ((N*2)%%(nteams)!=0){
stop("The number of total matches is not
the same for all the teams. Please,
provide an adequate number of matches
(hint: proportional to the number
of matches for each match day).")
}
weak <- rep(seq(1, weak_count ), each = nteams/2)
data <- data %>%
mutate(weak)
ntimes <- length(unique(weak))
#time_tot <- c(1:length(unique(weak[1:(N+N_prev)])))
time <- c(1:length(unique(weak)))
instants <- weak[1:N]
#ntimes_prev <- length(unique(weak[1:(N+N_prev)]))-length(unique(weak[1:N]))
#time_prev <- setdiff(time_tot, time)
instants_prev <- weak[(N+1):(N+N_prev)]
}
}else if(dynamic_type=="seasonal"){
dyn <- "dynamic_"
if (length(unique(data$season))==1){
dyn <-""
warning("When using seasonal dynamics,
please consider more than one season.
No dynamics is used to fit the model")
}
season_count <- length(unique(data$season))
season <- match(data$season, unique(data$season))
ntimes <- season_count
#time_tot <- c(1:length(unique(data$season)))
time <- c(1:season_count)
instants <- season[1:N]
#ntimes_prev <- length(unique(season[1:(N+N_prev)]))-length(unique(season[1:N]))
#time_prev <- setdiff(time_tot, time)
instants_prev <- season[(N+1):(N+N_prev)]
}
## PRIOR CHECKS
hyper_df <- 1 # initialization
if (missing(prior)){ # Normal as default weakly-inf. prior
prior_dist_num <- 1
prior <- normal(0,NULL)
hyper_location<- 0 # location
#hyper_sd_scale <- 5 # scale
}else{
prior_dist <- prior$dist
#good_prior_names <- c("normal", "student_t", "cauchy", "laplace")
#prior_dist <- match.arg(prior_dist, good_prior_names)
if (is.null(prior$scale)==FALSE){
warning("Group-level standard deviations cannot be fixed to
numerical values, rather they need to be assigned
a reasonable prior distribution. Thus, the 'scale'
argument in the 'prior' argument will be omitted
(by default, prior$scale=NULL).")
}
if (prior_dist == "normal"){
prior_dist_num <- 1
hyper_df <- 1
hyper_location <- prior$location
# if (is.null(prior_sd$scale)){
# hyper_sd_scale <-1
# }else{
# hyper_sd_scale <- prior_sd$scale
# }
}else if (prior_dist=="t" && prior$df!=1){
prior_dist_num <- 2 # student-t
hyper_df <- prior$df
hyper_location <- prior$location
# if (is.null(prior_sd$scale)){
# hyper_sd_scale <-1
# }else{
# hyper_sd_scale <- prior_sd$scale
# }
}else if (prior_dist=="t"&& prior$df==1){
prior_dist_num <- 3
hyper_df <- 1 # by default of Cauchy distribution
hyper_location <- prior$location
# if (is.null(prior$scale)){
# hyper_sd_scale <-1
# }else{
# hyper_sd_scale <- prior_sd$scale
# }
} else if (prior_dist =="laplace"){
prior_dist_num <- 4
hyper_df <- 1
hyper_location <- prior$location
# if (is.null(prior_sd$scale)){
# hyper_sd_scale <-1
# }else{
# hyper_sd_scale <- prior_sd$scale
# }
}
}
hyper_sd_df <- 1 # initialization
if (missing(prior_sd)){ # Cauchy as default weakly-inf. prior
prior_dist_sd_num <- 3
hyper_sd_df <- 1 # student_t with 1 df
hyper_sd_location<- 0 # location
hyper_sd_scale <- 5 # scale
}else{
prior_dist_sd <- prior_sd$dist
if (prior_dist_sd == "normal"){
prior_dist_sd_num <- 1
hyper_sd_df <- 1
hyper_sd_location <- prior_sd$location
if (is.null(prior_sd$scale)){
hyper_sd_scale <-1
}else{
hyper_sd_scale <- prior_sd$scale
}
}else if (prior_dist_sd=="t" && prior_sd$df!=1){
prior_dist_sd_num <- 2 # student-t
hyper_sd_df <- prior_sd$df
hyper_sd_location <- prior_sd$location
if (is.null(prior_sd$scale)){
hyper_sd_scale <-1
}else{
hyper_sd_scale <- prior_sd$scale
}
}else if (prior_dist_sd=="t"&& prior_sd$df==1){
prior_dist_sd_num <- 3
hyper_sd_df <- 1 # by default of Cauchy distribution
hyper_sd_location <- prior_sd$location
if (is.null(prior_sd$scale)){
hyper_sd_scale <-1
}else{
hyper_sd_scale <- prior_sd$scale
}
} else if (prior_dist_sd =="laplace"){
prior_dist_sd_num <- 4
hyper_sd_df <- 1
hyper_sd_location <- prior_sd$location
if (is.null(prior_sd$scale)){
hyper_sd_scale <-1
}else{
hyper_sd_scale <- prior_sd$scale
}
}
}
teams <- unique(data$home)
team_home <- match( data$home, teams)
team_away <- match( data$away, teams)
team1 <- team_home[1:N]
team2 <- team_away[1:N]
team1_prev <- team_home[(N+1):(N+N_prev)]
team2_prev <- team_away[(N+1):(N+N_prev)]
y <- matrix(NA, N, 2)
y[,1] <- as.numeric(as.vector(data$homegoals)[1:N])
y[,2] <- as.numeric(as.vector(data$awaygoals)[1:N])
diff_y <- y[,1]-y[,2]
## RANKING CHECKS
if (missing(ranking)){
ranking <- matrix(0, nteams,2)
}else if (is.matrix(ranking)==FALSE & is.data.frame(ranking)== FALSE ){
stop("Please, ranking must be a matrix or a data frame!")
}else{
colnames(ranking) <- c("rank_team", "points")
team_order <- match(teams, ranking$rank_team)
ranking[,1] <- ranking$rank_team[team_order]
ranking[,2] <- ranking$points[team_order]
ranking[,2] <- (as.numeric(as.vector(ranking[,2]))-mean(as.numeric(as.vector(ranking[,2]))))/(2*sd(as.numeric(as.vector(ranking[,2]))))
}
## HOME EFFECT CKECK
home_names <- c("TRUE", "FALSE")
ind_home <- match.arg(ind_home, home_names)
if (missing(ind_home)){
ind_home = "TRUE"
}else{
ind_home = ind_home
}
ind_home <- 0*(ind_home=="FALSE") + 1*(ind_home =="TRUE")
# Stan data
data_stan <- list( y=y,
spi_std = rep(0, nteams),
diff_y = diff_y,
N=N,
N_prev = N_prev,
nteams=nteams,
team1 = team1,
team2=team2,
team1_prev= team1_prev,
team2_prev=team2_prev,
prior_dist_num = prior_dist_num,
prior_dist_sd_num = prior_dist_sd_num,
hyper_df=hyper_df,
hyper_location=hyper_location,
hyper_sd_df=hyper_sd_df,
hyper_sd_location=hyper_sd_location,
hyper_sd_scale=hyper_sd_scale,
ranking = ranking[,2],
nu = user_dots$nu,
ind_home = ind_home)
if (!missing(dynamic_type)){
data_stan$ntimes <- ntimes
data_stan$instants <- instants
data_stan$time <- time
data_stan$instants_prev <- instants_prev
}
stanfoot_models <- function(model, dyn, type){
right_name <- paste(model,"_", dyn, type, sep="")
models_name <- c("biv_pois_dynamic_fit",
"biv_pois_dynamic_prev",
"biv_pois_fit",
"biv_pois_prev",
"diag_infl_biv_pois_dynamic_fit",
"diag_infl_biv_pois_dynamic_prev",
"diag_infl_biv_pois_fit",
"diag_infl_biv_pois_prev",
"double_pois_dynamic_fit",
"double_pois_dynamic_prev",
"double_pois_fit",
"double_pois_prev",
"skellam_dynamic_fit",
"skellam_dynamic_prev",
"skellam_fit",
"skellam_prev",
"zero_infl_skellam_dynamic_fit",
"zero_infl_skellam_dynamic_prev",
"zero_infl_skellam_fit",
"zero_infl_skellam_prev",
"student_t_dynamic_fit",
"student_t_dynamic_prev",
"student_t_fit",
"student_t_prev"
)
biv_pois_dynamic_fit<-
"functions{
real bipois_lpmf(int[] r , real mu1,real mu2,real mu3) {
real ss;
real log_s;
real mus;
int miny;
miny = min(r[1], r[2]);
ss = poisson_lpmf(r[1] | mu1) + poisson_lpmf(r[2] | mu2) -
exp(mu3);
if(miny > 0) {
mus = -mu1-mu2+mu3;
log_s = ss;
for(k in 1:miny) {
log_s = log_s + log(r[1] - k + 1) + mus
+ log(r[2] - k + 1)
- log(k);
ss = log_sum_exp(ss, log_s);
}
}
return(ss);
}
}
data{
int N; // number of games
int y[N,2];
int nteams;
int team1[N];
int team2[N];
int ntimes; // dynamic periods
int time[ntimes];
int instants[N];
real ranking[nteams];
// priors part
int<lower=1,upper=4> prior_dist_num; // 1 gaussian, 2 t, 3 cauchy, 4 laplace
int<lower=1,upper=4> prior_dist_sd_num; // 1 gaussian, 2 t, 3 cauchy, 4 laplace
real hyper_df;
real hyper_location;
real hyper_sd_df;
real hyper_sd_location;
real hyper_sd_scale;
}
parameters{
matrix[ntimes, nteams] att_raw; // raw attack ability
matrix[ntimes, nteams] def_raw; // raw defense ability
real rho;
real home;
real<lower=0> sigma_att;
real<lower=0> sigma_def;
real gamma;
}
transformed parameters{
matrix[ntimes, nteams] att; // attack abilities
matrix[ntimes, nteams] def; // defense abilities
// cov_matrix[ntimes] Sigma_att; // Gaussian process attack cov. funct.
// cov_matrix[ntimes] Sigma_def; // Gaussian process defense cov.funct.
matrix[ntimes, nteams] mu_att; // attack hyperparameter
matrix[ntimes, nteams] mu_def; // defense hyperparameter
vector[N] theta_home; // exponentiated linear pred.
vector[N] theta_away;
vector[N] theta_corr;
// Gaussian process covariance functions
// for (i in 1:(ntimes)){
// for (j in 1:(ntimes)){
// Sigma_att[i, j] = exp(-pow(time[i] - time[j], 2))
// + (i == j ? 0.1 : 0.0);
// Sigma_def[i, j] = exp(-pow(time[i] - time[j], 2))
// + (i == j ? 0.1 : 0.0);
// }}
// Sum-to-zero constraint for attack/defense parameters
att[1]=att_raw[1]-mean(att_raw[1]);
def[1]=def_raw[1]-mean(def_raw[1]);
for (t in 2:ntimes){
att[t]=att_raw[t]-mean(att_raw[t]);
def[t]=def_raw[t]-mean(def_raw[t]);
}
// Lagged prior mean for attack/defense parameters
for (t in 2:(ntimes)){
mu_att[1]=rep_row_vector(hyper_location,nteams);
mu_att[t]= att[t-1];
//rep_row_vector(0,nteams);
mu_def[1]=rep_row_vector(hyper_location, nteams);
mu_def[t]= def[t-1];
//rep_row_vector(0,nteams);
}
for (n in 1:N){
theta_home[n] = exp(home+att[instants[n], team1[n]]+def[instants[n], team2[n]]+
(gamma/2)*(ranking[team1[n]]-ranking[team2[n]]));
theta_away[n] = exp(att[instants[n], team2[n]]+def[instants[n], team1[n]]-
(gamma/2)*(ranking[team1[n]]-ranking[team2[n]]));
theta_corr[n] = exp(rho);
}
}
model{
// log-priors for team-specific abilities
for (h in 1:(nteams)){
if (prior_dist_num == 1 ){
att_raw[,h]~multi_normal(mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_normal(mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
else if (prior_dist_num == 2 ){
att_raw[,h]~multi_student_t(hyper_df, mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_student_t(hyper_df, mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
else if (prior_dist_num == 3 ){
att_raw[,h]~multi_student_t(1, mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_student_t(1, mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
}
// log-hyperpriors for sd parameters
if (prior_dist_sd_num == 1 ){
target+=normal_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=normal_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 2){
target+=student_t_lpdf(sigma_att|hyper_sd_df, hyper_sd_location, hyper_sd_scale);
target+=student_t_lpdf(sigma_def|hyper_sd_df, hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 3){
target+=cauchy_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=cauchy_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 4){
target+=double_exponential_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=double_exponential_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
// log-priors fixed effects
target+=normal_lpdf(home|0,5);
target+=normal_lpdf(rho|0,1);
target+=normal_lpdf(gamma|0,1);
// likelihood
for (n in 1:N){
//target+=bipois_lpmf(y[n,]| theta_home[n],
// theta_away[n], theta_corr[n]);
target+=poisson_lpmf(y[n,1]|theta_home[n]+theta_corr[n]);
target+=poisson_lpmf(y[n,2]|theta_away[n]+theta_corr[n]);
}
}
generated quantities{
int y_rep[N,2];
vector[N] log_lik;
int diff_y_rep[N];
//in-sample replications
for (n in 1:N){
y_rep[n,1] = poisson_rng(theta_home[n]+theta_corr[n]);
y_rep[n,2] = poisson_rng(theta_away[n]+theta_corr[n]);
diff_y_rep[n] = y_rep[n,1] - y_rep[n,2];
log_lik[n] = poisson_lpmf(y[n,1]|theta_home[n]+theta_corr[n])+
poisson_lpmf(y[n,2]|theta_away[n]+theta_corr[n]);
//bipois_lpmf(y[n,]| theta_home[n],
// theta_away[n], theta_corr[n]);
}
}"
biv_pois_dynamic_prev<-"
functions{
real bipois_lpmf(int[] r , real mu1,real mu2,real mu3) {
real ss;
real log_s;
real mus;
int miny;
miny = min(r[1], r[2]);
ss = poisson_lpmf(r[1] | mu1) + poisson_lpmf(r[2] | mu2) -
exp(mu3);
if(miny > 0) {
mus = -mu1-mu2+mu3;
log_s = ss;
for(k in 1:miny) {
log_s = log_s + log(r[1] - k + 1) + mus
+ log(r[2] - k + 1)
- log(k);
ss = log_sum_exp(ss, log_s);
}
}
return(ss);
}
}
data{
int N; // number of games
int N_prev;
int y[N,2];
int nteams;
int team1[N];
int team2[N];
int team1_prev[N_prev];
int team2_prev[N_prev];
int ntimes; // dynamic periods
int time[ntimes];
int instants[N];
int instants_prev[N_prev];
real ranking[nteams];
// priors part
int<lower=1,upper=4> prior_dist_num; // 1 gaussian, 2 t, 3 cauchy, 4 laplace
int<lower=1,upper=4> prior_dist_sd_num; // 1 gaussian, 2 t, 3 cauchy, 4 laplace
real hyper_df;
real hyper_location;
real hyper_sd_df;
real hyper_sd_location;
real hyper_sd_scale;
}
parameters{
matrix[ntimes, nteams] att_raw; // raw attack ability
matrix[ntimes, nteams] def_raw; // raw defense ability
real rho;
real home;
real<lower=0> sigma_att;
real<lower=0> sigma_def;
real gamma;
}
transformed parameters{
matrix[ntimes, nteams] att; // attack abilities
matrix[ntimes, nteams] def; // defense abilities
//cov_matrix[ntimes] Sigma_att; // Gaussian process attack cov. funct.
//cov_matrix[ntimes] Sigma_def; // Gaussian process defense cov.funct.
matrix[ntimes, nteams] mu_att; // attack hyperparameter
matrix[ntimes, nteams] mu_def; // defense hyperparameter
vector[N] theta_home; // exponentiated linear pred.
vector[N] theta_away;
vector[N] theta_corr;
// Gaussian process covariance functions
// for (i in 1:(ntimes)){
// for (j in 1:(ntimes)){
// Sigma_att[i, j] = exp(-pow(time[i] - time[j], 2))
// + (i == j ? 0.1 : 0.0);
// Sigma_def[i, j] = exp(-pow(time[i] - time[j], 2))
// + (i == j ? 0.1 : 0.0);
// }}
// Sum-to-zero constraint for attack/defense parameters
att[1]=att_raw[1]-mean(att_raw[1]);
def[1]=def_raw[1]-mean(def_raw[1]);
for (t in 2:ntimes){
att[t]=att_raw[t]-mean(att_raw[t]);
def[t]=def_raw[t]-mean(def_raw[t]);
}
// Lagged prior mean for attack/defense parameters
for (t in 2:(ntimes)){
mu_att[1]=rep_row_vector(hyper_location,nteams);
mu_att[t]=att[t-1];
//rep_row_vector(0,nteams);
mu_def[1]=rep_row_vector(hyper_location,nteams);
mu_def[t]=def[t-1];
//rep_row_vector(0,nteams);
}
for (n in 1:N){
theta_home[n] = exp(home+att[instants[n], team1[n]]+def[instants[n], team2[n]]+
(gamma/2)*(ranking[team1[n]]-ranking[team2[n]]));
theta_away[n] = exp(att[instants[n], team2[n]]+def[instants[n], team1[n]]-
(gamma/2)*(ranking[team1[n]]-ranking[team2[n]]));
theta_corr[n] = exp(rho);
}
}
model{
// log-priors for team-specific abilities
for (h in 1:(nteams)){
if (prior_dist_num == 1 ){
att_raw[,h]~multi_normal(mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_normal(mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
else if (prior_dist_num == 2 ){
att_raw[,h]~multi_student_t(hyper_df, mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_student_t(hyper_df, mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
else if (prior_dist_num == 3 ){
att_raw[,h]~multi_student_t(1, mu_att[,h], diag_matrix(rep_vector(square(sigma_att), ntimes)));
def_raw[,h]~multi_student_t(1, mu_def[,h], diag_matrix(rep_vector(square(sigma_def), ntimes)));
}
}
// log-hyperpriors for sd parameters
if (prior_dist_sd_num == 1 ){
target+=normal_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=normal_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 2){
target+=student_t_lpdf(sigma_att|hyper_sd_df, hyper_sd_location, hyper_sd_scale);
target+=student_t_lpdf(sigma_def|hyper_sd_df, hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 3){
target+=cauchy_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=cauchy_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
else if (prior_dist_sd_num == 4){
target+=double_exponential_lpdf(sigma_att|hyper_sd_location, hyper_sd_scale);
target+=double_exponential_lpdf(sigma_def|hyper_sd_location, hyper_sd_scale);
}
// log-priors fixed effects
target+=normal_lpdf(home|0,5);
target+=normal_lpdf(rho|0,1);
target+=normal_lpdf(gamma|0,1);
// likelihood
for (n in 1:N){
//target+=bipois_lpmf(y[n,]| theta_home[n],
// theta_away[n], theta_corr[n]);
target+=poisson_lpmf(y[n,1]|theta_home[n]+theta_corr[n]);
target+=poisson_lpmf(y[n,2]|theta_away[n]+theta_corr[n]);
}
}
generated quantities{
int y_rep[N,2];
vector[N] log_lik;
int diff_y_rep[N];
int y_prev[N_prev,2];
vector[N_prev] theta_home_prev; // exponentiated linear pred.
vector[N_prev] theta_away_prev;
vector[N_prev] theta_corr_prev;
//in-sample replications
for (n in 1:N){
y_rep[n,1] = poisson_rng(theta_home[n]+theta_corr[n]);
y_rep[n,2] = poisson_rng(theta_away[n]+theta_corr[n]);
diff_y_rep[n] = y_rep[n,1] - y_rep[n,2];
log_lik[n] = poisson_lpmf(y[n,1]|theta_home[n]+theta_corr[n]) +
poisson_lpmf(y[n,2]|theta_away[n]+theta_corr[n]);
//bipois_lpmf(y[n,]| theta_home[n],
// theta_away[n], theta_corr[n]);
}
for (n in 1:N_prev){
theta_home_prev[n] = exp(home+att[instants_prev[n], team1_prev[n]]+
def[instants_prev[n], team2_prev[n]]+
(gamma/2)*(ranking[team1_prev[n]]-ranking[team2_prev[n]]));
theta_away_prev[n] = exp(att[instants_prev[n], team2_prev[n]]+
def[instants_prev[n], team1_prev[n]]-
(gamma/2)*(ranking[team1_prev[n]]-ranking[team2_prev[n]]));
theta_corr_prev[n] = exp(rho);
y_prev[n,1] = poisson_rng(theta_home_prev[n]+theta_corr_prev[n]);
y_prev[n,2] = poisson_rng(theta_away_prev[n]+theta_corr_prev[n]);
}
}"
biv_pois_fit<-"
functions{
real bipois_lpmf(int[] r , real mu1,real mu2,real mu3) {
real ss;
real log_s;
real mus;
int miny;
miny = min(r[1], r[2]);
ss = poisson_lpmf(r[1] | mu1) + poisson_lpmf(r[2] | mu2) -
exp(mu3);
if(miny > 0) {
mus = -mu1-mu2+mu3;
log_s = ss;
for(k in 1:miny) {
log_s = log_s + log(r[1] - k + 1) + mus
+ log(r[2] - k + 1)
- log(k);
ss = log_sum_exp(ss, log_s);
}
}
return(ss);
}
}
data{
int N; // number of games
int y[N,2];
int nteams;
int team1[N];
int team2[N];
real ranking[nteams];