forked from ashattock/dynamice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.R
197 lines (150 loc) · 5.5 KB
/
results.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
###########################################################
# RESULTS
#
# Call plotting functions as defined by o$plot_xxx flags (see
# options.R). All plotting functions themselves live in
# plotting.R.
#
###########################################################
# ---------------------------------------------------------
# Call plots as defined by o$plot_xxx flags
# ---------------------------------------------------------
run_results = function() {
# Only continue if specified by do_step
if (!is.element(3, o$do_step)) return()
message("* Producing results")
# Create main output files
if (o$do_output_files) {
# Output files for VIMC
vimc_output()
# Output files for EPI50
epi50_output()
}
# Plot coverage
# if (o$plot_coverage)
# plot_coverage_all()
# Plot everything
# if (o$plot_everything)
# plot_everything()
# Plot deaths and DALYs averted
if (o$plot_burden_averted)
plot_burden_averted()
}
# ---------------------------------------------------------
# Main output csv files for VIMC
# ---------------------------------------------------------
vimc_output = function() {
message(" > Producing VIMC output files")
# ---- Format results template ----
# Load central estimates template
template_dt = readRDS(paste0(o$pth$input, "template.rds"))
# Trivial columns for which we are to insert values
keep_cols = names(which(colSums(is.na(template_dt)) != nrow(template_dt)))
# Redefine year scope
vimc_dt = template_dt %>%
filter(country %in% o$countries) %>%
select(-year) %>%
unique() %>%
expand_grid(year = o$years) %>%
select(all_of(keep_cols)) %>%
as.data.table()
# Loop through scenarios
for (scenario in o$scenarios) {
# ---- Load all results for this scenario
# We'll load results for all countrues for this scenario
load_names = paste1(o$countries_all, scenario)
load_files = paste0(o$pth$compiled, load_names, ".rds")
# Load these results
results_dt = rbindlist(lapply(load_files, readRDS))
# ---- Central estimates ----
# Central results for this scenario
model_dt = results_dt %>%
filter(scenario == !!scenario,
is.na(r0)) %>% # This identifies central estimate
select(-scenario, -r0) %>%
pivot_wider(names_from = "metric",
values_from = "value") %>%
as.data.table()
# Construct file name path to save to
save_name = paste1("total", scenario)
save_file = paste0(o$pth$results, save_name, ".csv")
# Save all country results to file
fwrite(model_dt, file = save_file)
# Append model results to VIMC template
output_dt = vimc_dt %>%
left_join(y = model_dt,
by = c("year", "age", "country"))
# select(all_of(names(template_dt)))
# Construct file name path to save to
save_name = paste1("central", scenario)
save_file = paste0(o$pth$results, save_name, ".csv")
# Save results to file
fwrite(output_dt, file = save_file)
}
# ---- Uncertainty simulations ----
# TODO: I'm not sure how LSHTM handle uncertainty
}
# ---------------------------------------------------------
# Output for EPI50 analysis
# ---------------------------------------------------------
epi50_output = function() {
message(" > Producing EPI50 output file")
# Convert scenarios into named list
scen_list = o$scenarios %>%
setNames(c("no_vaccine", "vaccine")) %>%
as.list()
# Function for loading results of given scenario
load_fn = function(ref) {
# File name and path for loading
load_name = paste1("total", scen_list[[ref]])
load_file = paste0(o$pth$results, load_name, ".csv")
# Load file and set to long format
result_dt = fread(load_file) %>%
select(country, year, age, deaths, dalys) %>%
mutate(scenario = ref, .before = 1) %>%
pivot_longer(cols = c("deaths", "dalys"),
names_to = "metric",
values_to = "value") %>%
arrange(country, year, age, metric) %>%
as.data.table()
return(result_dt)
}
# Apply loading function and format into single datatable
epi50_dt = names(scen_list) %>%
lapply(load_fn) %>%
rbindlist()
# Construct file name path to save to
save_file = paste0(o$pth$output, "epi50_dynamice_results.rds")
# Save results to file
saveRDS(epi50_dt, file = save_file)
}
# ---------------------------------------------------------
# Easy load of all central results
# ---------------------------------------------------------
load_central_results = function() {
# TODO: Throw helpful warning if vimc_output() has not yet been called
# Function for loading a single file
load_fn = function(scenario) {
# File name and path
load_name = paste1("central", scenario)
load_file = paste0(o$pth$results, load_name, ".csv")
# Load results and append scenario details
result_dt = fread(load_file) %>%
mutate(scenario = scenario,
.before = 1) %>%
select(-disease)
return(result_dt)
}
# Call loading function for each scenario
central_dt = lapply(o$scenarios, load_fn) %>%
rbindlist() %>%
# Convert to tidy format...
pivot_longer(cols = o$metrics,
names_to = "metric") %>%
# Tidy up...
select(country, country_name, scenario,
year, age, metric, value) %>%
arrange(metric, scenario, country, year, age) %>%
as.data.table()
return(central_dt)
}