-
Notifications
You must be signed in to change notification settings - Fork 1
/
VERSE.R
2122 lines (1833 loc) · 123 KB
/
VERSE.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
######################################################
# Vaccine Economics Research for Sustainability and Equity (VERSE)
# VERSE Equity Toolkit
######################################################
rm(list=ls()) # clear
# IMPORTANT
# Search for "ACTION NEEDED" to find where the model needs your input
# ACTION NEEDED: Set Your Working Directory (where the model outputs, i.e. figures and tables, will be saved)
setwd("YOUR DIRECTORY HERE")
##### VERSE Equity Tool Inputs #####
# ACTION NEEDED: Choose the country and DHS year based on the country list: https://dhsprogram.com/Countries/
COUNTRY <- "Madagascar"
YEAR <- 2021
# ACTION NEEDED: Leave default vaccines or add/remove vaccines based on the list below:
# List of vaccines: BCG, DTP1, DTP2, DTP3, POLIO1, POLIO2, POLIO3, MCV1, MCV2, PolioBD, HEPBBD, HEPB1, HEPB2, HEPB3, PENTA1, PENTA2, PENTA3, PCV1, PCV2, PCV3, ROTA1, ROTA2, ROTA3, HIB1, HIB2, HIB3, OPV1, OPV2, OPV3, IPV1, IPV2, IPV3, FULL, ZERO, COMPLETE
VACCINES <- c("BCG","DTP1","DTP2","DTP3","POLIO1","POLIO2","POLIO3","MCV1","ZERO","FULL","COMPLETE")
# ACTION NEEDED: Change whether you want maps to be generated ("YES" --> more processing time)
MAP = "YES"
# Other model inputs (keep default unless more advanced programming is required)
FACTORS <-c("region","rural","education","wealth","sex","insurance")
SCHEDULE <-c("DEFAULT")
DATA <- "DHS"
GEO <- "District"
# TROUBLESHOOTING: If error due to file not being able to unzip
# Type in the following:
# get_available_datasets(clear_cache=TRUE)
##### R Packages Installation #####
# Before running the program and install & load the packages, please ensure your R and R-Studio are up-to-date
# Please note the version numbers for packages "PHEindicatormethods" and "radiant"
if(!require(usethis)) install.packages("usethis", repos = "http://cran.us.r-project.org")
if(!require(rmapshaper)) install.packages("rmapshaper", repos = "http://cran.us.r-project.org")
if(!require(PHEindicatormethods)) install.packages("PHEindicatormethods", version="1.4.2", repos = "http://cran.us.r-project.org")
if(!require(radiant)) install.packages("radiant", version = "1.4.4", repos = "http://cran.us.r-project.org")
if(!require(grid)) install.packages("grid", repos = "http://cran.us.r-project.org")
if(!require(Matrix)) install.packages("Matrix", repos = "http://cran.us.r-project.org")
if(!require(dplyr)) install.packages("dplyr", repos = "http://cran.us.r-project.org")
if(!require(tidyverse)) install.packages("tidyverse", repos = "http://cran.us.r-project.org")
if(!require(devtools)) install.packages("devtools", repos = "http://cran.us.r-project.org")
if(!require(rdhs)) install.packages("rdhs", repos = "http://cran.us.r-project.org")
if(!require(survey)) install.packages("survey", repos = "http://cran.us.r-project.org")
if(!require(haven)) install.packages("haven", repos = "http://cran.us.r-project.org")
if(!require(margins)) install.packages("margins", repos = "http://cran.us.r-project.org")
if(!require(tibble)) install.packages("tibble", repos = "http://cran.us.r-project.org")
if(!require(rineq)) devtools::install_github("brechtdv/rineq")
if(!require(labelled)) install.packages("labelled", repos = "http://cran.us.r-project.org")
if(!require(ggplot2)) install.packages("ggplot2", repos = "http://cran.us.r-project.org")
if(!require(ggforce)) install.packages("ggforce", repos = "http://cran.us.r-project.org")
if(!require(ggrepel)) install.packages("ggrepel", repos = "http://cran.us.r-project.org")
if(!require(prevR)) install.packages("prevR", repos = "http://cran.us.r-project.org")
if(!require(sf)) install.packages("sf", repos = "http://cran.us.r-project.org")
if(!require(matchmaker)) install.packages("matchmaker", repos = "http://cran.us.r-project.org")
if(!require(RColorBrewer)) install.packages("RColorBrewer", repos = "http://cran.us.r-project.org")
if(!require(foreign)) install.packages("foreign", repos = "http://cran.us.r-project.org")
if(!require(readxl)) install.packages("readxl", repos = "http://cran.us.r-project.org")
if(!require(magrittr)) install.packages("magrittr", repos = "http://cran.us.r-project.org")
##### Connection to DHS database (require Internet connection) #####
# ACTION NEEDED: Enter "1" in Console when prompted ("1" = YES)
set_rdhs_config(email = "ewatts13@jhu.edu",
project = "Vaccine Economics Research for Sustainability and Equity (VERSE)",
config_path = "~/.rdhs.json",
global = TRUE)
1
# Enter password: verseteam
##### VERSE Function #####
VERSE <- function(DATA,COUNTRY,YEAR,VACCINES,SCHEDULE,FACTORS,GEO,MAP) {
#Mute unnecessary warnings
options(dplyr.summarise.inform = FALSE)
oldw <- getOption("warn")
options(warn = -1)
# Connect with DHS and import the data
if (DATA[1]=="DHS"){
DATA1<- DATA[1]
# Capture country IDs
ids <- dhs_countries(returnFields=c("CountryName", "DHS_CountryCode"))
invisible(capture.output(str(ids)))
# Collect VERSE COUNTRY input to use to search for surveys
newid<- subset(ids, ids$CountryName==COUNTRY)
country_id<- c(newid$DHS_CountryCode)
# Clear current cache to prevent downlad errors
get_available_datasets(clear_cache=TRUE)
# Find all the surveys that fit our search criteria
survs <- dhs_surveys(countryIds = country_id,
surveyType = "DHS",
surveyYearStart = YEAR)
#Save Survey ID
mapid<- toString(survs[1,2])
# Download corresponding geographic mapping data
if (MAP=="YES"){
mapping <- download_boundaries(surveyId = mapid, method = "sf", quiet_download = TRUE)
}
# Store relevant dataset names
datasets <- dhs_datasets(surveyIds = survs$SurveyId,
fileFormat = "flat")
invisible(capture.output(str(datasets)))
# Select the Children's Recode version of dataset
datasets <- filter(datasets, FileType == "Children's Recode")
# Download the DHS file names
downloads <- get_datasets(datasets$FileName)
# Download the correct DHS Children's Recode Version
DATA <- readRDS(downloads[[1]])
} else{
# If Data is not DHS and you are uploading your own data use that instead
DATA1<- DATA[1]
DATA <- DATA
# Find geographic shape files for specified country
ids <- dhs_countries(returnFields=c("CountryName", "DHS_CountryCode"))
invisible(capture.output(str(ids)))
# Store Country IDs
newid<- subset(ids, ids$CountryName==COUNTRY)
country_id<- c(newid$DHS_CountryCode)
# Find all the surveys that fit our search criteria
survs <- dhs_surveys(countryIds = country_id,
surveyType = "DHS",
surveyYearStart = YEAR)
# Save Survey ID
mapid<- toString(survs[2])
# Download corresponding geographic mapping data
if (MAP=="YES"){
mapping <- download_boundaries(surveyId = mapid, method = "sf", quiet_download = TRUE)
}
}
# Create Storage Vectors & Dataframes
output <- data.frame(matrix(NA, nrow = length(FACTORS)+3, ncol = 1))
output_list <- list()
efficiency_list <- list()
map_list <- list()
# Fix Mapping Data for countries with Errors in geographic indicator formatting
if ((COUNTRY=="Bangladesh") & (MAP=="YES")){
mapping$sdr_subnational_boundaries$REGCODE <- as.numeric(c(1,2,3,4,5,6,7,8))
}
#Fix b19 age data for countries where it is missing
if ((is.na(summary(DATA$b19)[4])=="TRUE")&(is.na(summary(DATA$hw1)[4])=="TRUE")) {
DATA <- DATA %>% mutate("b19" = DATA$v008 - DATA$b3)
}
# Create shell vectors for all national-level outputs
CI_Results<- c()
CI_Results_95ciLB<- c()
CI_Results_95ciUB<- c()
CI_Results<- c()
HII_Results<- c()
HII_Results_95ciLB<- c()
HII_Results_95ciUB<- c()
CI_E_Results<- c()
CI_E_Results_95ciLB<- c()
CI_E_Results_95ciUB<- c()
AEG_Composite_Results<- c()
AEG_Composite_Results_95ciLB<- c()
AEG_Composite_Results_95ciUB<- c()
AEG_Wealth_Results<- c()
AEG_Wealth_Results_95ciLB<- c()
AEG_Wealth_Results_95ciUB<- c()
AEG_Sex_Results<-c()
AEG_Rural_Results<-c()
AEG_Insurance_Results<-c()
REG_Sex_Results<-c()
REG_Rural_Results<-c()
REG_Insurance_Results<-c()
SII_Education_Results<-c()
SII_Wealth_Results<-c()
SII_Region_Results<-c()
RII_Education_Results<-c()
RII_Wealth_Results<-c()
RII_Region_Results<-c()
CI_Wealth_Results<- c()
CI_Wealth_Results_95ciLB<- c()
CI_Wealth_Results_95ciUB<- c()
CI_E_Wealth_Results<- c()
CI_E_Wealth_Results_95ciLB<- c()
CI_E_Wealth_Results_95ciUB<- c()
Coverage_Results<- c()
Coverage_Results_95ciLB<- c()
Coverage_Results_95ciUB<- c()
# Reduce the data size (if you are using the DHS, only)
if (DATA1=="DHS"){
# Keep selected variables only
default_keep <- c("caseid", "v001", "v002", "v003", "v005", "v012", "v013", "v020", "v024",
"v025", "v101", "v106", "v107", "v133", "v137", "v136", "v151", "v152",
"v155", "v190", "v191", "v202", "v203", "v481", "v501", "v701", "v702", "v131", "v130",
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h9a", "h0", "h10", "hw1",
"h50", "h61", "h62", "h63", "h51", "h52", "h53", "h54", "h55", "h56", "h57", "h58",
"h59", "h60", "h64", "h65", "h66", "hep0", "hep1", "hep2", "hep3", "s515", "b5", "b4","b8","b9", "b19", "sstate", "sdistri", "sslumc", "sslumo",
"sd005","s052", "s190s", "s190u", "s191u", "s190us", "s190r", "s191r", "s190rs", "sv005", "s515")
# Replace dataset iin memory with smaller dataset
dhs_data <- DATA %>% select(any_of(default_keep))
} else {
dhs_data <- DATA
}
# Notes for automatic upload of Country-Specific Schedules
# Load Vaccine Schedule Data (Need to make sure setwd() is specified before running VERSE)
# schedule <- read_csv("WHO.vaxsched.csv")
#Reduce schedule dataset to only selected country & vaccines specified
# schedule <- schedule %>% select(any_of(c(VACCINES,"Country")))
# schedule <- schedule[schedule$Country %in% c(COUNTRY),]
#Fix Pakistan's Obscure weighting system
if ((COUNTRY=="Pakistan")&(YEAR==2016)){
dhs_data <- dhs_data %>% mutate("v005" = dhs_data$v005+dhs_data$sv005)
}
# Create Vaccine Schedule from WHO General EPI Guidance
if (SCHEDULE[1]=="DEFAULT"){
schedule<-as.data.frame(matrix(as.numeric(c(0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 12, 24, 0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 0, 2, 3, 4, 5, 108, 6, 7, 6, 24, 24, 24, 18, 12, 12, 2, 3, 4, 12, 0, 24)),nrow=1))
schedule_names<- c("BCG","DTP1","DTP2","DTP3","POLIO1","POLIO2","POLIO3","OPV1","OPV2","OPV3","IPV1","IPV2","IPV3","MCV1","MCV2","PolioBD","HIB1","HIB2","HIB3","PCV1","PCV2","PCV3","ROTA1","ROTA2","ROTA3","HEPBBD","HEPB1","HEPB2","HEPB3","HEPB4","HPV","JE1","JE2","TCV","CHOLERA1","CHOLERA2","CHOLERA3","MENA","MENC","HEPA","PENTA1","PENTA2","PENTA3", "ZERO", "FULL", "COMPLETE")
colnames(schedule) <- schedule_names
} else {
# Create Schedule from input Vector (must align with VACCINES() input vector and be age in months)
schedule<-as.data.frame(matrix(as.numeric(SCHEDULE),nrow=1))
schedule_names<-VACCINES
colnames(schedule) <- schedule_names
}
# Create Age in Months Variable for Children who are Alive only
# Note in some datasets b19 holds complete child age info in others it is hw1
# Identfies if approriate variable is b19 or hw1 and renames the correct variable to hw1
if (((length(dhs_data$b19))!= 0)&(is.na(summary(dhs_data$b19)[4])!="TRUE")){
dhs_data <- dhs_data %>% mutate("hw1" = dhs_data$b19)
dhs_data$hw1<- replace(dhs_data$hw1,dhs_data$b5 ==0,NA)
}
#Defining fair inequity based upon Vaccine Schedule by creating vaccine-specific underage cutoffs
for (i in VACCINES){
dhs_data$q<- ifelse(dhs_data$hw1 < schedule[,i][1], 1,
ifelse(dhs_data$hw1>=schedule[,i][1], 0, 0))
dhs_data$q<- replace(dhs_data$q,is.na(dhs_data$q),0)
names(dhs_data)[names(dhs_data) == "q"] <- paste("underage_",i, sep="")
}
#Create binary IPV=1 vs. OPV=0 indicator for Polio since h4, h6, and h8 can be either OPV or IPV
if ((is.null(dhs_data$h60)[1]=="TRUE")|(table(is.na(dhs_data$h60))[1]==length(dhs_data$h60))){
dhs_data <- dhs_data %>% mutate("IPVind" = 0)
}else {
dhs_data <- dhs_data %>% mutate("IPVind" = ifelse(dhs_data$h60 ==0 | dhs_data$h60 >=8, 0,
ifelse(dhs_data$h60 ==1 | dhs_data$h60 ==2 | dhs_data$h60 ==3, 1,
"NA")))}
# Fix Missing Values in determinants:
if ((max(dhs_data$v106)[1]>=6)&(COUNTRY!="Yemen")){
dhs_data <- dhs_data %>% mutate("v106" = ifelse(dhs_data$v106>=6,NA,dhs_data$v106))
}
if (is.na(summary(dhs_data$v481)[4])=="TRUE"){
dhs_data <- dhs_data %>% mutate("v481" = 0)
}
if (summary(dhs_data$v481)[6] >=8){
dhs_data <- dhs_data %>% mutate("v481" = ifelse(dhs_data$v481>=8,0,dhs_data$v481))
}
if ((COUNTRY=="Uganda")&(YEAR<2005)){
dhs_data$v190<-dhs_data$s052
}
#Create Vaccine-Specific Outcomes
# BCG (1 = received, 0 = did not recive, NA = no data)
if ("BCG" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("BCG" = ifelse(dhs_data$h2 ==0 | dhs_data$h2 >=8, 0,
ifelse(dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3, 1,
"NA")))}
# DTP1 (1 = received, 0 = did not recive, NA = no data)
if ("DTP1" %in% VACCINES){
if (is.na(summary(dhs_data$h3)[4])=="TRUE"){
dhs_data$h3 <- dhs_data$h51
}
dhs_data <- dhs_data %>% mutate("DTP1" = ifelse(dhs_data$h3 ==0 | dhs_data$h3 >=8, 0,
ifelse(dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3, 1,
"NA")))}
# POLIO1 (1 = received, 0 = did not recive, NA = no data)
if ("POLIO1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("POLIO1" = ifelse(((dhs_data$h4 ==0 | dhs_data$h4 >=8) & (dhs_data$IPVind ==0))|((dhs_data$h4 ==0 | dhs_data$h4 >=8) & (dhs_data$IPVind ==1)), 0,
ifelse((dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & ((dhs_data$IPVind ==0)| (dhs_data$IPVind ==1)), 1,
"NA")))}
# OPV1 (1 = received, 0 = did not recive, NA = no data)
if ("OPV1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("OPV1" = ifelse((dhs_data$h4 ==0 | dhs_data$h4 >=8) & (dhs_data$IPVind ==0), 0,
ifelse((dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$IPVind ==0), 1,
"NA")))}
# DTP2 (1 = received, 0 = did not recive, NA = no data)
if ("DTP2" %in% VACCINES){
if (is.na(summary(dhs_data$h5)[4])=="TRUE"){
dhs_data$h3 <- dhs_data$h52
}
dhs_data <- dhs_data %>% mutate("DTP2" = ifelse(dhs_data$h5 ==0 | dhs_data$h5 >=8, 0,
ifelse(dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3, 1,
"NA")))}
# POLIO2 (1 = received, 0 = did not recive, NA = no data)
if ("POLIO2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("POLIO2" = ifelse(((dhs_data$h6 ==0 | dhs_data$h6 >=8) & (dhs_data$IPVind ==0))|((dhs_data$h6 ==0 | dhs_data$h6 >=8) & (dhs_data$IPVind ==1)), 0,
ifelse((dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & ((dhs_data$IPVind ==0)| (dhs_data$IPVind ==1)), 1,
"NA")))}
# OPV2 (1 = received, 0 = did not recive, NA = no data)
if ("OPV2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("OPV2" = ifelse((dhs_data$h6 ==0 | dhs_data$h6 >=8) & (dhs_data$IPVind ==0), 0,
ifelse((dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$IPVind ==0), 1,
"NA")))}
# DTP3 (1 = received, 0 = did not recive, NA = no data)
if ("DTP3" %in% VACCINES){
if (is.na(summary(dhs_data$h7)[4])=="TRUE"){
dhs_data$h3 <- dhs_data$h53
}
dhs_data <- dhs_data %>% mutate("DTP3" = ifelse(dhs_data$h7 ==0 | dhs_data$h7 >=8, 0,
ifelse(dhs_data$h7 ==1 | dhs_data$h7 ==2 | dhs_data$h7 ==3, 1,
"NA")))}
# POLIO3 (1 = received, 0 = did not recive, NA = no data)
if ("POLIO3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("POLIO3" = ifelse(((dhs_data$h8 ==0 | dhs_data$h8 >=8) & (dhs_data$IPVind ==0))|((dhs_data$h8 ==0 | dhs_data$h8 >=8) & (dhs_data$IPVind ==1)), 0,
ifelse((dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3) & ((dhs_data$IPVind ==0)| (dhs_data$IPVind ==1)), 1,
"NA")))}
# OPV3 (1 = received, 0 = did not recive, NA = no data)
if ("OPV3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("OPV3" = ifelse((dhs_data$h8 ==0 | dhs_data$h8 >=8) & (dhs_data$IPVind ==0), 0,
ifelse((dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3) & (dhs_data$IPVind ==0), 1,
"NA")))}
# MCV1 (1 = received, 0 = did not recive, NA = no data)
if ("MCV1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("MCV1" = ifelse(dhs_data$h9 ==0 | dhs_data$h9 >=8, 0,
ifelse(dhs_data$h9 ==1 | dhs_data$h9 ==2 | dhs_data$h9 ==3, 1,
"NA")))}
# MCV2 (1 = received, 0 = did not recive, NA = no data)
if ("MCV2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("MCV2" = ifelse(dhs_data$h9a ==0 | dhs_data$h9a >=8, 0,
ifelse(dhs_data$h9a ==1 | dhs_data$h9a ==2 | dhs_data$h9a ==3, 1,
"NA")))}
# Polio Birth Dose (1 = received, 0 = did not recive, NA = no data)
if ("PolioBD" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PolioBD" = ifelse(dhs_data$h0 ==0 | dhs_data$h0 >=8, 0,
ifelse(dhs_data$h0 ==1 | dhs_data$h0 ==2 | dhs_data$h0 ==3, 1,
"NA")))}
# Hep B Birth Dose (1 = received, 0 = did not recive, NA = no data)
if ("HEPBBD" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HEPBBD" = ifelse(dhs_data$h50 ==0 | dhs_data$h50 >=8, 0,
ifelse(dhs_data$h50 ==1 | dhs_data$h50 ==2 | dhs_data$h50 ==3, 1,
"NA")))}
# Hep B1 (1 = received, 0 = did not recive, NA = no data)
if ("HEPB1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HEPB1" = ifelse(dhs_data$h61 ==0 | dhs_data$h61 >=8, 0,
ifelse(dhs_data$h61 ==1 | dhs_data$h61 ==2 | dhs_data$h61 ==3, 1,
"NA")))}
# Hep B2 (1 = received, 0 = did not recive, NA = no data)
if ("HEPB2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HEPB2" = ifelse(dhs_data$h62 ==0 | dhs_data$h62 >=8, 0,
ifelse(dhs_data$h62 ==1 | dhs_data$h62 ==2 | dhs_data$h62 ==3, 1,
"NA")))}
# Hep B3 (1 = received, 0 = did not recive, NA = no data)
if ("HEPB3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HEPB3" = ifelse(dhs_data$h63 ==0 | dhs_data$h63 >=8, 0,
ifelse(dhs_data$h63 ==1 | dhs_data$h63 ==2 | dhs_data$h63 ==3, 1,
"NA")))}
# Pentavalent 1 (1 = received, 0 = did not recive, NA = no data)
if ("PENTA1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PENTA1" = ifelse(dhs_data$h51 ==0 | dhs_data$h51 >=8, 0,
ifelse(dhs_data$h51 ==1 | dhs_data$h51 ==2 | dhs_data$h51 ==3, 1,
"NA")))}
# Pentavalent 2 (1 = received, 0 = did not recive, NA = no data)
if ("PENTA2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PENTA2" = ifelse(dhs_data$h52 ==0 | dhs_data$h52 >=8, 0,
ifelse(dhs_data$h52 ==1 | dhs_data$h52 ==2 | dhs_data$h52 ==3, 1,
"NA")))}
# Pentavalent3 (1 = received, 0 = did not recive, NA = no data)
if ("PENTA3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PENTA3" = ifelse(dhs_data$h53 ==0 | dhs_data$h53 >=8, 0,
ifelse(dhs_data$h53 ==1 | dhs_data$h53 ==2 | dhs_data$h53 ==3, 1,
"NA")))}
# Pneumococcal 1 (1 = received, 0 = did not recive, NA = no data)
if ("PCV1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PCV1" = ifelse(dhs_data$h54 ==0 | dhs_data$h54 >=8, 0,
ifelse(dhs_data$h54 ==1 | dhs_data$h54 ==2 | dhs_data$h54 ==3, 1,
"NA")))}
# Pneumococcal 2 (1 = received, 0 = did not recive, NA = no data)
if ("PCV2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PCV2" = ifelse(dhs_data$h55 ==0 | dhs_data$h55 >=8, 0,
ifelse(dhs_data$h55 ==1 | dhs_data$h55 ==2 | dhs_data$h55 ==3, 1,
"NA")))}
# Pneumococcal 3 (1 = received, 0 = did not recive, NA = no data)
if ("PCV3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("PCV3" = ifelse(dhs_data$h56 ==0 | dhs_data$h56 >=8, 0,
ifelse(dhs_data$h56 ==1 | dhs_data$h56 ==2 | dhs_data$h56 ==3, 1,
"NA")))}
# Rotavirus 1 (1 = received, 0 = did not recive, NA = no data)
if ("ROTA1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("ROTA1" = ifelse(dhs_data$h57 ==0 | dhs_data$h57 >=8, 0,
ifelse(dhs_data$h57 ==1 | dhs_data$h57 ==2 | dhs_data$h57 ==3, 1,
"NA")))}
# Rotavirus 2 (1 = received, 0 = did not recive, NA = no data)
if ("ROTA2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("ROTA2" = ifelse(dhs_data$h58 ==0 | dhs_data$h58 >=8, 0,
ifelse(dhs_data$h58 ==1 | dhs_data$h58 ==2 | dhs_data$h58 ==3, 1,
"NA")))}
# Rotavirus 3 (1 = received, 0 = did not recive, NA = no data)
if ("ROTA3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("ROTA3" = ifelse(dhs_data$h59 ==0 | dhs_data$h59 >=8, 0,
ifelse(dhs_data$h59 ==1 | dhs_data$h59 ==2 | dhs_data$h59 ==3, 1,
"NA")))}
# HIB 1 (1 = received, 0 = did not recive, NA = no data)
if ("HIB1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HIB1" = ifelse(dhs_data$h64 ==0 | dhs_data$h64 >=8, 0,
ifelse(dhs_data$h64 ==1 | dhs_data$h64 ==2 | dhs_data$h64 ==3, 1,
"NA")))}
# HIB 2 (1 = received, 0 = did not recive, NA = no data)
if ("HIB2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HIB2" = ifelse(dhs_data$h65 ==0 | dhs_data$h65 >=8, 0,
ifelse(dhs_data$h65 ==1 | dhs_data$h65 ==2 | dhs_data$h65 ==3, 1,
"NA")))}
# HIB 3 (1 = received, 0 = did not recive, NA = no data)
if ("HIB3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("HIB3" = ifelse(dhs_data$h66 ==0 | dhs_data$h66 >=8, 0,
ifelse(dhs_data$h66 ==1 | dhs_data$h66 ==2 | dhs_data$h66 ==3, 1,
"NA")))}
# Polio - IPV 1 (1 = received, 0 = did not recive, NA = no data)
if ("IPV1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("IPV1" = ifelse((dhs_data$h4 ==0 | dhs_data$h4 >=8) & (dhs_data$IPVind ==1), 0,
ifelse(dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3, 1,
"NA")))}
# Polio - IPV 2 (1 = received, 0 = did not recive, NA = no data)
if ("IPV2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("IPV2" = ifelse((dhs_data$h6 ==0 | dhs_data$h6 >=8) & (dhs_data$IPVind ==1), 0,
ifelse(dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3, 1,
"NA")))}
# Polio - IPV 3 (1 = received, 0 = did not recive, NA = no data)
if ("IPV3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("IPV3" = ifelse((dhs_data$h8 ==0 | dhs_data$h8 >=8) & (dhs_data$IPVind ==1), 0,
ifelse(dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3, 1,
"NA")))}
if (COUNTRY=="Tanzania" & YEAR==2004){
if ("DTP1" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("DTP1" = ifelse((dhs_data$POLIO1 ==1 & dhs_data$DTP1==0), 1,
dhs_data$DTP1))}
if ("DTP2" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("DTP2" = ifelse((dhs_data$POLIO2 ==1 & dhs_data$DTP2==0), 1,
dhs_data$DTP2))}
if ("DTP3" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("DTP3" = ifelse((dhs_data$POLIO3 ==1 & dhs_data$DTP3==0), 1,
dhs_data$DTP3))}}
# Indicator for Fully Immunized for Age (1 = Fully Immuniized, 0 = Not Fully Immunized, NA = no data)
# At a minimum Fully Immunized utilizes data on BCG, MCV1, Polio1-3, DTP1-3 (and Penta1-3, depending on location)
# Remove MCV1 & MCV2 from FULL for countries where an ongoing campaign is concurrent with DHS data collection.
if ("FULL" %in% VACCINES){
if ((COUNTRY=="Afghanistan")
|(COUNTRY=="Angola")
|(COUNTRY=="Benin")
|(COUNTRY=="Cambodia")
|(COUNTRY=="Chad")
|(COUNTRY=="Cote d’Ivoire")
|(COUNTRY=="Eritrea")
|(COUNTRY=="Ethiopia")
|(COUNTRY=="Ghana")
|(COUNTRY=="Guinea")
|(COUNTRY=="Indonesia")
|(COUNTRY=="Lesotho")
|(COUNTRY=="Liberia")
|(COUNTRY=="Madagascar")
|(COUNTRY=="Malawi")
|(COUNTRY=="Mali")
|(COUNTRY=="Niger")
|(COUNTRY=="Nigeria")
|(COUNTRY=="Pakistan")
|(COUNTRY=="Rwanda")
|(COUNTRY=="Senegal")
|(COUNTRY=="Sierra Leone")
|(COUNTRY=="Tanzania")
|(COUNTRY=="Togo")
|(COUNTRY=="Timor-Leste")){
if(("BCG" %in% VACCINES) & (("DTP1" %in% VACCINES)|("PENTA1" %in% VACCINES)) & (("DTP2" %in% VACCINES)|("PENTA2" %in% VACCINES)) & (("DTP3" %in% VACCINES)|("PENTA3" %in% VACCINES)) & (("OPV1" %in% VACCINES)|("IPV1" %in% VACCINES)|("PENTA1" %in% VACCINES)|("POLIO1" %in% VACCINES)) & (("OPV2" %in% VACCINES)|("IPV2" %in% VACCINES)|("PENTA2" %in% VACCINES)|("POLIO2" %in% VACCINES)) & (("OPV3" %in% VACCINES)|("IPV3" %in% VACCINES)|("PENTA3" %in% VACCINES)|("POLIO3" %in% VACCINES))){
# Create VACCINES input, minus FULL, ZERO, and COMPLETE
VAX_TOTAL = VACCINES[VACCINES != "FULL"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="ZERO"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="COMPLETE"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="MCV1"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="MCV2"]
for (i in VAX_TOTAL){
FI <- paste("FI_",i, sep="")
dhs_data[,FI] = ifelse(((dhs_data[,i]==0)&(dhs_data$hw1 < schedule[,i][1]))|((dhs_data[,i]==1)&(dhs_data$hw1 >= schedule[,i][1])),1,0)
}
# Create small dataset with only the vaccine-specific immunized for age inicators produced above
newdata<- dhs_data %>%
select(starts_with("FI_"))
# Sum the binary indicator for appropriately immunized for age for each across observations
dhs_data <- dhs_data %>% mutate("FULL_SUM" = rowSums(newdata))
# Create fully immunized variable as the sum being equal to length of VACCINES input, minus FULL, ZERO, and COMPLETE (e.g. received all)
dhs_data <- dhs_data %>% mutate("FULL" = ifelse((dhs_data[,"FULL_SUM"]==length(VAX_TOTAL)),1,0))
# If only a few select vaccines are specified in VACCINES, pull data on other routine vaccines (BCG, DTP1-3, Polio1-3) to create FULL variable
} else {
dhs_data <- dhs_data %>% mutate("FULL" = ifelse((((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$hw1 < schedule[,"DTP1"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$hw1 >= schedule[,"DTP1"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3) & (dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$hw1 >= schedule[,"DTP2"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3) & (dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$h7 ==1 | dhs_data$h7 ==2 | dhs_data$h7 ==3) & (dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3) & (dhs_data$hw1 >= schedule[,"DTP3"][1]))
), 1, 0))
}
} else {
if(("BCG" %in% VACCINES) & (("DTP1" %in% VACCINES)|("PENTA1" %in% VACCINES)) & (("DTP2" %in% VACCINES)|("PENTA2" %in% VACCINES)) & (("DTP3" %in% VACCINES)|("PENTA3" %in% VACCINES)) & (("OPV1" %in% VACCINES)|("IPV1" %in% VACCINES)|("PENTA1" %in% VACCINES)|("POLIO1" %in% VACCINES)) & (("OPV2" %in% VACCINES)|("IPV2" %in% VACCINES)|("PENTA2" %in% VACCINES)|("POLIO2" %in% VACCINES)) & (("OPV3" %in% VACCINES)|("IPV3" %in% VACCINES)|("PENTA3" %in% VACCINES)|("POLIO3" %in% VACCINES)) & ("MCV1" %in% VACCINES)){
# Create VACCINES input, minus FULL, ZERO, and COMPLETE
VAX_TOTAL = VACCINES[VACCINES != "FULL"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="ZERO"]
VAX_TOTAL = VAX_TOTAL[VAX_TOTAL !="COMPLETE"]
for (i in VAX_TOTAL){
FI <- paste("FI_",i, sep="")
dhs_data[,FI] = ifelse(((dhs_data[,i]==0)&(dhs_data$hw1 < schedule[,i][1]))|((dhs_data[,i]==1)&(dhs_data$hw1 >= schedule[,i][1])),1,0)
}
# Create small dataset with only the vaccine-specific immunized for age inicators produced above
newdata<- dhs_data %>%
select(starts_with("FI_"))
# Sum the binary indicator for appropriately immunized for age for each across observations
dhs_data <- dhs_data %>% mutate("FULL_SUM" = rowSums(newdata))
# Create fully immunized variable as the sum being equal to length of VACCINES input, minus FULL, ZERO, and COMPLETE (e.g. received all)
dhs_data <- dhs_data %>% mutate("FULL" = ifelse((dhs_data[,"FULL_SUM"]==length(VAX_TOTAL)),1,0))
# If only a few select vaccines are specified in VACCINES, pull data on other routine vaccines (BCG, DTP1-3, Polio1-3, MCV1) to create FULL variable
} else {
dhs_data <- dhs_data %>% mutate("FULL" = ifelse((((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$hw1 < schedule[,"DTP1"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$hw1 >= schedule[,"DTP1"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3) & (dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$hw1 >= schedule[,"DTP2"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3) & (dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$h7 ==1 | dhs_data$h7 ==2 | dhs_data$h7 ==3) & (dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3) & (dhs_data$hw1 >= schedule[,"DTP3"][1]))|
((dhs_data$h2 ==1 | dhs_data$h2 ==2 | dhs_data$h2 ==3) & (dhs_data$h3 ==1 | dhs_data$h3 ==2 | dhs_data$h3 ==3) & (dhs_data$h4 ==1 | dhs_data$h4 ==2 | dhs_data$h4 ==3) & (dhs_data$h5 ==1 | dhs_data$h5 ==2 | dhs_data$h5 ==3) & (dhs_data$h6 ==1 | dhs_data$h6 ==2 | dhs_data$h6 ==3) & (dhs_data$h7 ==1 | dhs_data$h7 ==2 | dhs_data$h7 ==3) & (dhs_data$h8 ==1 | dhs_data$h8 ==2 | dhs_data$h8 ==3) & (dhs_data$h9 ==1 | dhs_data$h9 ==2 | dhs_data$h9 ==3) & (dhs_data$hw1 >= schedule[,"MCV1"][1]))
), 1, 0))
}
}
}
#Create indicator for completed the routine immunization schedule by 24 months of age
if ("COMPLETE" %in% VACCINES){
dhs_data <- dhs_data %>% mutate("COMPLETE" = ifelse(dhs_data$FULL==1 & dhs_data$hw1>=schedule[,"COMPLETE"][1], 1, 0))
dhs_data$COMPLETE<- replace(dhs_data$COMPLETE,dhs_data$hw1<schedule[,"COMPLETE"][1],NA)
}
# Create Zero-Dose Indicator (never having received a single dose of BCG, DTP, PENTA, OPV/IPV, or MCV1 by 12 months)
if ("ZERO" %in% VACCINES){
if(("BCG" %in% VACCINES) & (("DTP1" %in% VACCINES)|("PENTA1" %in% VACCINES)) & (("DTP2" %in% VACCINES)|("PENTA2" %in% VACCINES)) & (("DTP3" %in% VACCINES)|("PENTA3" %in% VACCINES)) & (("OPV1" %in% VACCINES)|("IPV1" %in% VACCINES)|("PENTA1" %in% VACCINES)) & (("OPV2" %in% VACCINES)|("IPV2" %in% VACCINES)|("PENTA2" %in% VACCINES)) & (("OPV3" %in% VACCINES)|("IPV3" %in% VACCINES)|("PENTA3" %in% VACCINES)) & ("MCV1" %in% VACCINES)){
for (i in VAX_TOTAL){
FI <- paste("FI_",i, sep="")
dhs_data[,FI] = ifelse(((dhs_data[,i]==0)&(dhs_data$hw1 < schedule[,i][1]))|((dhs_data[,i]==1)&(dhs_data$hw1 >= schedule[,i][1])),1,0)
}
newdata<- dhs_data %>%
select(starts_with("FI_"))
#Create indicator for no routine immunizations by 12 months of age
dhs_data <- dhs_data %>% mutate("FULL_SUM" = rowSums(newdata))
dhs_data <- dhs_data %>% mutate("ZERO" = ifelse((dhs_data$FULL_SUM ==0) & (dhs_data$hw1 >= schedule[,"ZERO"][1]), 1,
ifelse(dhs_data$FULL_SUM =="NA", "NA", 0)))
} else {
#Create indicator for no routine immunizations by 12 months of age (never having received a single dose of BCG, DTP, PENTA, OPV/IPV, or MCV1)
dhs_data <- dhs_data %>% mutate("ZERO" = ifelse(((dhs_data$h2 ==0 | dhs_data$h2 >=8) & (dhs_data$h3 ==0 | dhs_data$h3 >=8) & (dhs_data$h4 ==0 | dhs_data$h4 >=8) & (dhs_data$h5 ==0 | dhs_data$h5 >=8) & (dhs_data$h6 ==0 | dhs_data$h6 >=8) & (dhs_data$h7 ==0 | dhs_data$h7 >=8) & (dhs_data$h8 ==0 | dhs_data$h8 >=8) & (dhs_data$h9 ==0 | dhs_data$h9 >=8) & (dhs_data$hw1 >= schedule[,"ZERO"][1])),
1, 0))
}
}
#Align Sub-National Outputs with GIS Map Boundaries where v101 is different
FLAG<-0
if(COUNTRY[1]=="Nigeria"){
dhs_data$v101 <- dhs_data$sstate/10
dhs_data$v024 <- dhs_data$sstate/10
}
if((COUNTRY[1]=="Uganda")&(YEAR>2012)){
dhs_data$GEO<-dhs_data$v101
dhs_data$v101 <- dhs_data$v101+1
dhs_data$v024 <- dhs_data$v024+1
}
if((COUNTRY[1]=="Uganda")&(YEAR==2011)){
dhs_data$GEO<-dhs_data$v101
}
if(COUNTRY[1]=="Senegal"){
dhs_data$v101 <- dhs_data$v024
}
if(COUNTRY[1]=="Jordan"){
dhs_data$v101 <- dhs_data$v024
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101<20, dhs_data$v101-10,
ifelse(dhs_data$v101>=20 & dhs_data$v101<30, dhs_data$v101-16,
ifelse(dhs_data$v101>=30, dhs_data$v101-22,
dhs_data$v101))))
}
if(COUNTRY[1]=="Indonesia"){
dhs_data$v101 <- dhs_data$v024
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101<20, dhs_data$v101-10,
ifelse(dhs_data$v101>=20 & dhs_data$v101<30, dhs_data$v101-11,
ifelse(dhs_data$v101>=30 & dhs_data$v101<50, dhs_data$v101-20,
ifelse(dhs_data$v101>=50 & dhs_data$v101<60, dhs_data$v101-34,
ifelse(dhs_data$v101>=60 & dhs_data$v101<70, dhs_data$v101-41,
ifelse(dhs_data$v101>=70 & dhs_data$v101<80, dhs_data$v101-46,
ifelse(dhs_data$v101>=80 & dhs_data$v101<90, dhs_data$v101-50,
ifelse(dhs_data$v101>=90 & dhs_data$v101<93, dhs_data$v101-58,
ifelse(dhs_data$v101>=93, dhs_data$v101-60,
dhs_data$v101))))))))))
}
if(COUNTRY[1]=="Timor-Leste"){
dhs_data$v101 <- dhs_data$v024
}
if(COUNTRY[1]=="Yemen"){
dhs_data$GEO<-dhs_data$v101
dhs_data$v101 <- dhs_data$v101-10
}
if((COUNTRY[1]=="India") & (YEAR<=2007)){
dhs_data$GEO<-dhs_data$v101
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101>=5 & dhs_data$v101<=24, dhs_data$v101-1,
ifelse(dhs_data$v101>=25 & dhs_data$v101<=30, dhs_data$v101-3,
ifelse(dhs_data$v101>30, dhs_data$v101-5,dhs_data$v101))))
}
if((COUNTRY[1]=="Tanzania") & (YEAR==2004)){
dhs_data$GEO<-dhs_data$v101
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101>=22, dhs_data$v101-29, dhs_data$v101))
}
if((COUNTRY[1]=="Tanzania") & (YEAR==2015)){
dhs_data$GEO<-dhs_data$v101
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101>=26, dhs_data$v101-25, dhs_data$v101))
}
if((COUNTRY[1]=="Tanzania") & (YEAR==2022)){
dhs_data$GEO<-dhs_data$v101
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101>=27, dhs_data$v101-24, dhs_data$v101))
}
if((COUNTRY[1]=="Madagascar")&(YEAR>2018)){
dhs_data$GEO<-dhs_data$v101
dhs_data <- dhs_data %>% mutate("v101" = ifelse(dhs_data$v101>=1 & dhs_data$v101<=20, dhs_data$v101-9,
ifelse(dhs_data$v101>=20 & dhs_data$v101<=30, dhs_data$v101-15,
ifelse(dhs_data$v101>=30 & dhs_data$v101<=40, dhs_data$v101-20,
ifelse(dhs_data$v101>=40 & dhs_data$v101<=50, dhs_data$v101-27,
ifelse(dhs_data$v101>=50 & dhs_data$v101<=60, dhs_data$v101-33,
ifelse(dhs_data$v101>60, dhs_data$v101-39,dhs_data$v101)))))))
}
if(min(dhs_data$v101)==0){
FLAG <- 1
dhs_data$GEO<-dhs_data$v101
dhs_data$v101 <- dhs_data$v101+1
dhs_data$v024 <- dhs_data$v024+1
}
# Fix insurance & education variable to avoid reference level errors
dhs_data <- dhs_data %>% mutate("v481" = v481+1)
dhs_data$v481[is.na(dhs_data$v481)] <- 1
dhs_data <- dhs_data %>% mutate("v106" = v106+1)
if (COUNTRY[1]!="Yemen"){
dhs_data<- dhs_data[complete.cases(dhs_data$v106),]
}
if ((COUNTRY=="Senegal")&(YEAR==2019)){
dhs_data<- dhs_data[complete.cases(dhs_data$v106),]
}
if ((COUNTRY=="Burkina Faso")&(YEAR==2010)){
dhs_data<- dhs_data[complete.cases(dhs_data$v106),]
}
if ((COUNTRY=="Comoros")&(YEAR==2012)){
dhs_data<- dhs_data[complete.cases(dhs_data$v106),]
}
# Create Reference Values Vector
FACT<-c()
for (l in 1:length(FACTORS)){
ifelse(FACTORS[l]=="region", FACT[l]<-"v101",
ifelse(FACTORS[l]=="rural", FACT[l]<-"v025",
ifelse(FACTORS[l]=="education", FACT[l]<-"v106",
ifelse(FACTORS[l]=="wealth", FACT[l]<-"v190",
ifelse(FACTORS[l]=="sex", FACT[l]<-"b4",
ifelse(FACTORS[l]=="insurance", FACT[l]<-"v481","NA"))))))
}
# Create Reference Levels Based on Fully Immunized for Age Outcome
REF<- c()
for(l in FACT){
REF_DATA<-dhs_data
REF_DATA[,"FULL"] <- as.numeric(REF_DATA[,"FULL"])
REF_DATA<-subset(dhs_data, dhs_data[,"FULL"]==1|dhs_data[,"FULL"]==0)
REF_DATA[,"FULL"] <- as.numeric(REF_DATA[,"FULL"])
invisible(capture.output(REF_DATA<- REF_DATA %>%
group_by(REF_DATA[,l]) %>%
summarise(coverage=weighted.mean(FULL,v005))))
REF_DATA<-na.omit(REF_DATA)
REF_DATA<- filter(REF_DATA, coverage==max(REF_DATA$coverage))
value<-c(as.numeric(REF_DATA[,1][1]))
REF<-c(REF,value)
}
n = length(REF)
for (j in 1:n){
nameref <- paste("Ref",j, sep="")
dhs_data[,nameref] = REF[j]
}
# Correct geographiclabels if stored in sstate instead of v101
if(COUNTRY[1]=="Nigeria"){
GEO_CI<- c(val_labels(dhs_data$sstate))
} else{
if((COUNTRY[1]=="Uganda")&(YEAR>2012)){
GEO_CI<- c(val_labels(dhs_data$GEO))
} else {
if(COUNTRY[1]=="Jordan"){
GEO_CI<- c(val_labels(dhs_data$v024))
}else {
if(COUNTRY[1]=="Indonesia"){
GEO_CI<- c(val_labels(dhs_data$v024))
}else {
if(COUNTRY[1]=="Timor-Leste"){
GEO_CI<- c(val_labels(dhs_data$v024))
} else {
if(COUNTRY[1]=="Lesotho"){
GEO_CI<- c(val_labels(dhs_data$v024))
} else {
if(COUNTRY[1]=="Namibia"){
GEO_CI<- c(val_labels(dhs_data$v024))
if (length(GEO_CI)==14){
GEO_CI<- head(GEO_CI, -1)
}
} else {
if(COUNTRY[1]=="Zimbabwe"){
GEO_CI<- c(val_labels(dhs_data$v024))
} else {
if(COUNTRY[1]=="Egypt"){
GEO_CI<- c(val_labels(dhs_data$GEO))
} else {
if(COUNTRY[1]=="Yemen"){
GEO_CI<- c(val_labels(dhs_data$GEO))
} else {
if((COUNTRY[1]=="India") & (YEAR<=2007)){
GEO_CI<- c(val_labels(dhs_data$GEO))
} else {
if((COUNTRY[1]=="Madagascar") & (YEAR<=2021)){
GEO_CI<- c(val_labels(dhs_data$v024))
} else {
if((COUNTRY[1]=="Tanzania") & (YEAR<=2022)){
GEO_CI<- c(val_labels(dhs_data$v024))
} else {
if(FLAG[1]==1){
GEO_CI<- c(val_labels(dhs_data$GEO))
} else {
GEO_CI<- c(val_labels(dhs_data$v101))
}}}}}}}}}}}}}}
#Fix Uganda 2011
if((COUNTRY[1]=="Uganda")&(YEAR==2011)){
GEO_CI <- GEO_CI[1:10]
}
#Store Geographic Sub-unit Names
GEO_CI<- names(GEO_CI)
#Create Sub-National Composite Index Output Data Frames
CI_Results_GEO_Output <- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_Results_GEO_Output_95ciLB <- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_Results_GEO_Output_95ciUB <- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
HII_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
HII_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
HII_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Composite_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Composite_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Composite_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
#Create Sub-National Traditional Index Output Data Frames
AEG_Sex_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Rural_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Insurance_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
REG_Sex_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
REG_Rural_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
REG_Insurance_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
SII_Education_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
SII_Wealth_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
RII_Education_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
RII_Wealth_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_Wealth_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_Wealth_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_Wealth_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Wealth_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Wealth_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
CI_E_Wealth_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Wealth_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Wealth_Results_GEO_95ciLB_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
AEG_Wealth_Results_GEO_95ciUB_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
Coverage_Results_GEO_Output<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
Coverage_Results_GEO_Output_95ciLB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
Coverage_Results_GEO_Output_95ciUB<- data.frame(matrix(NA, nrow = length(GEO_CI), ncol = 1))
for (i in VACCINES){
# convert vaccines to to numeric vector
dhs_data[,i] <- as.numeric(dhs_data[,i])
#Create Vaccine-Specific Variables
underage_name <- paste("underage_",i, sep="")
dhs_data$underage_i = dhs_data[,underage_name]
outcome_name <- paste("",i, sep="")
dhs_data$outcome = dhs_data[, outcome_name]
dhs_data$new_location <- factor(dhs_data$v101)
# Create Design Matrix
design <- svydesign(data = dhs_data,
ids = ~caseid,
weights = ~v005)
# Subset Data to be only data where outcome data is available
data_i <- subset(dhs_data, dhs_data[,i]==1|dhs_data[,i]==0)
#Set Up Logistic Regression
if ((COUNTRY=="Maldives")&(YEAR==2016)){
if (i=="BCG"|i=="FULL"|i=="COMPLETE") {
logit_i <- svyglm(outcome ~
relevel(factor(new_location), ref = Ref1[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]),
design = design, family = binomial(link="logit"), data = data_i)
} else{
logit_i <- svyglm(outcome ~ factor(underage_i) +
relevel(factor(new_location), ref = Ref1[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]),
design = design, family = binomial(link="logit"), data = data_i)
}
} else {
if ((COUNTRY=="Yemen")&(YEAR==2013)){
if (i=="BCG"|i=="FULL"|i=="COMPLETE") {
logit_i <- svyglm(outcome ~
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]) +
relevel(factor(v481), ref = Ref6[1]),
design = design, family = binomial(link="logit"), data = data_i)
} else{
logit_i <- svyglm(outcome ~ factor(underage_i) +
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]) +
relevel(factor(v481), ref = Ref6[1]),
design = design, family = binomial(link="logit"), data = data_i)
}
} else {
if (min(data_i$v481)==max(data_i$v481)) {
if (i=="BCG"|i=="FULL"|i=="COMPLETE") {
logit_i <- svyglm(outcome ~
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]),
design = design, family = binomial(link="logit"), data = data_i)
} else{
logit_i <- svyglm(outcome ~ factor(underage_i) +
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]),
design = design, family = binomial(link="logit"), data = data_i)
}
} else {
if (i=="BCG"|i=="FULL"|i=="COMPLETE") {
logit_i <- svyglm(outcome ~
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]) +
relevel(factor(v481), ref = Ref6[1]),
design = design, family = binomial(link="logit"), data = data_i)
} else{
logit_i <- svyglm(outcome ~ factor(underage_i) +
relevel(factor(v101), ref = Ref1[1]) +
relevel(factor(v025), ref = Ref2[1]) +
relevel(factor(v106), ref = Ref3[1]) +
relevel(factor(v190), ref = Ref4[1]) +
relevel(factor(b4), ref = Ref5[1]) +
relevel(factor(v481), ref = Ref6[1]),
design = design, family = binomial(link="logit"), data = data_i)
}
}
}
}
summary(logit_i)
# Calculate Predicted Probabilities & Store in Pred_Probs Object
if ((COUNTRY=="Maldives") & (YEAR==2016)){
pred_probs <- data.frame(cbind(logit_i$fitted.values, as.numeric(names(logit_i$fitted.values))))
names(pred_probs)[2] <- 'MERGE_ID'
names(pred_probs)[1] <- 'hci_du.response'
} else {
pred_probs <- data.frame(hci=predict(logit_i, data = dhs_data, type="response",na.action = na.exclude))
}
# Computing Direct Unfairness Metric
# Compute predicted probability holding child age (fair equity) at reference category (DTP1_underage==0)
data_i_unfair <- data.frame(data_i)
data_i_unfair$underage_i <- replace(data_i_unfair[,underage_name],data_i_unfair[,underage_name]==1,0)
# Create New Predictions on Direct Unfairness Dataset
pred_probs_2 <- data.frame(hci_du = predict(logit_i, data_i_unfair, type="response"))
# Computing Predicted Fairness Metric
# Compute predicted probability holding all unfair variables at reference levels (fairness equity) at reference category levels
data_i_fair <- data.frame(data_i)
# Create Reference Level Variable Dataset for all of the Fair Predictors
# Set Unfair Predictors to Reference Levels