-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreproduce_results_revisions.R
1390 lines (1272 loc) · 70.4 KB
/
reproduce_results_revisions.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
# This script is for reproducing the results and figures of the manuscript at [...]
# Mihaly Koltai, May/2022
####
# clear workspace
rm(list=ls())
# To set the path we need the "here" package
if (!any(row.names(installed.packages()) %in% "here")) {install.packages("here")}; library(here)
# load constant parameters and functions for simulations, specify folder where inputs are stored
source(here("load_params.R"))
foldername <- "repo_data/"; figs_folder <- "repo_data/FIGS"
# NPI dates
npi_dates<-as.Date(c("2020-03-26","2021-05-17"))
# set up the table of parameter vectors by Latin Hypercube Sampling (LHS)
new_partable=F
if (new_partable){
source("fcns/create_lhs_partable.R")
} else {
partable <- read_csv("repo_data/partable_full_lhs.csv")
}
# check the size of objects (>x Mb) in the workspace by: fcn_objs_mem_use(min_size=1)
# start date of saved simulations
start_date_dyn_save <- "2016-09-01"
# SI FIG 2: Plot susceptibility as a function of age and exposure
source("fcns/create_suscept_plot.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# RUN SIMULATIONS with parallelisation (requires multiple cores)
# This code block does not have to be re-run as results are available:
# go down to RESULTS and read in the results from the parameter sampling
#
# # `start_date_dyn_save` will define the timepoint from which to save simulation outputs
# # `simul_length_yr` is the length of simulations (in years), `n_post_npi_yr` is the number of years after NPIs
# # `n_core`: number of cores used, `memory_max`: allocated memory (GB)
# # start_date_dyn_save: date to save results from
# simul_length_yr <- 30; n_post_npi_yr<-4; n_core<-64; memory_max <- 8; start_date_dyn_save <- "2016-09-01"
# contact_red<-0.9
# agegroup_res<-"broad_age" # this if summary stats should be saved in 0-1, 1-2, 2-5, 65+ groups only
# # these are parameters selected by criteria of 1) attack rates 2) seasonal concentration of cases
# partable_filename <- "repo_data/partable_full_lhs.csv"; n_row <- nrow(read_csv(partable_filename))
# # we split the parameter table into `n_core` batches and run them in parallel,
# # the sh file will launch the jobs
# # write the files launching jobs
# command_print_runs <- paste0(c("Rscript fcns/write_run_file.R",n_core,n_row,
# simul_length_yr,n_post_npi_yr,contact_red,
# partable_filename,"SAVE sep_qsub_files",start_date_dyn_save,
# agegroup_res,memory_max),collapse=" ")
# # run this command by:
# system(command_print_runs)
# # run calculation (on a cluster, requires multiple cores) by:
# sh master_start.sh
####################################
# Simulations are launched by the file "fcns/parscan_runner_cmd_line.R"
# receiving command line arguments
# from the parameter table and calling the function
# `sirs_seasonal_forc_mat_immun` that generates & solves ODEs.
# The fcn for `sirs_seasonal_forc_mat_immun` is in "fcns/essential_fcns.R".
# The structure of the force of infection is built outside this function
# and passed as a set of inputs: the input `contmatr_rowvector`
# is the contact matrix stacked 3x times (bc of 3 levels of infection)
# and normalised by population sizes to generate the force of infection
# terms normalised by age group sizes.
# See SI Methods
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# RESULTS
# results from the parameter sampling after filtering already stored in repo_data/
# starting date of simulations
# this is a ZIP file in the folder, first unzip:
# unzip("repo_data/results_summ_all.zip",exdir = "repo_data/")
results_summ_all <- read_csv("repo_data/results_summ_all.csv")
# param sets filtered out bc of attack rates or seasonal concentr
AR_crit=8; seas_conc_crit=AR_crit; seas_share_cutoff=85/100
# score: how many age groups satisfy
fullscan_score_AR_seasconc <- left_join(
results_summ_all %>%
filter(epi_year %in% c(2017,2018)) %>%
group_by(par_id,agegroup) %>%
summarise(attack_rate_perc=mean(attack_rate_perc),
seas_share=mean(seas_share)),
estim_attack_rates %>%
select(!c(attack_rate,sympt_attack_rate,
n_test,RSV_posit,RSV_sympt_posit)) %>%
mutate(agegroup=as.numeric(factor(agegroup_name,
levels=agegroup_name))),by="agegroup") %>%
mutate(attack_rate_check=(attack_rate_perc>=min_est &
attack_rate_perc<=max_est),
seas_share_check=(seas_share>=seas_share_cutoff)) %>%
group_by(par_id) %>%
summarise(n_attack_rate_check=sum(attack_rate_check),
n_seas_share_check=sum(seas_share_check))
# summary stats of filtering (how many accepted/rejected and by what criteria)
filtering_summ_stats <- fullscan_score_AR_seasconc %>%
mutate(attack_rate_fail=n_attack_rate_check<AR_crit,
seas_conc_fail=n_seas_share_check<seas_conc_crit,
both_fail=attack_rate_fail&seas_conc_fail) %>%
summarise(n_both_fail=sum(both_fail),
n_attack_rate_fail=sum(attack_rate_fail)-n_both_fail,
n_seas_conc_fail=sum(seas_conc_fail)-n_both_fail,
n_accepted=sum(!attack_rate_fail&!seas_conc_fail))
# filter for parameter sets where x/11 age groups satisfy criteria
# for attack rates and seasonal concentration
sel_yrs<-2019; n_sel_yr=length(sel_yrs)
# parameter sets where x/11 age-groups satisfy the AR and seasonal concentr criteria
parsets_AR_seas_share <- (fullscan_score_AR_seasconc %>%
filter(n_attack_rate_check>=AR_crit &
n_seas_share_check>=seas_conc_crit))$par_id
all_sum_inf_epiyear_age_filtered <- results_summ_all %>% filter(par_id %in% parsets_AR_seas_share)
# this leads to ~4.5e3 out of 2e4 parameter sets:
length(unique(all_sum_inf_epiyear_age_filtered$par_id))
# select parameter sets matching the first two criteria
create_AR_seas_filtered_partable=T
if (create_AR_seas_filtered_partable){
partable_filtered_AR_seasconc <- partable %>% filter(par_id %in% parsets_AR_seas_share)
write_csv(partable_filtered_AR_seasconc,here(foldername,"partable_filtered_AR_seasconc.csv"))
} else {
partable_filtered_AR_seasconc <- read_csv(here("repo_data/partable_filtered_AR_seasconc.csv"))
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# SI Figure 14: plot total infections in epi-years 2017,17,18 compared to 2019
# this is to see if seasonality is annual or
# there are large differences between years for some parameter sets
# Plot cumul incid (relative to 2019) of accepted parsets
results_summ_all %>%
filter(epi_year<2020 &
par_id %in% unique(all_sum_inf_epiyear_age_filtered$par_id)) %>%
mutate(agegroup_name=factor(rsv_age_groups$agegroup_name[agegroup],
levels=rsv_age_groups$agegroup_name)) %>%
group_by(par_id,agegroup_name) %>%
mutate(inf_in_seas=inf_in_seas/(inf_in_seas[epi_year==2019])) %>% # *1+(1/26)
filter(epi_year<2019) %>%
ggplot() +
geom_jitter(aes(x=factor(epi_year),y=inf_in_seas,group=par_id),alpha=1/8,size=1/2) +
facet_wrap(~agegroup_name,scales = "free") + scale_y_log10() +
xlab("epi-year") + ylab("number of infections compared to 2019 (1=2019 level)") +
theme(legend.position="top") + theme_bw() + standard_theme
# save
# ggsave(here(figs_folder,"2020_RSV_check_log.png"),width=32,height=20,units="cm")
# Biennial patterns? Number of param sets where cumul incidence is within 15% of 2019 level (or not)
irreg_toler=0.15; scaling_2019=1+1/26
results_summ_all %>%
filter(epi_year<2020 &
par_id %in% unique(all_sum_inf_epiyear_age_filtered$par_id)) %>%
mutate(agegroup_name=factor(rsv_age_groups$agegroup_name[agegroup],
levels=rsv_age_groups$agegroup_name)) %>%
group_by(par_id,agegroup_name) %>%
mutate(inf_in_seas=inf_in_seas/(scaling_2019*inf_in_seas[epi_year==2019])) %>%
filter(epi_year<2019) %>% group_by(par_id,epi_year) %>%
summarise(regular_by_agegr=sum(inf_in_seas>1-irreg_toler &
inf_in_seas<1+irreg_toler)) %>%
group_by(epi_year) %>%
summarise(regular=sum(regular_by_agegr>9),irregular=n()-regular)
# parsets with regular (annual) vs irregular patterns
reg_irreg_parsets <- results_summ_all %>%
filter(epi_year<2020 & par_id %in% parsets_AR_seas_share) %>%
group_by(par_id,agegroup) %>%
mutate(inf_in_seas=inf_in_seas/(scaling_2019*inf_in_seas[epi_year==2019])) %>%
filter(epi_year<2019) %>% group_by(par_id,epi_year) %>%
summarise(regular_by_agegr=sum(inf_in_seas>1-irreg_toler &
inf_in_seas<1+irreg_toler)) %>%
group_by(par_id) %>% summarise(regular=all(regular_by_agegr==11))
# number of regular/irregular
reg_irreg_parsets %>% summarise(regular=sum(regular),irreg=n()-regular)
# SI Figure 6: CDF of percentage deviation from 2019
results_summ_all %>%
filter(epi_year<2020 &
par_id %in% unique(all_sum_inf_epiyear_age_filtered$par_id)) %>%
mutate(agegroup_name=factor(rsv_age_groups$agegroup_name[agegroup],
levels=rsv_age_groups$agegroup_name)) %>%
group_by(par_id,agegroup_name) %>%
mutate(inf_in_seas=inf_in_seas/(scaling_2019*inf_in_seas[epi_year==2019])) %>%
filter(epi_year<2019) %>%
select(par_id,agegroup,inf_in_seas) %>%
mutate(inf_in_seas=abs(1-inf_in_seas)) %>%
ggplot(aes(inf_in_seas)) + stat_ecdf(geom="step") +
facet_wrap(~agegroup,nrow=2,labeller=labeller(agegroup=label_both)) +
geom_vline(xintercept=0.15,linetype="dashed",size=1/2,color="red") +
scale_x_log10(limits=c(0.001,1)) + # ,expand=expansion(0.02,0)
xlab("relative difference in incidence") + ylab("CDF") + labs(color="agegroups")+
theme_bw() + standard_theme +
theme(legend.position="top",strip.text=element_text(size=14),
legend.title=element_text(size=15),legend.text=element_text(size=14),
axis.text.x=element_text(size=12),axis.text.y=element_text(size=12),
axis.title.x=element_text(size=16))
# save
# ggsave(here(figs_folder,"interyear_difference_cumul_incid_reg_dyn.png"),
# width=35,height=25,units="cm")
ggsave(here(figs_folder,"SI_FIG7.png"),width=35,height=25,units="cm")
# they haven't settled or stable biennial? read in one of the dynamic batches to check
# unzip first:
# unzip("repo_data/dyn_parsets_main1_324.zip",exdir="repo_data/")
dyn_parsets_main1_324 <- read_csv("repo_data/dyn_parsets_main1_324.csv") %>%
filter(par_id %in% unique(all_sum_inf_epiyear_age_filtered$par_id)) %>%
mutate(date=as.Date("2016-09-01")+t-min(t)) %>% filter(date<as.Date("2020-04-01"))
# plot
ggplot(dyn_parsets_main1_324 %>% filter(date<as.Date("2020-04-01") &
par_id %in% reg_irreg_parsets$par_id & agegroup==1)) +
geom_line(aes(x=date,y=value),alpha=1/2) + facet_wrap(~par_id,scales="free_y") +
xlab("") + ylab("infections") + theme_bw() + standard_theme
# curves look stabilised, but let's also calculate
# % difference in dynamic curves, calculated as: sum(abs(diff(2016,2018)))/sum(mean(2016,2018))
# and same for 2017/2019
interyr_diff_cumul_incid <- left_join(
dyn_parsets_main1_324 %>% filter(date<as.Date("2020-04-01")),
reg_irreg_parsets) %>%
mutate(week=isoweek(date)) %>% filter(week>=35|week<=9) %>%
mutate(epi_year=ifelse((week>=35 & yday(date)>245) |
yday(date)>245,year(date),year(date)-1)) %>%
filter(epi_year>=2016) %>%
group_by(agegroup,epi_year,par_id,regular) %>%
arrange(epi_year,date) %>%
mutate(epi_day=row_number()) %>% filter(epi_day<=180) %>%
group_by(par_id,epi_day,agegroup,regular) %>%
summarise(`diff_2016_18`=abs(value[epi_year==2016]-value[epi_year==2018]),
`diff_2017_19`=abs(value[epi_year==2017]-value[epi_year==2019]),
mean_2016_18=mean(value[epi_year %in% c(2016,2018)]),
mean_2017_19=mean(value[epi_year %in% c(2017,2019)])) %>%
group_by(agegroup,par_id,regular) %>%
summarise(rel_diff_16_18=sum(diff_2016_18)/sum(mean_2016_18),
rel_diff_17_19=sum(diff_2017_19)/sum(mean_2017_19)) %>%
group_by(par_id,regular) %>%
summarise(rel_diff_16_18=mean(rel_diff_16_18)*100,
rel_diff_17_19=mean(rel_diff_17_19)*100) %>%
group_by(regular) %>%
summarise(perc_diff_16_18=mean(rel_diff_16_18),
perc_diff_17_19=mean(rel_diff_17_19))
# all under 1%, on average under 0.8% difference, so simulations have stabilised
# SI Fig 5: Plot curves comparing 2016 to 2018 and 2017 to 2019
left_join(dyn_parsets_main1_324 %>% filter(date<as.Date("2020-04-01")),
reg_irreg_parsets) %>%
mutate(week=isoweek(date)) %>% filter(week>=35|week<=9) %>%
mutate(epi_year=ifelse((week>=35 & yday(date)>245) |
yday(date)>245,year(date),year(date)-1)) %>%
filter(epi_year>=2016) %>%
group_by(agegroup,epi_year,par_id,regular) %>%
arrange(epi_year,date) %>% mutate(epi_day=row_number()) %>%
filter(epi_day<=180 & agegroup==2 & !regular) %>%
mutate(year_type=epi_year %% 2) %>% group_by(year_type) %>%
mutate(year_rank=ifelse(epi_year==min(epi_year),"even","odd")) %>%
ggplot(aes(x=epi_day,y=value,color=factor(epi_year),
group=epi_year,linetype=factor(year_rank))) +
geom_line(size=1.2) + facet_wrap(~par_id,scales="free_y") +
xlab("day of season") + ylab("# infections") + labs(linetype="",color="epi-year") +
theme_bw() + standard_theme + manuscript_large_font_theme + theme(strip.text=element_text(size=13.5))
# save
ggsave(here(figs_folder,"SI_FIG6.png"),width=35,height=25,units="cm")
# ggsave(here(figs_folder,"interyear_difference_regular_parsets.png"),
# width=30,height=25,units="cm")
# Plot again the comparison to 2019 for regular parameter sets only
results_summ_all %>%
filter(epi_year<2020 & par_id %in% parsets_AR_seas_share) %>%
filter(par_id %in% reg_irreg_parsets$par_id[reg_irreg_parsets$regular]) %>%
mutate(agegroup_name=factor(rsv_age_groups$agegroup_name[agegroup],
levels=rsv_age_groups$agegroup_name)) %>%
group_by(par_id,agegroup_name) %>%
mutate(inf_in_seas=inf_in_seas/(scaling_2019*inf_in_seas[epi_year==2019])) %>%
filter(epi_year<2019) %>%
ggplot() + geom_jitter(aes(x=factor(epi_year),y=inf_in_seas,group=par_id),alpha=1/8) +
facet_wrap(~agegroup_name) + xlab("epi-year") + ylab("number of infections (1=2019 level)") +
theme(legend.position="top") + theme_bw() + standard_theme
# save
# ggsave(here(figs_folder,"/cumul_incid_compared2019_by_agegrs.png"),
# width=30,height=25,units="cm")
# keep only parameters with regular annual patterns
regular_dyn_parset_load <- T
if (!regular_dyn_parset_load){
partable_regular_dyn <- partable_filtered_AR_seasconc %>%
filter(par_id %in% reg_irreg_parsets$par_id[reg_irreg_parsets$regular])
# write_csv(partable_regular_dyn,"simul_output/2e4_parsets/crit_8_11/partable_regular_dyn.csv")
# keep outputs with correct attack rate
results_summ_all_reg_dyn <- results_summ_all %>% filter(par_id %in% partable_regular_dyn$par_id)
# write_csv(results_summ_all_reg_dyn,"repo_data/results_summ_all_reg_dyn_FULL.csv")
} else {
# unzip("results_summ_all_reg_dyn.zip",exdir="repo_data/")
results_summ_all_reg_dyn <- read_csv(here(foldername,"results_summ_all_reg_dyn.csv"))
partable_regular_dyn <- read_csv(here(foldername,"partable_regular_dyn.csv"))
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# After filtering by attack rate and seasonal concentration
# we get full dynamics of *selected* parsets for likelihood calculations
# no need to read these in as further down we can just read in the dataframes
# with the likelihood calculations already done
# accepted parsets
need_full_dynamics_files <- F
create_dynamics_df <- F
if (need_full_dynamics_files){
if (create_dynamics_df) {
source("fcns/create_dynamics_df.R")
} else {
# download these two files from
# https://drive.google.com/file/d/1PAO0OjaI-MFugKwZVrD7dLFTktszi-1p/view?usp=sharing
# and unzip
all_dynamics_accepted <- read_csv(here(foldername,"all_dynamics_accepted_2e3.csv")) %>%
mutate(date=as.Date(start_date_dyn_save)+t-min(t)) %>%
filter(date>=as.Date("2017-09-25") & date<as.Date("2024-10-01"))
all_dynamics_rejected <- read_csv(here(foldername,"all_dynamics_rejected_2e3.csv")) %>%
mutate(date=as.Date(start_date_dyn_save)+t-min(t)) %>%
filter(date>=as.Date("2017-09-25") & date<as.Date("2024-10-01"))
gc()
# remove timepoints after SARI-Watch data
# all_dynamics_accepted <- all_dynamics_accepted %>% filter(date<=as.Date("2020-05-11"))
# all_dynamics_rejected <- all_dynamics_rejected %>% filter(date<=as.Date("2020-05-11"))
}
}
# load RSV hospitalisation counts
source("fcns/load_SARI_data.R")
# dataframes created:
# SARIwatch_RSVhosp_under5_2018_2020_weekly_counts, SARIwatch_RSVhosp_over65_2018_2020_weekly_counts
# under-reporting: under_report_factor_under5, under_report_factor_over65y
# calculate weekly hospitalisations of accepted parsets
# (leave `create_weekly_hosp_df` as FALSE, you can just read in the file instead)
create_weekly_hosp_df = F
if (create_weekly_hosp_df){
# create dataframe of weekly hospitalisations,
# by broad age groups (0-1, 1-2, 2-5, 65+) and by <5 and 65+:
# simul_hosp_rate_weekly_all_broad_agegroups | simul_hosp_rate_weekly_under5_over65
# this takes ~2 mins for 4e3 param sets
source("fcns/create_weekly_hosp_df.R")
} else {
# unzip: unzip("repo_data/simul_hosp_rate_weekly_LLH.zip",exdir="repo_data/")
simul_hosp_rate_weekly_SARIdates_LLH <- read_csv("repo_data/simul_hosp_rate_weekly_LLH.csv") %>%
mutate(year_week=factor(year_week,unique(year_week)))
}
# `simul_hosp_scaled` means that simulated hospitalisations were scaled by the under-reporting factor
# calculate likelihoods by parameter set (accepted/rejected)
hosp_dyn_likelihoods <- simul_hosp_rate_weekly_SARIdates_LLH %>%
group_by(par_id,accepted,broad_age) %>%
summarise(sum_neg_llh=unique(sum_neg_llh)) %>%
group_by(par_id,accepted) %>% mutate(sum_neg_llh_all=sum(sum_neg_llh)) %>%
pivot_longer(!c(par_id,accepted,broad_age)) %>%
mutate(broad_age=ifelse(grepl("all",name),"all",broad_age )) %>%
select(!name) %>% distinct()
# calculate likelihoods for attack rates
create_rejected_summary_stats=F
if (create_rejected_summary_stats){
results_summ_rejected <- results_summ_all %>%
filter(!(par_id %in% unique(partable_regular_dyn$par_id)) )
# save
# write_csv(results_summ_rejected,"simul_output/2e4_parsets/crit_8_11/results_summ_rejected.csv")
} else {
# unzip first:
unzip("repo_data/results_summ_rejected.zip",exdir="repo_data/")
results_summ_rejected <- read_csv("repo_data/results_summ_rejected.csv")
}
likelihoods_attackrates <- bind_rows(
results_summ_all_reg_dyn %>%
filter(par_id %in% unique(hosp_dyn_likelihoods$par_id)) %>% mutate(accepted=TRUE),
results_summ_rejected %>% mutate(accepted=FALSE)) %>%
filter(epi_year<2019) %>%
select(c(epi_year,par_id,agegroup,inf_in_seas_AR,attack_rate_perc,accepted)) %>%
mutate(AR_log_binom_LLH=dbinom(x=estim_attack_rates$RSV_sympt_posit[agegroup],
size=estim_attack_rates$n_test[agegroup],
prob=attack_rate_perc/100,log=T)) %>%
group_by(par_id,accepted) %>%
summarise(AR_negLLH_binom=-sum(AR_log_binom_LLH))
# sum of all likelihoods
all_likelihoods = left_join(
hosp_dyn_likelihoods %>%
filter(!broad_age %in% "all") %>%
pivot_wider(names_from="broad_age",
values_from="value",names_prefix="LLH from hospit. "),
likelihoods_attackrates) %>%
mutate(`complete likelihood`=`LLH from hospit. <5y` +
`LLH from hospit. >65y`+AR_negLLH_binom) %>%
rename(`LLH from attack rates`=AR_negLLH_binom) %>%
pivot_longer(!c(par_id,accepted))
# SAVE
# write_csv(all_likelihoods,"repo_data/all_likelihoods.csv")
# or just load it:
# all_likelihoods <- read_csv("repo_data/all_likelihoods.csv")
# SI FIG4A plot as jitter plot
ggplot(all_likelihoods,aes(x=accepted,y=value,color=accepted)) +
geom_jitter(width=0.4,alpha=1/3) + # geom_violin(fill=NA,show.legend=F) +
geom_boxplot(fill=NA,width=0.88,size=3/4,outlier.colour=NA,color="black") + #
facet_wrap(~name,scales="free_y",nrow=1) + scale_y_log10() +
scale_color_manual(values=c("darkgrey","blue")) + # labs(color="accepted") + # coord_fixed(ratio=3) +
xlab("") + ylab("negative log-likelihood") + theme_bw() + standard_theme + # element_text(size=14)
theme(strip.text=element_text(size=15),
axis.text.x=element_blank(),axis.text.y=element_text(size=15),
axis.title.y=element_text(size=20),
legend.position="top",legend.title=element_text(size=17),
legend.text=element_text(size=16)) # + manuscript_large_font_theme
# save
# ggsave(here(figs_folder,"all_LLH_accepted_rejected_boxplot.png"),
# width=35,height=22,units="cm")
ggsave(here(figs_folder,"SI_FIG4A.png"),width=30,height=25,units="cm")
# density plot
median_llh = all_likelihoods %>% group_by(accepted,name) %>%
summarise(median_llh=median(value,na.rm=T))
ggplot(all_likelihoods,aes(x=value,color=accepted)) +
geom_density(aes(y=..scaled..)) + # aes(y=..scaled..) # aes(y=..count..)
geom_vline(data=median_llh,aes(xintercept=median_llh,color=accepted),
linetype="dashed",size=1/2,show.legend=F) +
geom_text(data=median_llh,show.legend=F,
aes(x=median_llh*0.88,y=1/8,color=accepted,label=round(median_llh))) +
facet_wrap(~name,scales="free",nrow=3) + scale_x_log10(expand=expansion(0.01,0)) +
scale_color_manual(values=c("black","blue")) + labs(color="accepted") +
xlab("negative log-likelihood") + ylab("density") +
theme_bw() + standard_theme + manuscript_large_font_theme +
theme(axis.title.x=element_text(size=20),axis.title.y=element_text(size=20),
legend.title=element_text(size=18))
# save
# ggsave(here(figs_folder,"all_LLH_accepted_rejected_density.png"),width=35,height=22,units="cm")
ggsave(here(figs_folder,"SI_FIG4B.png"),width=30,height=25,units="cm")
# SI FIG4B: cumulative density of parsets up to LLH<x
ggplot(all_likelihoods) + stat_ecdf(aes(x=value,color=accepted),geom="step") +
facet_wrap(~name,scales="free",nrow=3) +
geom_segment(data=median_llh,
aes(x=median_llh,xend=median_llh,y=0,yend=1/2,color=accepted),
linetype="dashed",size=1/2,show.legend=F) +
geom_text(data=median_llh,show.legend=F,
aes(x=median_llh*1.12,y=0.44,color=accepted,label=round(median_llh))) +
scale_x_log10(expand=expansion(0.01,0)) + scale_y_continuous(expand=expansion(0.01,0)) +
scale_color_manual(values=c("black","blue")) + labs(color="accepted") +
xlab("negative log-likelihood") + ylab("cumulative density function") +
theme_bw() + standard_theme +
theme(legend.position="top",strip.text=element_text(size=14),
legend.title=element_text(size=15),legend.text=element_text(size=14),
axis.text.x=element_text(size=12),axis.text.y=element_text(size=12),
axis.title.x=element_text(size=16))
# SAVE
# ggsave(here(figs_folder,"all_LLH_accepted_rejected_CDF.png"),
# width=35,height=22,units="cm")
ggsave(here(figs_folder,"SI_FIG4B.png"),width=35,height=22,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# SI FIG3B
# distribution of attackrates and seasonal concentration for
# simulations with <3000 neg log-LLH
bind_rows(results_summ_rejected,
results_summ_all_reg_dyn) %>%
filter( (par_id %in% (all_likelihoods %>%
filter((name %in% "complete likelihood") & value<3e3))$par_id) &
epi_year<2019) %>%
select(c(par_id,agegroup,attack_rate_perc,seas_share)) %>%
mutate(accepted=
ifelse(par_id %in% unique(all_likelihoods$par_id[all_likelihoods$accepted]),
T,F)) %>%
pivot_longer(!c(par_id,agegroup,accepted)) %>%
mutate(name=ifelse(name %in% "attack_rate_perc",
"attack rate (%)",
"seasonal concentration (%)")) %>%
ggplot() +
geom_jitter(aes(x=factor(agegroup),y=ifelse(grepl("seas",name),value*100,value),
color=accepted,group=accepted),alpha=1/4,
position=position_jitterdodge(dodge.width=0.9,jitter.width=0.35)) +
geom_vline(xintercept=(1:10)+1/2,size=1/2) +
geom_segment(data=estim_attack_rates %>%
mutate(agegroup=row_number(),sympt_attack_rate=100*sympt_attack_rate) %>%
select(c(agegroup,min_est,max_est,sympt_attack_rate)) %>%
pivot_longer(!agegroup) %>%
mutate(type=name,name="attack rate (%)"),
aes(x=agegroup-1/2,xend=agegroup+1/2,y=value,yend=value,group=name,
linetype=ifelse(type %in% "sympt_attack_rate","solid","dashed")),
size=1/3,show.legend=F,color="red") +
geom_hline(aes(yintercept=ifelse(grepl("seas",name),85,NA)),linetype="dashed",color="red") +
facet_wrap(~name,scales="free_y",nrow=2) + xlab("") + ylab("") +
scale_x_discrete(expand=expansion(0,0)) + scale_y_log10() +
scale_color_manual(values = c("darkgrey","blue")) + theme_bw() + standard_theme +
theme(strip.text=element_text(size=15), # axis.text.x=element_blank(),
axis.text.y=element_text(size=15),axis.title.y=element_text(size=18),
legend.position="top",legend.title=element_text(size=15),
legend.text=element_text(size=14))
# SAVE
# ggsave(here(figs_folder,"goodfits_negLLH_below1500_filtered.png"),
# width=35,height=20,units="cm")
ggsave(here(figs_folder,"SI_FIG3B.png"),width=35,height=20,units="cm")
# dynamics
goodfit_pars <- (all_likelihoods %>% filter((name %in% "complete likelihood") & value<1.5e3))$par_id
sari_hosp_data_joint = simul_hosp_rate_weekly_SARIdates_LLH %>% ungroup() %>%
select(c(year_week,casesunder5total,cases65plustotal,broad_age,year)) %>%
distinct() %>% pivot_longer(!c(year_week,broad_age,year)) %>%
filter(!(broad_age %in% "<5y" & name %in% "cases65plustotal")) %>%
filter(!(broad_age %in% ">65y" & name %in% "casesunder5total"))
# plot
simul_hosp_rate_weekly_SARIdates_LLH %>%
select(c(par_id,accepted,broad_age,simul_hosp_scaled,year_week,year)) %>%
filter(par_id %in% goodfit_pars) %>%
mutate(year_week=factor(year_week,levels=unique(year_week))) %>%
ggplot(aes(x=year_week)) +
geom_line(aes(y=simul_hosp_scaled,color=accepted,group=par_id),alpha=1/2) +
geom_point(data=sari_hosp_data_joint,aes(y=value),size=2.5) +
geom_line(data=sari_hosp_data_joint,aes(y=value,group=1),linetype="dashed",size=1/2) +
scale_color_manual(values=c("grey","blue")) +
scale_x_discrete(breaks=show_every_nth(n=2)) +
facet_grid(broad_age~year,scales="free") +
xlab("") + ylab("hospitalisations (count)") +
theme_bw() + standard_theme + manuscript_large_font_theme
# save
# ggsave(here(figs_folder,"goodfits_negLLH_below1e3_filtered.png"),width=35,height=16,units="cm")
ggsave(here(figs_folder,"SI_FIG4C.png"),width=35,height=25,units="cm")
### ### ### ### ### ### ### ### ### ### ###
# FIG 1C
# Plot showing why low neg-LLH parsets are filtered out
x_dodge=1/5
all_crit_summarised =
left_join(
left_join(
left_join(bind_rows(results_summ_all_reg_dyn %>% mutate(accepted=T),
results_summ_rejected %>% mutate(accepted=F)) %>%
filter(epi_year<2020) %>%
select(c(par_id,agegroup,attack_rate_perc,seas_share,accepted)),
reg_irreg_parsets),
fullscan_score_AR_seasconc) %>% group_by(par_id) %>%
mutate(seas_share=mean(seas_share)) %>%
select(!c(agegroup,attack_rate_perc)) %>% distinct(),
all_likelihoods %>%
filter(name %in% "complete likelihood") %>% select(!name) %>% rename(negLLH=value)) %>%
mutate(x_pos_AR=ifelse(accepted,
n_attack_rate_check+x_dodge+runif(1,min=-1/7,max=1/7),
n_attack_rate_check-x_dodge+runif(1,min=-1/7,max=1/7)))
# if sampling 1000-1000 of all parsets
# sample_pars <- c(sample(all_crit_summarised$par_id[all_crit_summarised$accepted],size=1000),
# sample(all_crit_summarised$par_id[!all_crit_summarised$accepted],size=1000))
# plot
ggplot(all_crit_summarised) + # %>% filter(par_id %in% sample_pars)
geom_point(aes(x=x_pos_AR,y=negLLH,fill=ifelse(accepted,"accepted","rejected")),
alpha=0.4,shape=21,stroke=0,size=3) +
# put a cross (x) if attack rates <80% correct
geom_point(data=all_crit_summarised %>% filter(n_seas_share_check<8),color="black",
aes(x=x_pos_AR,y=negLLH,shape="seasonal concentration\n <8/11 correct")) +
# red circle if seasons irregular
geom_point(data=all_crit_summarised %>% filter(!regular),
aes(x=x_pos_AR,y=negLLH,color="biennial/irregular seasons"),
shape=21,fill=NA,stroke=2/3,size=2.5) +
scale_fill_manual(values=c("accepted"="blue","rejected"="darkgrey"),
guide=guide_legend(nrow=2,byrow=TRUE,
override.aes=list(color=NA,stroke=NA))) +
scale_color_manual(values=c("biennial/irregular seasons"="red"),
guide=guide_legend(override.aes=list(size=4,stroke=1.2))) +
scale_shape_manual(values=c("seasonal concentration\n <8/11 correct"=4),
guide=guide_legend(override.aes=list(size=3))) +
geom_vline(xintercept=c(5:6,8:10)+1/2,size=1/2,linetype="dashed") +
geom_vline(xintercept=7.5,size=1/2) +
scale_x_continuous(breaks=1:11,expand=expansion(0.01,0),limits=c(4.6,11.36)) +
scale_y_log10(limits=c(720,1e4)) +
xlab("age groups with correct attack rates") + ylab("Negative log-likelihood") +
labs(fill="",color="",shape="",size="correct \nattack rates")+
theme_bw() + standard_theme +
theme(strip.text=element_text(size=15),
axis.text.x=element_text(size=15),axis.text.y=element_text(size=15),
axis.title.x=element_text(size=18),axis.title.y=element_text(size=18),
legend.position="top",legend.title=element_text(size=15),
legend.text=element_text(size=15))
# save
ggsave(here(figs_folder,"FIG1C.png"),width=35,height=16,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# SI FIGURE 3A: simulated attack rates vs LIT estimates
attack_rate_limits <- estim_attack_rates %>%
mutate(agegroup=row_number(),sympt_attack_rate=100*sympt_attack_rate) %>%
select(c(agegroup,min_est,max_est,sympt_attack_rate)) %>%
pivot_longer(!agegroup) %>%
mutate(type=name,name="attack_rate_perc")
# plot
bind_rows(results_summ_all_reg_dyn %>%
filter(par_id %in% sample(unique(par_id),size=5e2)) %>% mutate(accepted=T),
results_summ_rejected %>%
filter(par_id %in% sample(unique(par_id),size=5e2)) %>% mutate(accepted=F)) %>%
filter(epi_year<2020) %>%
select(c(par_id,agegroup,attack_rate_perc,accepted)) %>%
ggplot() +
geom_jitter(aes(x=factor(rsv_age_groups$agegroup_name[agegroup],
levels=unique(rsv_age_groups$agegroup_name)),
y=attack_rate_perc,color=accepted,group=accepted),
position=position_jitterdodge(dodge.width=0.9,jitter.width=0.4),
alpha=1/4,size=2/3) +
geom_vline(xintercept=(0:11)+1/2,size=1/2,color="grey") +
geom_segment(data=attack_rate_limits,
aes(x=agegroup-1/2,xend=agegroup+1/2,y=value,yend=value,group=name,
linetype=ifelse(type %in% "sympt_attack_rate","solid","dashed")),
show.legend=F,size=1/2,color="red") +
scale_y_log10(limits=c(0.1,110),expand=expansion(0.02,0)) +
scale_x_discrete(expand=expansion(0,0)) +
scale_color_manual(values=c("black","blue"),
guide=guide_legend(override.aes=list(size=3))) +
xlab("age group") + ylab("attack rate (%)") +
theme_bw() + standard_theme + theme(legend.position="top") + manuscript_large_font_theme
# SAVE
# ggsave(here(figs_folder,"attack_rate_comparison_with_lit.png"),width=25,height=20,units="cm")
ggsave(here(figs_folder,"SI_FIG3A.png"),width=25,height=20,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# SI FIG 4E
# distribution of all parameters separated by accepted/rejected
# jitter with boxplot
plot_partable_histogram = partable %>%
mutate(accepted=par_id %in% partable_regular_dyn$par_id) %>% select(!const_delta) %>%
mutate(`waning (days)`=1/omega,`peak R0`=R0*(1+seasforce_peak),
`maximal forcing (% above baseline)`=1e2*seasforce_peak) %>%
select(!c(omega,seasforce_peak,peak_week)) %>%
rename(`R0 (baseline)`=R0,
`age-dependence`=age_dep,`exposure-dependence`=exp_dep,
`season width (weeks)`=seasforc_width_wks) %>%
group_by(accepted) %>%
slice_sample(n=2e3) %>% pivot_longer(!c(par_id,accepted))
# median values
median_parvals_accept = plot_partable_histogram %>%
group_by(accepted,name) %>% summarise(median_parval=median(value))
# KS significance test
KS_test_accept = plot_partable_histogram %>% filter(!name %in% "peak forcing (week)") %>%
# select(!const_delta) %>% pivot_longer(!c(par_id,`early off season`)) %>%
group_by(name) %>%
summarise(min_val=min(value),max_val=max(value),
p_val=ks.test(x=value[accepted],y=value[!accepted])$p.value,
signif=p_val<0.01)
# SI FIG4D: CDF plot
ggplot(plot_partable_histogram %>%
filter(!grepl("maximal",name) & !grepl("baseline",name)),
aes(x=value,color=accepted)) +
stat_ecdf(geom="step") +
geom_vline(data=median_parvals_accept %>%
filter(!grepl("maximal",name) & !grepl("baseline",name)),
aes(xintercept=median_parval,color=accepted),
linetype="dashed",show.legend=F) +
geom_text(data=KS_test_accept %>% filter(!grepl("maximal",name) & !grepl("baseline",name)),
aes(x=max_val*0.9,y=0.1,label=paste0("p=",signif(p_val,3),ifelse(signif,"**","")) ),
color="black",size=6) +
facet_wrap(~name,scales="free",nrow=3) +
scale_color_manual(values=c("grey","blue")) + labs(color="accepted") +
xlab("") + ylab("density") + theme_bw() + standard_theme +
manuscript_large_font_theme
#
# ggsave("simul_output/2e4_parsets/crit_8_11/param_distrib_accept_CDF.png",width=28,height=18,units="cm")
ggsave(here(figs_folder,"SI_FIG4D.png"),width=28,height=18,units="cm")
# plot correlations btwn params
library(GGally)
ggpairs(plot_partable_histogram %>%
mutate(name=gsub("exposure","exp.",gsub("dependence","dep.",name)),
name=gsub("width","width\n",name), name=gsub("forcing","forcing\n",name)) %>%
filter(!grepl("maximal",name) & !grepl("baseline",name)) %>%
pivot_wider(names_from=name) %>%
select(!c(par_id)), # `R0 peak`,`peak forcing (week)`,`season width (weeks)`
columns=2:(length(unique(plot_partable_histogram$name))-1),
aes(color=accepted,alpha=1/4)) +
scale_color_manual(values=c("grey","blue")) + scale_fill_manual(values=c("grey","blue")) +
theme_bw() + standard_theme +
manuscript_large_font_theme + theme(strip.text=element_text(size=14))
# save
# ggsave("simul_output/2e4_parsets/crit_8_11/param_correlations.png",
# width=28,height=18,units="cm")
ggsave(here(figs_folder,"SI_FIG5.png"),width=28,height=20,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# distribution of accepted parameter sets in terms of age_dep-exp_dep
ggplot(partable_regular_dyn %>%
filter(par_id %in% sample(partable_regular_dyn$par_id,size=2e3))) +
geom_point(aes(x=age_dep,y=exp_dep),color="blue",alpha=1/2) +
scale_x_continuous(limits=c(0,0.4)) + scale_y_continuous(limits=c(0,5/4)) +
theme_bw() + standard_theme
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Entire range of outputs by each parameter (not in manuscript)
source("fcns/create_results_fullscan_hosp.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# calculate ranges: fix 4 params to the median,
# calculate the values at the min and max of the parameter that is varied
# take median parameter set, calculate the range in ONE parameter,
# for ALL param sets and for the accepted parsets
source("fcns/create_df_fullrange_plots.R")
# creates the dataframes: output_ranges_full_scan | mean_age_shift_ranges
# create plots (not included in manuscript)
source("fcns/create_fullrange_plots.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Sensitivity analysis: PRCC
#
library(epiR)
# dependencies: sf <--- units <-- libudunits2-dev (start from right)
# create dataframe of PRCC values
source("fcns/create_df_prcc.R")
# We do not use this PRCC calculation in the article, because here epi-year is defined as
# from week 40 to 39, but in 2021 out of season outbreaks often start before week 40
# plot cumul age and peak
bind_rows(list_prcc_output) %>%
filter((!name %in% "kappa") &
!grepl("mean_age",output) & (!agegroup_broad %in% "5+y")) %>%
rename(`age`=agegroup_broad) %>%
mutate(name=factor(name,levels=unique(name))) %>%
ggplot() + facet_wrap(~output,scales = "free_x") +
geom_col(aes(y=est,x=name,group=age,fill=age),
color="black",size=1/3,position=position_dodge(width=0.85),width=4/5) +
coord_flip() + xlab("") + ylab("PRCC") +
geom_vline(xintercept=1/2+1:5,size=1/3) + geom_hline(yintercept=0) +
theme_bw() + standard_theme + manuscript_large_font_theme +
theme(panel.grid.major.y=element_blank(),axis.text.y=element_text(size=16),
legend.text=element_text(size=16),legend.title=element_text(size=17))
# save
# ggsave(here(figs_folder,"PRCC_cumul_peak_hosp_under5.png"),width=28,height=20,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# outcomes by the value of parameters
create_summ_by_val=F
if (create_summ_by_val){
source("fcns/create_summ_broad_age_groups_byvalue.R")
# save
# write_csv(summ_broad_age_groups_byvalue,
# here("repo_data/summ_broad_age_groups_byvalue.csv"))
} else {
summ_broad_age_groups_byvalue <- read_csv(here(foldername,
"summ_broad_age_groups_byvalue.csv"))
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# PLOT summary stats disaggregated by parameter VALUES
# this function outputs Fig2B & SI FIG9
source("fcns/create_summary_plots_narrow_age_groups.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Evaluating the DYNAMICS
# Figure 1B: SARI-Watch hosp rates compared to simulations
date_limits <- as.Date(c("2017-09-15","2020-04-01"))
# take paramsets with <1500 negative-LLH
subsample_par = (all_likelihoods %>%
filter(name %in% "complete likelihood" & accepted & value<1500))$par_id
# load simulations
# unzip first
# unzip("repo_data/simul_hosp_rate_weekly_under5_over65_from2017.zip",exdir = "repo_data/")
simul_hosp_rate_weekly_under5_over65 <- read_csv("repo_data/simul_hosp_rate_weekly_under5_over65_from2017.csv")
# simul_hosp_rate_weekly_under5_over65: contains both accepted and rejected parsets
# simul_hosp_rate_weekly_under5_over65_grad_relax: only accepted parsets, with gradual relaxation
# sample trajectories
hosp_plot_df <- simul_hosp_rate_weekly_under5_over65 %>%
select(broad_age,par_id,date,simul_hosp_rate_100k) %>% ungroup() %>%
filter(broad_age %in% "<5y") %>%
mutate(sel_par=ifelse(par_id %in% partable_regular_dyn$par_id,"accepted","rejected")) %>%
# select best LLH params?
filter((par_id %in% subsample_par) | sel_par %in% "rejected") %>%
group_by(sel_par) %>% filter(par_id %in% sample(unique(par_id),size=100)) %>%
filter(date>=date_limits[1] & date<=date_limits[2])
# medians across simulated (accepted) parameter sets
# simul_hosp_rate_weekly_under5_over65 OR simul_hosp_rate_weekly_under5_over65_grad_relax
median_weekly_pred <- simul_hosp_rate_weekly_under5_over65 %>% ungroup() %>%
filter(par_id %in% hosp_plot_df$par_id[hosp_plot_df$sel_par %in% "accepted"]) %>%
ungroup() %>% group_by(date,year_week,broad_age) %>%
summarise(incid_hosp_med_val=median(simul_hosp_sum_full),
incid_hosp_per100k=median(simul_hosp_rate_100k),
incid_hosp_per100k_with_underrep=incid_hosp_per100k*ifelse(broad_age %in% "<5y",
under_report_factor_under5,under_report_factor_over65y) ) %>% distinct()
# data from SARI_watch
SARI_watch_under5y_hosp_rate <- left_join(
read_csv(here(foldername,"SARI_watch_under5y_hosp.csv")) %>%
mutate(year_week=gsub("-0","-",year_week)),
median_weekly_pred %>% select(date,year_week) %>% ungroup() %>% distinct()) %>%
relocate(date,.before=year_week)
# this data is under-reported, so simulations are scaled by under-reporting factor
# plot
ggplot() +
geom_line(data=hosp_plot_df,
aes(x=date,y=simul_hosp_rate_100k*under_report_factor_under5,
group=par_id,alpha=sel_par,color=sel_par)) +
scale_color_manual(values=c("blue","black")) + scale_alpha_manual(values=c(1/5,1/8)) +
geom_point(data=SARI_watch_under5y_hosp_rate %>%
filter(date>date_limits[1] & date<date_limits[2]),
aes(x=date,y=rate_under5yrs)) + # overlay data # ,shape=21,fill=NA,size=2
geom_line(data=median_weekly_pred %>%
filter(date>date_limits[1] & date<date_limits[2] & broad_age %in% "<5y"),
aes(x=date,y=incid_hosp_per100k_with_underrep),
color="red",linetype="dashed",size=1.02) + # median simulation
theme_bw() + standard_theme +
xlab("") + ylab("weekly hospitalisations <5y per 100.000 persons") + labs(alpha="",color="") +
scale_x_date(expand=expansion(1/100,0),date_breaks="2 months") +
scale_y_continuous(expand=expansion(0.01,0)) +
theme(strip.text=element_text(size=15),axis.text.x=element_text(size=13),
axis.text.y=element_text(size=12),legend.text=element_text(size=11),
legend.title=element_text(size=12),legend.position="none")
# SAVE
# ggsave(here(foldername,"prepandemic_dyn_compare_SARIwatch_under5_median_simul_LLH1500.png"),
# width=28,height=16,units="cm")
ggsave(here(figs_folder,"FIG1B.png"), width=28,height=16,units="cm")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# select params where off-season outbreak is earlier (as what was observed)
# these are from simulations where contact rates started to recover from March/2021
unzip("repo_data/simul_hosp_rate_weekly_under5_over65_grad_relax.zip",exdir = "repo_data/")
simul_hosp_rate_weekly_under5_over65_grad_relax <-
read_csv("repo_data/simul_hosp_rate_weekly_under5_over65_grad_relax.csv")
# calculate distance for 2020-2021
subsample_par = (all_likelihoods %>%
filter(name %in% "complete likelihood" & accepted & value<2e3))$par_id
hosp_plot_df = simul_hosp_rate_weekly_under5_over65_grad_relax %>%
select(broad_age,par_id,date,year_week,simul_hosp_rate_100k) %>% ungroup() %>%
filter(broad_age %in% "<5y") %>%
mutate(sel_par=ifelse(par_id %in% partable_regular_dyn$par_id,"accepted","rejected")) %>%
# select best LLH params?
filter((par_id %in% subsample_par) | sel_par %in% "rejected")
# we calculate euclidean distance from observed hosp rate in 2021-22
dist_hosp_2021_22 = left_join(
hosp_plot_df %>% filter(date>=as.Date("2021-01-01") &
date<=max(SARI_watch_under5y_hosp_rate$date)),
SARI_watch_under5y_hosp_rate,
by=c("date","year_week","broad_age")) %>%
mutate(abs_dist=abs(rate_under5yrs-simul_hosp_rate_100k),
sqrd_dist=abs_dist^2) %>%
group_by(par_id,broad_age) %>%
summarise(mean_abs_dist=mean(abs_dist),
mean_sqrd_dist=mean(sqrd_dist))
early_off_season = (dist_hosp_2021_22 %>%
filter(mean_sqrd_dist<=quantile(dist_hosp_2021_22$mean_sqrd_dist,probs=0.1)))$par_id
# median values
median_early_resurg = simul_hosp_rate_weekly_under5_over65_grad_relax %>% ungroup() %>%
filter(par_id %in% hosp_plot_df$par_id[hosp_plot_df$sel_par %in% "accepted"]) %>%
filter(par_id %in% early_off_season) %>%
ungroup() %>% group_by(date,year_week,broad_age) %>%
summarise(incid_hosp_med_val=median(simul_hosp_sum_full),
incid_hosp_per100k=median(simul_hosp_rate_100k),
incid_hosp_per100k_with_underrep=incid_hosp_per100k*ifelse(broad_age %in% "<5y",
under_report_factor_under5,under_report_factor_over65y) ) %>% distinct()
# plot param sets where off-season outbreak in 2021 is earlier (as it was in reality)
ggplot() +
geom_line(data=hosp_plot_df %>%
filter(par_id %in% early_off_season | sel_par %in% "rejected") %>%
group_by(sel_par) %>%
filter(par_id %in% sample(unique(par_id),size=length(early_off_season)) &
date<=as.Date("2022-04-01")),
aes(x=date,y=simul_hosp_rate_100k*under_report_factor_under5,
group=par_id,alpha=sel_par,color=sel_par)) +
geom_line(data=median_early_resurg %>%
filter(date>min(SARI_watch_under5y_hosp_rate$date) &
date<as.Date("2022-04-01") & broad_age %in% "<5y"),
aes(x=date,y=incid_hosp_per100k_with_underrep),
color="red",linetype="dashed",size=1.02) + # median simulation
scale_color_manual(values=c("blue","black")) +
scale_alpha_manual(values=c(1/5,1/8)) +
geom_point(data=SARI_watch_under5y_hosp_rate %>% filter(date>date_limits[1]),
aes(x=date,y=rate_under5yrs)) + # overlay data # ,shape=21,fill=NA,size=2
theme_bw() + standard_theme +
xlab("") + ylab("weekly hospitalisations <5y per 100.000 persons") + labs(alpha="",color="") +
scale_x_date(expand=expansion(1/100,0),date_breaks="2 months") +
scale_y_continuous(expand=expansion(0.01,0)) +
theme(strip.text=element_text(size=15),axis.text.x=element_text(size=13),
axis.text.y=element_text(size=12),legend.text=element_text(size=11),
legend.title=element_text(size=12),legend.position="none")
# SAVE
ggsave(here(figs_folder,"FIG4_main.png"),width=28,height=16,units="cm")
# create inset of FIG4
source("fcns/create_FIG4_inset.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# create summary dataframes of peak hospitalisations
source("fcns/create_summ_dyn_peak_cumul.R")
##############################################################
# plot PEAK VALUE of cases/hospitalisations (+ season length),
# disaggregated by parameter values
# SI Figure 8
# this file also saves the plots!
source("fcns/create_summ_plots_from_dyn.R")
##############################################################
# calculate PRCC values wrt parameters
source("fcns/create_df_prcc_from_dynamics.R")
# creates the list: list_prcc_dyn
# plot PRCC on cumulative/peak hospitalisations (not in manuscript)
source("fcns/create_prcc_plot_from_dyn.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Evaluating DYNAMICS as a function of parameters (relative timing of peak and duration/shape of season)
if (!exists("simul_hosp_rate_weekly_all_broad_agegroups")){
unzip("repo_data/simul_hosp_rate_weekly_all_broad_agegroups.zip",exdir = "repo_data/")
simul_hosp_rate_weekly_all_broad_agegroups <-
read_csv("repo_data/simul_hosp_rate_weekly_all_broad_agegroups.csv")
}
epi_year_week_start=23
# if dataframe starts with Autumn 2017!!!!
source("fcns/create_dyn_hosp_weekly_norm_to_peak.R")
#######
# calculate statistics at time points relative to peak week of selected reference year,
# for each bin of the selected parameters
# this takes about 1 min
create_df_dyn_relat_time=F
if (create_df_dyn_relat_time) {
source("fcns/create_dyn_wk_hosp_norm_time_norm_value.R")} else {
summ_dyn_all_parsets_broad_age_relat_time <-
read_csv("repo_data/summ_dyn_all_parsets_broad_age_relat_time.csv")
}
# this creates the dataframe `summ_dyn_all_parsets_broad_age_relat_time`
##############################################################
# Plot dynamics faceted by the age vs immunity dependence of susceptibility (Figure 3)
# FIGURE 4 and SI FIG : plot faceted by years, color-coded by parameter values
source("fcns/create_norm_dyn_plots.R")
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# DATA PLOTS (for SI)
# Plot UK RSV case data, calculate seasonal concentration
season_weeks=c(13,40)
resp_detects_weekly_all_age <-
read_csv(here(foldername,"Respiratory_viral_detections_by_any_method_UK.csv")) %>%
mutate(year_week=factor(paste0(Year,"-",Week),unique(paste0(Year,"-",Week))),
RSV_rolling_av=roll_mean(RSV,n=7,align="center",fill=NA) ) %>%
select(-(contains("virus")|contains("flu"))) %>%
mutate(epi_year=ifelse(Week>=season_weeks[2],Year+1,Year)-min(Year)+1) %>%
group_by(epi_year) %>% mutate(perc_yearly=RSV/sum(RSV)) %>% group_by(epi_year) %>%
mutate(season_share=sum(perc_yearly[Week>=season_weeks[2] | Week<=season_weeks[1]]),
on_off_season=ifelse(findInterval(Week,season_weeks+c(1,0))==1,"off","on"))
# SI Table 5
resp_detects_weekly_all_age_means_shares <- left_join(
resp_detects_weekly_all_age %>%
group_by(epi_year,on_off_season) %>%
summarise(mean_on_off=mean(RSV,na.rm=T),
cal_year=paste0(unique(Year),collapse="_")) %>%
filter(epi_year>1&epi_year<8) %>%
mutate(cal_year=ifelse(on_off_season %in% "off",
paste0(as.numeric(cal_year)-1,"_",
as.numeric(cal_year)),cal_year)) %>%
pivot_wider(names_from=on_off_season,values_from=mean_on_off,names_prefix="mean_"),
resp_detects_weekly_all_age %>%
group_by(epi_year) %>%
summarise(season_share=unique(season_share)),by="epi_year" ) %>%