-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample_data.qmd
286 lines (246 loc) · 7.85 KB
/
sample_data.qmd
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
# Appendix A: Sample Data
## Introduction
The sample data used in this book was generated from the Malawi Intergrated Household Survey Fifth Edition 2018-2019 downloaded from [here](https://microdata.worldbank.org/index.php/catalog/3818).
The data was generated randomly using the following code:
## Define functions used
### Create case_id generation
```{r}
generate_case_ids <- function(n) {
start_id <- 201011000001
end_id <- start_id + n-1
case_ids <- as.character(seq(start_id, end_id, by = 1))
return(case_ids)
}
```
### Create HHID generation function
```{r}
generate_HHIDs <- function(n) {
hhids <- sapply(1:n, function(x) {
paste(sample(c(0:9, letters[1:6]), 32, replace = TRUE), collapse = "")
})
return(hhids)
}
```
## Set seed and number of households to generate
```{r}
# Set seed
set.seed(123)
# Set number of households to generate
households <- 100
```
## Load Original data and extract food and unit lists
```{r}
# Import Malawi IHS5 HCES consumption module data
original_data <-
haven::read_dta(here::here("data-ignore", "IHS5", "HH_MOD_G1.dta"))
# Extract "standard" food list from the original data
food_list <-
original_data |>
dplyr::select(hh_g02) |>
dplyr::distinct()
# Extract "non-standard" food lists from the original data
other_food_list_codes <-
original_data |>
dplyr::distinct(hh_g02, hh_g01_oth) |>
dplyr::filter(hh_g01_oth != "") |>
dplyr::distinct(hh_g02) |>
dplyr::arrange()
other_food_list_options <-
original_data |>
dplyr::distinct(hh_g02, hh_g01_oth) |>
dplyr::filter(hh_g01_oth != "")
# Extract Food unit lists from the original data
food_unit_lists <-
original_data |>
dplyr::distinct(hh_g03b, hh_g03b_label, hh_g03b_oth, hh_g03c, hh_g03c_1)
# Extract the length of Number of foods from the food list
n_foods <- length(food_list$hh_g02)
```
## Data creation
### Create HHIDs
```{r}
# Creeate case_ids
case_id <- generate_case_ids(households)
# Generate HHIDs
hhids <- generate_HHIDs(households)
```
### Create data
```{r}
sample_data <- tibble::tibble(
case_id = rep(case_id, each = n_foods),
HHID = rep(hhids, each = n_foods),
hh_g00_1 = 2,
hh_g00_2 = 2,
food_list |> dplyr::slice(rep(1:dplyr::n(), households)),
hh_g01 = sample(
original_data$hh_g01,
# replace = T,
size = households * 142
)
) |>
# Add "other food items"
dplyr::rowwise() |>
dplyr::mutate(
hh_g01_oth = dplyr::case_when(
hh_g02 == 414 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 414) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 515 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 515) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 117 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 117) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 830 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 830) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 310 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 310) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 412 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 412) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 610 &
hh_g01 == 1 ~ sample(
dplyr::filter( other_food_list_options,hh_g02 == 610) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 916 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 916) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 209 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 209) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 709 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 709) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 818 &
hh_g01 == 1 ~ sample(
dplyr::filter(other_food_list_options,hh_g02 == 818) |> dplyr::pull(hh_g01_oth),
1
),
hh_g02 == 804 &
hh_g01 == 1 ~ sample(dplyr::filter(other_food_list_options,hh_g02 == 804) |> dplyr::pull(hh_g01_oth),
1
),
TRUE ~ ""
)
) |>
dplyr::mutate(hh_g03a = dplyr::case_when(hh_g01 == 1 ~ sample(c(1:10, 0.5:10), 1),
TRUE ~ NA)) |>
dplyr::rowwise() |>
dplyr::mutate(unit_key = dplyr::case_when(hh_g01 == 1 ~ sample(1:214, 1), TRUE ~
NA)) |>
dplyr::mutate(
hh_g03b = food_unit_lists$hh_g03b[unit_key],
hh_g03b_label = food_unit_lists$hh_g03b_label[unit_key],
hh_g03b_oth = food_unit_lists$hh_g03b_oth[unit_key],
hh_g03c = food_unit_lists$hh_g03c[unit_key],
hh_g03c_1 = food_unit_lists$hh_g03c_1[unit_key]
) |>
dplyr::select(
-unit_key,
"case_id",
"HHID",
"hh_g00_1",
"hh_g00_2",
"hh_g01",
"hh_g01_oth",
"hh_g02",
"hh_g03a",
"hh_g03b",
"hh_g03b_label",
"hh_g03b_oth",
"hh_g03c",
"hh_g03c_1"
)
```
```{r}
# Add the rest of the columns
sample_data <- original_data |> dplyr::filter(is.na(case_id)) |>
dplyr::bind_rows(sample_data)
```
```{r}
# Attach stata column labels
for (i in names(sample_data)){
attr(sample_data[[i]], "label") <- attr(original_data[[i]], "label")
}
```
```{r}
# Export sample data as stata file
haven::write_dta(sample_data,here::here("data","sample_data","MWI-IHSV","HH_MOD_G1_vMAPS.dta"))
```
### Create `hh_mod_a_filt.dta` file
```{r}
sample_data |>
dplyr::select(case_id,HHID) |>
dplyr::distinct() |>
dplyr::rowwise() |>
dplyr::mutate(region = sample(1:3,1)) |>
haven::write_dta(here::here("data","sample_data","MWI-IHSV","hh_mod_a_filt_vMAPS.dta"))
```
### Create `hh_roster.dta`
```{r}
# Import original roster from IHS5
ihs5_roster <- haven::read_dta(here::here("data-ignore", "IHS5", "HH_MOD_B.dta"))
# create a dataframe with the case_ids and HHIDs of our sample data
sample_roster <- sample_data |> dplyr::distinct(case_id,HHID)
# replicate each row a random number of times between 1 and 10 to simulate household members
n <- sample(1:10, nrow(sample_roster), replace = TRUE)
sample_roster <- sample_roster[rep(seq_len(nrow(sample_roster)), times = n), ]
# Create other variables
sample_roster <- sample_roster |>
dplyr::rowwise() |>
dplyr::mutate(hh_b03 = sample(ihs5_roster$hh_b03,1),
hh_b05a = sample(ihs5_roster$hh_b05a,1),
hh_b05b = dplyr::case_when(hh_b05a < 5~sample(1:11,1),TRUE~NA))
# Add the other blank columns from the original dataset
sample_roster <- ihs5_roster |>
dplyr::filter(case_id == "") |>
dplyr::bind_rows(sample_roster)
# Attach stata column labels
for (i in names(sample_roster)){
attr(sample_roster[[i]], "label") <- attr(ihs5_roster[[i]], "label")
}
# writeout the sample_ihs5_roster
haven::write_dta(sample_roster,here::here("data","sample_data","MWI-IHSV","HH_MOD_B_vMAPS.dta"))
```
### Create sample "HH_MOD_D.dta"
```{r}
# import original data
original_health <- haven::read_dta(here::here("data-ignore", "IHS5", "HH_MOD_D.dta"))
# Use the sample_roster to create a sample_health dataset
sample_health <- sample_roster |>
dplyr::select(case_id,HHID) |>
dplyr::rowwise()|>
dplyr::mutate(hh_d05a = sample(c(original_health$hh_d05a),1),
hh_d05b = sample(original_health$hh_d05b,1))
# Add the other blank columns from the original dataset
sample_health <- original_health |>
dplyr::filter(case_id == "") |>
dplyr::bind_rows(sample_health)
# Attach stata column labels
for (i in names(sample_health)){
attr(sample_health[[i]], "label") <- attr(sample_health[[i]], "label")
}
# writeout the sample_ihs5_roster
haven::write_dta(sample_health,here::here("data","sample_data","MWI-IHSV","HH_MOD_D_vMAPS.dta"))
```