-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
1583 lines (1281 loc) · 69.3 KB
/
utils.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
## Auxiliary functions for my analysis
library(dplyr)
options(dplyr.summarise.inform = FALSE)
library(tidyr)
library(tibble)
library(ggplot2)
library(spdep)
library(gridExtra)
library(ggpubr)
library(stringr)
library(lubridate)
library(progress)
library(readxl)
library(mipfp)
#### Preprocessing of mortality dataset (code taken from https://github.com/RiccardoScimone/Mortality-densities-italy-analysis) ----
# Function to aggregate mortality data into age classes
aggregate_classes = function(data,keyname,firstvec,lastvec) #### BE VERY CAREFUL THAT THE VECTORS ARE COHERENT!
{
n = length(firstvec)
newdata = data
for (i in 1:n){
first = firstvec[i]
last = lastvec[i]
classes = as.character(first:last)
newdata$CL_ETA[newdata$CL_ETA %in% classes] = paste0(first,"_",last)
newdata = newdata |> group_by(get(keyname), partial_date_death, CL_ETA) |>
summarise_at(c(paste0("M_",c(11:22)),paste0("F_",c(11:22)),paste0("T_",c(11:22))), sum) |> ungroup()
names(newdata)[1] = keyname
}
return(newdata)
}
# Function to preprocess the raw ISTAT mortality data
preprocess_mortality <- function(deaths, comuni_ISTAT){
# Removing columms of 2203 with no data
deaths = deaths |> dplyr::select(-c(M_23, F_23, T_23))
# Filtering only Lombardia data
deaths = deaths |> filter(NOME_REGIONE == "Lombardia")
# Transform some columns
deaths$GE = as.character(deaths$GE)
months = str_sub(deaths$GE,1,-3)
days = str_sub(deaths$GE,-2,-1)
deaths$partial_date_death = as.Date(paste0("2020-",months,"-",days))
deaths$CL_ETA = as.character(deaths$CL_ETA)
deaths$COD_PROVCOM = as.character(deaths$COD_PROVCOM)
deaths$T_20 = as.numeric(deaths$T_20)
deaths$F_20 = as.numeric(deaths$F_20)
deaths$M_20 = as.numeric(deaths$M_20)
deaths = deaths |> mutate_at(vars(-partial_date_death), ~replace(., is.na(.), 0))
deaths = deaths |> dplyr::select(-GE)
#### Note: All dates are specified at 2020 for simplicity, but the actual year is specified in each column.
#### Aggragation by age (See notation in the pdf from ISTAT) ######
firstvec = c(0,11,15)
lastvec = c(10,14,21)
#### So we are aggregating 0-49 years, 50-69 years, 70+ years #####
#### Aggregate on age basis the Municipality data and carry useful information. This will require some time ############
municipalities_codes = sort(unique(deaths$COD_PROVCOM))
municipalities_data = deaths |> group_by(COD_PROVCOM,CL_ETA)
municipalities_georef = comuni_ISTAT |> arrange(`Codice Comune formato numerico`) |> dplyr::select(16, 12, 11, 7) |>
distinct(`Codice Comune formato numerico`,.keep_all = TRUE)
colnames(municipalities_georef) <- c("COD_PROVCOM", "NOME_PROVINCIA", "NOME_REGIONE", "NOME_COMUNE")
deaths = aggregate_classes(municipalities_data,keyname = "COD_PROVCOM",firstvec,lastvec)
deaths = deaths |> mutate(COD_PROVCOM = as.numeric(COD_PROVCOM)) |>
left_join(municipalities_georef, by = c("COD_PROVCOM" = "COD_PROVCOM"))
# Remove one municipality created in 2023
deaths <- deaths |> filter(COD_PROVCOM != 12144)
return(deaths)
}
# Function to convert the population age data into age classes
factorization = function (age)
{
if(age < 50)
return("0_10")
if(age < 70)
return("11_14")
return("15_21")
}
age_to_fact = Vectorize(factorization)
# Function to preprocess the population data
preprocess_population_data <- function(pop, comuni_ISTAT){
### retrieve resident by age in each municipality. Some data field is in italian with accents and needs their names changed
pop <- pop |> dplyr::select(1,3,11,19)
colnames(pop)[2] <- "Eta"
pop <- pop |> dplyr::filter(Eta != 999) |> dplyr::mutate(istat = Codice.comune, num_residenti = Totale.Maschi + Totale.Femmine) |>
dplyr::select(istat,num_residenti, Eta,Totale.Maschi, Totale.Femmine)
### Now we perform aggregation, see factorization function in Utility_functions, which is written according to our aggregation
pop <- pop |> dplyr::mutate(CL_ETA = age_to_fact(Eta)) |> dplyr::select(-Eta)
pop <- pop |> dplyr::group_by(istat, CL_ETA) |> dplyr::summarise_at(c("Totale.Femmine", "Totale.Maschi", "num_residenti"),sum)
pop$istat = as.character(pop$istat)
# I now want to filter the municipalities in Lombardia
# Select the codes of all the Lombardia municipalities from ISTAT general municipalities info
Lombardia_codes <- comuni_ISTAT |> dplyr::filter(`Denominazione Regione` == "Lombardia") |> pull(`Codice Comune formato numerico`)
pop <- pop |> dplyr::filter(istat %in% Lombardia_codes)
return(pop)
}
#### Analysis of Lombardia area ----
# Function to clean OD data of Regione Lombardia
OD_Lombardia_cleaned <- function(OD){
# Aggregating zones divided into subzones
OD <- OD %>%
mutate(
ZONA_ORIG = str_replace(ZONA_ORIG, " [0-9]+", ""),
ZONA_DEST = str_replace(ZONA_DEST, " [0-9]+", "")
)
# Drop fascia oraria and sum by groups
OD <- OD |> dplyr::select(-(FASCIA_ORARIA)) |> group_by(PROV_ORIG, ZONA_ORIG, PROV_DEST, ZONA_DEST) |>
summarise(across(where(is.numeric), sum)) |> ungroup()
# Selecting only provinces in Lombardia
Lombardia_provinces <- c("BG", "BS", "CO", "CR", "LC", "LO", "MB", "MI", "MN", "SO", "VA", "PV")
OD <- OD |> dplyr::filter(PROV_ORIG %in% Lombardia_provinces & PROV_DEST %in% Lombardia_provinces)
## FIXING SPELLING
# REM: Municipalities separated by "-" represent a unique comune, while if they are separated by " - " they represent
# a group of distant ISTAT comunis
A <- c("ALME'", "BREMBILLA", "CAPRIATE SAN GERVASO", "COSTA DI SERINA", "GARDONE VALTROMPIA",
"LONATO", "PUEGNAGO SUL GARDA", "ROE' VOLCIANO", "SALO'", "TEMU'", "TOSCOLANO MADERNO",
"TREMOSINE", "VALLIO", "CAGNO", "CANTU'", "CAVALLASCA", "FENEGRO'",
"DREZZO", "GIRONICO", "LENNO", "MEZZEGRA", "OLTRONA CON SAN MAMETTE", "OSSUCCIO", "PARH",
"PELLIO INTELVI", "SOLBIATE COMASCO", "CORTENOVA", "GRASSOBIO", "SANT'OMOBONO IMAGNA - VALSECCA",
"VILLA D'ALME'", "COMEZZANO - CIZZAGO", "RODENGO - SAIANO", "CASASCO D'INTELVI - CASTIGLIONE D'INTELVI - CERANO INTELVI - SAN FEDELE INTELVI",
"LANZO D'INTELVI - RAMPONIO VERNA", "TREMEZZO", "UGGIATE - TREVANO", "CA' D'ANDREA",
"DRIZZONA", "GABBIONETA BINANUOVA", "GADESCO PIEVE DELMONA", "GERRE DE'CAPRIOLI", "SAN GIOVANNI INCROCE",
"BARZANO'", "PEREGO", "ROVAGNATE", "SANTA MARIA HOE'", "VERDERIO INFERIORE", "VERDERIO SUPERIORE", "VIGANO'",
"BOVISIO MASCIAGO", "MUGGIO'", "CASSINA DE PECCHI", "VERMEZZO", "ZELO SURRIGONE", "BIGARELLO",
"BORGOFORTE", "BORGOFRANCO SUL PO", "CARBONARA DI PO", "FELONICA", "PIEVE DI CORIANO", "REVERE",
"SAN GIORGIO DI MANTOVA", "SERMIDE", "VILLA POMA", "VIRGILIO", "BASCAPE'", "CORNALE",
"CORTEOLONA", "GAMBOLO'", "MORNICO LOSANNA", "RUINO", "TRAVACO' SICCOMARIO", "ZERBOLO'",
"BRISSAGO - VALTRAVAGLIA", "CADEGLIANO - VICONAGO", "CADREZZATE - OSMATE", "COCQUIO - TREVISAGO",
"CUGLIATE - FABIASCO", "GAZZADA - SCHIANNO", "LAVENO MOMBELLO", "TRAVEDONA - MONATE", "VIGGIU'",
"CAMAIRAGO", "CAVACURTA", "SAN MARTINO IN SCORTE BRUGNATELLA", "TERRANUOVA DEI PASSERINI", "BASTIDA DE' DOSSI",
"MACCAGNO - PINO SULLA SPONDA DEL LAGO MAGGIORE - TRONZANO LAGO MAGGIORE - VEDDASCA", "MONTESCANO - MONTU' BECCARIA - ZENEVREDO",
"BASTIDA DE' DOSSI - CASEI GEROLA", "CASALE CREMASCO - VIDOLASCO - CASTEL GABBIANO", "INTROZZO - PAGNONA - TREMENICO",
"GEROSA - SAN GIOVANNI BIANCO", "BIENNO - PRESTINE", "CIVENNA - MAGREGLIO", "BELLAGIO", "PIADENA - VOLTIDO",
"CASARGO - MARGNO - VENDROGNO", "BELLANO", "SUEGLIO - VESTRENO", "CANEVINO - MONTECALVO VERSIGGIA - ROCCA DE' GIORGI - VOLPARA",
"COPIANO - GENZONE", "VALVERDE - VARZI", "GORDONA - MENAROLA"
)
B <- c("ALMÈ", "VAL BREMBILLA - SAN GIOVANNI BIANCO", "CAPRIATE SAN GERVASIO", "COSTA SERINA", "GARDONE VAL TROMPIA",
"LONATO DEL GARDA", "PUEGNAGO DEL GARDA", "ROÈ VOLCIANO", "SALÒ", "TEMÙ", "TOSCOLANO-MADERNO",
"TREMOSINE SUL GARDA", "VALLIO TERME", "SOLBIATE CON CAGNO", "CANTÙ", "SAN FERMO DELLA BATTAGLIA", "FENEGRÒ",
"COLVERDE", "COLVERDE", "TREMEZZINA", "TREMEZZINA", "OLTRONA DI SAN MAMETTE", "TREMEZZINA", "COLVERDE",
"ALTA VALLE INTELVI", "SOLBIATE CON CAGNO", "CORTENUOVA - PARLASCO - TACENO", "GRASSOBBIO", "SANT'OMOBONO TERME",
"VILLA D'ALMÈ", "COMEZZANO-CIZZAGO", "RODENGO SAIANO", "CERANO D'INTELVI - CENTRO VALLE INTELVI",
"CERANO D'INTELVI - CENTRO VALLE INTELVI", "TREMEZZINA", "UGGIATE-TREVANO", "TORRE DE' PICENARDI",
"PIADENA DRIZZONA - VOLTIDO", "GABBIONETA-BINANUOVA", "GADESCO-PIEVE DELMONA", "GERRE DE' CAPRIOLI", "SAN GIOVANNI IN CROCE",
"BARZANÒ", "LA VALLETTA BRIANZA", "LA VALLETTA BRIANZA", "SANTA MARIA HOÈ", "VERDERIO", "VERDERIO", "VIGANÒ",
"BOVISIO-MASCIAGO", "MUGGIÒ", "CASSINA DE' PECCHI", "VERMEZZO CON ZELO", "VERMEZZO CON ZELO", "SAN GIORGIO BIGARELLO",
"BORGO VIRGILIO", "BORGOCARBONARA", "BORGOCARBONARA", "SERMIDE E FELONICA", "BORGO MANTOVANO", "BORGO MANTOVANO",
"SAN GIORGIO BIGARELLO", "SERMIDE E FELONICA", "BORGO MANTOVANO", "BORGO VIRGILIO", "BASCAPÈ", "CORNALE E BASTIDA - CASEI GEROLA",
"COPIANO - CORTEOLONA E GENZONE", "GAMBOLÒ", "MORNICO LOSANA", "COLLI VERDI - MONTECALVO VERSIGGIA - ROCCA DE' GIORGI - VOLPARA - VARZI", "TRAVACÒ SICCOMARIO", "ZERBOLÒ",
"BRISSAGO-VALTRAVAGLIA", "CADEGLIANO-VICONAGO", "CADREZZATE CON OSMATE", "COCQUIO-TREVISAGO",
"CUGLIATE-FABIASCO", "GAZZADA SCHIANNO", "LAVENO-MOMBELLO", "TRAVEDONA-MONATE", "VIGGIÙ",
"CASTELGERUNDO", "CASTELGERUNDO", "SAN MARTINO IN STRADA", "TERRANOVA DEI PASSERINI", "CORNALE E BASTIDA - CASEI GEROLA",
"MACCAGNO CON PINO E VEDDASCA - TRONZANO LAGO MAGGIORE", "MONTESCANO - MONTÙ BECCARIA - ZENEVREDO",
"CORNALE E BASTIDA - CASEI GEROLA", "CASALE CREMASCO-VIDOLASCO - CASTEL GABBIANO", "SUEGLIO - VALVARRONE - PAGNONA",
"VAL BREMBILLA - SAN GIOVANNI BIANCO", "BIENNO", "BELLAGIO - MAGREGLIO", "BELLAGIO - MAGREGLIO", "PIADENA DRIZZONA - VOLTIDO",
"CASARGO - MARGNO - BELLANO", "CASARGO - MARGNO - BELLANO", "SUEGLIO - VALVARRONE - PAGNONA", "COLLI VERDI - MONTECALVO VERSIGGIA - ROCCA DE' GIORGI - VOLPARA - VARZI",
"COPIANO - CORTEOLONA E GENZONE", "COLLI VERDI - MONTECALVO VERSIGGIA - ROCCA DE' GIORGI - VOLPARA - VARZI", "GORDONA"
)
# Substitute denominations in vector A in OD with denominations in vector B
OD <- OD %>%
mutate(
ZONA_ORIG = case_when(
ZONA_ORIG %in% A ~ B[match(ZONA_ORIG, A)],
TRUE ~ ZONA_ORIG
),
ZONA_DEST = case_when(
ZONA_DEST %in% A ~ B[match(ZONA_DEST, A)],
TRUE ~ ZONA_DEST
)
)
# Aggregate
OD <- OD |> group_by(PROV_ORIG, ZONA_ORIG, PROV_DEST, ZONA_DEST) |>
summarise(across(where(is.numeric), sum)) |> ungroup()
# Remove self-loops
OD <- OD |> dplyr::filter(!(ZONA_ORIG == ZONA_DEST))
# Compute the colums summarizing the overall number of movements for each OD couple
OD <- OD |> mutate(TOT = rowSums(across(where(is.numeric))))
# Compute the colums summarizing the railway number of movements for each OD couple
OD <- OD |> mutate(FERRO = rowSums(select(cur_data(), ends_with("FERRO"))))
return(OD)
}
# Function to generate the dataset of correspondances between OD areas and ISTAT municipalities in Lombardia
generate_matches_OD_municipalities <- function(OD, ISTAT){
# Select only ISTAT municipalities in Lombardia
ISTAT <- ISTAT |> filter(`Codice Regione` == "03")
# Prepare dataset of correspondances
# NOTE: I need to select the ISTAT dataset by column indexes because of their naming which causes problems
Correspondances <- ISTAT[,c(5,7)] |>
rename(ISTAT_code = 1, Comune_name = 2) |>
add_column(OD_name = NA)
# FIRST CASE: if the OD matrix contains an OD_area which matches exactly one municipality name, then I add the match
Correspondances <- Correspondances %>%
mutate(
OD_name = ifelse(tolower(Comune_name) %in% tolower(OD$ZONA_ORIG), toupper(Comune_name), OD_name)
)
# SECOND CASE: when the OD areas are aggregation of municipalities, I separe them and look for the match with an ISTAT municipality
# This was needed to check if there were unmached municipalities
unmatched_comunis <- NULL
# The zones resulting from agregated municipalities can be identified looking for "-" in the name of the zone
aggr_zones <- unique(OD$ZONA_ORIG)[grep("-", unique(OD$ZONA_ORIG))]
for (act in aggr_zones){
# I separate the municipalities which constitute the aggregated zone
municipalities <- unlist(str_split(act, " - "))
for (mun in municipalities){
# If I have a match with a ISTAT municipality, I add it in the dataset
if (tolower(mun) %in% tolower(Correspondances$Comune_name))
Correspondances[tolower(Correspondances$Comune_name) == tolower(mun),"OD_name"] <- act
# If not, I add it to the unmatched comunis
else
unmatched_comunis <- c(unmatched_comunis, tolower(mun))
}
}
# Throw an error if I have an unmatched OD name
if (!is.null(unmatched_comunis))
stop("There are unmatched OD zones")
# Throw an error if I have an unmatched ISTAT municiplaity
if (any(is.na(Correspondances$OD_name)))
stop("There are unmatched municipalities")
return(Correspondances)
}
#### Cleaning and exploratory analysis of ISTAT death data ----
# Function to get mortality data aggregated at Lombardia level and to generate matches between OD areas and
# municipalities, applying an aggregation for computation of mortality divided by population
deaths_data_cleaned_Lombardia <- function(deaths, pop, matches, mortalityWindows){
# Select only tot of deaths in the years of 2019, 2020, 2021
deaths <- deaths |>
select(all_of(c("COD_PROVCOM", "partial_date_death", "CL_ETA", "T_19", "T_20", "T_21",
"NOME_PROVINCIA", "NOME_REGIONE", "NOME_COMUNE"))) |>
# Select only Lombardia
filter(NOME_REGIONE == "Lombardia") |>
# Now I transform the dataset to have dates dependent on year and only one column counting the deaths for that day
pivot_longer(cols = T_19:T_21, names_to = "Year", values_to = "T_deaths") |>
# Then I apply my numbering in weeks of the column partial_date_death
# weeks of 2020 are 00-52
# weeks of 2019 are -52-00
# weeks of 2021 are 52-104
mutate(partial_date_death = case_when(
Year == "T_19" ~ -(52-as.numeric(format(as.Date(partial_date_death), "%W"))),
Year == "T_20" ~ as.numeric(format(as.Date(partial_date_death), "%W")),
Year == "T_21" ~ 52 + as.numeric(format(as.Date(partial_date_death), "%W")),
)) |> select(-Year) |>
# Aggregation into weeks
group_by(COD_PROVCOM, partial_date_death, CL_ETA, NOME_PROVINCIA, NOME_REGIONE, NOME_COMUNE) |>
summarise(T_deaths = sum(T_deaths)) |> ungroup() |>
# Join population info
left_join(pop |> select(istat, CL_ETA, num_residenti),
by = c("COD_PROVCOM" = "istat", "CL_ETA")) |>
# Aggregation in the OD zones: substitute in deaths NOME_COMUNE with OD_name
mutate(NOME_COMUNE = matches$OD_name[match(NOME_COMUNE, matches$Comune_name)]) |>
rename(OD_AREA = NOME_COMUNE) |>
# Aggregate
select(-COD_PROVCOM) |>
group_by(CL_ETA, partial_date_death, NOME_PROVINCIA, OD_AREA, NOME_REGIONE) |>
summarise(across(where(is.numeric), sum)) |> ungroup()
## COMPUTATION OF THE MORTALITY DENSITIES
# Add one empty column for every element of the vector mortality Windows
cols <- paste0("md_window", mortalityWindows)
deaths[,cols] <- NA
# Create progress bar
pb <- progress_bar$new(
format = " computing [:bar] :percent eta: :eta",
total = dim(deaths)[1], clear = FALSE, width= 60)
# Aggregation in the sliding window
for (i in 1:dim(deaths)[1]){
r <- deaths[i,]
cl_eta <- r$CL_ETA
area <- r$OD_AREA
w <- as.numeric(r$partial_date_death)
for (mw in mortalityWindows){
# Compute the number of deaths in the sliding window
act <- deaths |> dplyr::filter(CL_ETA == cl_eta & OD_AREA == area & as.numeric(partial_date_death) %in% (w-mw+1):w) |>
summarise(across(where(is.numeric), sum)) |> dplyr::select(T_deaths)
# Compute mortality density as deaths / population
deaths[i, paste0("md_window", mw)] <- act / r$num_residenti
}
pb$tick()
}
# Convert from list to numeric
deaths <- deaths |>
mutate(across(starts_with("md_window"), as.numeric))
return(deaths)
}
# Function to generate geographical dataset aggregated into OD areas
Lombardia_geodf_OD <- function(OD, matches){
italy_geo <- st_read("Data/ISTAT/General_data/Limiti01012022_g/Com01012022_g")
Lombardia_geo <- italy_geo |> dplyr::filter(COD_REG == 3)
# Join the informations about OD correspondance
matches$ISTAT_code <- as.numeric(matches$ISTAT_code)
Lombardia_geo <- Lombardia_geo |> left_join(matches, by = c("PRO_COM" = "ISTAT_code"))
# Aggregation
Lombardia_geo_agg <- Lombardia_geo %>%
group_by(OD_name) %>%
summarize(geometry = st_union(geometry))
return(Lombardia_geo_agg)
}
# Function to produce exploratory plots
Exploratory_mortality_Lombardia <- function(deaths, geo_df, cl_eta, mortalityWindow, path, mode = "png"){
if (mode == 'pdf')
pdf(paste0(path,".pdf"), onefile = TRUE)
# Get all OD areas
names = sort(unique(deaths$OD_AREA))
# Selecting only the relevant age class
deaths <- deaths |> filter(CL_ETA == cl_eta)
# Name of the column which contain the mortality density of the correct mortalityWindow
col <-paste0("md_window", mortalityWindow)
# Set subtitle
if(cl_eta == "15_21")
subtitle <- paste0("Age class 70+, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "11_14")
subtitle <- paste0("Age class 50-69, aggregation in ", mortalityWindow, " weeks")
weeks <- 1:52
for (w in weeks){
deaths_cur <- deaths |> dplyr::filter(partial_date_death == as.numeric(w)) |> dplyr::select(all_of(c("OD_AREA", col)))
# Create result
ep <- data.frame("OD_AREA" = names)
ep <- ep |> dplyr::left_join(deaths_cur, by = c("OD_AREA" = "OD_AREA"))
ep[is.na(ep[,col]), col] <- 0
geo_df_cur <- geo_df |> left_join(ep, by = c("OD_name" = "OD_AREA"))
# Generate plot with correct dates of start and end of the week
if (w == 1){
title <- paste0("Mortality rate from ", as.Date(paste(2019, 12, 30, sep="-"), "%Y-%m-%d"),
" to ", as.Date(paste(2020, as.numeric(w)+1, 7, sep="-"), "%Y-%W-%u"))
g = ggplot(geo_df_cur) + geom_sf(aes(fill = .data[[col]])) +
theme_bw() + theme(text = element_text(size=10),legend.key.size = unit(1,"line") )+ rremove("axis") + rremove("axis") + rremove("xy.title") + rremove("axis.text") + rremove("ticks") +
ggtitle(title, subtitle = subtitle) +
scale_fill_gradient(low='white', high='red4', name = "Mortality rate")
}
if (w > 1 & w < 52){
title <- paste0("Mortality rate from ", as.Date(paste(2020, w-1, 1, sep="-"), "%Y-%U-%u"),
" to ", as.Date(paste(2020, as.numeric(w)+1, 7, sep="-"), "%Y-%W-%u"))
g = ggplot(geo_df_cur) + geom_sf(aes(fill = .data[[col]])) +
theme_bw() + theme(text = element_text(size=10),legend.key.size = unit(1,"line") )+ rremove("axis") + rremove("axis") + rremove("xy.title") + rremove("axis.text") + rremove("ticks") +
ggtitle(title, subtitle = subtitle) +
scale_fill_gradient(low='white', high='red4', name = "Mortality rate")
}
if (w == 52){
title <- paste0("Mortality rate from ", as.Date(paste(2020, w-1, 1, sep="-"), "%Y-%U-%u"),
" to ", as.Date(paste(2021, 1, 2, sep="-"), "%Y-%m-%d"))
g = ggplot(geo_df_cur) + geom_sf(aes(fill = .data[[col]])) +
theme_bw() + theme(text = element_text(size=10),legend.key.size = unit(1,"line") )+ rremove("axis") + rremove("axis") + rremove("xy.title") + rremove("axis.text") + rremove("ticks") +
ggtitle(title, subtitle = subtitle) +
scale_fill_gradient(low='white', high='red4', name = "Mortality rate")
}
# Save
if (mode == 'pdf')
grid.arrange(g)
if (mode == 'png')
ggsave(paste0(path,"/week",w,".png"),g)
}
if (mode == 'pdf')
dev.off()
}
#### Spatial analysis of the Lombardia area ----
# REM: The function with suffix _RL are designed for the OD dataset of Regione Lombardia
## WEIGHTS DEFINITION
## Defining functions and auxiliary function to perform weights definition
get_OD_RL <- function(Total_net, nodes, col){
OD_week <- Total_net |>
filter(ZONA_ORIG %in% nodes, ZONA_DEST %in% nodes) |>
arrange(ZONA_DEST) |>
select(ZONA_ORIG, ZONA_DEST, all_of(col)) |>
pivot_wider(names_from = ZONA_DEST, values_from = all_of(col), values_fill = 0) |>
arrange(ZONA_ORIG) |>
column_to_rownames(var = "ZONA_ORIG")
return(OD_week)
}
# Function to symmetrize the OD matrix, which should be in MATRIX form
make_OD_symmetrical <- function(OD){
# I copy the OD structure
OD2 <- OD
# Setting diagonal to 0
diag(OD2) <- 0
# Every element is given by (OD[i,j] + OD[j,i])/2
OD2 <- (OD + t(OD))/2
return(OD2)
}
# WEIGHT FUNCTION (Case of OD counts)
get_weights_OD_RL <- function(OD, col, symmetrical = F){
nodes = sort(unique(OD$ZONA_ORIG))
M <- get_OD_RL(OD, nodes, col)
# I set the diagonal equal to 0
diag(M) <- 0
if (symmetrical == T)
M <- make_OD_symmetrical(M)
# Row-standardization
M <- M / apply(M,1,sum)
M <- as.matrix(M)
# Creating the weight structure
W <- mat2listw(M, row.names = row.names(M), style="M")
return(W)
}
## EPIDEMIC QUANTITITIES DEFINITION
# function to get the epidemic feature.
get_epidemic_exmort_RL <- function (deaths, w, col, names, scale = FALSE){
deaths <- deaths |> filter(partial_date_death == w) |> dplyr::select(OD_AREA, all_of(col))
# Order dataset to match weights
deaths <- deaths[order(deaths$OD_AREA),]
# Create result
ep <- data.frame("OD_AREA" = names)
ep <- ep |> dplyr::left_join(deaths, by = c("OD_AREA" = "OD_AREA"))
ep[is.na(ep[,col]), col] <- 0
# Scale if needed
if (scale == TRUE)
X <- ep |> pull(col) |> as.numeric() |> scale()
else
X <- ep |> pull(col) |> as.numeric()
# Return the result (a vector of the epidemic variable in alphabetic order)
return(X)
}
## PLOTTING FUNCTION
my_plot_Moran_RL <- function(geo_df, X, W, w, cl_eta, mob_mode, local.p.value, signif){
geo_df <- geo_df[order(geo_df$OD_name),]
comunis <- geo_df
comunis$scaled = scale(X)
comunis$lagged = lag.listw(W,X, zero.policy = T)
moran.map = cbind(geo_df, local.p.value)
quadrant = vector(mode = "numeric", length = length(local.p.value))
quadrant[comunis$scaled >0 & comunis$lagged>0] <- 4
quadrant[comunis$scaled <0 & comunis$lagged<0] <- 1
quadrant[comunis$scaled <0 & comunis$lagged>0] <- 2
quadrant[comunis$scaled >0 & comunis$lagged<0] <- 3
quadrant[local.p.value>signif] <- 0
brks <- c(0,1,2,3,4)
Moran = c("insignificant","low-low","low-high","high-low", "high-high")
colors <- c("white","blue",rgb(0,0,1,alpha=0.4),rgb(1,0,0,alpha=0.4),"red")
names(colors) = Moran
comunis$Moran = Moran[findInterval(quadrant,brks,all.inside=FALSE)]
# Set subtitle
if(cl_eta == "15_21" & mob_mode == "OD_counts")
subtitle <- paste0("Mobility-based spatial weights, age class 70+, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "11_14" & mob_mode == "OD_counts")
subtitle <- paste0("Mobility-based spatial weights, age class 50-69, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "15_21" & mob_mode == "Cont_weights")
subtitle <- paste0("Contiguity-based spatial weights, age class 70+, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "11_14" & mob_mode == "Cont_weights")
subtitle <- paste0("Contiguity-based spatial weights, age class 50-69, aggregation in ", mortalityWindow, " weeks")
if(w != 52){
g = ggplot(comunis) + geom_sf(aes(fill = Moran)) + # ggrepel::geom_text_repel(aes(label = OD_name, geometry = geometry), size = 1.7, stat = "sf_coordinates",max.overlaps = 20) +
scale_fill_manual(name = "",values = colors)+ theme_bw() + rremove("axis") + rremove("axis") + rremove("xy.title") + rremove("axis.text") + rremove("ticks") +
labs(title =paste("Local Moran Index of week from", as.Date(paste(2020, w, 1, sep="-"), "%Y-%U-%u"), "to", as.Date(paste(2020, as.numeric(w)+1, 7, sep="-"), "%Y-%W-%u")), subtitle = subtitle) +
theme(text = element_text(size=15),legend.key.size = unit(1,"line"), plot.subtitle = element_text(size = 12),
plot.margin = unit(c(0,0,0,0), "cm"))
}
else
{
g = ggplot(comunis) + geom_sf(aes(fill = Moran)) + # ggrepel::geom_text_repel(aes(label = OD_name, geometry = geometry), size = 1.7, stat = "sf_coordinates",max.overlaps = 20) +
scale_fill_manual(name = "",values = colors)+ theme_bw() + rremove("axis") + rremove("axis") + rremove("xy.title") + rremove("axis.text") + rremove("ticks") +
labs(title = paste("Local Moran Index of week from", as.Date(paste(2020, w, 1, sep="-"), "%Y-%U-%u"), "to", as.Date(paste(2020, 12, 31, sep="-"), "%Y-%m-%d")), subtitle = subtitle) +
theme(text = element_text(size=15),legend.key.size = unit(1,"line"), plot.subtitle = element_text(size = 12),
plot.margin = unit(c(0,0,0,0), "cm"))
}
return(g)
}
# I implement a function to perform Global and Local Moran analysis weekly
MORAN_ANALYSIS_RL <- function(Mob, col, deaths, geo_df, mob_mode = "OD_counts", ep_mode = "mortality_pop", path = "Plots/Local_Moran", cl_eta, mortalityWindow,
test_mode = "base", p_adjust = T, signif = 0.05){
####
# mob_mode = "OD_counts" -> uses OD counts
# = "Cont_weights" -> Contiguity weights
# ep_mode = "mortality_pop" -> uses total mortality divided by population (of the station's basin), age 70+
# test_mode = "base" -> use standard Moran tests
# = "perm" -> use Moran test based on permutations
####
# Prepare dataset to store the results
# Global Moran
Global_Moran_df <- NULL
# Local Moran
Local_Moran_df <- NULL
# Select only the relevant age class
deaths <- deaths |> filter(CL_ETA == cl_eta)
# Areas are
names = sort(unique(Mob$ZONA_ORIG))
# Weeks are
weeks <- 1:52
## GET WEIGHTS
# REM: In the case of the OD of Regione Lombardia, weights are not time-varying
if (mob_mode == "Cont_weights"){
# Contiguity weights
geo_df <- geo_df[order(geo_df$OD_name),]
nb = poly2nb(geo_df,row.names = geo_df$OD_name)
W = nb2listw(nb, zero.policy = T)
}
if (mob_mode == "OD_counts"){
# REM: this function performs ROW SCALING
# Weights are given by the OD counts divided by the total of outgoing trips
W <- get_weights_OD_RL(Mob, col, symmetrical = F)
}
# Create list to save epidemic quantities
list_X <- NULL
# Iterating over weeks
for (i in 1:length(weeks)){
w <- weeks[i]
if(ep_mode == "mortality_pop")
# This function gets mortality divided by population of the area in 2020
X <- get_epidemic_exmort_RL(deaths, w = w, col = paste0("md_window", mortalityWindow), names, scale = F)
# Append to list
list_X[[i]] <- X
# MORAN ANALYSIS
if (test_mode == "base"){
# Global Moran
c <- as.data.frame(t(unlist(moran.test(X, W, zero.policy = T))))
Global_Moran_df <- rbind(Global_Moran_df, cbind(rep(w, dim(c)[1]), c))
# Local Moran
c <- as.data.frame(cbind(sort(geo_df$OD_name),localmoran(X, W, zero.policy = T)))
Local_Moran_df <- rbind(Local_Moran_df, cbind(rep(w, dim(c)[1]), c))
}
if (test_mode == "perm"){
# Global Moran
c <- moran.mc(X, W, nsim = 999, zero.policy = T)
temp <- data.frame("Week" = w,
"Moran.I" = c$statistic,
"p-value" = c$p.value)
Global_Moran_df <- rbind(Global_Moran_df, temp)
# Local Moran
c <- as.data.frame(localmoran_perm(X, W, nsim=999, zero.policy=T))
Local_Moran_df <- rbind(Local_Moran_df, cbind(rep(w, dim(c)[1]), names, c))
}
}
# Fixing dataframes format
if (test_mode == "base"){
Global_Moran_df = as.data.frame(Global_Moran_df)
colnames(Global_Moran_df)[1:4] <- c("Week", "Statistic", "p.value", "Moran.I")
colnames(Local_Moran_df)[1:7] <- c("Week", "OD_name", "Local.Moran.I", "E.Ii", "Var.Ii", "z.I1", "p.value")
}
if (test_mode == "perm"){
Global_Moran_df = as.data.frame(Global_Moran_df)
colnames(Global_Moran_df) <- c("Week", "Moran.I", "p.value")
Local_Moran_df = as.data.frame(Local_Moran_df)
Local_Moran_df <- Local_Moran_df |> select(1,2,3,4,5,7)
colnames(Local_Moran_df) <- c("Week", "OD_name", "Local.Moran.I", "E.Ii", "Var.Ii", "p.value")
}
## P VALUE ADJUSTMENT
if(p_adjust == T){
Global_Moran_df$p.value <- p.adjust(Global_Moran_df$p.value, "BH")
Local_Moran_df$p.value <- p.adjust(Local_Moran_df$p.value, "BH")
}
## PLOTS
# Prepare plotting
# pdf(path, onefile = TRUE)
C <- Global_Moran_df
C[,1:dim(C)[2]] <- sapply(C[,1:dim(C)[2]], as.numeric)
C <- C |>
mutate(
sig = case_when(
p.value < signif ~ "red",
p.value >= signif ~ "grey"
))
C$Week <- as.Date(as.Date(paste(2020, C$Week, 1, sep="-"), "%Y-%U-%u"))
# Set subtitle
if(cl_eta == "15_21" & mob_mode == "OD_counts")
subtitle <- paste0("Mobility-based spatial weights, age class 70+, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "11_14" & mob_mode == "OD_counts")
subtitle <- paste0("Mobility-based spatial weights, age class 50-69, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "15_21" & mob_mode == "Cont_weights")
subtitle <- paste0("Contiguity-based spatial weights, age class 70+, aggregation in ", mortalityWindow, " weeks")
if(cl_eta == "11_14" & mob_mode == "Cont_weights")
subtitle <- paste0("Contiguity-based spatial weights, age class 50-69, aggregation in ", mortalityWindow, " weeks")
p <- ggplot(C, aes(Week, Moran.I))+
geom_point(color = C$sig)+
geom_line()+
# ggtitle(paste("Global Moran Index, aggregation in",step_aggr,"weeks")) +
labs(title = paste("Global Moran Index"), subtitle = subtitle) +
theme_bw() + ylab("Moran I") + xlab("Date") + # ylim(-0.151, 0.51) +
scale_x_date(date_breaks = "1 month",
date_labels = "%B") + theme(axis.text.x = element_text(angle = 50, hjust=0.95), text = element_text(size=15), axis.title.x = element_blank(),
plot.subtitle = element_text(size = 12))
ggsave(paste0(path,"/GM.png"),p)
## LOCAL PLOTS
# Iterate weekly
for (i in 1:length(weeks)){
w <- weeks[i]
X <- list_X[[i]]
# Plot
local.p.value <- Local_Moran_df |> dplyr::filter(Week == w) |> pull(p.value)
g <- my_plot_Moran_RL(geo_df, X, W, w, cl_eta, mob_mode, local.p.value, signif)
# grid.arrange(g)
ggsave(paste0(path,"/LM_",w,".png"),g, width = 20, height = 17, units = "cm")
}
# dev.off()
return(list("Global_Moran" = Global_Moran_df, "Local_Moran" = Local_Moran_df))
}
#### Spatial analysis of the Brescia-Bergamo-Milano area ----
# Function to produce the geographical dataset of BreBeMi area aggregated into basins
BreBeMi_geodf <- function(matches){
# Load geographical dataset of Italy
italy_geo <- st_read("Data/ISTAT/General_data/Limiti01012022_g/Com01012022_g")
italy_geo <- italy_geo |> dplyr::filter(COMUNE %in% matches$Comune_name) |>
left_join(matches, by = c("COMUNE" = "Comune_name"))
# Aggregation
stat_agg <- italy_geo %>%
group_by(Station_ref_name) %>%
summarize(geometry = st_union(geometry))
rm(italy_geo)
# Remove Verona and Peschiera
stat_agg <- stat_agg |> dplyr::filter(!(Station_ref_name %in% c("Verona", "Peschiera del Garda")))
return(stat_agg)
}
# Function to clean distances data
distances_cleaned <- function(net){
# load comuni informations from ISTAT
# The correspondance between the two files is given by "Codice Comune formato alfanumerico"
info <- read_excel("Data/ISTAT/General_data/Codici-statistici-e-denominazioni-al-01_01_2022.xls")
# Remove white spaces since they are causing problems
colnames(info) <- make.names(names(info))
# To start, keep only net rows having origin AND destination in the provinces of interest
provinces_of_interest = c("Monza e della Brianza", "Milano", "Bergamo", "Lecco", "Brescia", "Verona")
# List of the codes respectig this condition
list_codes <- info |>
filter(Denominazione.Regione %in% c("Lombardia", "Veneto")) |>
dplyr::select(Codice.Comune.formato.numerico) |> pull(Codice.Comune.formato.numerico)
# Selecting only edeges having origin AND destination in this list
net2 <- net |> filter(Origine %in% list_codes & Destinazione %in% list_codes)
## Defining nodes and edges
# I want the IDs with names and provinces
nodes <- info |>
filter(Denominazione.Regione %in% c("Lombardia", "Veneto"))
nodes <- nodes[,c(16, 7, 12)]
colnames(nodes) <- c("ID", "Comune", "Province")
edges <- net2 |> dplyr::select(-Name) |> rename(Minuti = Total_Minu, Metri = Total_Mete)
# Removing edges having same origin and destination
edges <- edges |> filter(Origine != Destinazione)
## 25 municipalities are new; copy their data from old municipalities
new_codes <- nodes |> filter(!(ID %in% edges$Origine) | !(list_codes %in% edges$Destinazione)) |> pull(ID)
# I import the dataset of suppressed comunis
suppressed_comunis <- read_excel("Data/ISTAT/General_data/Elenco_comuni_soppressi.xls")
colnames(suppressed_comunis) <- make.names(names(suppressed_comunis))
correspondances <- suppressed_comunis |>
filter(as.numeric(Codice.del.Comune.associato.alla.variazione) %in% new_codes) |>
group_by(Codice.del.Comune.associato.alla.variazione) |>
filter(row_number()==1) |>
dplyr::select(Codice.del.Comune.associato.alla.variazione, Codice.Comune)
colnames(correspondances) = c("New_code", "Old_code")
correspondances$Old_code <- as.numeric(correspondances$Old_code)
correspondances$New_code <- as.numeric(correspondances$New_code)
idx <- net$Origine %in% correspondances$Old_code
net$Origine[idx] <- correspondances$New_code[match(net$Origine[idx], correspondances$Old_code)]
idx <- net$Destinazione %in% correspondances$Old_code
net$Destinazione[idx] <- correspondances$New_code[match(net$Destinazione[idx], correspondances$Old_code)]
# I handle Torre de' Busi since it has not be suppressed; it changed province
net[net$Origine == 97080,"Origine"] = 16215
net[net$Destinazione == 97080,"Destinazione"] = 16215
# Check if there are other problems in codes correspondance
net2 <- net |> filter(Origine %in% list_codes & Destinazione %in% list_codes)
edges <- net2 |> dplyr::select(-Name) |> rename(Minuti = Total_Minu, Metri = Total_Mete)
edges <- edges |> filter(Origine != Destinazione)
nodes |> filter(!(ID %in% edges$Origine) | !(list_codes %in% edges$Destinazione))
## TWO OTHER CASES
# Campione d'Italia -> REMOVE; I CANNOT ESTIMATE TIMES
nodes <- nodes |> filter(!(ID == 13040))
list_codes <- setdiff(list_codes, 13040)
# Monte Isola -> ADD TIMES TO GO BY BOAT TO SULZANO - 22 minutes, searched by google maps; 5200 metri
# Select net of Sulzano
Sulzano_code <- info |> filter(Denominazione.in.italiano == "Sulzano") |> pull(Codice.Comune.formato.numerico)
net_MoIs <- net |> filter(Origine == Sulzano_code| Destinazione == Sulzano_code)
# Substitute code with Monte Isola code
MoIs_code <- info |> filter(Denominazione.in.italiano == "Monte Isola") |> pull(Codice.Comune.formato.numerico)
net_MoIs[net_MoIs$Origine == Sulzano_code,"Origine"] = MoIs_code
net_MoIs[net_MoIs$Destinazione == Sulzano_code,"Destinazione"] = MoIs_code
# remove duplicates
net_MoIs <- net_MoIs |> filter(!(Origine==Destinazione))
# To every arc, add 22 minutes and 5200 metres
net_MoIs$Total_Minu = net_MoIs$Total_Minu + 22
net_MoIs$Total_Mete = net_MoIs$Total_Mete + 5200
# add arc for MoIs-Sulzano $ Sulzano-MoIs
add_arcs <- data.frame("Name" = c(paste(MoIs_code, Sulzano_code, sep="-"), paste(Sulzano_code, MoIs_code, sep="-")),
"Origine" = c(MoIs_code, Sulzano_code),
"Destinazione" = c(Sulzano_code, MoIs_code),
"Total_Minu" = c(22,22),
"Total_Mete" = c(5200,5200))
net_MoIs <- rbind(net_MoIs, add_arcs)
# add to net
net <- rbind(net, net_MoIs)
net$Total_Minu <- as.numeric(net$Total_Minu)
net$Total_Mete <- as.numeric(net$Total_Mete)
return(net)
}
# Function that assigns each municipality to the closest Trenord station
closest_station <- function(Stations_correspondances, stats_codes, comuni_distances){
# Skip Campione d'Italia since distances estimates are not available
skip_code <- 13040
# Check if the act_code is in stats_codes
# is_in_stats <- Stations_correspondances$Code %in% stats_codes
# # Find the comune with a station
# station_indexes <- which(is_in_stats)
# Stations_correspondances[station_indexes, "Station_ref_code"] <- Stations_correspondances[station_indexes, "Code"]
# Stations_correspondances[station_indexes, c("Station_distance_meters", "Station_distance_minutes")] <- 0
#
# Find candidates and select the one with minimum distance
candidates <- comuni_distances %>%
filter(Origine %in% Stations_correspondances$Code, Destinazione %in% stats_codes) %>%
group_by(Origine) %>%
filter(Total_Mete == min(Total_Mete)) %>% slice(1) |>
ungroup() %>%
select(Destinazione, Total_Mete, Total_Minu)
# Update Stations_correspondances with the selected candidates
# update_indexes <- !is_in_stats & Stations_correspondances$Code != skip_code
Stations_correspondances[Stations_correspondances$Code != skip_code, c("Station_ref_code", "Station_distance_meters", "Station_distance_minutes")] <-
candidates
return(Stations_correspondances)
}
# Function that builds the stations correspondances dataset
build_stations_correspondances <- function(comuni_ISTAT, stations, comuni_distances, IS_area) {
# Filter Stations_correspondances based on Denominazione.Regione
Stations_correspondances <- comuni_ISTAT %>%
filter(Denominazione.Regione %in% c("Lombardia", "Veneto")) %>%
select(Code = 16, Name = 7, Province = 12)
# Filter stats_comuni using inner_join
stats_comuni <- stations %>%
filter(Comune %in% Stations_correspondances$Name) %>%
pull(Comune)
# Filter stats_codes using semi_join
stats_codes <- comuni_ISTAT %>%
filter(Denominazione.in.italiano %in% stats_comuni) %>%
pull(Codice.Comune.formato.numerico)
# Initialize the dataset with NA values
Stations_correspondances <- Stations_correspondances %>%
mutate(Station_ref_code = NA,
Station_distance_meters = NA,
Station_distance_minutes = NA)
# Apply the function that assigns each municipality to the closest stations
Stations_correspondances <- closest_station(Stations_correspondances, stats_codes, comuni_distances)
# Perform a left_join with comuni_ISTAT to add comune and province of the station
Stations_correspondances <- Stations_correspondances %>%
left_join(comuni_ISTAT, by = c("Station_ref_code" = "Codice.Comune.formato.numerico")) %>%
select(Comune_code = Code, Comune_name = Name, Comune_province = Province,
Station_ref_code, Station_ref_meters = Station_distance_meters, Station_ref_minutes = Station_distance_minutes,
Station_ref_name = Denominazione.in.italiano, Station_ref_province = Denominazione.Regione)
# Replace stations of Milan area with "IS area"
IS_area_comunis <- stations %>%
filter(Codice %in% IS_area) %>%
pull(Comune)
Stations_correspondances$Station_ref_name[Stations_correspondances$Station_ref_name %in% IS_area_comunis] <- "IS area"
Stations_correspondances$Station_ref_province[Stations_correspondances$Station_ref_name == "IS area"] <- "Milano"
return(Stations_correspondances)
}
#### Aggregation and exploratory analysis of ISTAT death data into stations basins ----
# Function to get mortality data aggregated at BreBeMi basins level, applying an aggregation
# for computation of mortality divided by population
deaths_data_cleaned_BreBeMi <- function(deaths, pop, matches, mortalityWindows, IS_area){
deaths <- deaths |>
select(all_of(c("COD_PROVCOM", "partial_date_death", "CL_ETA", "T_19", "T_20", "T_21",
"NOME_PROVINCIA", "NOME_REGIONE", "NOME_COMUNE"))) |>
# Select only Lombardia
filter(NOME_REGIONE == "Lombardia") |>
# Now I transform the dataset to have dates dependent on year and only one column counting the deaths for that day
pivot_longer(cols = T_19:T_21, names_to = "Year", values_to = "T_deaths") |>
# Then I apply my numbering in weeks of the column partial_date_death
# weeks of 2020 are 00-52
# weeks of 2019 are -52-00
# weeks of 2021 are 52-104
mutate(partial_date_death = case_when(
Year == "T_19" ~ -(52-as.numeric(format(as.Date(partial_date_death), "%W"))),
Year == "T_20" ~ as.numeric(format(as.Date(partial_date_death), "%W")),
Year == "T_21" ~ 52 + as.numeric(format(as.Date(partial_date_death), "%W")),
)) |> select(-Year) |>
# Aggregation into weeks
group_by(COD_PROVCOM, partial_date_death, CL_ETA, NOME_PROVINCIA, NOME_REGIONE, NOME_COMUNE) |>
summarise(T_deaths = sum(T_deaths)) |> ungroup() |>
# Join population info
left_join(pop |> select(istat, CL_ETA, num_residenti),
by = c("COD_PROVCOM" = "istat", "CL_ETA")) |>
mutate(COD_PROVCOM = as.numeric(COD_PROVCOM)) |>
# Join with the matches dataset to recover the corresponding station
right_join(matches, by = c("COD_PROVCOM" = "ISTAT_code")) |>
# Removing Verona and Peschiera
filter(!(Station_ref_name %in% c("Verona", "Peschiera del Garda"))) |>
# Aggregate
select(-c(Station_ref_code, COD_PROVCOM, Station_ref_meters, Station_ref_minutes)) |>
group_by(partial_date_death, CL_ETA, Station_ref_name, Station_ref_province) |>
summarise(across(where(is.numeric), sum)) |>
ungroup()
## COMPUTATION OF MORTALITY DENSITY
# Add one empty column for every element of the vector mortality Windows
cols <- paste0("md_window", mortalityWindows)
deaths[,cols] <- NA
# Create progress bar
pb <- progress_bar$new(
format = " computing [:bar] :percent eta: :eta",
total = dim(deaths)[1], clear = FALSE, width= 60)
for (i in 1:dim(deaths)[1]){
r <- deaths[i,]
cl_eta <- r$CL_ETA
area <- r$Station_ref_name
w <- as.numeric(r$partial_date_death)
for (mw in mortalityWindows){
# Compute the number of deaths in the sliding window
act <- deaths |> dplyr::filter(CL_ETA == cl_eta & Station_ref_name == area & as.numeric(partial_date_death) %in% (w-mw+1):w) |>
summarise(across(where(is.numeric), sum)) |> dplyr::select(T_deaths)
# Compute mortality density as deaths / population
deaths[i, paste0("md_window", mw)] <- act / r$num_residenti
}
pb$tick()
}
# Convert from list to numeric
deaths <- deaths |>
mutate(across(starts_with("md_window"), as.numeric))
return(deaths)
}
# Aggregate OD of Regione Lombardia and clean to use at BreBeMi level
aggregate_OD_RL_BreBeMi <- function(OD, matches, stations, IS_area){
# Compute total movements and railway movements
OD <- OD |> mutate(TOT = rowSums(across(where(is.numeric))),
FERRO = rowSums(across(ends_with('_FERRO')))) |>
# Filter only the area
filter(ZONA_ORIG %in% unique(matches$OD_name) & ZONA_DEST %in% unique(matches$OD_name))
# I also have to aggregate the IS area
# Comuni in Milan area are
comuni_IS_area <- stations |> filter(Codice %in% IS_area) |> pull(Comune) |> unique()
matches <- matches |> mutate(Station_ref_name = replace(Station_ref_name, Station_ref_name %in% comuni_IS_area, "IS area"))
# Convert zone names to stations names
OD$ZONA_ORIG <- with(matches, Station_ref_name[match(OD$ZONA_ORIG, OD_name)])
OD$ZONA_DEST <- with(matches, Station_ref_name[match(OD$ZONA_DEST, OD_name)])
OD <- OD |> dplyr::select(!c(PROV_ORIG, PROV_DEST)) |> group_by(ZONA_ORIG, ZONA_DEST) |>
summarise(across(where(is.numeric), sum)) |> na.omit()
return(OD)