forked from ashattock/epi50
-
Notifications
You must be signed in to change notification settings - Fork 4
/
impact.R
571 lines (447 loc) · 15.2 KB
/
impact.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
###########################################################
# IMPACT
#
# An alternative to impact factor for representing impact as
# a function of vaccine coverage.
#
###########################################################
# ---------------------------------------------------------
# Parent function for calculating non-linear impact
# ---------------------------------------------------------
run_impact = function(metric) {
# Only continue if specified by run_module
if (!is.element(5, o$run_module)) return()
message("* Fitting impact functions: ", metric)
# ---- FVPs and impact estimates ----
message(" > Preparing FVP-impact data")
# Prepare impact-FVP data to fit to
data_dt = get_impact_data(metric)
# ---- Model fitting ----
message(" > Evaluating impact functions")
# Country-disease-vaccine-activity combinations
run_dt = data_dt %>%
select(d_v_a_id, country) %>%
unique() %>%
mutate(run_id = paste1(d_v_a_id, country))
# Iterate through d-v-a one by one
for (id in unique(run_dt$d_v_a_id)) {
# Details of this d_v_a
d_v_a_name = data.table(d_v_a_id = id) %>%
format_d_v_a_name() %>%
pull(d_v_a_name)
# Display progress message to user
message(" - ", d_v_a_name)
# Subset what to run, and data to use
run = run_dt[d_v_a_id == id]
data = data_dt[d_v_a_id == id]
# Initiate progress bar
pb = start_progress_bar(nrow(run))
# Run get_best_model
results_list = lapply(
X = run$run_id,
FUN = get_best_model,
run = run,
data = data,
pb = pb)
# Squash results into single datatable
results_dt = rbindlist(results_list)
# Save to file
save_rds(results_dt, "impact", "impact", metric, id)
# Close connections opened by sink
closeAllConnections()
}
# ---- Model selection ----
# Select best function for each country-d_v_a combination
model_selection(run_dt, metric)
# ---- Plot results ----
# Plot function selection statistics
plot_model_selection(metric)
# Plot impact function evaluation
plot_model_fits(metric)
}
# ---------------------------------------------------------
# Prepare impact-FVP data to fit to
# ---------------------------------------------------------
get_impact_data = function(metric) {
# Population size of each country over time
pop_dt = table("wpp_pop") %>%
lazy_dt() %>%
group_by(country, year) %>%
summarise(pop = sum(pop)) %>%
ungroup() %>%
as.data.table()
# Load impact estimates from VIMC (inlcuding imputed)
#
# NOTE: Result of imputation is already in cumulative form
vimc_dt = read_rds("impute", "impute", metric, "result", err = FALSE)
# Load static model impact estimates
static_dt = read_rds("static", metric, "averted_vaccine", err = FALSE) %>%
lazy_dt() %>%
# Scale results to per capita...
left_join(y = pop_dt,
by = c("country", "year")) %>%
mutate(fvps = fvps / pop,
impact = impact / pop) %>%
select(-pop) %>%
# Convert to cumulative FVP and impact...
group_by(country, d_v_a_id) %>%
mutate(fvps = cumsum(fvps),
impact = cumsum(impact)) %>%
ungroup() %>%
as.data.table()
# Impact estimates per capita from all sources
data_dt = rbind(vimc_dt, static_dt) %>%
mutate(impact_fvp = impact / fvps)
# Save to file
save_rds(data_dt, "impact", "impact", metric, "data")
return(data_dt)
}
# ---------------------------------------------------------
# Set of functions to fit - we'll determine the 'best'
# ---------------------------------------------------------
fn_set = function(params = FALSE, dict = FALSE) {
# Set of statistical models / functions we want to test
out = list(
lin = function(x, p) y = x * p[1],
exp = function(x, p) y = exponential_growth(x, p[1], p[2]),
log = function(x, p) y = logarithmic_growth(x, p[1], p[2]),
sig = function(x, p) y = sigmoidal_growth(x, p[1], p[2], p[3]))
# Alternative functionality - return number of params
if (params == TRUE)
out = c(lin = 1, exp = 2, log = 2, sig = 3)
# Alternative functionality - return dictionary
if (dict == TRUE)
out = c(
lin = "Linear gradient (1 parameter)",
exp = "Exponential growth (2 parameters)",
log = "Logarithmic growth (2 parameters)",
sig = "Sigmoidal growth (3 parameters)")
return(out)
}
# ---------------------------------------------------------
# Parent function to determine best fitting function
# ---------------------------------------------------------
get_best_model = function(id, run, data, pb) {
# Initiate trivial output
result = NULL
# Details of this run
this_run = run[run_id == id]
# Reduce data down to what we're interested in
fit_data_dt = data %>%
lazy_dt() %>%
filter(country == this_run$country,
d_v_a_id == this_run$d_v_a_id) %>%
select(x = fvps,
y = impact) %>%
# Multiply impact for more consistent x-y scales...
mutate(y = y * o$impact_scaler) %>%
as.data.table()
# Append the origin (zero vaccine, zero impact)
fit_data_dt = data.table(x = 1e-12, y = 0) %>%
rbind(fit_data_dt)
# Number of genuine data points (aside from the origin)
n_data = nrow(fit_data_dt) - 1
# Do not fit if insufficient data
if (n_data >= 1) {
# Declare x and y values for which we want to determine a relationship
x = fit_data_dt$x
y = fit_data_dt$y
# Functions we'll attempt to fit with
fns = fn_set()[fn_set(params = TRUE) <= n_data]
# Attempt to determine global minimum for each function
optim = run_optim(fns, x, y)
# Apply MCMC using assumed global optimum as strong prior
fit = run_mcmc(fns, optim, x, y)
# Determine AICc value for model suitability
result = model_quality(fns, fit, x, y, id)
}
# Update progress bar
pb$tick()
# ---- Diagnostic plots ----
# data_dt = data.table(x = x, value = y)
# models_dt = data.table(x = x)
#
# for (fn in names(fit))
# models_dt[[fn]] = fns[[fn]](x, fit[[fn]]$coef)
#
# models_dt %<>%
# pivot_longer(cols = -x) %>%
# select(fn = name, x, value) %>%
# arrange(fn, x) %>%
# as.data.table()
#
# plot1_dt = result %>%
# filter(param == "ll") %>%
# mutate(lab = paste0(
# fn, "\nll = ",
# round(value, 2))) %>%
# select(fn, lab) %>%
# left_join(y = models_dt,
# by = "fn")
#
# g1 = ggplot(plot1_dt) +
# aes(x = x, y = value) +
# geom_line(
# mapping = aes(colour = lab)) +
# geom_point(
# data = data_dt,
# colour = "black")
#
# plot2_dt = result %>%
# filter(param == "aicc",
# value == min(value)) %>%
# select(fn) %>%
# left_join(y = result,
# by = "fn") %>%
# filter(!is.na(iter))
#
# g2 = ggplot(plot2_dt) +
# aes(x = value,
# y = after_stat(scaled)) +
# geom_density() +
# facet_wrap(
# facets = vars(param),
# scales = "free_x")
return(result)
}
# ---------------------------------------------------------
# Attempt to determine global minimum for each function
# ---------------------------------------------------------
run_optim = function(fns, x, y) {
# Define an objective function to minimise - sum of squares
obj_fn = function(p, fn) {
# Squared difference
diff_sq = (y - fns[[fn]](x, p)) ^ 2
# The sum of the squared difference
obj_val = list(y = sum(diff_sq))
return(obj_val)
}
# Initiate optimal results list
optim = list()
# Iterate through stats models
for (fn in names(fns)) {
# Number of parameters for this model
n_pars = fn_set(params = TRUE)[[fn]]
par_ref = letters[1 : n_pars]
# Set lower and upper parameter bounds
lb = rep(1e-10, n_pars)
ub = rep(1e3, n_pars)
# Inititae list to store results
asd_results = list()
# Repeat optimisation several times
for (i in 1 : o$n_optim) {
# Different starting point each time
x0 = pmax(runif(n_pars), lb)
# Run ASD optimisation algorithm
asd_result = asd(
fn = obj_fn,
args = fn,
x0 = x0,
lb = lb,
ub = ub,
iters = 200)
# Store result and optimal parameters
asd_results[[i]] = c(
asd_result$y,
asd_result$x)
}
# Select best fitting parameters for this function
optim[[fn]] = do.call(rbind, asd_results) %>%
as_named_dt(c("y", par_ref)) %>%
# Sort by objective function value...
arrange(y) %>%
slice_head(n = 1) %>%
select(-y) %>%
as.list()
}
return(optim)
}
# ---------------------------------------------------------
# Apply MCMC using assumed global optimum as strong prior
# ---------------------------------------------------------
run_mcmc = function(fns, optim, x, y) {
# Log-likelihood function
likelihood_fn = function(p) {
# Set poor likelihood when parameter bounds are violated
if (any(p < 1e-10))
return(-1e6)
# Evaluate model emulator for given parameters
y_pred = fns[[fn]](x, p)
# Calculate the log-likelihood
ll = dnorm(
x = y_pred,
mean = y,
sd = sd(y - y_pred),
log = TRUE)
# Calculate log-prior for all parameters
lp = dnorm(
x = p,
mean = x0,
sd = x0 * o$prior_sd,
log = TRUE)
# Weighting to be applied to priors
#
# NOTE: Dividing by number of parameters such that more complex
# models are not double punished when computing AICc
prior_weight = o$prior_weight / length(p)
# Sum and appply weighting to priors
likelihood = sum(ll) + sum(lp) * prior_weight
return(likelihood)
}
# Wrapper function for MCMC call
mcmc_fn = function() {
# Call Metropolis-Hasting algorithm
mcmc_result = MCMCmetrop1R(
fun = likelihood_fn,
burnin = o$mcmc_burnin,
mcmc = o$mcmc_iter,
thin = o$mcmc_iter / o$mcmc_samples,
tune = 1.5,
seed = 1,
theta.init = x0,
optim.method = "L-BFGS-B",
optim.lower = 1e-10)
return(mcmc_result)
}
# We'll send noisy output to a null file
sink(nullfile())
# Initiate results list
fit = list()
# Iterate through stats models
for (fn in names(fns)) {
# Parameter reference
par_ref = names(optim[[fn]])
# Assumed global minimum
x0 = unlist(optim[[fn]])
# Wrap MCMC call in try catch in case of errors
mcmc_result = tryCatch(
expr = suppressWarnings(mcmc_fn()),
error = function(e) return())
# Store unless null result
if (!is.null(mcmc_result)) {
# Format resulting chain (burn-in already discarded)
mcmc_chain = mcmc_result %>%
as_named_dt(names(optim[[fn]]))
# Take mean of posteriors as best fitting coefficients
coef = colMeans(mcmc_result) %>%
setNames(par_ref)
# Store fit with associated log likelihood
fit[[fn]] = list(
chain = mcmc_chain,
coef = coef,
ll = likelihood_fn(coef))
}
}
# Sink the output
sink()
return(fit)
}
# ---------------------------------------------------------
# Determine model quality - primarily this is via AICc
# ---------------------------------------------------------
model_quality = function(fns, fit, x, y, run_id) {
# Return out if no fits succesful
if (length(fit) == 0)
return()
# ---- Model selection metrics ----
# Calculate AIC - adjusted for sample size
aicc = sapply(fit, aicc, n = length(y)) %>%
as.list() %>%
as.data.table() %>%
pivot_longer(cols = everything(),
names_to = "fn") %>%
mutate(param = "aicc") %>%
as.data.table()
# Extract log likelihood
ll = lapply(fit, function(a) a$ll) %>%
as.data.table() %>%
pivot_longer(cols = everything(),
names_to = "fn") %>%
mutate(param = "ll") %>%
as.data.table()
# ---- Model parameters ----
# Coefficients for each model
coef = unlist(lapply(fit, function(a) a$coef))
coef = tibble(var = names(coef), value = coef) %>%
separate(var, c("fn", "param")) %>%
as.data.table()
# ---- Parameter posteriors ----
# MCMC chains for each model
chains = lapply(fit, function(a) a$chain) %>%
as.data.table() %>%
mutate(iter = 1 : n()) %>%
pivot_longer(cols = -iter) %>%
separate(col = "name",
into = c("fn", "param"),
fill = "right") %>%
replace_na(list(param = "a")) %>%
select(fn, param, iter, value) %>%
arrange(fn, param, iter) %>%
as.data.table()
# ---- Concatenate output ----
# Squash all details into single datatable()
quality_dt = bind_rows(aicc, ll, coef, chains) %>%
mutate(run_id = run_id) %>%
select(run_id, fn, param, iter, value)
return(quality_dt)
}
# ---------------------------------------------------------
# Use AICc rather than AIC to reduce overfitting
# ---------------------------------------------------------
aicc = function(x, n) {
# See en.wikipedia.org/wiki/Akaike_information_criterion
# Number of parameters
k = length(x$coef)
# Log likelihood associated with these parameters
l = x$ll
# The usual AIC term
aic_term = 2*k - 2*l
# An additional penalty term for small sample size
pen_term = (2*k^2 + 2*k) / (n - k - 1)
# Sum these terms
aicc = aic_term + pen_term
return(aicc)
}
# ---------------------------------------------------------
# Select best function considering complexity
# ---------------------------------------------------------
model_selection = function(run_dt, metric) {
message(" > Selecting best functions")
# ---- Extract results ----
# All d-v-a combinations considered
d_v_a = unique(run_dt$d_v_a_id)
# Construct paths to results files
names = paste1("impact", metric, d_v_a)
files = paste0(o$pth$impact, names, ".rds")
# Extract best fitting function based on AICc
results_dt = lapply(files, read_rds) %>%
rbindlist() %>%
left_join(y = run_dt,
by = "run_id")
# ---- Model selection ----
# Select best model based on AICc or LL
selection_dt = results_dt %>%
filter(param %in% c("aicc", "ll")) %>%
# Transform log-likelihood so we search for the lowest...
mutate(value = ifelse(param == "ll", -value, value)) %>%
# Select models according to AICc and LL...
group_by(d_v_a_id, country, param) %>%
slice_min(value, n = 1, with_ties = FALSE) %>%
ungroup() %>%
# Model selection according to o$selection_metric...
filter(param == o$selection_metric) %>%
# Tidy up...
select(d_v_a_id, country, fn) %>%
as.data.table()
# Save to file
save_rds(selection_dt, "impact", "model_choice", metric)
# ---- Posterior chains ----
# Select best model based on AICc or LL
posteriors_dt = results_dt %>%
filter(iter > 0) %>%
inner_join(y = selection_dt,
by = c("d_v_a_id", "country", "fn")) %>%
select(d_v_a_id, country, fn, param, iter, value)
# Save to file
save_rds(posteriors_dt, "impact", "posteriors", metric)
}