forked from TheEconomist/covid-19-excess-deaths-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaning_script.R
1361 lines (1211 loc) · 61.7 KB
/
cleaning_script.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
# Step 1: import libraries and data ---------------------------------------
# Import libraries
library(tidyverse)
library(readxl)
library(data.table)
library(lubridate)
options(scipen=999)
# Import global JHU data from Our World In Data
global_covid_source_latest <- read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/jhu/new_deaths.csv") %>%
add_row(date=seq(as.Date("2019-12-31"), as.Date("2020-01-21"), by="days")) %>% arrange(date)
global_covid_source_cumulative <- read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/jhu/total_deaths.csv") %>%
add_row(date=seq(as.Date("2019-12-31"), as.Date("2020-01-21"), by="days")) %>% arrange(date)
# Step 2: import and clean Austria's data ---------------------------------------
# Import Austria's data
austria_total_source_latest <- fread("https://data.statistik.gv.at/data/OGD_gest_kalwo_GEST_KALWOCHE_100.csv")
austria_week_windows <- fread("source-data/austria/austria_week_windows.csv")
# Group total and expected deaths by week
austria_weekly_total_deaths <- austria_total_source_latest %>%
left_join(austria_week_windows) %>%
mutate(country = "Austria",
region = "Austria",
population = 8902600,
start_date = dmy(start_date),
end_date = dmy(end_date)) %>%
filter(year >= 2010) %>%
group_by(country,region,population,start_date,end_date,year,week) %>%
summarise(total_deaths = sum(`F-ANZ-1`, na.rm=T)) %>%
ungroup()
# Group covid deaths by week
austria_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(week_date = date - 5, # Use Austria's weekly windows
week = week(week_date),
year = year(week_date),
covid_deaths = Austria) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
austria_weekly_deaths <- austria_weekly_total_deaths %>%
left_join(austria_weekly_covid_deaths) %>%
mutate(region_code = 0,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(austria_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/austria_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 3: import and clean Belgium's data ---------------------------------------
# Import Belgium's data
belgium_total_source_latest <- fread("source-data/belgium/belgium_total_source_latest.csv")
belgium_covid_source_latest <- fread("https://epistat.sciensano.be/Data/COVID19BE_MORT.csv")
# Group total and expected deaths by week
belgium_weekly_total_deaths <- belgium_total_source_latest %>%
mutate(start_date = dmy(start_date),
year = year(start_date),
week = week(start_date)) %>%
group_by(country,region,week,year,population) %>%
summarise(total_deaths = sum(total_deaths)) %>%
arrange(year,week) %>%
drop_na()
# Group covid deaths by week
belgium_weekly_covid_deaths <- belgium_covid_source_latest %>%
mutate(date = ymd(DATE)) %>%
group_by(date) %>%
summarise(covid_deaths = sum(DEATHS,na.rm=T)) %>%
bind_rows(expand.grid(date = seq(as.Date("2020-01-01"), as.Date("2020-03-09"), by="days"),
covid_deaths = 0)) %>%
mutate(country = "Belgium",
week = week(date),
year = year(date)) %>%
dplyr::select(country,date,year,week,covid_deaths) %>%
group_by(country,year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na() %>%
dplyr::select(country,year,week,covid_deaths)
# Join weekly total deaths and weekly covid deaths together
belgium_weekly_deaths <- belgium_weekly_total_deaths %>%
left_join(belgium_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53,
end_date <= as.Date("2020-12-15")) # Remove weeks with incomplete data
# Export as CSV
write.csv(belgium_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/belgium_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 4: import and clean Brazil's data ---------------------------------------
# Import Brazil's data
brazil_total_source_latest <- fread("source-data/brazil/brazil_total_source_latest.csv")
# Group total and expected deaths by week
brazil_weekly_total_deaths <- brazil_total_source_latest %>%
mutate(week = nu_semana_epidemiologica) %>%
group_by(week) %>%
summarise(total_deaths = sum(qt_obitos_2020_corrigido,na.rm=T),
expected_deaths = sum(qt_obitos_2020_esperado,na.rm=T)) %>%
ungroup() %>%
filter(total_deaths > 0) %>%
mutate(country = "Brazil",
region = "Brazil",
region_code = 0,
start_date = as.Date("2019-12-29") + (week-1)*7,
end_date = start_date + 6,
year = 2020,
population = 210147125)
# Group covid deaths by week
brazil_weekly_covid_deaths <- global_covid_source_latest %>%
mutate(week_date = date + 3, # Use Brazil's weekly windows
year = year(week_date),
week = week(week_date),
covid_deaths = Brazil) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year, week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T))
# Join weekly total deaths and weekly covid deaths together
brazil_weekly_deaths <- brazil_weekly_total_deaths %>%
left_join(brazil_weekly_covid_deaths) %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths)
# Export as CSV
write.csv(brazil_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/brazil_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 5: import and clean Britain's data ---------------------------------------
# Import Britain's data
britain_regions <- fread("source-data/britain/britain_regions.csv")
britain_total_source_latest <- read_excel("source-data/britain/britain_total_source_latest.xlsx")
britain_covid_source_latest <- read_excel("source-data/britain/britain_covid_source_latest.xlsx")
# Group total deaths by week and region
britain_regions_weekly_total_deaths <- gather(britain_total_source_latest,"region","total_deaths",
-c(country,start_date,end_date,week)) %>%
left_join(britain_regions %>%
dplyr::select(region,region_code,population)) %>%
mutate(year = year(start_date),
week = week(start_date))
# Group covid deaths by week and region
britain_regions_weekly_covid_deaths <- gather(britain_covid_source_latest,"region","covid_deaths",
-c(country,start_date,end_date,week)) %>%
left_join(britain_regions %>%
dplyr::select(region,region_code,population)) %>%
mutate(year = year(start_date),
week = week(start_date))
# Join weekly total deaths and weekly covid deaths together
britain_regions_weekly_deaths <- britain_regions_weekly_total_deaths %>%
left_join(britain_regions_weekly_covid_deaths) %>%
mutate(expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(britain_regions_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/britain_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 6: import and clean Chile's data ---------------------------------------
# Import Chile's data
chile_regions <- read_excel("source-data/chile/chile_regions.xlsx")
chile_total_source_2015_12_31 <- read_excel("source-data/chile/chile_total_source_2015_12_31.xlsx")
chile_total_source_2016_12_31 <- read_excel("source-data/chile/chile_total_source_2016_12_31.xlsx")
chile_total_source_2017_12_31 <- read_excel("source-data/chile/chile_total_source_2017_12_31.xlsx")
chile_total_source_2018_12_31 <- read_excel("source-data/chile/chile_total_source_2018_12_31.xlsx")
chile_total_source_2019_12_31 <- read_excel("source-data/chile/chile_total_source_2019_12_31.xlsx")
chile_total_source_latest <- read_excel("source-data/chile/chile_total_source_latest.xlsx")
chile_covid_source_latest <- read_csv("https://raw.githubusercontent.com/DataScienceResearchPeru/covid-19_latinoamerica/master/latam_covid_19_data/time_series/time_series_deaths.csv") %>%
filter(Country == "Chile")
# Group total deaths by week and region
chile_regions_weekly_total_deaths <- bind_rows(chile_total_source_2015_12_31,chile_total_source_2016_12_31,
chile_total_source_2017_12_31,chile_total_source_2018_12_31,
chile_total_source_2019_12_31,chile_total_source_latest) %>%
mutate(year = AÑO, month = MES, day = DIA, region_long_name = REGION) %>%
left_join(chile_regions) %>%
group_by(region_number,year,month,day) %>%
summarise(total_deaths = sum(TOTAL,na.rm=T)) %>%
ungroup() %>%
mutate(date = as.Date(ISOdate(year, month, day)),
week = week(date)) %>%
group_by(region_number,year,week) %>%
summarise(total_deaths = sum(total_deaths,na.rm=T))
# Group covid deaths by week and region
chile_regions_weekly_covid_deaths <- chile_covid_source_latest %>%
mutate(region_code = `ISO 3166-2 Code`) %>%
dplyr::select(-c(`ISO 3166-2 Code`,Country,Subdivision,`Last Update`)) %>%
pivot_longer(cols = c(-region_code), names_to = "date", values_to = "cumulative_deaths") %>%
# avoid double-counting if value is NA
fill(cumulative_deaths, .direction = "down") %>%
mutate(date = ymd(date)) %>%
bind_rows(expand.grid(region_code = unique(chile_regions$region_code),
date = seq(as.Date("2015-01-01"), as.Date("2020-02-24"), by="days"), # Bind on rows with 0 covid deaths before February 24th
cumulative_deaths = 0)) %>%
filter(region_code != "CL") %>%
arrange(region_code,date) %>%
group_by(region_code) %>%
mutate(previous_day_deaths = lag(cumulative_deaths, n = 1, default = NA), # Create a lag, to calculate daily deaths from cumulative ones
covid_deaths = case_when(!is.na(cumulative_deaths) & !is.na(previous_day_deaths) ~ cumulative_deaths - previous_day_deaths,
!is.na(cumulative_deaths) ~ cumulative_deaths)) %>%
mutate(week = week(date), year = year(date)) %>%
left_join(chile_regions) %>%
group_by(region_number, year, week) %>%
summarise(covid_deaths = sum(covid_deaths,na.rm=T))
# Join weekly total deaths and weekly covid deaths together in each region
chile_regions_weekly_deaths <- chile_regions_weekly_covid_deaths %>%
left_join(chile_regions_weekly_total_deaths) %>%
left_join(chile_regions %>% filter(region_code != "CL-NB")) %>%
mutate(start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = covid_deaths,
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53,
end_date <= as.Date("2020-12-29")) # Remove weeks with incomplete data
# Aggregate at the national level
chile_national_weekly_deaths <- chile_regions_weekly_deaths %>%
ungroup() %>%
mutate(region = "Chile",
region_code = "CL") %>%
group_by(country,region,region_code,start_date,end_date,year,week) %>%
summarise(population = sum(population,na.rm=T),
total_deaths = sum(total_deaths,na.rm=T),
covid_deaths = sum(covid_deaths,na.rm=T),
expected_deaths = "TBC") %>% # To be calculated
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(bind_rows(chile_regions_weekly_deaths,chile_national_weekly_deaths) %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/chile_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 7: import and clean Denmark's data ---------------------------------------
# Import Denmark's data
denmark_total_source_latest <- fread("source-data/denmark/denmark_total_source_latest.csv")
# Group total deaths by week
denmark_weekly_total_deaths <- denmark_total_source_latest %>%
mutate(start_date = dmy(start_date),
year = year(start_date),
week = week(start_date)) %>%
group_by(country,region,year,week,population) %>%
summarise(total_deaths = sum(total_deaths))
# Group covid deaths by week
denmark_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(week = week(date),
year = year(date),
covid_deaths = Denmark) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
denmark_weekly_deaths <- denmark_weekly_total_deaths %>%
left_join(denmark_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53,
end_date <= as.Date("2020-12-08")) # Remove weeks with incomplete data
# Export as CSV
write.csv(denmark_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/denmark_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 8: import and clean Ecuador's data ---------------------------------------
# Import Ecuador's data
ecuador_total_source_latest <- read_excel("source-data/ecuador/ecuador_total_source_latest.xlsx")
# Group total deaths by month and region
ecuador_monthly_total_deaths <- ecuador_total_source_latest
# Group national covid deaths by month
ecuador_monthly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(month = month(date),
year = year(date),
covid_deaths = Ecuador,
region_code = "EC") %>%
dplyr::select(date,region_code,year,month,covid_deaths) %>%
group_by(region_code,year,month) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join monthly total deaths and monthly covid deaths together
ecuador_monthly_deaths <- ecuador_monthly_total_deaths %>%
left_join(ecuador_monthly_covid_deaths) %>%
mutate(covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,month,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
arrange(region_code,year,month)
# Export as CSV
write.csv(ecuador_monthly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/ecuador_monthly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 9: import and clean France's data ---------------------------------------
# Import France's data
france_depts <- read_excel("source-data/france/france_depts.xlsx")
france_total_source_2015_12_31 <- fread("source-data/france/france_total_source_2015_12_31.csv")
france_total_source_2016_12_31 <- fread("source-data/france/france_total_source_2016_12_31.csv")
france_total_source_2017_12_31 <- fread("source-data/france/france_total_source_2017_12_31.csv")
france_total_source_2018_12_31 <- fread("source-data/france/france_total_source_2018_12_31.csv")
france_total_source_2019_12_31 <- fread("source-data/france/france_total_source_2019_12_31.csv")
france_total_source_2020_01_31 <- fread("source-data/france/france_total_source_2020_01_31.csv")
france_total_source_2020_02_29 <- fread("source-data/france/france_total_source_2020_02_29.csv")
france_total_source_2020_03_31 <- fread("source-data/france/france_total_source_2020_03_31.csv")
france_total_source_2020_04_30 <- fread("source-data/france/france_total_source_2020_04_30.csv")
france_total_source_latest <- fread("source-data/france/france_total_source_latest.csv")
france_covid_source_latest <- fread("https://www.data.gouv.fr/fr/datasets/r/63352e38-d353-4b54-bfd1-f1b3ee1cabd7")
# Group France's departements into regions
france_regions <- france_depts %>%
group_by(country,region,region_code) %>%
summarise(population = sum(population,na.rm=T))
# Define function that extracts dept_code from France's historical deaths register
get_french_dept_code <- function(x) { dept_code <- substr(x,1,nchar(x)-3) }
# Using France's historical deaths register, calculate the number of daily total deaths in each region
france_regions_daily_total_deaths <- bind_rows(france_total_source_2015_12_31,france_total_source_2016_12_31,
france_total_source_2017_12_31,france_total_source_2018_12_31,
france_total_source_2019_12_31,france_total_source_2020_01_31,
france_total_source_2020_02_29,france_total_source_2020_03_31,
france_total_source_2020_04_30) %>%
mutate(date = ymd(datedeces),
dept_code = get_french_dept_code(lieudeces)) %>%
left_join(france_depts %>%
dplyr::select(dept_code,region, region_code)) %>%
filter(date >= as.Date("2015-01-01"),
date <= as.Date("2020-02-29"), # Calculate daily total deaths up until the end of February 2020
!is.na(region)) %>%
group_by(region,region_code,date) %>%
summarise(total_deaths = n())
# Group total deaths by week and region, binding the historical register and latest data together
france_regions_weekly_total_deaths <- france_total_source_latest %>%
mutate(date = dmy(Date_evenement)) %>%
group_by(Zone) %>% # Create a lag, to calculate daily deaths from cumulative ones
mutate(zone = Zone,
previous_day_deaths = lag(Total_deces_2020, n = 1, default = NA),
total_deaths = case_when(!is.na(Total_deces_2020) & !is.na(previous_day_deaths) ~ Total_deces_2020 - previous_day_deaths,
!is.na(Total_deces_2020) ~ Total_deces_2020)) %>%
left_join(france_depts %>% # Join the region name and code
dplyr::select(zone,region,region_code)) %>%
ungroup() %>%
dplyr::select(region,region_code,date,total_deaths) %>%
bind_rows(france_regions_daily_total_deaths) %>% # Bind on rows from historical deaths register
drop_na() %>%
arrange(region_code,date) %>%
mutate(week = week(date), # Group total deaths by week and departement
year = year(date)) %>%
left_join(france_regions) %>% # Join the region population, remove regions outside mainland France
filter(population > 0) %>%
group_by(country,region,region_code,year,week,population) %>%
summarise(total_deaths = sum(total_deaths,na.rm=T)) %>%
mutate(start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6)
# The next sections of code train a model to impute covid deaths in each departement before March 18th
# Calculate daily cumulative covid deaths in each departement after March 18th
france_depts_daily_covid_deaths <- france_covid_source_latest %>%
filter(sexe == 0) %>%
mutate(dept_code = case_when(str_detect(dep,"^0") ~ str_replace(dep,"^0",""), TRUE ~ as.character(dep)),
date = ymd(jour),
cumulative_deaths = dc) %>%
dplyr::select(dept_code, date, cumulative_deaths) %>%
left_join(france_depts %>%
dplyr::select(dept_code,dept)) %>%
arrange(dept_code,date) %>%
drop_na()
# Generate dataframe to impute each departement's share of national covid deaths before March 18th
france_covid_model_df <- france_depts_daily_covid_deaths %>%
left_join(france_depts_daily_covid_deaths %>% # Join on the daily sum of national covid deaths
group_by(date) %>%
summarise(national_deaths = sum(cumulative_deaths,na.rm=T))) %>%
mutate(national_share = cumulative_deaths / national_deaths, # Calculate each departement's share of national covid deaths
yday = yday(date))
# Train logistic model to impute each departement's share of national covid deaths
france_covid_model <- glm(national_share ~ yday + dept_code + dept_code:yday, # Use an interaction between each departement and day
data = france_covid_model_df, family="binomial")
summary(france_covid_model)
# Use model to make imputations of each departement's daily share of national covid deaths before March 18th
france_modelled_covid_shares <- expand.grid(date = seq(as.Date("2015-01-01"), Sys.Date(), by="days"), # Create empty grid
dept_code = unique(france_covid_model_df$dept_code)) %>%
left_join(france_covid_model_df %>%
dplyr::select(dept_code, dept)) %>% # Join on departement name
left_join(france_covid_model_df) %>%
distinct() %>%
mutate(date = ymd(date),
yday = yday(date)) %>%
mutate(imputed_national_share = predict(france_covid_model,.,type="response"))
# Group covid deaths by week and region, combining observed data with imputed values
france_regions_weekly_covid_deaths <- france_modelled_covid_shares %>%
left_join(global_covid_source_cumulative %>% # Join the ECDC's daily cumulative covid deaths for the whole of France before March 18th
mutate(ECDC_deaths = France) %>%
dplyr::select(date,ECDC_deaths)) %>%
mutate(modelled_deaths = case_when(is.na(cumulative_deaths) ~ imputed_national_share * ECDC_deaths,
TRUE ~ as.numeric(cumulative_deaths)), # Impute cumulative covid deaths in each dept from ECDC data
week = week(date),
year = year(date)) %>%
group_by(dept_code) %>% # Create a lag, to calculate daily deaths from cumulative ones
mutate(previous_day_deaths = lag(modelled_deaths, n = 1, default = NA),
covid_deaths = case_when(!is.na(modelled_deaths) & !is.na(previous_day_deaths) ~ modelled_deaths - previous_day_deaths,
!is.na(modelled_deaths) ~ modelled_deaths,
TRUE ~ 0),
covid_deaths = case_when(covid_deaths < 0 ~ 0, TRUE ~ as.numeric(covid_deaths))) %>%
left_join(france_depts) %>% # Join and group by region
group_by(country,region,region_code,year,week) %>%
summarise(covid_deaths = sum(covid_deaths,na.rm=T))
# Join weekly total deaths and weekly covid deaths together in each region
france_regions_weekly_deaths <- france_regions_weekly_total_deaths %>%
left_join(france_regions_weekly_covid_deaths) %>%
mutate(expected_deaths = "TBC") %>% # To be calculated
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53)
# Aggregate at the national level, use ECDC covid data to include nursing homes
france_national_weekly_deaths <- france_regions_weekly_deaths %>%
ungroup() %>%
mutate(region = "France",
region_code = 0) %>%
group_by(country,region,region_code,start_date,end_date,year,week) %>%
summarise(population = sum(population,na.rm=T),
total_deaths = sum(total_deaths,na.rm=T),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
left_join(global_covid_source_latest %>% # Join ECDC covid data
dplyr::select(date,France) %>%
mutate(covid_deaths = France,
date = ymd(date),
week = week(date),
year = year(date)) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths,na.rm=T))) %>%
mutate(covid_deaths = replace_na(covid_deaths,0)) %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(bind_rows(france_regions_weekly_deaths,france_national_weekly_deaths) %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")) %>%
filter(end_date <= as.Date("2020-12-01")), # Remove weeks with incomplete data
"output-data/historical-deaths/france_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 10: import and clean Germany's data ---------------------------------------
# Import Germany's data
germany_total_source_latest <- fread("source-data/germany/germany_total_source_latest.csv")
# Group total deaths by week
germany_weekly_total_deaths <- germany_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
year = year(start_date),
week = week(start_date)) %>%
group_by(country,region,year,week,population) %>%
summarise(total_deaths = sum(total_deaths))
# Group covid deaths by week
germany_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(week = week(date),
year = year(date),
covid_deaths = Germany) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
germany_weekly_deaths <- germany_weekly_total_deaths %>%
left_join(germany_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53,
end_date <= as.Date("2020-11-30")) # Remove weeks with incomplete data
# Export as CSV
write.csv(germany_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/germany_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 11: import and clean Indonesia's data ---------------------------------------
# Import Indonesia's data
indonesia_total_source_latest <- fread("source-data/indonesia/indonesia_total_source_latest.csv")
# Join weekly total deaths and weekly covid deaths together
indonesia_monthly_deaths <- indonesia_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
region_code = 0,
expected_deaths = "TBC") %>% # To be calculated
dplyr::select(country,region,region_code,start_date,end_date,year,month,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(indonesia_monthly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/indonesia_monthly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 12: import and clean Italy's data ---------------------------------------
# Import Italy's data
italy_comunes <- read_excel("source-data/italy/italy_comunes.xlsx")
italy_total_source_latest <- fread("source-data/italy/italy_total_source_latest.csv")
italy_covid_source_latest <- read_csv("https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni.csv")
# Create list of Italian comunes with reliable data
italy_comunes_reliable <- italy_total_source_latest %>%
filter(GE <= 831, T_20 != "n.d.") %>% # Filter out any comunes missing data before June 30th
dplyr::select(COD_PROVCOM) %>%
distinct() %>%
pull()
# Group Italian comunes into regions
italy_regions <- italy_comunes %>%
filter(comune_code %in% italy_comunes_reliable) %>%
group_by(country,region,region_code) %>%
summarise(population = sum(population,na.rm=T))
# Group total deaths by week and region
italy_regions_weekly_total_deaths <- italy_total_source_latest %>%
mutate(comune_code = COD_PROVCOM) %>%
left_join(italy_comunes) %>%
filter(GE <= 1130, T_20 != "n.d.") %>% # Remove any missing days
group_by(region_code,GE) %>%
summarise(total_deaths_2015 = sum(T_15,na.rm=T),
total_deaths_2016 = sum(T_16,na.rm=T),
total_deaths_2017 = sum(T_17,na.rm=T),
total_deaths_2018 = sum(T_18,na.rm=T),
total_deaths_2019 = sum(T_19,na.rm=T),
total_deaths_2020 = sum(as.numeric(T_20),na.rm=T)) %>%
gather("period","total_deaths",
-c(region_code,GE)) %>%
ungroup() %>%
mutate(month = as.numeric(round(GE/100)), # Extract date from GE character variable
day = as.numeric(GE-(month*100)),
year = as.numeric(map_chr(period,substr,14,18)),
date = as.Date(ISOdate(year, month, day)),
week = week(date)) %>%
group_by(region_code,year,week) %>%
summarise(days = n(),
total_deaths = sum(total_deaths)) %>%
filter(days == 7) %>% # Remove any incomplete weeks
left_join(italy_regions) %>%
ungroup()
# Group covid deaths by week and region
italy_regions_weekly_covid_deaths <- italy_covid_source_latest %>%
mutate(date = as.Date(data),
region_code = as.numeric(codice_regione)) %>%
group_by(date,region_code) %>% # Group Trentino and Sudtirol together
summarise(cumulative_deaths = sum(deceduti)) %>%
ungroup() %>%
dplyr::select(date,region_code,cumulative_deaths) %>%
bind_rows(expand.grid(date = seq(as.Date("2015-01-01"), as.Date("2020-02-23"), by="days"), # Bind on rows with 0 covid deaths before February 23rd
region_code = as.numeric(unique(italy_covid_source_latest$codice_regione)),
cumulative_deaths = 0)) %>%
left_join(italy_regions) %>%
arrange(region_code,date) %>%
group_by(region_code) %>%
mutate(previous_day_deaths = lag(cumulative_deaths, n = 1, default = NA), # Create a lag, to calculate daily deaths from cumulative ones
covid_deaths = case_when(!is.na(cumulative_deaths) & !is.na(previous_day_deaths) ~ cumulative_deaths - previous_day_deaths,
!is.na(cumulative_deaths) ~ cumulative_deaths)) %>%
ungroup() %>%
mutate(year = year(date),
week = week(date)) %>%
group_by(country,region,region_code,year,week) %>%
summarise(covid_deaths = sum(covid_deaths,na.rm=T)) %>%
ungroup()
# Join weekly total deaths and weekly covid deaths together in each region
italy_regions_weekly_deaths <- italy_regions_weekly_total_deaths %>%
left_join(italy_regions_weekly_covid_deaths %>%
dplyr::select(region_code,year,week,covid_deaths)) %>%
mutate(start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = covid_deaths,
expected_deaths = "TBC") %>% # To be calculated
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Aggregate at the national level
italy_national_weekly_deaths <- italy_regions_weekly_deaths %>%
ungroup() %>%
mutate(region = "Italy",
region_code = 0) %>%
group_by(country,region,region_code,start_date,end_date,year,week) %>%
summarise(population = sum(population,na.rm=T),
total_deaths = sum(total_deaths,na.rm=T),
covid_deaths = sum(covid_deaths,na.rm=T),
expected_deaths = "TBC") %>% # To be calculated
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(bind_rows(italy_regions_weekly_deaths,italy_national_weekly_deaths) %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/italy_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 13: import and clean Mexico's data ---------------------------------------
# Import Mexico's data
mexico_total_source_latest <- fread("source-data/mexico/mexico_total_source_latest.csv")
# Group covid deaths by week
mexico_weekly_covid_deaths <- global_covid_source_latest %>%
mutate(week_date = date + 3, # Use Mexico's weekly windows
year = year(week_date),
week = week(week_date),
covid_deaths = Mexico) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year, week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T))
# Join weekly total deaths and weekly covid deaths together
mexico_weekly_deaths <- mexico_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
week = week(end_date),
year = year(end_date)) %>%
left_join(mexico_weekly_covid_deaths) %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(mexico_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/mexico_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 14: import and clean the Netherlands' data ---------------------------------------
# Import the Netherlands' data library
library(cbsodataR)
# Group total deaths by week
netherlands_weekly_total_deaths <- cbs_get_data("70895ENG") %>%
cbs_add_date_column() %>%
filter(Periods_freq=="W", Sex=="T001038", Age31December=="10000") %>%
group_by(Periods_Date) %>%
summarise(total_deaths = sum(Deaths_1)) %>%
mutate(country = "Netherlands", region = "Netherlands", start_date = Periods_Date, end_date = start_date+6,
population = 17414806, year = year(start_date), week = week(start_date)) %>%
filter(year >= 2015) %>%
ungroup() %>%
dplyr::select(-Periods_Date)
# Group covid deaths by week
netherlands_weekly_covid_deaths <- global_covid_source_latest %>%
mutate(week_date = date - 3, # Use the Netherlands' weekly windows
year = year(week_date),
week = week(week_date),
covid_deaths = Netherlands) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year, week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T))
# Join weekly total deaths and weekly covid deaths together
netherlands_weekly_deaths <- netherlands_weekly_total_deaths %>%
left_join(netherlands_weekly_covid_deaths) %>%
mutate(region_code = 0,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(netherlands_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/netherlands_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 15: import and clean Norway's data ---------------------------------------
# Import Norway's data
norway_total_source_latest <- fread("source-data/norway/norway_total_source_latest.csv")
# Group total deaths by week
norway_weekly_total_deaths <- norway_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
year = year(start_date),
week = week(start_date))
# Group covid deaths by week
norway_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(week = week(date),
year = year(date),
covid_deaths = Norway) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
norway_weekly_deaths <- norway_weekly_total_deaths %>%
left_join(norway_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53)
# Export as CSV
write.csv(norway_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/norway_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 16: import and clean Peru's data ---------------------------------------
# Import Peru's data
peru_total_source_latest <- fread("source-data/peru/peru_total_source_latest.csv")
# Group total deaths by month and region
peru_monthly_total_deaths <- peru_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date))
# Group national covid deaths by month
peru_monthly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(month = month(date),
year = year(date),
covid_deaths = Peru,
region_code = "PE") %>%
dplyr::select(date,region_code,year,month,covid_deaths) %>%
group_by(region_code,year,month) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join monthly total deaths and monthly covid deaths together
peru_monthly_deaths <- peru_monthly_total_deaths %>%
left_join(peru_monthly_covid_deaths) %>%
mutate(covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,month,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
arrange(region_code,year,month)
# Export as CSV
write.csv(peru_monthly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/peru_monthly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 17: import and clean Portugal's data ---------------------------------------
# Import Portugal's data
portugal_total_source_latest <- fread("source-data/portugal/portugal_total_source_latest.csv")
# Group total deaths by week
portugal_weekly_total_deaths <- portugal_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
year = year(start_date),
week = week(start_date)) %>%
group_by(country,region,year,week,population) %>%
summarise(total_deaths = sum(total_deaths,na.rm=T))
# Group covid deaths by week
portugal_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(year = year(date),
week = week(date),
covid_deaths = Portugal) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
portugal_weekly_deaths <- portugal_weekly_total_deaths %>%
left_join(portugal_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6,
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC") %>% # To be calculated
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na() %>%
filter(week != 53,
end_date <= as.Date("2020-12-31")) # Remove weeks with incomplete data
# Export as CSV
write.csv(portugal_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/portugal_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 18: import and clean Russia's data ---------------------------------------
# Import Russia's data
russia_total_source_latest <- fread("source-data/russia/russia_total_source_latest.csv")
# Join monthly total deaths and monthly covid deaths together
russia_monthly_deaths <- russia_total_source_latest %>%
left_join(global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(month = month(date),
year = year(date),
covid_deaths = Russia) %>%
group_by(year,month) %>%
summarise(covid_deaths = sum(covid_deaths,na.rm=T)) %>%
dplyr::select(month,year,covid_deaths)) %>%
ungroup() %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
covid_deaths = replace_na(covid_deaths,0),
expected_deaths = "TBC")
# Export as CSV
write.csv(russia_monthly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/russia_monthly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 19: import and clean South Africa's data ---------------------------------------
# Import South Africa's data
south_africa_total_source_latest <- fread("source-data/south-africa/south_africa_total_source_latest.csv")
# Group total deaths by week
south_africa_weekly_total_deaths <- south_africa_total_source_latest %>%
mutate(start_date = dmy(start_date),
end_date = dmy(end_date),
year = year(start_date),
week = week(start_date))
# Group covid deaths by week
south_africa_weekly_covid_deaths <- global_covid_source_latest %>%
filter(date >= as.Date("2020-01-01")) %>%
mutate(year = year(date),
week = week(date),
covid_deaths = `South Africa`) %>%
dplyr::select(date,year,week,covid_deaths) %>%
group_by(year,week) %>%
summarise(covid_deaths = sum(covid_deaths, na.rm=T)) %>%
drop_na()
# Join weekly total deaths and weekly covid deaths together
south_africa_weekly_deaths <- south_africa_weekly_total_deaths %>%
left_join(south_africa_weekly_covid_deaths) %>%
mutate(region_code = 0,
start_date = as.Date(ISOdate(year-1, 12, 31)) + (week*7) - 6,
end_date = start_date + 6) %>%
ungroup() %>%
dplyr::select(country,region,region_code,start_date,end_date,year,week,
population,total_deaths,covid_deaths,expected_deaths) %>%
drop_na()
# Export as CSV
write.csv(south_africa_weekly_deaths %>%
mutate(start_date = format(start_date, "%Y-%m-%d"),
end_date = format(end_date, "%Y-%m-%d")),
"output-data/historical-deaths/south_africa_weekly_deaths.csv",
fileEncoding = "UTF-8",
row.names=FALSE)
# Step 20: import and clean Spain's data ---------------------------------------