-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmulti_city_voi.R
1411 lines (1142 loc) · 73.8 KB
/
multi_city_voi.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
#' Main script to run ITHIM Global in sampling mode
#'
#' Script to run ITHIM Global using input parameter distributions. Outputs the health impacts associated with transport in a given city
#' via an air pollution, physical activity and injury pathway. Also performs a Value of Information analysis to calculate the the
#' expected values of partially perfect information, i.e the reduction in variance in the outcome were we to know
#' an input parameter or several interdependent input parameters exactly.
#'
#' The ITHIM Global main sampling script works as follows:
#'
#' - the following variables need to be defined before running the script:
#' - the name(s) of the city or cities for which the model is to be run
#' - the number of samples to be drawn from the input parameters distributions which
#' equals the number of model runs
#' - whether a VoI analysis is to be performed, if yes, also set the following parameters:
#' - define the list of outcomes for which the VoI analysis is to be performed
#' - define whether you want to run the VoI analysis split by sex
#' - define whether you want to run the VoI analysis split by sex and age
#' - define whether you want to calculate the sum of the outcomes of interest and whether
#' whether to include this sum in the VoI analysis
#' - The input parameter file name containing the global and local input parameters and their distribution parameters
#' - define the output_version
#' - define whether to write the key aspect of the model run to the OutputVersionControl.txt file which
#' documents the key aspects of the model run (timestamp, author name, cities for which model was run,
#' input parameter file name, output version number, number of samples, and any comments. If yes, also define
#' - author name
#' - any comments that are to be written to the file
#' - The scenarios need defining by:
#' - updating the character defining which scenario script is to be called
#' - giving the reference scenario against which all other scenarios are compared,
#' this reference scenario needs to be the scenario name which corresponds to the current input parameter files
#' - giving the percentage increase in each mode for the BOGOTA (GLOBAL, LATAM, and AFRICA_INDIA) scenarios
#'
#' - the remainder of the code does not need to be changed:
#'
#' - local and global input parameters from the input parameter spreadsheet are read in and put into the correct format needed
#' for the model run
#'
#' - The\code{\link{run_ithim_setup()}} script is called which prepares the input data needed for the health impact assessment
#' and samples for the input parameter distributions
#'
#' - The \code{\link{run_ithim()}} script is called which performs the health impact assessment NSAMPLE times
#'
#' - The \code{\link{extract_data_for_voi()}} function is called which gets the data into the correct
#' format for plotting and the VoI analysis
#'
#' - Plots are created for all cities and each city individually showing the total YLL outcomes and their
#' 95% confidence intervals for each scenario ('results/voi/city_yll_',output_version,'.pdf')
#'
#' - Plots are created for each city individually showing the total YLL outcomes for the entire population and
#' both sexes and their 95% confidence intervals for each scenario ('results/voi/city_yll_sex',output_version,'.pdf')
#'
#' - One plot is created showing the change in total YLL per person relative to the baseline summed
#' across all cities ('results/voi/combined_yll_pp','_',output_version,'.pdf')
#'
#' - if required the VoI analysis is started:
#' - EVPPI values for the different input parameters of the total population outcomes are calculated for
#' each city ('results/voi/evppi_',output_version,".csv")
#' - EVPPI values are plotted for each city ('results/voi/evppi_',output_version,".pdf")
#' - if required the VoI analysis by sex is started:
#' - EVPPI values for the different input parameters of the total population outcomes by sex are
#' calculated for each city ('results/voi/evppi_sex_',output_version,".csv")
#' - EVPPI values are plotted for each city ('results/voi/evppi_sex_',output_version,".pdf")
#' - if required the VoI analysis by sex and age group is started:
#' - EVPPI values for the different input parameters of the total population outcomes by sex and age group are
#' calculated for each city (results/voi/evppi_agesex_',output_version,".csv")
#' - EVPPI values are plotted for each city and outcome ('results/voi/evppi_agesex_',output_version,".pdf")
#'
#' - The OutputVersionControl.txt file is updated if needed
#'
#'
#'
library(ithimr)
library(earth)
library(RColorBrewer)
library(plotrix)
library(foreach)
library(future)
plan(multisession)
library(doRNG)
library(future.apply)
library(voi) #install_github("chjackson/voi")
library(readxl)
library(rlist)
library(janitor)
if (!require("drpa",character.only = TRUE)) {
print('Installing "drpa" package...')
remotes::install_github("meta-analyses/drpa")
library(drpa)
print("")
}
rm(list=ls())
#
# cities <- c('belo_horizonte', 'bogota', 'buenos_aires',
# 'cali', 'medellin', 'mexico_city', 'montevideo',
# 'santiago', 'sao_paulo', 'accra', 'bangalore', 'cape_town','delhi',
# 'vizag', 'kisumu', 'nairobi', 'port_louis')
# cities <- c('antofagasta', 'arica', 'belo_horizonte', 'bogota', 'buenos_aires',
# 'cali', 'copiapo', 'coquimbo_laserena', 'gran_valparaiso',
# 'iquique_altohospicio', 'medellin', 'mexico_city', 'montevideo',
# 'osorno', 'puerto_montt', 'san_antonio',
# 'santiago', 'sao_paulo', 'temuco_padrelascasas', 'valdivia',
# 'accra', 'bangalore', 'cape_town','delhi', 'vizag', 'kisumu', 'nairobi', 'port_louis')
cities <- c('bogota')
# number of times input values are sampled from each input parameter distribution
nsamples <- 10
voi_analysis <- T # set to T if want to run VoI analysis and to F otherwise
# list of potential values for the outcome_voi_list
# 'pa_ap_all_cause', 'pa_ap_IHD', 'pa_total_cancer', 'pa_ap_lung_cancer', 'ap_COPD',
# 'pa_ap_stroke', 'pa_ap_T2D', 'ap_LRI', 'pa_breast_cancer', 'pa_colon_cancer', 'pa_endo_cancer',
# 'pa_liver_cancer', 'pa_ap_CVD', 'pa_total_dementia', 'pa_myeloma', 'pa_Parkinson',
# 'pa_head_neck_cancer', 'pa_stomach_cancer', 'inj'
# outcome_voi_list <- c('pa_ap_all_cause', 'pa_ap_IHD', 'pa_total_cancer', 'pa_ap_lung_cancer', 'ap_COPD',
# 'pa_ap_stroke', 'pa_ap_T2D', 'ap_LRI', 'pa_breast_cancer', 'pa_colon_cancer', 'pa_endo_cancer',
# 'pa_liver_cancer', 'pa_ap_CVD', 'pa_total_dementia', 'pa_myeloma', 'pa_Parkinson',
# 'pa_head_neck_cancer', 'pa_stomach_cancer', 'inj',
# 'level1', 'level2', 'level3')
outcome_voi_list <- c('pa_ap_all_cause', 'inj', 'level1')
# flag whether to run VOI analysis split gender
voi_gender <- T # set to T if want to include split and to F otherwise
# flag whether to run VOI analysis split by age and gender
voi_age_gender <- T # set to T if want to include split and to F otherwise
# add total across all outputs in VOI list for each scenario - only makes sense if results are independent of each other
# i.e. combining e.g. "total_cancer" with "lung_cancer" results in double-counting and invalid VOI analysis for the sum
voi_add_sum <- T
input_parameter_file <- "InputParameters_v41.0.xlsx"
## Get the current repo sha
gitArgs <- c("rev-parse", "--short", "HEAD", ">", file.path("repo_sha"))
# Use shell command for Windows as it's failing with system2 for Windows (giving status 128)
if (.Platform$OS.type == "windows"){
shell(paste(append("git", gitArgs), collapse = " "), wait = T)
} else {
system2("git", gitArgs, wait = T)
}
repo_sha <- as.character(readLines(file.path("repo_sha")))
#output_version <- paste0(repo_sha, "_test_run") # gives the version number of the output documents, independent of the input parameter file name
output_version <- 'bogota_10samples'
# records the main aspects of an ithim run in the OutputVersionControl.txt document
# text file records timestamp of run, author name, cities the script is run for,
# the input parameter file version used, the output version,
# the number of samples (which is 1 in constant mode), the path to any other input files,
# any comments and the runtime of the code
write_output_control = T # whether you want to save the model run specifics or not
author <- "AKS"
comment <- "Added CO2 emission sampling"
# scenario definition
scenario_name <- "BOGOTA"
reference_scenario <- 'Baseline'
scenario_increase <- 0.05 # increase for each mode in each scenario
compute_mode <- 'sample' # sample from the given input parameter distributions
############################### No need to change the following ##################################
# keep record when code started:
starttime <- Sys.time()
all_inputs <- read_excel(input_parameter_file, sheet = "all_city_parameter_inputs")
all_inputs[is.na(all_inputs)] <- ""
all_inputs <- as.data.frame(all_inputs)
#all_inputs <- read.csv('all_city_parameter_inputs.csv',stringsAsFactors = F) # read in parameter list
# get input parameters into correct format
parameter_names <- all_inputs$parameter
parameter_starts <- which(parameter_names!='')
parameter_stops <- c(parameter_starts[-1] - 1, nrow(all_inputs))
parameter_names <- parameter_names[parameter_names!='']
parameter_list <- list()
for(i in 1:length(parameter_names)){
parameter_list[[parameter_names[i]]] <- list()
parameter_index <- which(all_inputs$parameter==parameter_names[i])
if(all_inputs[parameter_index,2]=='') {
parameter_list[[parameter_names[i]]] <- lapply(cities,function(x) {
city_index <- which(colnames(all_inputs)==x)
val <- all_inputs[parameter_index,city_index]
ifelse(val%in%c('T','F'),val,ifelse(is.numeric(val), as.numeric(val), as.character(val)))
})
names(parameter_list[[parameter_names[i]]]) <- cities
}else if(all_inputs[parameter_index,2]=='constant'){
if (compute_mode != 'sample'){
indices <- 0
parameter_list[[parameter_names[i]]] <- lapply(cities,function(x) {
city_index <- which(colnames(all_inputs)==x)
val <- all_inputs[parameter_index+indices,city_index]
ifelse(val=='',0,as.numeric(val))
})
}
if(compute_mode=='sample'){ # if sampling from distribution, check that distribution parameters exist
parameter_list[[parameter_names[i]]] <- lapply(cities,function(x) {
indices <- 1:2
city_index <- which(colnames(all_inputs)==x)
val <- all_inputs[parameter_index+indices,city_index]
if (val[1] == '' & val[2]==''){ # if no distribution parameters given in input file, read in constant value instead
indices <-0
city_index <- which(colnames(all_inputs)==x)
val <- all_inputs[parameter_index+indices,city_index]}
val <- as.numeric(val)
})
}
names(parameter_list[[parameter_names[i]]]) <- cities
}else{
parameter_list[[parameter_names[i]]] <- lapply(cities,function(x) {
city_index <- which(colnames(all_inputs)==x)
if(any(all_inputs[parameter_starts[i]:parameter_stops[i],city_index]!='')){
sublist_indices <- which(all_inputs[parameter_starts[i]:parameter_stops[i],city_index]!='')
thing <- as.list(as.numeric(c(all_inputs[parameter_starts[i]:parameter_stops[i],city_index])[sublist_indices]))
names(thing) <- c(all_inputs[parameter_starts[i]:parameter_stops[i],2])[sublist_indices]
thing
}
}
)
names(parameter_list[[parameter_names[i]]]) <- cities
}
}
list2env(parameter_list, environment())
# read in global parameters
all_global_inputs <- read_excel(input_parameter_file, sheet = "all_global_parameter_inputs")
all_global_inputs[is.na(all_global_inputs)] <- ""
all_global_inputs <- as.data.frame(all_global_inputs)
# get input parameters into correct format
global_parameter_names <- all_global_inputs$parameter
global_parameter_starts <- which(global_parameter_names!='')
global_parameter_stops <- c(global_parameter_starts[-1] - 1, nrow(all_global_inputs))
global_parameter_names <- global_parameter_names[global_parameter_names!='']
global_parameter_list <- list()
for(i in 1:length(global_parameter_names)){
global_parameter_list[[global_parameter_names[i]]] <- list()
global_parameter_index <- which(all_global_inputs$parameter==global_parameter_names[i])
if(all_global_inputs[global_parameter_index,2]=='') {
global_parameter_list[[global_parameter_names[i]]] <- all_global_inputs[global_parameter_index,'global']
}else if(all_global_inputs[global_parameter_index,2]=='constant'){
if (compute_mode != 'sample'){
global_parameter_list[[global_parameter_names[i]]] <- ifelse(all_global_inputs[global_parameter_index,'global']=='',
0,as.numeric(all_global_inputs[global_parameter_index,'global']))
}
else if(compute_mode=='sample'){ # if sampling from distribution, check that distribution parameters exist
indices <- 1:2
val <- all_global_inputs[global_parameter_index+indices,'global']
if (val[1] == '' & val[2]==''){ # if no distribution parameters given in input file, read in constant value instead
val <- all_global_inputs[global_parameter_index,'global']}
val <- as.numeric(val)
global_parameter_list[[global_parameter_names[i]]] <- val
}
}
}
list2env(global_parameter_list, environment())
dist_cat <- unlist(strsplit(gsub(" ", "", dist_cat, fixed = TRUE), "\\,"))
outcome_age_min <- as.numeric(unlist(strsplit(gsub(" ", "", outcome_age_min, fixed = TRUE), "\\,")))
outcome_age_max <- as.numeric(unlist(strsplit(gsub(" ", "", outcome_age_max, fixed = TRUE), "\\,")))
outcome_age_groups <- unlist(strsplit(gsub(" ", "", outcome_age_groups, fixed = TRUE), "\\,"))
min_age <- as.numeric(min_age)
max_age <- as.numeric(max_age)
################################### Start running the the actual analysis
## with uncertainty
## comparison across cities
setting_parameters <- c("PM_CONC_BASE","BACKGROUND_PA_SCALAR","BACKGROUND_PA_ZEROS","PM_EMISSION_INVENTORY","CO2_EMISSION_INVENTORY",
"CHRONIC_DISEASE_SCALAR","PM_TRANS_SHARE","INJURY_REPORTING_RATE","BUS_TO_PASSENGER_RATIO", "CAR_OCCUPANCY_RATIO",
"TRUCK_TO_CAR_RATIO", "FLEET_TO_MOTORCYCLE_RATIO","BUS_WALK_TIME", 'RAIL_WALK_TIME',"PROPORTION_MOTORCYCLE_TRIPS" ,
"DISTANCE_SCALAR_CAR_TAXI",
"DISTANCE_SCALAR_WALKING", "DISTANCE_SCALAR_PT", "DISTANCE_SCALAR_CYCLING", "DISTANCE_SCALAR_MOTORCYCLE")
# logical for PA dose response: set T for city 1, and reuse values in 2 and 3; no need to recompute
pa_dr_quantile <- c(rep(as.logical(pa_dr_quantile_city1), length(cities)))
# logical for AP dose response: set T for city 1, and reuse values in 2 and 3; no need to recompute
ap_dr_quantile <- c(rep(as.logical(ap_dr_quantile_city1), length(cities)))
betaVariables <- c("PM_TRANS_SHARE",
"INJURY_REPORTING_RATE",
"CASUALTY_EXPONENT_FRACTION",
"BUS_TO_PASSENGER_RATIO",
"CAR_OCCUPANCY_RATIO",
"TRUCK_TO_CAR_RATIO",
"FLEET_TO_MOTORCYCLE_RATIO",
"PROPORTION_MOTORCYCLE_TRIPS",
"CHRONIC_DISEASE_SCALAR",
"SIN_EXPONENT_SUM",
"SIN_EXPONENT_SUM_NOV",
"SIN_EXPONENT_SUM_CYCLE",
"SIN_EXPONENT_SUM_PED",
"SIN_EXPONENT_SUM_VEH")
normVariables <- c('CYCLING_MMET',
'WALKING_MMET',
'PASSENGER_MMET',
'CAR_DRIVER_MMET',
'MOTORCYCLIST_MMET',
'SEDENTARY_ACTIVITY_MMET',
'LIGHT_ACTIVITY_MMET',
'MODERATE_PA_MMET',
'VIGOROUS_PA_MMET',
"PM_CONC_BASE",
"BACKGROUND_PA_SCALAR",
"CASUALTY_EXPONENT_FRACTION",
"CASUALTY_EXPONENT_FRACTION_CYCLE",
"CASUALTY_EXPONENT_FRACTION_PED",
"CASUALTY_EXPONENT_FRACTION_VEH",
"DISTANCE_SCALAR_CAR_TAXI",
"DISTANCE_SCALAR_WALKING",
"DISTANCE_SCALAR_PT",
"DISTANCE_SCALAR_CYCLING",
"DISTANCE_SCALAR_MOTORCYCLE")
save(cities,setting_parameters,injury_reporting_rate,chronic_disease_scalar,pm_conc_base,pm_trans_share,
background_pa_scalar,background_pa_confidence,cycling_mmet,walking_mmet,passenger_mmet,
car_driver_mmet,motorcyclist_mmet,sedentary_activity_mmet,light_activity_mmet,moderate_pa_mmet,
PM_emission_inventories,CO2_emission_inventories,
sin_exponent_sum,casualty_exponent_fraction, sin_exponent_sum_nov,
sin_exponent_sum_cycle,casualty_exponent_fraction_cycle, sin_exponent_sum_ped,casualty_exponent_fraction_ped,
sin_exponent_sum_veh,casualty_exponent_fraction_veh, pa_dr_quantile,ap_dr_quantile,
bus_to_passenger_ratio,car_occupancy_ratio,truck_to_car_ratio,PM_emission_confidence,CO2_emission_confidence,
distance_scalar_car_taxi,distance_scalar_motorcycle,
distance_scalar_pt,distance_scalar_walking,distance_scalar_cycling,add_motorcycle_fleet,add_personal_motorcycle_trips,
fleet_to_motorcycle_ratio, proportion_motorcycle_trips,
betaVariables,normVariables,file='diagnostic/parameter_settings.Rdata')
#parameters_only <- F
#numcores <- parallel::detectCores() - 1
multi_city_ithim <- outcome <- outcome_pp <- yll_per_hundred_thousand <- list()
print(system.time(
for(ci in 1:length(cities)){
city <- cities[ci]
print(city)
multi_city_ithim[[city]] <- run_ithim_setup(NSAMPLES = nsamples,
seed=ci,
# from multi city script
DIST_CAT = as.character(dist_cat),
ADD_WALK_TO_PT_TRIPS = as.logical(add_walk_to_pt_trips[[city]]),# originally = F,
CITY=city,
AGE_RANGE = c(min_age,max_age),
TREAT_TAXI_AS_CAR = as.logical(treat_taxi_as_car[[city]]),
ADD_TRUCK_DRIVERS = as.logical(add_truck_drivers),
ADD_BUS_DRIVERS = as.logical(add_bus_drivers),
ADD_CAR_DRIVERS = as.logical(add_car_drivers),
ADD_MOTORCYCLE_FLEET = as.logical(add_motorcycle_fleet[[city]]), #ADD_MOTORCYCLE_FLEET = add_motorcycle_fleet[[city]],
ADD_PERSONAL_MOTORCYCLE_TRIPS = as.character(add_personal_motorcycle_trips[[city]]),
PM_emission_inventory = PM_emission_inventories[[city]],
CO2_emission_inventory = CO2_emission_inventories[[city]], # added
speeds = speeds[[city]],
FLEET_TO_MOTORCYCLE_RATIO = fleet_to_motorcycle_ratio[[city]],
PROPORTION_MOTORCYCLE_TRIPS = proportion_motorcycle_trips[[city]],
CYCLING_MMET = cycling_mmet,
WALKING_MMET = walking_mmet,
PASSENGER_MMET = passenger_mmet,
CAR_DRIVER_MMET = car_driver_mmet,
MOTORCYCLIST_MMET = motorcyclist_mmet,
SEDENTARY_ACTIVITY_MMET = sedentary_activity_mmet,
LIGHT_ACTIVITY_MMET = light_activity_mmet,
MODERATE_PA_MMET = moderate_pa_mmet,
VIGOROUS_PA_MMET = vigorous_pa_mmet,
DAY_TO_WEEK_TRAVEL_SCALAR = as.numeric(day_to_week_scalar[[city]]),
SIN_EXPONENT_SUM = sin_exponent_sum,
CASUALTY_EXPONENT_FRACTION = casualty_exponent_fraction,
SIN_EXPONENT_SUM_NOV = sin_exponent_sum_nov,
SIN_EXPONENT_SUM_CYCLE = sin_exponent_sum_cycle,
CASUALTY_EXPONENT_FRACTION_CYCLE = casualty_exponent_fraction_cycle,
SIN_EXPONENT_SUM_PED = sin_exponent_sum_ped,
CASUALTY_EXPONENT_FRACTION_PED = casualty_exponent_fraction_ped,
SIN_EXPONENT_SUM_VEH = sin_exponent_sum_veh,
CASUALTY_EXPONENT_FRACTION_VEH = casualty_exponent_fraction_veh,
CALL_INDIVIDUAL_SIN = as.logical(call_individual_sin),
PA_DOSE_RESPONSE_QUANTILE = pa_dr_quantile[ci],
AP_DOSE_RESPONSE_QUANTILE = ap_dr_quantile[ci],
INJURY_REPORTING_RATE = injury_reporting_rate[[city]],
CHRONIC_DISEASE_SCALAR = chronic_disease_scalar[[city]],
PM_CONC_BASE = pm_conc_base[[city]],
PM_TRANS_SHARE = pm_trans_share[[city]],
BACKGROUND_PA_SCALAR = background_pa_scalar[[city]],
BUS_WALK_TIME = bus_walk_time[[city]],
RAIL_WALK_TIME = rail_walk_time[[city]],
#additional in VoI script
REFERENCE_SCENARIO= reference_scenario,
BACKGROUND_PA_CONFIDENCE = as.numeric(background_pa_confidence[[city]]),
BUS_TO_PASSENGER_RATIO = bus_to_passenger_ratio[[city]],
CAR_OCCUPANCY_RATIO = car_occupancy_ratio[[city]],
TRUCK_TO_CAR_RATIO = truck_to_car_ratio[[city]],
PM_EMISSION_INVENTORY_CONFIDENCE = as.numeric(PM_emission_confidence[[city]]),
CO2_EMISSION_INVENTORY_CONFIDENCE = as.numeric(CO2_emission_confidence[[city]]),
DISTANCE_SCALAR_CAR_TAXI = distance_scalar_car_taxi[[city]],
DISTANCE_SCALAR_WALKING = distance_scalar_walking[[city]],
DISTANCE_SCALAR_PT = distance_scalar_pt[[city]],
DISTANCE_SCALAR_CYCLING = distance_scalar_cycling[[city]],
DISTANCE_SCALAR_MOTORCYCLE = distance_scalar_motorcycle[[city]],
SCENARIO_NAME = scenario_name,
SCENARIO_INCREASE = scenario_increase,
BUS_DRIVER_PROP_MALE = as.numeric(bus_driver_prop_male[[city]]),
BUS_DRIVER_MALE_AGERANGE = bus_driver_male_agerange[[city]],
BUS_DRIVER_FEMALE_AGERANGE = bus_driver_female_agerange[[city]],
TRUCK_DRIVER_PROP_MALE = as.numeric(truck_driver_prop_male[[city]]),
TRUCK_DRIVER_MALE_AGERANGE = truck_driver_male_agerange[[city]],
TRUCK_DRIVER_FEMALE_AGERANGE = truck_driver_female_agerange[[city]],
COMMERCIAL_MBIKE_PROP_MALE = as.numeric(commerical_mbike_prop_male[[city]]),
COMMERCIAL_MBIKE_MALE_AGERANGE = commerical_mbike_male_agerange[[city]],
COMMERCIAL_MBIKE_FEMALE_AGERANGE = commerical_mbike_female_agerange[[city]],
MINIMUM_PT_TIME = as.numeric(minimum_pt_time)
)
# for first city, store model parameters. For subsequent cities, copy parameters over.
if(ci==1){
model_parameters <- names(multi_city_ithim[[city]]$parameters)[!names(multi_city_ithim[[city]]$parameters)%in%setting_parameters]
parameter_names <- model_parameters[model_parameters!="DR_AP_LIST"]
parameter_samples <- sapply(parameter_names,function(x)multi_city_ithim[[city]]$parameters[[x]])
}else{
for(param in model_parameters) multi_city_ithim[[city]]$parameters[[param]] <- multi_city_ithim[[1]]$parameters[[param]]
if (!is.null(multi_city_ithim[[1]]$parameters$PM_CONC_BASE)){
background_quantile <- plnorm(multi_city_ithim[[1]]$parameters$PM_CONC_BASE,log(pm_conc_base[[1]][1]),log(pm_conc_base[[1]][2]))
multi_city_ithim[[city]]$parameters$PM_CONC_BASE <- qlnorm(background_quantile,log(pm_conc_base[[city]][1]),log(pm_conc_base[[city]][2]))
}
if (!is.null(multi_city_ithim[[1]]$parameters$PM_TRANS_SHARE)){
proportion_quantile <- pbeta(multi_city_ithim[[1]]$parameters$PM_TRANS_SHARE,pm_trans_share[[1]][1],pm_trans_share[[1]][2])
multi_city_ithim[[city]]$parameters$PM_TRANS_SHARE <- qbeta(proportion_quantile,pm_trans_share[[city]][1],pm_trans_share[[city]][2])
}
}
multi_city_ithim[[city]]$outcomes <- list()
doFuture::registerDoFuture()
run_ithm_fn <- function(nsamples, ithim_object,seed, FUN=run_ithim){
foreach(i=1:nsamples, .export = ls(globalenv()) ) %dorng% { #%dopar%
FUN(ithim_object, seed=i)
}
}
multi_city_ithim[[city]]$outcomes <- run_ithm_fn(nsamples,ithim_object = multi_city_ithim[[city]], seed)
#multi_city_ithim[[city]]$outcomes <- run_ithim(ithim_object=multi_city_ithim[[city]], seed = 1)
multi_city_ithim[[city]]$DEMOGRAPHIC <- DEMOGRAPHIC
scenario_names <- multi_city_ithim[[city]]$outcome[[1]]$SCEN
## rename city-specific parameters according to city
for(i in 1:length(multi_city_ithim[[city]]$parameters$PM_EMISSION_INVENTORY[[1]])){
extract_vals <- sapply(multi_city_ithim[[city]]$parameters$PM_EMISSION_INVENTORY,function(x)x[[i]])
if(sum(extract_vals)!=0)
multi_city_ithim[[city]]$parameters[[paste0('PM_EMISSION_INVENTORY_',names(multi_city_ithim[[city]]$parameters$PM_EMISSION_INVENTORY[[1]])[i],'_',city)]] <- extract_vals
}
for(i in 1:length(multi_city_ithim[[city]]$parameters$CO2_EMISSION_INVENTORY[[1]])){
extract_vals <- sapply(multi_city_ithim[[city]]$parameters$CO2_EMISSION_INVENTORY,function(x)x[[i]])
if(sum(extract_vals)!=0)
multi_city_ithim[[city]]$parameters[[paste0('CO2_EMISSION_INVENTORY_',names(multi_city_ithim[[city]]$parameters$CO2_EMISSION_INVENTORY[[1]])[i],'_',city)]] <- extract_vals
}
for(param in setting_parameters) names(multi_city_ithim[[city]]$parameters)[which(names(multi_city_ithim[[city]]$parameters)==param)] <- paste0(param,'_',city)
multi_city_ithim[[city]]$parameters <- multi_city_ithim[[city]]$parameters[-which(names(multi_city_ithim[[city]]$parameters)==paste0('PM_EMISSION_INVENTORY_',city))]
multi_city_ithim[[city]]$parameters <- multi_city_ithim[[city]]$parameters[-which(names(multi_city_ithim[[city]]$parameters)==paste0('CO2_EMISSION_INVENTORY_',city))]
parameter_names_city <- names(multi_city_ithim[[city]]$parameters)[sapply(names(multi_city_ithim[[city]]$parameters),function(x)grepl(x,pattern=city))]
## add to parameter names
parameter_names <- c(parameter_names,parameter_names_city)
## get parameter samples and add to array of parameter samples
parameter_samples <- cbind(parameter_samples,sapply(parameter_names_city,function(x)multi_city_ithim[[city]]$parameters[[x]]))
#if(ci>1) multi_city_ithim[[city]]$parameters <- c()
# save results for city and then delete
saveRDS(multi_city_ithim[[city]],paste0('results/voi/',city,'_',output_version,'.Rds'))
# if(ci>1){
# multi_city_ithim[[ci]] <- 0
# }else{
# multi_city_ithim[[ci]]$outcomes <- 0
# }
}
))
# add relevant model run information to multi_city_ithim list
timestamp <- Sys.time()
multi_city_ithim$ithim_run <- list()
multi_city_ithim$ithim_run$input_parameter_file <- input_parameter_file
multi_city_ithim$ithim_run$scenarios_used <- scenario_name
multi_city_ithim$ithim_run$reference_scenario <- reference_scenario
multi_city_ithim$ithim_run$scenario_increase <- scenario_increase
multi_city_ithim$ithim_run$scenario_names <- scenario_names
multi_city_ithim$ithim_run$compute_mode <- compute_mode
multi_city_ithim$ithim_run$timestamp <- timestamp
multi_city_ithim$ithim_run$output_version <- output_version
multi_city_ithim$ithim_run$author <- author
multi_city_ithim$ithim_run$comment <- comment
# save the sampled parameters for all model runs and cities
saveRDS(parameter_samples,paste('diagnostic/parameter_samples_',output_version,'.Rds'),version=2)
print('finished ithim-run')
########################################### re-read and extract results #########################################
# set parameters
NSCEN <- length(scenario_names) - 1 # number of scenarios not including baseline scenario
SCEN_SHORT_NAME <- scenario_names
NSAMPLES <- nsamples
# get outputs from ithim run into correct formats and calculate summary statistics
ithim_results <- ithimr::extract_data_for_voi(NSCEN, NSAMPLES, SCEN_SHORT_NAME,outcome_age_groups,cities,multi_city_ithim, output_version)
voi_complete_df <- ithim_results$voi_complete
voi_complete_summary_df <- ithim_results$voi_complete_summary
voi_complete_100k_df <- ithim_results$voi_complete_100k
voi_complete_100k_summary_df <- ithim_results$voi_complete_100k_summary
voi_outputs_all_csv <- paste0('results/voi/All_samples_output_',output_version,".csv")
write.csv(voi_complete_df ,voi_outputs_all_csv,row.names = FALSE) # save as csv file
voi_outputs_all_100k_csv <- paste0('results/voi/All_samples_100k_output_',output_version,".csv")
write.csv(voi_complete_100k_df ,voi_outputs_all_100k_csv,row.names = FALSE) # save as csv file
ci_outputstats_csv <- paste0('results/voi/VoI_CI_output_stats_',output_version,".csv")
write.csv(voi_complete_summary_df,ci_outputstats_csv,row.names = FALSE) # save as csv file
ci_100k_outputstats_csv <- paste0('results/voi/VoI_CI_100k_output_stats_',output_version,".csv")
write.csv(voi_complete_100k_summary_df,ci_100k_outputstats_csv,row.names = FALSE) # save as csv file
######################################################### plot results #######################################################
print('plot results')
# plots only work if more than one sample was selected
if(nsamples > 1){
# plot level 1 results - one plot for all cities plus individual plots for each city
{pdf(paste0('results/voi/city_yll_level1_',output_version,'.pdf'),height=6,width=6)
# one plot for all cities - might be difficult to read if too many cities
par <- par(mar=c(5,5,1,1))
level1_df <- voi_complete_summary_df %>% filter(age_sex == 'all all') %>% dplyr::select(city, value_type, matches('level1'))
means <- as.matrix(transpose(level1_df %>% filter(value_type == 'mean') %>% dplyr::select(matches('level1')) ))
ninefive <- list()
for (city in cities){
ninefive[[city]] <- as.matrix(level1_df %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')) %>% dplyr::select(matches('level1')))
}
yvals <- rep(1:length(cities),each=NSCEN)/10 + rep(1:NSCEN,times=length(cities))
cols <- rainbow(length(cities))
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab='Change in total YLL relative to baseline - Level 1',
col=rep(cols,each=NSCEN),yaxt='n',xlim=range(unlist(ninefive)))
axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
for(i in 1:length(cities)) for(j in 1:NSCEN) lines(ninefive[[i]][,j],rep(yvals[j+(i-1)*NSCEN],2),lwd=2,col=cols[i])
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.2,x=ninefive[[1]][1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*1))
legend(col=rev(cols),lty=1,bty='n',x= mean(means),legend=rev(cities),y=NSCEN-1,lwd=2)
par(par)
# one plot per city
for(city in cities){
sp_index <- which(cities==city)
scen_out_city <- level1_df %>% filter(city == city)
means <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean') %>% dplyr::select(matches('level1')) ))
ninefive <- as.matrix(scen_out_city %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
cols <- rainbow(length(cities))
col_city <- cols[sp_index]
par_city <- par(mar=c(5,5,1,1))
xlab <- paste0(city,': Change in total YLL relative to baseline per - Level 1')
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col=rep(col_city,each=NSCEN),
yaxt='n',xlim=range(unlist(ninefive)))
axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
for(j in 1:NSCEN) lines(ninefive[,j],rep(yvals[j],2),lwd=2,col=col_city)
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.2,x=ninefive[1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*sp_index))
legend(col=col_city, lty=1,bty='n',x= mean(means),legend=city,y=NSCEN-1,lwd=2)
par(par_city)
}
dev.off()
}
# plot level 1 results per 100k - one plot for all cities plus individual plots for each city
{pdf(paste0('results/voi/city_yll_level1_100k_',output_version,'.pdf'),height=6,width=6)
# one plot for all cities - might be difficult to read if too many cities
par <- par(mar=c(5,5,1,1))
level1_df <- voi_complete_summary_100k_df %>% filter(age_sex == 'all all') %>% dplyr::select(city, value_type, matches('level1'))
means <- as.matrix(transpose(level1_df %>% filter(value_type == 'mean') %>% dplyr::select(matches('level1')) ))
ninefive <- list()
for (city in cities){
ninefive[[city]] <- as.matrix(level1_df %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')) %>% dplyr::select(matches('level1')))
}
yvals <- rep(1:length(cities),each=NSCEN)/10 + rep(1:NSCEN,times=length(cities))
cols <- rainbow(length(cities))
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab='Change in total YLL relative to baseline per 100k - Level 1',
col=rep(cols,each=NSCEN),yaxt='n',xlim=range(unlist(ninefive)))
axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
for(i in 1:length(cities)) for(j in 1:NSCEN) lines(ninefive[[i]][,j],rep(yvals[j+(i-1)*NSCEN],2),lwd=2,col=cols[i])
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.2,x=ninefive[[1]][1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*1))
legend(col=rev(cols),lty=1,bty='n',x= mean(means),legend=rev(cities),y=NSCEN-1,lwd=2)
par(par)
# one plot per city
for(city in cities){
sp_index <- which(cities==city)
scen_out_city <- level1_df %>% filter(city == city)
means <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean') %>% dplyr::select(matches('level1')) ))
ninefive <- as.matrix(scen_out_city %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
cols <- rainbow(length(cities))
col_city <- cols[sp_index]
par_city <- par(mar=c(5,5,1,1))
xlab <- paste0(city,': Change in total YLL relative to baseline per 100k - Level 1')
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col=rep(col_city,each=NSCEN),
yaxt='n',xlim=range(unlist(ninefive)))
axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
for(j in 1:NSCEN) lines(ninefive[,j],rep(yvals[j],2),lwd=2,col=col_city)
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.2,x=ninefive[1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*sp_index))
legend(col=col_city, lty=1,bty='n',x= mean(means),legend=city,y=NSCEN-1,lwd=2)
par(par_city)
}
dev.off()
}
# plot total YLL per 100k for 3 levels for all scenarios - one plot per city
{pdf(paste0('results/voi/city_yll_all_levels_100k_',output_version,'.pdf'),height=6,width=6)
# one plot per city
for(city in cities){
sp_index <- which(cities==city)
# extract information for all 3 levels
scen_out_city <- voi_complete_summary_df %>% filter(age_sex == 'all all' & city == city
) %>% dplyr::select(city, value_type, matches('level'))
# level 1 means and CI interval values
means <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean') %>% dplyr::select(matches('level1')) ))
ninefive <- as.matrix(scen_out_city %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals <- rep(3,each=NSCEN)/10 + rep(1:NSCEN)
# level 2
means_l2 <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean') %>% dplyr::select(matches('level2')) ))
ninefive_l2 <- as.matrix(scen_out_city %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level2')))
yvals_l2<- rep(2,each=NSCEN)/10 + rep(1:NSCEN)
# level 3
means_l3 <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean') %>% dplyr::select(matches('level3')) ))
ninefive_l3 <- as.matrix(scen_out_city %>% filter(city == city & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level3')))
yvals_l3 <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
par_city <- par(mar=c(5,7,1,1))
xlab <- paste0(city,': Change YLL per 100k relative to baseline for each level')
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col='black',
yaxt='n', ylim = range(.9,4.2),xlim=range(unlist(ninefive),unlist(ninefive_l2),unlist(ninefive_l3)))
axis(2,las=2,at= seq(1.2, 1.05*NSCEN + 0.2, by = 1.05)
,labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
points(as.vector(means_l2),yvals_l2,pch=16,cex=1,col='blue')
points(as.vector(means_l3),yvals_l3,pch=16,cex=1,col='red')
for(j in 1:NSCEN){
lines(ninefive[,j],rep(yvals[j],2),lwd=2,col='black')
lines(ninefive_l2[,j],rep(yvals_l2[j],2),lwd=2, col='blue')
lines(ninefive_l3[,j],rep(yvals_l3[j],2),lwd=2, col='red')
}
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.4,x=ninefive[1,(NSCEN-1)],'95%',col='black',adj=c(-0,-0.3*sp_index))
legend(col='black', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 1'),y=NSCEN-1,lwd=2, cex = 0.8)
legend(col='blue', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 2'),y=NSCEN-1.1,lwd=2, cex = 0.8)
legend(col='red', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 3'),y=NSCEN-1.2,lwd=2, cex = 0.8)
par(par_city)
}
dev.off()
}
# plot total YLL per 100k for 3 levels, both sexes and for all scenarios - one plot per city
{pdf(paste0('results/voi/city_yll_all_levels_sex_100k_',output_version,'.pdf'),height=6,width=6)
# one plot per city
for(city in cities){
sp_index <- which(cities==city)
# extract information for all 3 levels
scen_out_city <- voi_complete_summary_df %>% filter(age_cat == 'all' & city == city
) %>% dplyr::select(city, sex, value_type, matches('level'))
# level 1 means and CI interval values
means <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'all') %>% dplyr::select(matches('level1')) ))
ninefive <- as.matrix(scen_out_city %>% filter(city == city & sex == 'all' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals <- rep(6,each=NSCEN)/10 + rep(1:NSCEN)
# level 1 means and CI interval values - male
means_m <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'male') %>% dplyr::select(matches('level1')) ))
ninefive_m <- as.matrix(scen_out_city %>% filter(city == city & sex == 'male' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals_m <- rep(5.5,each=NSCEN)/10 + rep(1:NSCEN)
# level 1 means and CI interval values - female
means_f <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'female') %>% dplyr::select(matches('level1')) ))
ninefive_f <- as.matrix(scen_out_city %>% filter(city == city & sex == 'female' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level1')))
yvals_f <- rep(5,each=NSCEN)/10 + rep(1:NSCEN)
# level 2
means_l2 <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'all') %>% dplyr::select(matches('level2')) ))
ninefive_l2 <- as.matrix(scen_out_city %>% filter(city == city & sex == 'all' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level2')))
yvals_l2<- rep(4,each=NSCEN)/10 + rep(1:NSCEN)
# level 2 - male
means_l2_m <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'male') %>% dplyr::select(matches('level2')) ))
ninefive_l2_m <- as.matrix(scen_out_city %>% filter(city == city & sex == 'male' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level2')))
yvals_l2_m<- rep(3.5,each=NSCEN)/10 + rep(1:NSCEN)
# level 2 - female
means_l2_f <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'female') %>% dplyr::select(matches('level2')) ))
ninefive_l2_f <- as.matrix(scen_out_city %>% filter(city == city & sex == 'female' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level2')))
yvals_l2_f<- rep(3,each=NSCEN)/10 + rep(1:NSCEN)
# level 3
means_l3 <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'all') %>% dplyr::select(matches('level3')) ))
ninefive_l3 <- as.matrix(scen_out_city %>% filter(city == city & sex == 'all' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level3')))
yvals_l3 <- rep(2,each=NSCEN)/10 + rep(1:NSCEN)
# level 3 - male
means_l3_m <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'male') %>% dplyr::select(matches('level3')) ))
ninefive_l3_m <- as.matrix(scen_out_city %>% filter(city == city & sex == 'male' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level3')))
yvals_l3_m <- rep(1.5,each=NSCEN)/10 + rep(1:NSCEN)
# level 3 - female
means_l3_f <- as.matrix(transpose(scen_out_city %>% filter(value_type == 'mean' & sex == 'female') %>% dplyr::select(matches('level3')) ))
ninefive_l3_f <- as.matrix(scen_out_city %>% filter(city == city & sex == 'female' & (value_type == '2.5perc' | value_type == '97.5perc')
) %>% dplyr::select(matches('level3')))
yvals_l3_f <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
par_city <- par(mar=c(5,7,1,1))
xlab <- paste0(city,': Change YLL per 100k relative to baseline for each level and by sex')
plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col='black',
yaxt='n', ylim = range(.9,4.2),xlim=range(unlist(ninefive),unlist(ninefive_m),unlist(ninefive_f)))
axis(2,las=2,at= seq(1.2, 1.05*NSCEN + 0.2, by = 1.05)
,labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
# levels
points(as.vector(means_l2),yvals_l2,pch=16,cex=1,col='blue')
points(as.vector(means_l3),yvals_l3,pch=16,cex=1,col='red')
# male
points(as.vector(means_m),yvals_m,pch=16,cex=1,col='grey39')
points(as.vector(means_l2_m),yvals_l2_m,pch=16,cex=1,col='cornflowerblue')
points(as.vector(means_l3_m),yvals_l3_m,pch=16,cex=1,col='coral1')
# female
points(as.vector(means_f),yvals_f,pch=16,cex=1,col='grey')
points(as.vector(means_l2_f),yvals_l2_f,pch=16,cex=1,col='dodgerblue')
points(as.vector(means_l3_f),yvals_l3_f,pch=16,cex=1,col='orange')
for(j in 1:NSCEN){ # levels both sexes
lines(ninefive[,j],rep(yvals[j],2),lwd=2,col='black')
lines(ninefive_l2[,j],rep(yvals_l2[j],2),lwd=2, col='blue')
lines(ninefive_l3[,j],rep(yvals_l3[j],2),lwd=2, col='red')
}
for(j in 1:NSCEN){ # levels for males
lines(ninefive_m[,j],rep(yvals_m[j],2),lwd=2,col='grey39', lty = 1)
lines(ninefive_l2_m[,j],rep(yvals_l2_m[j],2),lwd=2, col='cornflowerblue', lty = 1)
lines(ninefive_l3_m[,j],rep(yvals_l3_m[j],2),lwd=2, col='coral1', lty = 1)
}
for(j in 1:NSCEN){ # levels for females
lines(ninefive_f[,j],rep(yvals_f[j],2),lwd=2,col='grey', lty = 1)
lines(ninefive_l2_f[,j],rep(yvals_l2_f[j],2),lwd=2, col='dodgerblue', lty = 1)
lines(ninefive_l3_f[,j],rep(yvals_l3_f[j],2),lwd=2, col='orange', lty = 1)
}
abline(v=0,col='grey',lty=2,lwd=2)
text(y=(NSCEN-1)+0.4,x=ninefive[1,(NSCEN-1)],'95%',col='black',adj=c(-0,-0.3*sp_index))
starting_y = 3
legend(col='black', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 1'),y=starting_y,lwd=2, cex = 0.8)
legend(col='grey39', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 1 - male'),y=starting_y-0.1,lwd=2, cex = 0.8)
legend(col='grey', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 1 - female'),y=starting_y-0.2,lwd=2, cex = 0.8)
legend(col='blue', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 2'),y=starting_y-0.4,lwd=2, cex = 0.8)
legend(col='cornflowerblue', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 2 - male'),y=starting_y-0.5,lwd=2, cex = 0.8)
legend(col='dodgerblue', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 2 - female'),y=starting_y-0.6,lwd=2, cex = 0.8)
legend(col='red', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 3'),y=starting_y-0.8,lwd=2, cex = 0.8)
legend(col='coral1', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 3 - male'),y=starting_y-0.9,lwd=2, cex = 0.8)
legend(col='orange', lty=1,bty='n',x= mean(means),legend=paste0(city,': Level 3 - female'),y=starting_y-1,lwd=2, cex = 0.8)
par(par_city)
}
dev.off()
}
# # plot total YLL sum
# {pdf(paste0('results/voi/city_yll_',output_version,'.pdf'),height=6,width=6)
#
# # one plot for all cities - might be difficult to read
# par <- par(mar=c(5,5,1,1))
# sp_index <- which(cities==city)
# scen_out <- lapply(outcome[-length(outcome)],function(x)sapply(1:NSCEN,function(y)rowSums(x[,seq(y,ncol(x),by=NSCEN)])))
# means <- sapply(scen_out,function(x)apply(x,2,mean))
# ninefive <- lapply(scen_out,function(x) apply(x,2,quantile,c(0.025,0.975)))
# yvals <- rep(1:length(scen_out),each=NSCEN)/10 + rep(1:NSCEN,times=length(scen_out))
# cols <- rainbow(length(outcome)-1)
#
# plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab='Change in total YLL relative to baseline',
# col=rep(cols,each=NSCEN),yaxt='n',xlim=range(unlist(ninefive)))
# axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
# for(i in 1:length(outcome[-length(outcome)])) for(j in 1:NSCEN) lines(ninefive[[i]][,j],rep(yvals[j+(i-1)*NSCEN],2),lwd=2,col=cols[i])
# abline(v=0,col='grey',lty=2,lwd=2)
# text(y=(NSCEN-1)+0.2,x=ninefive[[sp_index]][1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*sp_index))
# legend(col=rev(cols),lty=1,bty='n',x= mean(means),legend=rev(names(outcome)[-length(outcome)]),y=NSCEN-1,lwd=2)
# par(par)
#
# # one plot per city
# for(city in cities){
# sp_index <- which(cities==city)
# scen_out <- lapply(outcome[-length(outcome)],function(x)sapply(1:NSCEN,function(y)rowSums(x[,seq(y,ncol(x),by=NSCEN)])))
# scen_out_city <- scen_out[[city]]
# means <- colMeans(scen_out_city)
# ninefive <- apply(scen_out_city,2,quantile,probs = c(0.025,0.975))
# yvals <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
# cols <- rainbow(length(outcome)-1)
# col_city <- cols[sp_index]
#
# par_city <- par(mar=c(5,5,1,1))
# xlab <- paste0(city,': Change in total YLL relative to baseline')
# plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col=rep(col_city,each=NSCEN),
# yaxt='n',xlim=range(unlist(ninefive)))
# axis(2,las=2,at=(1+0.1):(NSCEN+0.1),labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
# for(j in 1:NSCEN) lines(ninefive[,j],rep(yvals[j],2),lwd=2,col=col_city)
# abline(v=0,col='grey',lty=2,lwd=2)
# text(y=(NSCEN-1)+0.2,x=ninefive[1,(NSCEN-1)],'95%',col='navyblue',adj=c(-0,-0.3*sp_index))
# legend(col=col_city, lty=1,bty='n',x= mean(means),legend=city,y=NSCEN-1,lwd=2)
# par(par_city)
# }
# dev.off()
# }
#
#
# # plot total YLL sum plus include split by sex (95% CIs)
# {pdf(paste0('results/voi/city_yll_sex_',output_version,'.pdf'),height=6,width=6)
#
# # one plot per city
# for(city in cities){
# sp_index <- which(cities==city)
# scen_out <- lapply(outcome[-length(outcome)],function(x)sapply(1:NSCEN,function(y)rowSums(x[,seq(y,ncol(x),by=NSCEN)])))
# scen_out_city <- scen_out[[city]]
# means <- colMeans(scen_out_city)
# ninefive <- apply(scen_out_city,2,quantile,probs = c(0.025,0.975))
# yvals <- rep(3,each=NSCEN)/10 + rep(1:NSCEN)
# cols <- rainbow(length(outcome)-1)
# col_city <- cols[sp_index]
#
# # male
# scen_city_male <- voi_data_all_sex_df %>% filter(sex == 'male', city == city) %>% dplyr::select(-c(sex,city,run, age_cat, age_sex))
# scen_out_city_male <- sapply(1:NSCEN,function(y)rowSums(scen_city_male[,seq(y,ncol(scen_city_male),by=NSCEN)]))
# means_male <- colMeans(scen_out_city_male)
# ninefive_male <- apply(scen_out_city_male,2,quantile,probs = c(0.025,0.975))
# yvals_male <- rep(2,each=NSCEN)/10 + rep(1:NSCEN)
#
# # female
# scen_city_female <- voi_data_all_sex_df %>% filter(sex == 'female', city == city) %>% dplyr::select(-c(sex,city,run, age_cat, age_sex))
# scen_out_city_female <- sapply(1:NSCEN,function(y)rowSums(scen_city_female[,seq(y,ncol(scen_city_female),by=NSCEN)]))
# means_female <- colMeans(scen_out_city_female)
# ninefive_female <- apply(scen_out_city_female,2,quantile,probs = c(0.025,0.975))
# yvals_female <- rep(1,each=NSCEN)/10 + rep(1:NSCEN)
#
# par_city <- par(mar=c(5,7,1,1))
# xlab <- paste0(city,': Change in total YLL relative to baseline')
# plot(as.vector(means),yvals,pch=16,cex=1,frame=F,ylab='',xlab=xlab,col='black',
# yaxt='n', ylim = range(.9,4.2),xlim=range(unlist(ninefive),unlist(ninefive_male),unlist(ninefive_female)))
# axis(2,las=2,at= seq(1.2, 1.05*NSCEN + 0.2, by = 1.05) #(1+0.1):(NSCEN+.1)
# ,labels=SCEN_SHORT_NAME[2:length(SCEN_SHORT_NAME)])
#
# points(as.vector(means_male),yvals_male,pch=16,cex=1,col='blue')
# points(as.vector(means_female),yvals_female,pch=16,cex=1,col='red')
#
# for(j in 1:NSCEN){
# lines(ninefive[,j],rep(yvals[j],2),lwd=2,col='black')
# lines(ninefive_male[,j],rep(yvals_male[j],2),lwd=2, col='blue')
# lines(ninefive_female[,j],rep(yvals_female[j],2),lwd=2, col='red')
# }
# abline(v=0,col='grey',lty=2,lwd=2)
# text(y=(NSCEN-1)+0.4,x=ninefive[1,(NSCEN-1)],'95%',col='black',adj=c(-0,-0.3*sp_index))
# legend(col='black', lty=1,bty='n',x= mean(means),legend=paste0(city,': all'),y=NSCEN-1,lwd=2, cex = 0.8)
# legend(col='blue', lty=1,bty='n',x= mean(means),legend=paste0(city,': male'),y=NSCEN-1.1,lwd=2, cex = 0.8)
# legend(col='red', lty=1,bty='n',x= mean(means),legend=paste0(city,': female'),y=NSCEN-1.2,lwd=2, cex = 0.8)
# par(par_city)
# }
# dev.off()
# }
#
#
#
#
#
# ################## repeat plots per 100,000