forked from hfu915/dynamice_ph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulations.R
201 lines (148 loc) · 5.51 KB
/
simulations.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
############################################################
# SIMULATIONS
#
# Create, run, and process all simulations.
#
############################################################
# ---------------------------------------------------------
# Parent function for creating and running all simulations
# ---------------------------------------------------------
run_simulations = function() {
# Only continue if specified by do_step
if (!is.element(2, o$do_step)) return()
message("* Running vaccine simulations")
# Generate full set of simulations to run
sims = get_simulations()
# ---- Submit simulations to cluster ----
# Number of jobs to be run
n_jobs = sum(sims$run)
# Skip if nothing to run
if (n_jobs > 0) {
# Submit all jobs to the cluster (see auxiliary.R)
submit_cluster_jobs(n_jobs, "submit.sh", "run_sim")
# Throw an error if any cluster jobs failed (see auxiliary.R)
stop_if_errors(o$pth$log, o$err_file, err_tol = 1)
}
# ---- Concatenate output ----
# Aggregate results for each country
run_aggregate(sims)
}
# ---------------------------------------------------------
# Generate full set of simulations to run
# ---------------------------------------------------------
get_simulations = function() {
# ---- Full set of simulations ----
# Grid of countries and scenarios to run
sims = expand_grid(country = o$countries,
scenario = o$scenarios,
r0 = c(NA, o$vary_r0)) %>%
# Append scenario ID...
mutate(id = get_simulation_id(.),
.before = 1) %>%
arrange(scenario, country) %>%
as.data.table()
# ---- Skip existing sims ----
message(" > Identifying previously completed simulations")
# Extract IDs of sims that have already been run
exist_id = intersect(
str_remove(list.files(o$pth$sims), ".rds$"),
str_remove(list.files(o$pth$burden), ".rds$"))
# Logical whether simulation should be run / rerun
run_sim = !(sims$id %in% exist_id)
if (o$overwrite) run_sim[] = TRUE
# Skip any existing sims (unless overwriting)
sims %<>%
cbind(run = run_sim) %>%
mutate(job_num = cumsum(run * 1),
job_num = ifelse(run, job_num, NA))
# Save scenario dataframe to file
saveRDS(sims, file = paste0(o$pth$sims, "all_simulations.rds"))
# ---- Display number of sims ----
# Number of sims
n_total = nrow(sims)
n_run = sum(sims$run)
# Report total number of sims
message(" > Total number of simulations: ", thou_sep(n_total))
# Report number of sims we'll run now
message(" - Skipping: ", thou_sep(n_total - n_run))
message(" - Simulating: ", thou_sep(n_run))
return(sims)
}
# ---------------------------------------------------------
# Create simulation ID convention
# ---------------------------------------------------------
get_simulation_id = function(sims) {
# Combine scenario details to create sim ID
ids = sims %>%
mutate(r0_str = ifelse(is.na(r0), "def", r0),
r0_str = str_pad(r0_str, 2, pad = "0")) %>%
mutate(id = paste1(country, r0_str, scenario)) %>%
pull(id)
return(ids)
}
# ---------------------------------------------------------
# All steps to actually simulate the model
# ---------------------------------------------------------
run_sim = function(job_id) {
# ---- Details of this simulation ----
# Load full scenario vaccination details
scenarios_dt = fread(paste0(o$pth$config, "scenarios.csv"))
# Load full set of simulations
sims_dt = readRDS(paste0(o$pth$sims, "all_simulations.rds"))
# Select simulation assocaited with this job_id
sim = sims_dt %>%
filter(job_num == job_id) %>%
left_join(y = scenarios_dt,
by = "scenario") %>%
select(-run, -job_num, -scenario_name)
message(" > Running ", sim$id)
# ---- Load data for this simulation ----
# Prepare model input data
data = prepare_data(sim) # See prepare.R
# ---- Simulate model ----
# Run DynaMICE model
run_model(sim, data) # See model.R
# Estimate disease burden from model outcomes
run_burden(sim, data) # See model.R
message(" > Simulation complete")
}
# ---------------------------------------------------------
# Aggregate results for each country
# ---------------------------------------------------------
run_aggregate = function(sims) {
message(" > Aggregating simulation outputs")
# Initiate a progress bar
n = length(o$countries) * length(o$scenarios)
pb = start_progress_bar(n)
# Initiate a counter
i = 0
# Loop through countries and scenarios
for (country in o$countries) {
for (scenario in o$scenarios) {
# IDs for this country and scenario
ids = sims %>%
filter(country == !!country,
scenario == !!scenario) %>%
pull(id)
# Associated disease burden files
files = paste0(o$pth$burden, ids, ".rds")
# Load results and append details
results_dt = lapply(files, readRDS) %>%
rbindlist() %>%
left_join(y = sims,
by = "id") %>%
select(country, scenario, r0,
year, age, metric, value)
# Construct file name and path to save
save_name = paste1(country, scenario)
save_file = paste0(o$pth$compiled, save_name, ".rds")
# Save disease burden estimates
saveRDS(results_dt, file = save_file)
# Update progress bar
i = i + 1
setTxtProgressBar(pb, i)
}
}
# Close progress bar
close(pb)
}