-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_output.R
377 lines (250 loc) · 11.4 KB
/
format_output.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
# -----------------------------------------------------------------------------
#' The function formats human-realetd model outputs as a long data frame.
#'
#' @title Format human model outputs
#'
#' @param x Zika_model_simulation object.
#'
#' @param var_select Vector of human compartment names, \code{c("S", "I1", "R1")}. In
#' addition a number of additional variables can be requested. These include:
#' \itemize{
#' \item{"Ntotal"}{ Total number of people }
#' \item{"inf_1"}{ Daily infections }
#' \item{"MC"}{ Daily microcephaly cases }
#' \item{"inf_1_w"}{ Weekly infections per 1000 }
#' \item{"MC_w"}{ Weekly microcephaly cases per 1000 }
#' }
#'
#' @param keep name of variable to stratify by
#' (allowed are \code{c("patch", "vaccine", "all")}. Default is no stratification)
#'
#' @param patch_id ID of patch to keep in the summary. Default to keep all patches.
#'
#' @return Formatted long data.frame
#'
#' @export
format_output_H <- function(x,
var_select = NULL,
keep = NULL,
patch_id = NULL) {
if(!is.null(keep) && keep == "all") {
if((!is.null(var_select) && (length(var_select) > 1)) | is.null(var_select))
stop("Can not select more than one compartment when stratifying by both patch and vaccine status")
}
# Get model details
nt <- nrow(x$output)
index <- odin_index(x$model)
# Extracting relevant columns for compartment variables
# -> if var_select = NULL extract all compartments
# -> if var_select = names extract specific compartments or summary variables
# Summarise
# -> if keep = NULL calculate totals across age, vaccine status and patches
# -> if keep = patch summarise by patch
# -> if keep = vaccine summarise by vaccine status
compartments <- c("S", "I1", "R1")
summary_vars_present <- c("Ntotal", "inf_1")
summary_vars_not_present <- c("MC")
weekly_summary_vars <- c("inf_1_w", "MC_w")
compartments_requested <- var_select[var_select %in% compartments]
compartments_requested <- if (identical(compartments_requested, character(0))) NULL else compartments_requested
summary_vars_present_requested <- var_select[var_select %in% summary_vars_present]
summary_vars_present_requested <- if (identical(summary_vars_present_requested, character(0))) NULL else summary_vars_present_requested
summary_vars_not_present_requested <- var_select[var_select %in% summary_vars_not_present]
summary_vars_not_present_requested <- if (identical(summary_vars_not_present_requested, character(0))) NULL else summary_vars_not_present_requested
weekly_summary_vars_requested <- var_select[var_select %in% weekly_summary_vars]
weekly_summary_vars_requested <- if (identical(weekly_summary_vars_requested, character(0))) NULL else weekly_summary_vars_requested
requested_vars <- c(compartments_requested,
summary_vars_present_requested,
summary_vars_not_present_requested,
weekly_summary_vars_requested)
if (is.null(var_select)) {
# here the option of summarising by patch or vaccine status
compartments_output_list <- lapply(compartments, function(j) {
temp <- x$output[,unlist(index[j])]
temp_array <- array(temp, dim = c(dim(temp)[1], x$parameters$na, 2, x$parameters$NP))
sum_across_array_dims(temp_array, keep)
})
names(compartments_output_list) <- compartments
output_list <- compartments_output_list
} else if (!is.null(requested_vars)) {
output_list <- list()
vars_present <- c(compartments_requested, summary_vars_present_requested)
if (!is.null(vars_present)) {
compartments_output_list <- lapply(vars_present, function(j) {
temp <- x$output[,unlist(index[j])]
temp_array <- array(temp, dim = c(dim(temp)[1], x$parameters$na, 2, x$parameters$NP))
sum_across_array_dims(temp_array, keep)
})
names(compartments_output_list) <- vars_present
output_list <- c(output_list, compartments_output_list)
}
if ("MC" %in% requested_vars) {
MC <- calculate_microcases(x)
MC_output <- sum_across_array_dims(MC, keep)
output_list <- c(output_list, MC = list(MC_output))
}
if ("MC_w" %in% requested_vars) {
MC <- calculate_microcases(x)
Ntotal <- unpack_odin(x, "Ntotal")
sum_MC <- sum_across_array_dims(MC, keep)
sum_Ntotal <- sum_across_array_dims(Ntotal, keep)
cumsum_sum_MC <- cumsum_across_array_dims(sum_MC, keep)
MC_w <- calculate_incidence(cumsum_sum_MC, sum_Ntotal, 7)
output_list <- c(output_list, MC_w = list(MC_w))
}
if ("inf_1_w" %in% requested_vars) {
inf_1 <- unpack_odin(x, "inf_1")
Ntotal <- unpack_odin(x, "Ntotal")
sum_inf_1 <- sum_across_array_dims(inf_1, keep)
sum_Ntotal <- sum_across_array_dims(Ntotal, keep)
cumsum_sum_inf_1 <- cumsum_across_array_dims(sum_inf_1, keep)
inf_1_w <- calculate_incidence(cumsum_sum_inf_1, sum_Ntotal, 7)
output_list <- c(output_list, inf_1_w = list(inf_1_w))
}
} else {
output_list <- list()
}
vars <- names(output_list)
if(is.null(keep)) {
out <- data.frame(t = as.numeric(x$output[,index$time]),
compartment = as.character(mapply(rep, vars, nt)),
y = unlist(output_list, use.names = FALSE))
} else if (keep == "patch") {
out <- data.frame(t = rep(as.numeric(x$output[,index$time]), x$parameters$NP),
patch = rep(seq_len(x$parameters$NP), each = nt),
compartment = as.character(mapply(rep, vars, x$parameters$NP*nt)),
y = unlist(output_list, use.names = FALSE))
} else if (keep == "vaccine") {
out <- data.frame(t = rep(as.numeric(x$output[,index$time]), 2),
vaccine = rep(seq_len(2), each = nt),
compartment = as.character(mapply(rep, vars, 2*nt)),
y = unlist(output_list, use.names = FALSE))
} else if (keep == "all") {
out <- data.frame(t = rep(as.numeric(x$output[,index$time]), x$parameters$na*2*x$parameters$NP),
age = as.factor(rep(seq_len(x$parameters$na), each = nt)),
vaccine = rep(seq_len(2), each = nt*x$parameters$na),
patch = rep(seq_len(x$parameters$NP), each = nt*x$parameters$na*2),
compartment = as.character(mapply(rep, vars, nt*x$parameters$na*2*x$parameters$NP)),
y = unlist(output_list, use.names = FALSE))
if(!is.null(patch_id)) {
out <- subset(out, patch == patch_id)
}
}
out
}
# -----------------------------------------------------------------------------
#' The function formats mosquito-related model outputs as a long data frame.
#'
#' @title Format mosquito model outputs
#'
#' @param x Zika_model_simulation object.
#'
#' @param var_select Vector of mosquito compartment names,
#' e.g. \code{c("Lwt", "Mwt_S", "Mwb_E1")}, or mosquito-related variables,
#' e.g. \code{c("Kc", "Delta", "R0t_1")}.
#'
#' @param keep name of variable to stratify by
#' (only allowed \code{"patch"}. Default is no stratification)
#'
#' @return Formatted long data.frame
#'
#' @export
format_output_M <- function(x,
var_select = NULL,
keep = NULL) {
# Get model details
nt <- nrow(x$output)
index <- odin_index(x$model)
# Extracting relevant columns for compartment variables
# -> if var_select = NULL extract all compartments
# -> if var_select = names specific compartments, extract those
# Summarise
# -> if keep = NULL calculate totals across patches (sum or mean depending on the variable)
# -> if keep = patch summarise by patch
compartments <- c("Mwt_S", "Mwt_E1", "Mwt_E2", "Mwt_I1",
"Mwb_S", "Mwb_E1", "Mwb_E2", "Mwb_I1")
summary_vars <- c("Lwt", "Lwb", "Kc", "eip", "Delta", "R0t_1", "FOI1")
compartments_requested <- var_select[var_select %in% compartments]
compartments_requested <- if (identical(compartments_requested, character(0))) NULL else compartments_requested
summary_vars_requested <- var_select[var_select %in% summary_vars]
summary_vars_requested <- if (identical(summary_vars_requested, character(0))) NULL else summary_vars_requested
requested_vars <- c(compartments_requested, summary_vars_requested)
if (is.null(var_select)) {
# here the option of summarising by patch
output_list <- lapply(compartments, function(j) {
temp <- x$output[,unlist(index[j])]
temp_array <- array(temp, dim = c(dim(temp)[1], x$parameters$NP))
sum_across_array_dims(temp_array, keep, j)
})
names(output_list) <- compartments
} else if (!is.null(requested_vars)) {
output_list <- lapply(requested_vars, function(j) {
temp <- x$output[,unlist(index[j])]
temp_array <- array(temp, dim = c(dim(temp)[1], x$parameters$NP))
sum_across_array_dims(temp_array, keep, j)
})
names(output_list) <- requested_vars
} else {
output_list <- list()
}
vars <- names(output_list)
if(is.null(keep)) {
out <- data.frame(t = as.numeric(x$output[,index$time]),
compartment = as.character(mapply(rep, vars, nt)),
y = unlist(output_list, use.names = FALSE))
} else if (keep == "patch") {
out <- data.frame(t = as.numeric(x$output[,index$time]),
patch = rep(seq_len(x$parameters$NP), each = nt),
compartment = as.character(mapply(rep, vars, x$parameters$NP*nt)),
y = unlist(output_list, use.names = FALSE))
}
out
}
# -----------------------------------------------------------------------------
#' The function formats mosquito-related proportions as a long data frame.
#'
#' @title Format mosquito proportions
#'
#' @param x Zika_model_simulation object.
#'
#' @param keep name of variable to stratify by
#' (only allowed \code{"patch"}. Default is no stratification)
#'
#' @return Formatted long data.frame
#'
#' @export
format_output_Mprop <- function(x, keep = NULL) {
# Get model details
nt <- nrow(x$output)
index <- odin_index(x$model)
# Summarise
# -> if keep = NULL calculate totals across patches (sum or mean depending on the variable)
# -> if keep = patch summarise by patch
Mwt_comp <- c("Mwt_S", "Mwt_E1", "Mwt_E2", "Mwt_I1")
Mwb_comp <- c("Mwb_S", "Mwb_E1", "Mwb_E2", "Mwb_I1")
M_comp <- c(Mwt_comp, Mwb_comp)
# here the option of summarising by patch
output_list <- lapply(c(Mwt_comp, Mwb_comp), function(j) {
temp <- x$output[,unlist(index[j])]
temp_array <- array(temp, dim = c(dim(temp)[1], x$parameters$NP))
sum_across_array_dims(temp_array, keep, j)
})
names(output_list) <- M_comp
M_tot <- Reduce(`+`, output_list)
Mwb_tot <- Reduce(`+`, output_list[Mwb_comp])
prop_wb <- Mwb_tot / (M_tot + 1e-10)
vars <- "prop_wb"
if(is.null(keep)) {
out <- data.frame(t = as.numeric(x$output[,index$time]),
compartment = rep(vars, nt),
y = unlist(prop_wb, use.names = FALSE),
stringsAsFactors = FALSE)
} else if (keep == "patch") {
out <- data.frame(t = as.numeric(x$output[,index$time]),
patch = rep(seq_len(x$parameters$NP), each = nt),
compartment = rep(vars, x$parameters$NP*nt),
y = c(prop_wb),
stringsAsFactors = FALSE)
}
out
}