-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_DHS_conn.R
More file actions
146 lines (120 loc) · 3.78 KB
/
Copy path01_DHS_conn.R
File metadata and controls
146 lines (120 loc) · 3.78 KB
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
#----- Load packages
pacman::p_load(
here,
rdhs,
survey,srvyr,
haven,
plotly,
tidyverse,
kableExtra,
tools,
Hmisc
)
#---------------------------
# Retrieve data
#---------------------------
#----- Link to API
## set up your credentials
set_rdhs_config(email = "************",
password_prompt = TRUE,
project = "**************",
config_path = "rdhs.json",
global=FALSE)
Sys.setenv("rdhs_RENVIRON_PERMISSION"=1)
Sys.setenv(rdhs_DATA_TABLE = "TRUE")
#----- Retreive data
## Identify all DHS surveys for the following countries
countries <- dhs_countries()
cc <- c("AO","ET", "KH", "KY", "MW", "CM", "BJ", "CF", "ML", "NP", "PK", "ZA",
"BU", "BD", "GM", "HT", "LB", "NG", "PG", "PH", "SN", "SL", "TJ", "TZ",
"TL", "UG", "ZM", "ZW", "BF", "CI", "GH", "GA", "KE", "MZ", "MD", "RW",
"GN", "ID", "MM", "NI", "AF", "TD", "CG", "CD", "BO", "KM", "SZ", "GU",
"LS", "NM", "ST", "TG", "YE", "HN", "MR")
# "AO" = Angola
# "ET" = Ethiopia
# "KH" = Cambodia
# "KY" = Kyrgyzstan
# "MW" = Malawi
# "CM" = Cameroon
# "BJ" = Benin
# "CF" = Central African Republic
# "ML" = Mali
# "NP" = Nepal
# "PK" = Pakistan
# "ZA" = South Africa
# "BU" = Burkina Faso
# "BD" = Bangladesh
# "GM" = Gambia
# "HT" = Haiti
# "LB" = Lebanon
# "NG" = Nigeria
# "PG" = Papua New Guinea
# "PH" = Philippines
# "SN" = Senegal
# "SL" = Sierra Leone
# "TJ" = Tajikistan
# "TZ" = Tanzania
# "TL" = Timor-Leste
# "UG" = Uganda
# "ZM" = Zambia
# "ZW" = Zimbabwe
# "BF" = Burkina Faso
# "CI" = Côte d'Ivoire
# "GH" = Ghana
# "GA" = Gabon
# "KE" = Kenya
# "MZ" = Mozambique
# "MD" = Moldova
# "RW" = Rwanda
# "GN" = Guinea
# "ID" = Indonesia
# "MM" = Myanmar
# "NI" = Nicaragua
# "AF" = Afghanistan
# "TD" = Chad
# "CG" = Republic of Congo (Congo-Brazzaville)
# "CD" = Democratic Republic of the Congo (Congo-Kinshasa)
# "BO" = Bolivia
# "KM" = Comoros
# "SZ" = Eswatini (Swaziland)
# "GU" = Guatemala
# "LS" = Lesotho
# "NM" = Namibia
# "ST" = São Tomé and Príncipe
# "TG" = Togo
# "YE" = Yemen
# "HN" = Honduras
# "MR" = Mauritania
# Use rdhs to retreive datasets, downloading them from DHS website if not already in the rdhs cache.
# Surveys from 2012
surveys <- dhs_surveys(countryIds = cc, surveyYearStart=2012, surveyType = "DHS")
surveys <- surveys %>% dplyr::group_by(DHS_CountryCode) %>% filter(SurveyYear == max(SurveyYear))
# Identify Individual recode datasets corresponding to these surveys and select the most recent.
step <- dhs_datasets(fileType = "IR", fileFormat = "flat")
ird <- step[which(step$SurveyId %in% surveys$SurveyId),]
ird <- ird %>% dplyr::group_by(DHS_CountryCode) %>% filter(SurveyYear == max(SurveyYear))
# Use rdhs to retreive datasets, downloading them from DHS website if not already in the rdhs cache.
Sys.setenv("rdhs_LOUD_DOWNLOAD" = TRUE)
ird$path <- unlist(get_datasets(ird$FileName,clear_cache = TRUE))
#create list and select important indicators for zd, VAS and deworming
ir <- list()
for(survid in ird$SurveyId){
print(survid)
dat <- readRDS(ird[which(ird$SurveyId == survid),]$path)
dat <- dat[grep("caseid|v001|v005|v002|h3_1|h34_1||h43_1", names(dat))]
ir[[survid]] <- dat
}
# Add survey-level variables
ir <- Map(data.frame,
SurveyId = ird$SurveyId,
CountryName = ird$CountryName,
SurveyYear = ird$SurveyYear,
ir)
# Convert variables to factors and scale weights
ir <- lapply(ir, function(x) {
x$SurveyId <- factor(x$SurveyId)
x$CountryName <- factor(x$CountryName)
x$SurveyYear <- factor(x$SurveyYear)
x$v005 <- x$v005 / 1000000
return(x)
})