-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelling.Rmd
541 lines (400 loc) · 16.8 KB
/
modelling.Rmd
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
---
title: 'Exposure and Fixed-Effects modelling '
output: html_notebook
---
# Introduction
This aim of this research is to build a series of models which include the following response variables:
1. Male age-standardised mortality rates (all-cause)
2. Female age-standardised mortality rates (all-cause)
3. Relative difference in all-cause rates (1 / 2)
4. Absolute difference in all-cause rates (1 - 2)
5. Male age-standardised mortality rates (NCDs)
6. Female age-standardised mortality rates (NCDs)
7. Relative difference in NCD rates (5 / 6)
8. Absolute differnece in NCD rates (5 - 6)
This will be done for each country (level 3 in location hierarchy).
And a series of exposures from the World Bank will be used:
1. GDP per cap by purchasing power parity
2. Male literacy rates
3. Female literacy rates
4. etc etc
# Loading GBD data
- Source is [here](http://s3.healthdata.org/gbd-api-2016-production/3f1a784fe33bf6d9447c7b90e8828c3f_files/IHME-GBD_2016_DATA-3f1a784f-1.zip)
```{r}
require(tidyverse)
source("scripts/download_completed_request.R")
```
Will `read_csv_directly` work?
```{r, eval = F}
asr_dta <- read_csv_directly(
url_loc = "http://s3.healthdata.org/gbd-api-2016-production/3f1a784fe33bf6d9447c7b90e8828c3f_files/IHME-GBD_2016_DATA-3f1a784f-1.zip",
csv_name = "IHME-GBD_2016_DATA-3f1a784f-1.csv")
```
*Note:* Though `read_csv_directly()` works as a function, the links are not permanent, so downloading them is preferable. The aim should be to reproduce the above and check with the online webpage to confirm.
```{r, eval = F}
download_completed_request("http://s3.healthdata.org/gbd-api-2016-production/67c84b9e938574bd56e66ab1c58653db_files",
"IHME-GBD_2016_DATA-67c84b9e-",
"data/age_standardised_allcause_ncd/"
)
```
```{r}
asr_dta <- read_csv("data/age_standardised_allcause_ncd/1.csv")
```
Now to link this to the hierarchy
```{r}
gbd_locations <- readxl::read_excel("raw_data/IHME_GBD_2016_CODEBOOK/IHME_GBD_2016_GBD_LOCATION_HIERARCHY_Y2018M05D23.XLSX")
gbd_country_ids <- gbd_locations %>%
filter(location_id %in% 1:218) %>%
filter(level == 3) %>%
pull(location_id)
asr_dta %>%
filter(location_id %in% gbd_country_ids) %>%
filter(measure_name == "Deaths") %>%
select(location = location_name, year, sex = sex_name, cause = cause_name, std_mort_rate = val) -> mort_rate_data
asr_dta %>%
filter(location_id %in% gbd_country_ids) %>%
filter(measure_name == "DALYs (Disability-Adjusted Life Years)") %>%
select(location = location_name, year, sex = sex_name, cause = cause_name, std_mort_rate = val) -> daly_rate_data
```
And now to join this to the world bank measures
```{r, cache = T}
wb_data <- read_rds("data/world_bank/extracted_indicators.RData")
nrow(wb_data)
gdp_percap_data <- wbstats::wb(indicator= "NY.GDP.PCAP.PP.CD") %>% as_tibble()
nrow(gdp_percap_data)
wb_data <- bind_rows(wb_data, gdp_percap_data)
rm(gdp_percap_data)
nrow(wb_data)
```
join to mort rate
```{r}
mort_rate_data_joined <- mort_rate_data %>%
mutate(country = location) %>%
inner_join(
wb_data %>%
mutate(year = as.integer(date)) %>%
select(year, country, indicator_id = indicatorID, indicator, indicator_value = value)
)
```
```{r}
daly_rate_data_joined <- daly_rate_data %>%
mutate(country = location) %>%
inner_join(
wb_data %>%
mutate(year = as.integer(date)) %>%
select(year, country, indicator_id = indicatorID, indicator, indicator_value = value)
)
```
Models by gender and indicator
```{r}
pull_indicator_coeff <- function(x){x %>% filter(term == "indicator_value") %>% pull(estimate) }
pull_indicator_se <- function(x){x %>% filter(term == "indicator_value") %>% pull(std.error) }
simple_models <- mort_rate_data_joined %>%
filter(cause == "All causes") %>%
select(country, year, sex, std_mort_rate, indicator_id, indicator, indicator_value) %>%
filter(!is.na(indicator_value)) %>%
group_by(indicator_id) %>%
nest() %>%
mutate(male_model = map(
data,
function(x) {
x %>%
filter(sex == "Male") %>%
lm(std_mort_rate ~ year + indicator_value, data = .)
}
)
) %>%
mutate(female_model = map(
data,
function(x) {
x %>%
filter(sex == "Female") %>%
lm(std_mort_rate ~ year + indicator_value, data = .)
}
)
) %>%
mutate(
male_model_tidy = map(male_model, broom::tidy),
female_model_tidy = map(female_model, broom::tidy)
) %>%
mutate(
male_indicator_coeff = map_dbl(male_model_tidy, pull_indicator_coeff),
female_indicator_coeff = map_dbl(female_model_tidy, pull_indicator_coeff),
male_indicator_se = map_dbl(male_model_tidy, pull_indicator_se),
female_indicator_se = map_dbl(female_model_tidy, pull_indicator_se)
)
```
Note: for some of these indicators the log should probably be used
```{r, results = "asis"}
simple_models %>%
select(indicator_id, male_indicator_coeff:female_indicator_se) %>%
left_join(wb_data %>% select(indicator_id = indicatorID, indicator) %>% distinct) %>%
mutate(
male_t = male_indicator_coeff / male_indicator_se,
female_t = female_indicator_coeff / female_indicator_se
) %>%
select(indicator_id, indicator, male_t, female_t) %>%
arrange(desc(abs(male_t))) %>%
mutate(
male_rank = 1 + length(male_t) - rank(abs(male_t)),
female_rank = 1 + length(female_t) - rank(abs(female_t))) %>%
filter(abs(male_t) > 2 | abs(female_t) > 2) %>%
mutate(male_t = round(male_t, 2), female_t = round(female_t, 2)) %>%
mutate(diff = male_rank - female_rank) %>%
knitr::kable(caption = "Signal strength of indicators on male and female age-standardised all-cause mortality trends")
```
The above shows the 'signal strength' of the different indicators once year is controlled for. For both genders GDP per capital has the strongest signal, followed by gender parity in primary school enrollment.
For males, age-standardised mortality rates are then most strongly predicted by the female urban population (% of total), followed by male urban population as % of total.
For females, the third strongest signal is gender parity in primary and secondary school enrollment, followed by gender parity in secondary school (only) enrollment rates.
# As above, but CVD only
```{r}
produce_table <- function(DTA, this_cause, this_caption){
DTA %>%
filter(cause == this_cause) %>%
select(country, year, sex, std_mort_rate, indicator_id, indicator, indicator_value) %>%
filter(!is.na(indicator_value)) %>%
group_by(indicator_id) %>%
nest() %>%
mutate(male_model = map(
data,
function(x) {
x %>%
filter(sex == "Male") %>%
lm(std_mort_rate ~ year + indicator_value, data = .)
}
)
) %>%
mutate(female_model = map(
data,
function(x) {
x %>%
filter(sex == "Female") %>%
lm(std_mort_rate ~ year + indicator_value, data = .)
}
)
) %>%
mutate(
male_model_tidy = map(male_model, broom::tidy),
female_model_tidy = map(female_model, broom::tidy)
) %>%
mutate(
male_indicator_coeff = map_dbl(male_model_tidy, pull_indicator_coeff),
female_indicator_coeff = map_dbl(female_model_tidy, pull_indicator_coeff),
male_indicator_se = map_dbl(male_model_tidy, pull_indicator_se),
female_indicator_se = map_dbl(female_model_tidy, pull_indicator_se)
) %>%
select(indicator_id, male_indicator_coeff:female_indicator_se) %>%
left_join(wb_data %>% select(indicator_id = indicatorID, indicator) %>% distinct) %>%
mutate(
male_t = male_indicator_coeff / male_indicator_se,
female_t = female_indicator_coeff / female_indicator_se
) %>%
select(indicator_id, indicator, male_t, female_t) %>%
arrange(desc(abs(male_t))) %>%
mutate(
male_rank = 1 + length(male_t) - rank(abs(male_t)),
female_rank = 1 + length(female_t) - rank(abs(female_t))) %>%
filter(abs(male_t) > 2 | abs(female_t) > 2) %>%
mutate(male_t = round(male_t, 2), female_t = round(female_t, 2)) %>%
mutate(diff = male_rank - female_rank) %>%
knitr::kable(caption = this_caption)
}
```
```{r, results = "asis"}
mort_rate_data_joined %>%
produce_table(
this_cause = "Non-communicable diseases",
this_caption = "Signal strength of indicators on male and female age-standardised NCD mortality trends"
)
```
Gender differences in the signal strength of these indicators appear much larger for NCD mortality than all cause mortality. Note also that the direction of effects goes in the opposite direction for some indicators, though only for the Mo Ibrahim index are the effects in opposite directions and the t values > 2.
The rank importance of access to anti-retroviral drugs is increased, to 2nd and 3rd place after GDP per capita.
# Signal strength, all cause, DALY
Now to look at the equivalent for DALYs
```{r, results = "asis"}
daly_rate_data_joined %>%
produce_table(
this_cause = "All causes",
this_caption = "Signal strength of indicators on male and female age-standardised all-cause DALY rate trends")
```
For DALY rate trends, the signal strength of school enrollment gender parity is stronger for females than that of GDP per capita.
Now, finally, to look at DALY rates related to NCDs
```{r, results = "asis"}
daly_rate_data_joined %>%
produce_table(
this_cause = "Non-communicable diseases",
this_caption = "Signal strength of indicators on male and female age-standardised NCD DALY rate trends")
```
Let's now try to regress this on abs or rel difference
```{r}
diffs_mort_joined <- mort_rate_data_joined %>%
select(location, year, sex, cause, std_mort_rate) %>%
distinct() %>%
spread(sex, std_mort_rate) %>%
mutate(rel = Male / Female, abs = Male - Female, logrel = log(rel)) %>%
select(location, year, cause, rel, abs, logrel) %>%
mutate(country = location) %>%
inner_join(
wb_data %>%
mutate(year = as.integer(date)) %>%
select(year, country, indicator_id = indicatorID, indicator, indicator_value = value)
)
```
```{r}
diffs_daly_joined <- daly_rate_data_joined %>%
select(location, year, sex, cause, std_mort_rate) %>%
distinct() %>%
spread(sex, std_mort_rate) %>%
mutate(rel = Male / Female, abs = Male - Female, logrel = log(rel)) %>%
select(location, year, cause, rel, abs, logrel) %>%
mutate(country = location) %>%
inner_join(
wb_data %>%
mutate(year = as.integer(date)) %>%
select(year, country, indicator_id = indicatorID, indicator, indicator_value = value)
)
```
Now produce similar tables as above
```{r}
produce_diff_table <- function(DTA, this_cause, this_caption){
DTA %>%
gather(rel:logrel, key = "outcome", value = "outcome_value") %>%
filter(cause == this_cause) %>%
select(country, year, outcome, outcome_value, indicator_id, indicator, indicator_value) %>%
filter(!is.na(indicator_value)) %>%
group_by(indicator_id) %>%
nest() %>%
mutate(rel_model = map(
data,
function(x) {
x %>%
filter(outcome == "rel") %>%
lm(outcome_value ~ year + indicator_value, data = .)
}
)
) %>%
mutate(abs_model = map(
data,
function(x) {
x %>%
filter(outcome == "abs") %>%
lm(outcome_value ~ year + indicator_value, data = .)
}
)
) %>%
mutate(
rel_model_tidy = map(rel_model, broom::tidy),
abs_model_tidy = map(abs_model, broom::tidy)
) %>%
mutate(
rel_indicator_coeff = map_dbl(rel_model_tidy, pull_indicator_coeff),
abs_indicator_coeff = map_dbl(abs_model_tidy, pull_indicator_coeff),
rel_indicator_se = map_dbl(rel_model_tidy, pull_indicator_se),
abs_indicator_se = map_dbl(abs_model_tidy, pull_indicator_se)
) %>%
select(indicator_id, rel_indicator_coeff:abs_indicator_se) %>%
left_join(wb_data %>% select(indicator_id = indicatorID, indicator) %>% distinct) %>%
mutate(
rel_t = rel_indicator_coeff / rel_indicator_se,
abs_t = abs_indicator_coeff / abs_indicator_se
) %>%
select(indicator_id, indicator, rel_t, abs_t) %>%
arrange(desc(abs(rel_t))) %>%
mutate(
rel_rank = 1 + length(rel_t) - rank(abs(rel_t)),
abs_rank = 1 + length(abs_t) - rank(abs(abs_t))) %>%
filter(abs(rel_t) > 2 | abs(abs_t) > 2) %>%
mutate(rel_t = round(rel_t, 2), abs_t = round(abs_t, 2)) %>%
knitr::kable(caption = this_caption)
}
```
```{r, results = 'asis'}
diffs_mort_joined %>%
produce_diff_table(
this_cause = "All causes",
this_caption = "Signal strength of indicators: gender inequalities in health; mort due to all causes")
```
```{r, results = 'asis'}
diffs_mort_joined %>%
produce_diff_table(
this_cause = "Non-communicable diseases",
this_caption = "Signal strength of indicators: gender inequalities in health; mort due to NCDs")
```
```{r, results = 'asis'}
diffs_daly_joined %>%
produce_diff_table(
this_cause = "All causes",
this_caption = "Signal strength of indicators: gender inequalities in health; DALYs due to all causes")
```
```{r, results = 'asis'}
diffs_daly_joined %>%
produce_diff_table(
this_cause = "Non-communicable diseases",
this_caption = "Signal strength of indicators: gender inequalities in health; DALYs due to NCDs")
```
Now a CART to group countries into quintile of gender inequality in outcomes
```{r}
tmp_dta <- diffs_mort_joined %>%
filter(cause == "All causes") %>%
mutate(quint_rel = cut(rel, breaks = quantile(rel, seq(0, 1, by = 0.2)), labels = F) %>% as.factor() ) %>%
select(location, year, cause, quint_rel, indicator_id, indicator_value) %>%
spread(indicator_id, indicator_value)
mod_simple <- tmp_dta %>%
rpart::rpart(quint_rel ~ year + NY.GDP.PCAP.PP.CD, data = ., method = "class")
# plot(model_01)
# text(model_01, cex = 0.8, use.n = T, xpd = T)
rpart.plot::prp(mod_simple, faclen = 0, cex = 0.8, varlen = 0, extra = 2, under = T)
mod_full <- tmp_dta %>%
select(-location) %>%
rpart::rpart(quint_rel ~ ., data = ., method = "class")
rpart.plot::prp(mod_full, faclen = 0, cex = 0.8, varlen = 0, extra = 2, under = T)
```
|indicator_id |indicator |
|:--------------------|:-------------------------------------------------------------------------------------|
| NY.GDP.PCAP.PP.CD | GDP per capita, PPP (current international $) |
| SE.ENR.SECO.FM.ZS | School enrollment, secondary (gross), gender parity index (GPI) |
| MO.INDEX.XQ | Overall Mo Ibrahim index |
| SL.UEM.1524.FM.ZS | Ratio of female to male youth unemployment rate (% ages 15-24) (modeled ILO estimate) |
| SP.URB.TOTL.MA.ZS | Urban population, male (% of total) |
| SE.ENR.TERT.FM.ZS | School enrollment, tertiary (gross), gender parity index (GPI) |
So, the above model seems to be suggesting:
* If GDP per cap < $3k, gender parity in 2ndry school enrolment is low, and Mo Ibrahim index high -> lowest/negative male gender disadvantage
* If GDP per cap < $3k, gender parity in 2ndry school enrolment is now, and Mo Ibrahim index low -> 2nd quintile
* If GDP per cap < $3k, gender parity in 2ndry school enrolment is high -> 3rd quintile
* If GDP per cap < $13k -> 3rd quintile
* If GDP per cap > $13k, high female:male youth unemployment rate -> 3rd quintile
* If GDP per cap > $13k, < 38% of males in urban areas (?) -> 5th quintile
* If GDP per cap > $13k, >= 38% of males in urban areas (?) but low gender parity index in tertiary school enrolment -> 5th quintile
* If GDP per cap > $13k, >= 38% of males in urban areas, high gender parity in tertiary school enrolment, but female:male youth unemployment >= 110 -> 5th quintile
* Otherwise 4th quintile
Now the same for NCDs
```{r}
tmp_dta <- diffs_mort_joined %>%
filter(cause == "Non-communicable diseases") %>%
mutate(quint_rel = cut(rel, breaks = quantile(rel, seq(0, 1, by = 0.2)), labels = F) %>% as.factor() ) %>%
select(location, year, cause, quint_rel, indicator_id, indicator_value) %>%
spread(indicator_id, indicator_value)
mod_simple <- tmp_dta %>%
rpart::rpart(quint_rel ~ year + NY.GDP.PCAP.PP.CD, data = ., method = "class")
# plot(model_01)
# text(model_01, cex = 0.8, use.n = T, xpd = T)
rpart.plot::prp(mod_simple, faclen = 0, cex = 0.8, varlen = 0, extra = 2, under = T)
mod_full <- tmp_dta %>%
select(-location) %>%
rpart::rpart(quint_rel ~ ., data = ., method = "class")
rpart.plot::prp(mod_full, faclen = 0, cex = 0.8, varlen = 0, extra = 2, under = T)
```
But what are these thresholds?
```{r}
diffs_mort_joined %>%
filter(cause == "All causes") %>%
pull(rel) %>%
quantile(seq(0, 1, by = 0.2)) -> tmp1
diffs_mort_joined %>%
filter(cause == "Non-communicable diseases") %>%
pull(rel) %>%
quantile(seq(0, 1, by = 0.2)) -> tmp2
tab <- rbind(tmp1, tmp2); rm(tmp1, tmp2)
row.names(tab) <- c("All causes", "NCDs")
tab
```