-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHESIS_GP_MODELS.Rmd
932 lines (722 loc) · 29.6 KB
/
THESIS_GP_MODELS.Rmd
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
---
title: "Untitled"
author: "francesco"
date: "2024-01-23"
output: html_document
---
---
title: "GP_CODE.Rmd"
author: "francesco"
date: "2024-01-23"
output: html_document
---
Import libraries
```{r}
library(devtools)
library(footBayes)
library(bayesplot)
library(loo)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(rstan)
library(ggplot2)
library(ggrepel)
library(plotly)
library(cluster)
library(knitr)
library(kableExtra)
library(dagitty)
library(ggdist)
library(tidyr)
set.seed(1)
```
Create dataframe for multiple seasons (REMOTE data from footBayes library)
```{r}
data("italy")
italy <- as.data.frame(italy)
#italy_19_to_21 <- subset(italy[, c(2, 3, 4, 6, 7)], Season %in% c("2019", "2020", "2021"))
#colnames(italy_19_to_21) <- c("season", "home", "away", "homegoals", "awaygoals")
italy_13_to_21 <- subset(italy[, c(2, 3, 4, 6, 7)], Season %in% c("2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"))
colnames(italy_13_to_21) <- c("season", "home", "away", "homegoals", "awaygoals")
head(italy_13_to_21)
```
"data_definition" function -\> returns the data needed for the STAN model
```{r}
data_definition <- function(data,
predict){
#DATA CHECK
if (!is.matrix(data) & !is.data.frame(data)){
stop("Data are not stored in matrix/data frame
structure.")
}
if (dim(data)[2]<5){
stop("Data dimensions are wrong! Supply a matrix/data frame containing the following mandatory column items:
season, home team, away team, home goals, away goals.")
}
if ( !is.numeric(data$homegoals) |!is.numeric(data$awaygoals)){
stop("Goals are not numeric!")
}
if (dim(data)[2]>5){
warning("Dataset too large! The function will evaluate the first five columns as follows: season, home team, away team, home goals, away goals")
}
colnames(data) <- c("season", "home", "away", "homegoals", "awaygoals")
#PREDICT CHECK
if (predict == 0){
predict = 0
ngames = dim(data)[1]
nfit = ngames
npred = predict
}else if(is.numeric(predict)){
ngames = dim(data)[1]
nfit = ngames-predict
npred = predict
}
#DATA FOR STAN MODEL
nteams = length(unique(data$home)) #number of teams
teams = unique(data$home) #unique team names
home = match(data$home, teams) #team home (indexes of the whole ngames)
away = match(data$away, teams) #team away (indexes of the whole ngames)
team1 = home[1:nfit] #idx home extraction for the first nfirst matches (total - predicted)
team2 = away[1:nfit] #idx away extraction for the first nfirst matches (total - predicted)
score1 = data$homegoals[1:nfit] #score home team
score2 = data$awaygoals[1:nfit] #score away team
team1pred = home[(nfit+1):(ngames)] #home prev || ngames = nfit + nprev
team2pred = away[(nfit+1):(ngames)] #away prev || ngames = nfit + nprev
diff_score = score1 - score2 #diff for Skellam
#data for GP
seasons <- length(unique(data$season)) #number of seasons (single number)
time <- seq(1, seasons) #tracking time
timetrack <- rep(1:seasons, each = 380) #indicates which season each match belongs to
data_stan = list(
nteams = nteams,
teams = teams,
home = home,
away = away,
team1 = team1,
team2 = team2,
team1pred = team1pred,
team2pred = team2pred,
score1 = score1,
score2 = score2,
diff_score = (score1-score2),
ngames = ngames,
nfit = nfit,
npred = npred,
seasons = seasons,
time = time,
timetrack = timetrack
)
return(data_stan)
}
```
"data_definition_PRED" function -\> returns the data needed for the STAN model
```{r}
data_definition_PRED <- function(data,
predict){
#DATA CHECK
if (!is.matrix(data) & !is.data.frame(data)){
stop("Data are not stored in matrix/data frame
structure.")
}
if (dim(data)[2]<5){
stop("Data dimensions are wrong! Supply a matrix/data frame containing the following mandatory column items:
season, home team, away team, home goals, away goals.")
}
if ( !is.numeric(data$homegoals) |!is.numeric(data$awaygoals)){
stop("Goals are not numeric!")
}
if (dim(data)[2]>5){
warning("Dataset too large! The function will evaluate the first five columns as follows: season, home team, away team, home goals, away goals")
}
colnames(data) <- c("season", "home", "away", "homegoals", "awaygoals")
#PREDICT CHECK
if (predict == 0){
predict = 0
ngames = dim(data)[1]
nfit = ngames
npred = predict
}else if(is.numeric(predict)){
ngames = dim(data)[1]
nfit = ngames-predict
npred = predict
}
#DATA FOR STAN MODEL
nteams = length(unique(data$home)) #number of teams
teams = unique(data$home) #unique team names
home = match(data$home, teams) #team home (indexes of the whole ngames)
away = match(data$away, teams) #team away (indexes of the whole ngames)
team1 = home[1:nfit] #idx home extraction for the first nfirst matches (total - predicted)
team2 = away[1:nfit] #idx away extraction for the first nfirst matches (total - predicted)
score1 = data$homegoals[1:nfit] #score home team
score2 = data$awaygoals[1:nfit] #score away team
team1pred = home[(nfit+1):(ngames)] #home prev || ngames = nfit + nprev
team2pred = away[(nfit+1):(ngames)] #away prev || ngames = nfit + nprev
diff_score = score1 - score2 #diff for Skellam
#data for GP
seasons <- length(unique(data$season)) + 1 #number of seasons (single number)
time <- seq(1, seasons) #tracking time
timetrack <- rep(1:seasons, each = 380) #indicates which season each match belongs to
data_stan = list(
nteams = nteams,
teams = teams,
home = home,
away = away,
team1 = team1,
team2 = team2,
team1pred = team1pred,
team2pred = team2pred,
score1 = score1,
score2 = score2,
diff_score = (score1-score2),
ngames = ngames,
nfit = nfit,
npred = npred,
seasons = seasons,
time = time,
timetrack = timetrack
)
return(data_stan)
}
```
Recall data_definition function providing a dataset and the number of games to predict as argument.
```{r}
up_data = data_definition(italy_13_to_21, 0)
```
GP STAN CODE
```{r}
gp_multinormal <- "
data{
int nteams; // number of teams
int ngames; // number of games
int team1[ngames]; // home team index
int team2[ngames]; // away team index
//int score[ngames,2]; // scores
int score1[ngames]; //score home team
int score2[ngames]; //score away team
int seasons; // (numero di stagioni considerate)
int time[seasons];
int timetrack[ngames]; # in input da R --> timetrack <- rep(1:10, each = 380)
}
parameters{
matrix[seasons, nteams] att_raw; // raw attack ability
matrix[seasons, nteams] def_raw; // raw defense ability
real home;
}
transformed parameters{
matrix[seasons, nteams] att; // attack abilities
matrix[seasons, nteams] def; // defense abilities
cov_matrix[seasons] Sigma_att; // GP attack cov. funct.
cov_matrix[seasons] Sigma_def; // GP defense cov.funct.
//matrix[seasons, nteams] mu_att; // attack hyperparameter
//matrix[seasons, nteams] mu_def; // defense hyperparameter
vector[ngames] theta_home; // exponentiated linear pred.
vector[ngames] theta_away;
// GP
for (i in 1:(seasons)) {
for (j in 1:(seasons)) {
//Sigma_att[i, j] = exp(-(time[i] - time[j])^2) + (i == j ? 0.000001 : 0.0); // check valore 0.1/0.0001
//Sigma_def[i, j] = exp(-(time[i] - time[j])^2) + (i == j ? 0.000001 : 0.0); // check valore 0.1/0.0001
Sigma_att[i, j] = exp(-0.1 * abs(time[i] - time[j])) + (i == j ? 0.01 : 0.0);
Sigma_def[i, j] = exp(-0.1 * abs(time[i] - time[j])) + (i == j ? 0.01 : 0.0);
}
}
// la dicitura att[t] indica operazione sulla t-esima riga | effettuando la media del valore att_raw[t], e sottraendo questo a tutti i valori della t-esima riga (quindi a tutti i valori di att delel 20 squadre nella t-esima stagione), sto introducendo la sum-to-zero per la i-esima stagione.
// Sum-to-zero
att[1]=att_raw[1]-mean(att_raw[1]);
def[1]=def_raw[1]-mean(def_raw[1]);
for (t in 2:seasons){
att[t]=att_raw[t]-mean(att_raw[t]);
def[t]=def_raw[t]-mean(def_raw[t]);
}
for (n in 1:ngames){
theta_home[n] = exp(home+att[timetrack[n], team1[n]]+def[timetrack[n],team2[n]]); //exp(home + att[del team x nella stagione x] + def[del team x nella stagione x]);
theta_away[n] = exp(att[timetrack[n], team2[n]]+def[timetrack[n], team1[n]]); //exp(att[del team x nella stagione x] + def[del team x nella stagione x]);
}
}
model{
// priors for team abilities
for (h in 1:(nteams)){
att_raw[,h]~multi_normal(rep_vector(0, seasons), Sigma_att);
def_raw[,h]~multi_normal(rep_vector(0, seasons), Sigma_def);
}
// priors fixed effects
home ~ normal(0, 5);
// likelihood
score1 ~ poisson(theta_home);
score2 ~ poisson(theta_away);
}
"
```
GP CON MATERN FUNCTION 1° trial
```{r}
gp_matern <- "
data {
int nteams; // number of teams
int ngames; // number of games
int team1[ngames]; // home team index
int team2[ngames]; // away team index
int score1[ngames]; // score home team
int score2[ngames]; // score away team
int seasons; // number of seasons
int time[seasons];
int timetrack[ngames];
}
parameters {
matrix[seasons, nteams] att_raw; // raw attack ability
matrix[seasons, nteams] def_raw; // raw defense ability
real home;
real<lower=0> sigma_att; // Matern 3/2 kernel parameter for attack
real<lower=0> length_scale_att; // Matern 3/2 kernel parameter for attack
real<lower=0> sigma_def; // Matern 3/2 kernel parameter for defense
real<lower=0> length_scale_def; // Matern 3/2 kernel parameter for defense
}
transformed parameters {
matrix[seasons, nteams] att; // attack abilities
matrix[seasons, nteams] def; // defense abilities
cov_matrix[seasons] Sigma_att; // GP attack cov. funct.
cov_matrix[seasons] Sigma_def; // GP defense cov.funct.
vector[ngames] theta_home; // exponentiated linear pred.
vector[ngames] theta_away;
// Nuova matrice di covarianza Matérn 3/2
for (i in 1:seasons) {
for (j in 1:seasons) {
Sigma_att[i, j] = sigma_att^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_att) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_att);
Sigma_def[i, j] = sigma_def^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_def) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_def);
}
}
// Sum-to-zero
att[1] = att_raw[1] - mean(att_raw[1]);
def[1] = def_raw[1] - mean(def_raw[1]);
for (t in 2:seasons) {
att[t] = att_raw[t] - mean(att_raw[t]);
def[t] = def_raw[t] - mean(def_raw[t]);
}
for (n in 1:ngames) {
theta_home[n] = exp(home + att[timetrack[n], team1[n]] + def[timetrack[n], team2[n]]);
theta_away[n] = exp(att[timetrack[n], team2[n]] + def[timetrack[n], team1[n]]);
}
}
model {
// priors for team abilities
for (h in 1:nteams) {
att_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_att);
def_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_def);
}
// priors fixed effects
home ~ normal(0, 5);
sigma_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
length_scale_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
sigma_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
length_scale_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
// likelihood
score1 ~ poisson(theta_home);
score2 ~ poisson(theta_away);
}
"
```
GP CON MATERN FUNCTION (- def)
```{r}
gp_matern_neg <- "
data {
int nteams; // number of teams
int ngames; // number of games
int team1[ngames]; // home team index
int team2[ngames]; // away team index
int score1[ngames]; // score home team
int score2[ngames]; // score away team
int seasons; // number of seasons
int time[seasons];
int timetrack[ngames];
}
parameters {
matrix[seasons, nteams] att_raw; // raw attack ability
matrix[seasons, nteams] def_raw; // raw defense ability
real home;
real<lower=0> sigma_att; // Matern 3/2 kernel parameter for attack
real<lower=0> length_scale_att; // Matern 3/2 kernel parameter for attack
real<lower=0> sigma_def; // Matern 3/2 kernel parameter for defense
real<lower=0> length_scale_def; // Matern 3/2 kernel parameter for defense
}
transformed parameters {
matrix[seasons, nteams] att; // attack abilities
matrix[seasons, nteams] def; // defense abilities
cov_matrix[seasons] Sigma_att; // GP attack cov. funct.
cov_matrix[seasons] Sigma_def; // GP defense cov.funct.
vector[ngames] theta_home; // exponentiated linear pred.
vector[ngames] theta_away;
// Nuova matrice di covarianza Matérn 3/2
for (i in 1:seasons) {
for (j in 1:seasons) {
Sigma_att[i, j] = sigma_att^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_att) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_att);
Sigma_def[i, j] = sigma_def^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_def) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_def);
}
}
// Sum-to-zero
att[1] = att_raw[1] - mean(att_raw[1]);
def[1] = def_raw[1] - mean(def_raw[1]);
for (t in 2:seasons) {
att[t] = att_raw[t] - mean(att_raw[t]);
def[t] = def_raw[t] - mean(def_raw[t]);
}
for (n in 1:ngames) {
theta_home[n] = exp(home + att[timetrack[n], team1[n]] - def[timetrack[n], team2[n]]);
theta_away[n] = exp(att[timetrack[n], team2[n]] - def[timetrack[n], team1[n]]);
}
}
model {
// priors for team abilities
for (h in 1:nteams) {
att_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_att);
def_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_def);
}
// priors fixed effects
home ~ normal(0, 5);
sigma_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
length_scale_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
sigma_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
length_scale_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
// likelihood
score1 ~ poisson(theta_home);
score2 ~ poisson(theta_away);
}
"
```
GP CON PREDICTION
```{r}
gp_matern_neg_PRED <- "
data {
int nteams; // number of teams
int ngames; // number of games
int team1[ngames]; // home team index
int team2[ngames]; // away team index
int score1[ngames]; // score home team
int score2[ngames]; // score away team
int seasons; // number of seasons
int time[seasons];
int timetrack[ngames];
}
parameters {
matrix[seasons, nteams] att_raw; // raw attack ability, aggiungi una stagione
matrix[seasons, nteams] def_raw; // raw defense ability, aggiungi una stagione
real home;
real<lower=0> sigma_att; // Matern 3/2 kernel parameter for attack
real<lower=0> length_scale_att; // Matern 3/2 kernel parameter for attack
real<lower=0> sigma_def; // Matern 3/2 kernel parameter for defense
real<lower=0> length_scale_def; // Matern 3/2 kernel parameter for defense
}
transformed parameters {
matrix[seasons, nteams] att; // attack abilities
matrix[seasons, nteams] def; // defense abilities
cov_matrix[seasons] Sigma_att; // GP attack cov. funct.
cov_matrix[seasons] Sigma_def; // GP defense cov.funct.
vector[ngames] theta_home; // exponentiated linear pred.
vector[ngames] theta_away;
// Nuova matrice di covarianza Matérn 3/2
for (i in 1:seasons + 1) {
for (j in 1:seasons + 1) {
Sigma_att[i, j] = sigma_att^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_att) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_att);
Sigma_def[i, j] = sigma_def^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / length_scale_def) * exp(-sqrt(3) * abs(time[i] - time[j]) / length_scale_def);
}
}
// Sum-to-zero
att[1] = att_raw[1] - mean(att_raw[1]);
def[1] = def_raw[1] - mean(def_raw[1]);
for (t in 2:seasons) {
att[t] = att_raw[t] - mean(att_raw[t]);
def[t] = def_raw[t] - mean(def_raw[t]);
}
for (n in 1:ngames) {
theta_home[n] = exp(home + att[timetrack[n], team1[n]] - def[timetrack[n], team2[n]]);
theta_away[n] = exp(att[timetrack[n], team2[n]] - def[timetrack[n], team1[n]]);
}
}
model {
// priors for team abilities
for (h in 1:nteams) {
att_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_att);
def_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_def);
}
// priors fixed effects
home ~ normal(0, 5);
sigma_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
length_scale_att ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for attack
sigma_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
length_scale_def ~ gamma(2, 0.1); // Prior for Matern 3/2 kernel parameter for defense
// New observations for season 2022
for (h in 1:nteams) {
att_raw[seasons, h] ~ multi_normal(att_raw[seasons, h], Sigma_att);
def_raw[seasons, h] ~ multi_normal(def_raw[seasons, h], Sigma_def);
}
// likelihood
score1 ~ poisson(theta_home);
score2 ~ poisson(theta_away);
}
"
```
GP CON MATERN FUNCTION no prior on scale and sigma
```{r}
gp_matern_2 <- "
data {
int nteams; // number of teams
int ngames; // number of games
int team1[ngames]; // home team index
int team2[ngames]; // away team index
int score1[ngames]; // score home team
int score2[ngames]; // score away team
int seasons; // number of seasons
int time[seasons];
int timetrack[ngames];
}
parameters {
matrix[seasons, nteams] att_raw; // raw attack ability
matrix[seasons, nteams] def_raw; // raw defense ability
real home;
//real<lower=0> sig_att; // Matern 3/2 kernel parameter for attack
//real<lower=0> length_scale_att; // Matern 3/2 kernel parameter for attack
//real<lower=0> sig_def; // Matern 3/2 kernel parameter for defense
//real<lower=0> length_scale_def; // Matern 3/2 kernel parameter for defense
}
transformed parameters {
matrix[seasons, nteams] att; // attack abilities
matrix[seasons, nteams] def; // defense abilities
cov_matrix[seasons] Sigma_att; // GP attack cov. funct.
cov_matrix[seasons] Sigma_def; // GP defense cov.funct.
vector[ngames] theta_home; // exponentiated linear pred.
vector[ngames] theta_away;
// Nuova matrice di covarianza Matérn 3/2
for (i in 1:seasons) {
for (j in 1:seasons) {
Sigma_att[i, j] = 20^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / 20) * exp(-sqrt(3) * abs(time[i] - time[j]) / 20);
Sigma_def[i, j] = 20^2 * (1 + sqrt(3) * abs(time[i] - time[j]) / 20) * exp(-sqrt(3) * abs(time[i] - time[j]) / 20);
}
}
// Sum-to-zero
att[1] = att_raw[1] - mean(att_raw[1]);
def[1] = def_raw[1] - mean(def_raw[1]);
for (t in 2:seasons) {
att[t] = att_raw[t] - mean(att_raw[t]);
def[t] = def_raw[t] - mean(def_raw[t]);
}
for (n in 1:ngames) {
theta_home[n] = exp(home + att[timetrack[n], team1[n]] + def[timetrack[n], team2[n]]);
theta_away[n] = exp(att[timetrack[n], team2[n]] + def[timetrack[n], team1[n]]);
}
}
model {
// priors for team abilities
for (h in 1:nteams) {
att_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_att);
def_raw[, h] ~ multi_normal(rep_vector(0, seasons), Sigma_def);
}
// priors fixed effects
home ~ normal(0, 5);
//sig_att ~ gamma(2, 0.5); // Prior for Matern 3/2 kernel parameter for attack
//length_scale_att ~ gamma(2, 1); // Prior for Matern 3/2 kernel parameter for attack
//sig_def ~ gamma(2, 0.5); // Prior for Matern 3/2 kernel parameter for defense
//length_scale_def ~ gamma(2, 1); // Prior for Matern 3/2 kernel parameter for defense
// likelihood
score1 ~ poisson(theta_home);
score2 ~ poisson(theta_away);
}
"
```
```{r}
writeLines(gp_matern_neg, "gp_matern_neg.stan")
first_model = stan(file ="gp_matern_neg.stan", data = up_data, verbose = FALSE)
```
ù
```{r}
summary(model_param)
```
Model parameters extraction
```{r}
model_param = rstan::extract(first_model)
```
##############################
```{r}
num_plots <- 34
# Ciclo per generare e salvare tutti i plot
for (i in 1:num_plots) {
GP_Data <- model_param$att[,,i]
team_name <- up_data$teams[i]
# Plot delle linee grigie
matplot(t(GP_Data), type = "l", col = "gray", lty = 1, xlab = "Season", ylab = "Values", main = paste("Latent Attack values |", team_name), axes = FALSE)
# Calcola e aggiungi una linea media in rosso
media_colonne <- colMeans(GP_Data)
lines(media_colonne, col = "turquoise", lwd = 2)
# Aggiungi i punti della media come pallini neri
points(media_colonne, col = "black", pch = 16)
axis(1, at = 1:9, labels = c("2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"))
axis(2)
# Aggiungi griglia
grid()
}
```
```{r}
num_plots <- 34
# Ciclo per generare e salvare tutti i plot
for (i in 1:num_plots) {
GP_Data <- model_param$def[,,i]
team_name <- up_data$teams[i]
# Plot delle linee grigie
matplot(t(GP_Data), type = "l", col = "gray", lty = 1, xlab = "Season", ylab = "Values", main = paste("Latent Defense values |", team_name), axes = FALSE)
# Calcola e aggiungi una linea media in rosso
media_colonne <- colMeans(GP_Data)
lines(media_colonne, col = "turquoise", lwd = 2)
# Aggiungi i punti della media come pallini neri
points(media_colonne, col = "black", pch = 16)
axis(1, at = 1:9, labels = c("2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"))
axis(2) # Aggiungi l'asse Y con i valori predefiniti
# Aggiungi griglia
grid()
}
```
HO GLI INDICI UNIVOCI PER OGNI SQUADRA DA 1 A 34
```{r}
team_indices <- match(up_data$teams, unique(up_data$teams))
up_data$teams[1]
for (i in seq_along(up_data$teams)) {
cat("squadra:", up_data$teams[i], " - ", team_indices[i], "\n")
}
```
```{r}
# media e deviazione standard (calcolate sulle 4000 iterazioni) per ogni squadra (att e def) in ogni stagione.
media_per_stagione_att <- apply(model_param$att, MARGIN = c(2, 3), mean)
sd_per_stagione_att <- apply(model_param$att, MARGIN = c(2, 3), sd)
media_per_stagione_def <- apply(model_param$def, MARGIN = c(2, 3), mean)
sd_per_stagione_def <- apply(model_param$def, MARGIN = c(2, 3), sd)
```
```{r}
# Creazione di un dataframe per le capacità di attacco senza la colonna "Stagione"
df_att <- setNames(
data.frame(
media_per_stagione_att,
check.names = FALSE
),
paste0(team_indices, "-", up_data$teams)
)
# Creazione di un dataframe per le capacità di difesa senza la colonna "Stagione"
df_def <- setNames(
data.frame(
media_per_stagione_def,
check.names = FALSE
),
paste0(team_indices, "-", up_data$teams)
)
# Creazione di un dataframe per le deviazioni standard di attacco senza la colonna "Stagione"
df_sd_att <- setNames(
data.frame(
sd_per_stagione_att,
check.names = FALSE
),
paste0(team_indices, "-", up_data$teams)
)
# Creazione di un dataframe per le deviazioni standard di difesa senza la colonna "Stagione"
df_sd_def <- setNames(
data.frame(
sd_per_stagione_def,
check.names = FALSE
),
paste0(team_indices, "-", up_data$teams)
)
# Visualizza i primi 6 record del dataframe
df_att
df_def
df_sd_att
df_sd_def
```
FILTRARE TUTTE LE STAGIONI PER SQUADRE PRESENTI SOLO NELL'ULTIMA
```{r}
unique_ss <- unique(subset(italy_13_to_21$home, italy_13_to_21$season == "2021")) #TO CHANGE DEPENDING ON SEASON
indici_20_teams_ss <- match(unique_ss, up_data$teams)
indici_20_teams_ss
```
DF FINALI SOLO CON SQUADRE DELLA STAGIONE D'INTERESSE MA CON TUTTI I VALORI DELLE STAGIONI CONSIDERATE
```{r}
df_att_ss <- df_att[, team_indices %in% indici_20_teams_ss, drop = FALSE]
df_sd_att_ss <- df_sd_att[, team_indices %in% indici_20_teams_ss, drop = FALSE]
df_def_ss <- df_def[, team_indices %in% indici_20_teams_ss, drop = FALSE]
df_sd_def_ss <- df_sd_def[, team_indices %in% indici_20_teams_ss, drop = FALSE]
```
PLOT FINALE SOLO CON SINGOLA SQUADRA
DF FINALI SOLO CON SQUADRE STAGIONE D'INTERESSE E CON VALORI DELLA STAGIONE D'INTERESSE
```{r}
df_att_ss_final <- df_att_ss[nrow(df_att_ss), ]
df_sd_att_ss_final <- df_sd_att_ss[nrow(df_sd_att_ss), ]
df_def_ss_final <- df_def_ss[nrow(df_def_ss), ]
df_sd_def_ss_final <- df_sd_def_ss[nrow(df_sd_def_ss), ]
df_att_ss_final
df_sd_att_ss_final
df_def_ss_final
df_sd_def_ss_final
```
```{r}
library(ggplot2)
# I tuoi dati
valori_attacco <- unlist(df_att_ss_final)
sd_attacco <- unlist(df_sd_att_ss_final)
valori_difesa <- unlist(df_def_ss_final)
sd_difesa <- unlist(df_sd_def_ss_final)
nomi_squadre <- colnames(df_att_ss_final)
nomi_squadre <- gsub("\\d+-", "", nomi_squadre)
data <- data.frame(
Attack = valori_attacco,
Defense = valori_difesa,
SD_Attack = sd_attacco,
SD_Defense = sd_difesa,
Team = nomi_squadre
)
print(data)
```
```{r}
attach(data)
k <- 4
# Perform K-means clustering
set.seed(123) # Set seed for reproducibility
data$cluster <- kmeans(data[, c("Defense", "Attack")], centers = k)$cluster
# Create a ggplot scatter plot
p <- ggplot(data, aes(x = Defense, y = Attack, label = Team, color = factor(cluster))) +
labs(x = "Defense", y = "Attack", title = "Attack & Defense") +
# Add arrows for standard deviations
geom_errorbar(aes(x = Defense, ymin = Attack - SD_Attack, ymax = Attack + SD_Attack), width = 0, linetype = "dashed", color = "darkgrey", alpha = 0.8) +
geom_errorbarh(aes(y = Attack, xmin = Defense - SD_Defense, xmax = Defense + SD_Defense), height = 0, linetype = "dashed", color = "darkgrey", alpha = 0.8) +
# Add labels with repel to avoid overlapping
geom_text_repel(aes(color = factor(cluster)), box.padding = 0.68, size = 3, max.overlaps = Inf, color = "black") +
geom_point(size = 3.5, alpha = 0.7) +
# Set theme with a white background
theme_minimal() +
theme(
panel.grid = element_blank(),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white")
) +
# Add cluster colors and change the legend title
scale_color_manual(name = "Cluster", values = c("red", "blue", "green", "orange")) +
# Add dashed line at x = 0.5 and y = 0.5
geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
# Set X and Y axis limits
xlim(-0.5, 0.8) +
ylim(-0.5, 0.8)
# Visualizza il grafico
print(p)
ggsave("C:/Users/kecco/Desktop/TESI_magistrale/images_thesis/GP_final_plot_2021_1.png", width = 8, height = 6, dpi = 300)
```
```{r}
# Trova le righe con valori mancanti nelle colonne utilizzate per geom_errorbarh
righe_mancanti_errorbarh <- data[is.na(data$Defense) | is.na(data$SD_Defense), ]
# Stampa le righe con valori mancanti
print(righe_mancanti_errorbarh)
```
DISTANCES
```{r}
# Euclidean: radice quadrata della somma dei quadrati delle differenze tra le coordinate dei punti
euclidean_distance <- sqrt(valori_attacco^2 + valori_difesa^2)
# Creazione del dataframe
distance_euc <- data.frame(
Team = nomi_squadre,
Euclidean = euclidean_distance
)
distance_euc <- arrange(distance_euc, desc(Euclidean))
```