-
Notifications
You must be signed in to change notification settings - Fork 1
/
4-1_calculations.R
269 lines (206 loc) · 8.54 KB
/
4-1_calculations.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
### 4.1 Country calculations
# This script takes the 10th percentile frontier and the scaled country
# projections and calculates the value of eliminating avoidable mortality
# by cause and country/region.
# 1 Loading data ----------------------------------------------------------
# Applying the standard project environment
applyEnv()
# Loading data
sarahLoad(c("country_info", "country_scaled", "frontier_scaled", "population"),
folder = "data/processed")
envelope <- read.csv("data/input/chang_envelope.csv", as.is = TRUE) %>%
filter(scenario == "Base")
# Defining focus years
focus <- c(2000, 2019, 2050)
# 2 Mortality differential ------------------------------------------------
# Prepping country data
country_scaled %<>%
filter(year %in% focus) %>%
mutate(sex_match = ifelse(causename %in% sex.specific, sex, NA)) %>%
dplyr::select(iso3, year, age, sex_match, sex, ghecause, causename, dths_rate) %>%
arrange(iso3, year, age, ghecause, sex)
# Prepping frontier data
frontier_scaled %<>%
filter(definition == "10th percentile", year %in% focus) %>%
dplyr::select(year, age, sex_match = sex, ghecause, causename, frontier) %>%
arrange(year, age, ghecause, sex_match)
# Prepping population data
population %<>%
filter(year %in% focus) %>%
arrange(region, iso3, year)
# Ensuring equivalence between regions and world
check1 <- population %>% filter(region == "World") %>% select(-region) %>%
arrange(iso3, year, age, sex)
check2 <- population %>% filter(region != "World") %>% select(-region) %>%
arrange(iso3, year, age, sex)
all.equal(check1, check2)
# Combining country and frontier data
data <- inner_join(country_scaled, frontier_scaled,
by = c("year", "age", "sex_match", "ghecause", "causename")) %>%
mutate_at(vars(dths_rate, frontier), ~ . /100000) %>%
dplyr::select(-sex_match) %>%
arrange(iso3, ghecause, age, sex, year)
# Checking data
containsNA(data)
# 3 Calculations ----------------------------------------------------------
# * delta -----------------------------------------------------------------
# Difference between frontier mortality and country mortality
delta <- data %>%
mutate(delta = exp(-1 * frontier) - exp(-1 * dths_rate)) %>%
mutate(delta = ifelse(delta < 0, 0, delta))
# Checking data
containsNA(delta)
# * p ---------------------------------------------------------------------
# Proportion of avoidable mortality by cause
temp1 <- delta %>%
left_join(cause_hierarchy %>% select(ghecause, starts_with("mece")),
by = "ghecause") %>%
pivot_longer(cols = starts_with("mece"), names_to = "mece.lvl", values_to = "mece.ind") %>%
mutate(mece.lvl = as.factor(as.numeric(substr(mece.lvl, nchar(mece.lvl), nchar(mece.lvl)))))
temp2 <- list()
for(i in unique(temp1$mece.lvl)){
temp2[[i]] <- temp1 %>%
filter(mece.lvl == i, mece.ind) %>%
group_by(iso3, year, age, sex, mece.lvl) %>%
mutate(Delta = sum(delta)) %>%
ungroup() %>% select(-mece.ind)
if(i == tail(unique(temp1$mece.lvl), 1)){
temp2 <- bind_rows(temp2)
}
}
p <- temp2 %>%
mutate(p = delta / Delta) %>%
select(iso3, year, age, sex, ghecause, causename, mece.lvl, everything()) %>%
arrange(iso3, sex, age, ghecause, mece.lvl)
# Checking data
# - Verifying p sums to 1
check <- p %>%
group_by(iso3, year, age, sex, mece.lvl) %>%
summarize(p = sum(p), .groups = "drop")
check %>% filter(round(p, 4) != 1)
# - Checking NA, NAN, infinite
containsNA(p)
p <- p %>% mutate(p = ifelse(is.nan(p), 0, p))
# - Checking again NA, NAN, infinite
containsNA(p)
# * alpha ------------------------------------------------------------------
# Age-sex weights
alpha <- population %>%
filter(region != "World") %>%
group_by(iso3, year, sex) %>%
mutate(alpha = pop / sum(pop)) %>%
ungroup() %>%
select(iso3, year, age, sex, alpha)
# Checking data
# = Verifying alpha sums to 1
check <- alpha %>%
group_by(iso3, year, sex) %>%
summarize(alpha = sum(alpha), .groups = "drop")
check %>% filter(round(alpha, 4) != 1)
# - Checking NA, NAN, infinite
containsNA(alpha)
# - Checking consistency w envelope
check <- alpha %>%
left_join(envelope %>%
select("iso3", "year", "age", "sex", "pop.sex.weight"),
by = c("iso3", "year", "age", "sex"))
check %>% filter(round(alpha, 4) != round(pop.sex.weight, 4))
alpha <- p %>% left_join(alpha, by = c("iso3", "year", "age", "sex"))
# * v.c --------------------------------------------------------------------
envelope2 <- envelope %>%
filter(region != "World") %>%
select(iso3, year, age, sex, b_log_1yr) %>%
unique()
v.country <- inner_join(alpha, envelope2, by = c("iso3", "year", "age", "sex")) %>%
mutate(v.c = alpha * p * b_log_1yr) %>%
group_by(iso3, year, sex, ghecause, causename, mece.lvl) %>%
summarize(v.c = sum(v.c), .groups = "drop") %>%
arrange(iso3, year, sex, mece.lvl, ghecause)
country_calculations <- v.country
# __ + country_calculations -----------------------------------------------
sarahSave("country_calculations", folder = "output/data")
write.csv(country_calculations, file = "output/data/country_calculations.csv",
na = "", row.names = FALSE)
# * w ---------------------------------------------------------------------
# Country weights
country.w <- population %>%
filter(iso3 %in% unique(v.country$iso3)) %>%
group_by(region, iso3, year, sex) %>%
summarize(pop = sum(pop), .groups = "drop") %>%
group_by(region, year, sex) %>%
mutate(w = pop / sum(pop)) %>%
ungroup() %>% select(-pop)
# Checking data
# = Verifying alpha sums to 1
check <- country.w %>%
group_by(region, year, sex) %>%
summarize(w = sum(w), .groups = "drop")
check %>% filter(round(w, 4) != 1)
# - Checking NA, NAN, infinite
containsNA(country.w)
w <- left_join(v.country, country.w, by = c("iso3", "year", "sex"), relationship = "many-to-many") %>%
select(region, iso3, everything()) %>%
arrange(region, iso3)
# * v.r --------------------------------------------------------------------
v.region <- w %>%
mutate(v.r = w * v.c) %>%
group_by(region, year, sex, ghecause, causename, mece.lvl) %>%
summarize(v.r = sum(v.r), .groups = "drop") %>%
arrange(region, year, sex, mece.lvl, ghecause)
# Checking data
# = Verifying China and India v.c = v.r
check <- v.country %>% filter(iso3 %in% c("CHN", "IND")) %>%
mutate(region = ifelse(iso3 == "CHN", "China", "India")) %>%
left_join(v.region,
by = c("year", "sex", "ghecause", "causename", "mece.lvl", "region"))
check %>% filter(v.c != v.r)
# - Checking NA, NAN, infinite
containsNA(v.region)
# Adding total (sex-weighting)
# Sex weights
sex.w <- population %>%
filter(iso3 %in% unique(v.country$iso3)) %>%
group_by(region, year, sex) %>%
summarize(pop = sum(pop), .groups = "drop") %>%
group_by(region, year) %>%
mutate(w = pop / sum(pop)) %>%
ungroup() %>% select(-pop)
temp1 <- v.region %>%
left_join(sex.w, by = join_by(region, year, sex)) %>%
mutate(v.r = v.r * w) %>%
group_by(region, year, ghecause, causename, mece.lvl) %>%
summarize(v.r = sum(v.r), .groups = "drop") %>%
mutate(sex = 3)
v.region <- bind_rows(v.region, temp1) %>%
arrange(region, year, sex)
check <- v.region %>%
pivot_wider(id_cols = c(region, year, ghecause, causename, mece.lvl), names_from = sex, values_from = v.r) %>%
mutate(check = `3` < `2` & `3` > `1` | `3` < `1` & `3` > `2`)
# * R_bar -----------------------------------------------------------------
R_bar <- v.region %>%
group_by(region, year, sex, mece.lvl) %>%
dplyr::mutate(R_bar = v.r / sum(v.r))
# Checking data
# = Verifying alpha sums to 1
check <- R_bar %>%
group_by(region, year, sex, mece.lvl) %>%
summarize(R_bar = sum(R_bar), .groups = "drop")
check %>% filter(round(R_bar, 4) != 1)
# - Checking NA, NAN, infinite
containsNA(R_bar)
region_calculations <- R_bar
# __ + region_calculations ------------------------------------------------
sarahSave("region_calculations", folder = "output/data")
write.csv(region_calculations, file = "output/data/region_calculations.csv",
na = "", row.names = FALSE)
# * roc -------------------------------------------------------------------
roc <- region_calculations %>%
pivot_wider(id_cols = c(region, sex, ghecause, causename, mece.lvl),
names_from = year, values_from = v.r) %>%
mutate(roc1 = (`2019` / `2000`)^(1/(2019-2000)) - 1,
roc2 = (`2050` / `2019`)^(1/(2050-2019)) - 1) %>%
select(region, sex, ghecause, causename, mece.lvl, roc1, roc2)
# __ + roc -----------------------------------------------------------------
sarahSave("roc", folder = "output/data")
write.csv(roc, file = "output/data/roc.csv",
na = "", row.names = FALSE)