-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathget.R
327 lines (318 loc) · 10.2 KB
/
get.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
#' Get Folders with Results
#'
#' @description `r lifecycle::badge("stable")`
#'
#' @param results_dir A character string giving the directory in which results
#' are stored (as produced by `regional_rt_pipeline`).
#'
#' @return A named character vector containing the results to plot.
#' @author Sam Abbott
#' @export
get_regions <- function(results_dir) {
# regions to include - based on folder names
regions <- list.dirs(results_dir,
recursive = FALSE,
full.names = FALSE
)
# put into alphabetical order
regions <- regions[!(regions %in% "runtimes.csv")]
regions <- regions[order(regions)]
names(regions) <- regions
return(regions)
}
#' Get a Single Raw Result
#'
#' @description `r lifecycle::badge("stable")`
#'
#' @param file Character string giving the result files name.
#'
#' @param region Character string giving the region of interest.
#'
#' @param date Target date (in the format `"yyyy-mm-dd`).
#'
#' @param result_dir Character string giving the location of the target
#' directory.
#'
#' @return An R object read in from the targeted .rds file
#' @author Sam Abbott
#' @export
get_raw_result <- function(file, region, date,
result_dir) {
file_path <- file.path(result_dir, region, date, file)
object <- readRDS(file_path)
return(object)
}
#' Get Combined Regional Results
#'
#' @description `r lifecycle::badge("stable")`
#' Summarises results across regions either from input or from disk. See the
#' examples for details.
#'
#' @param regional_output A list of output as produced by `regional_epinow` and
#' stored in the `regional` list.
#'
#' @param results_dir A character string indicating the folder containing the
#' `EpiNow2` results to extract.
#'
#' @param date A Character string (in the format "yyyy-mm-dd") indicating the
#' date to extract data for. Defaults to "latest" which finds the latest
#' results available.
#'
#' @param samples Logical, defaults to `TRUE`. Should samples be returned.
#'
#' @param forecast Logical, defaults to `FALSE`. Should forecast results be
#' returned.
#'
#' @return A list of estimates, forecasts and estimated cases by date of report.
#' @author Sam Abbott
#' @export
#' @importFrom purrr map safely
#' @importFrom data.table rbindlist
#' @examples
#' \donttest{
#' # construct example distributions
#' generation_time <- get_generation_time(
#' disease = "SARS-CoV-2", source = "ganyani"
#' )
#' incubation_period <- get_incubation_period(
#' disease = "SARS-CoV-2", source = "lauer"
#' )
#' reporting_delay <- estimate_delay(rlnorm(100, log(6), 1), max_value = 10)
#'
#' # example case vector
#' cases <- example_confirmed[1:30]
#' cases <- data.table::rbindlist(list(
#' data.table::copy(cases)[, region := "testland"],
#' cases[, region := "realland"]
#' ))
#'
#' # save results to tmp folder
#' dir <- file.path(tempdir(check = TRUE), "results")
#' # run multiregion estimates
#' regional_out <- regional_epinow(
#' reported_cases = cases,
#' generation_time = generation_time_opts(generation_time),
#' delays = delay_opts(incubation_period + reporting_delay),
#' rt = rt_opts(rw = 7), gp = NULL,
#' output = c("regions", "latest"),
#' target_folder = dir,
#' return_output = TRUE
#' )
#' # from output
#' results <- get_regional_results(regional_out$regional, samples = FALSE)
#' names(results)
#'
#' # from a folder
#' folder_results <- get_regional_results(results_dir = dir, samples = FALSE)
#' names(folder_results)
#' }
get_regional_results <- function(regional_output,
results_dir, date,
samples = TRUE,
forecast = FALSE) {
if (missing(regional_output)) {
regional_output <- NULL
}
if (is.null(regional_output)) {
# assign to latest likely date if not given
if (missing(date)) {
date <- "latest"
}
# find all regions
regions <- get_regions(results_dir)
load_data <- purrr::safely(EpiNow2::get_raw_result) # nolint
# get estimates
get_estimates_file <- function(samples_path, summarised_path) {
out <- list()
if (samples) {
samples <- purrr::map(regions, ~ load_data(samples_path, .,
result_dir = results_dir,
date = date
)[[1]])
samples <- data.table::rbindlist(samples, idcol = "region", fill = TRUE)
out$samples <- samples
}
# get incidence values and combine
summarised <- purrr::map(regions, ~ load_data(summarised_path, .,
result_dir = results_dir,
date = date
)[[1]])
summarised <- data.table::rbindlist(
summarised, idcol = "region", fill = TRUE
)
out$summarised <- summarised
return(out)
}
out <- list()
out$estimates <- get_estimates_file(
samples_path = "estimate_samples.rds",
summarised_path = "summarised_estimates.rds"
)
if (forecast) {
out$estimated_reported_cases <- get_estimates_file(
samples_path = "estimated_reported_cases_samples.rds",
summarised_path = "summarised_estimated_reported_cases.rds"
)
}
} else {
get_estimates_data <- function(data) {
out <- list()
if (samples) {
samples <- purrr::map(regional_output, ~ .[[data]]$samples)
samples <- data.table::rbindlist(samples, idcol = "region", fill = TRUE)
out$samples <- samples
}
# get incidence values and combine
summarised <- purrr::map(regional_output, ~ .[[data]]$summarised)
summarised <- data.table::rbindlist(
summarised, idcol = "region", fill = TRUE
)
out$summarised <- summarised
return(out)
}
out <- list()
out$estimates <- get_estimates_data("estimates")
if (forecast) {
out$estimated_reported_cases <-
get_estimates_data("estimated_reported_cases")
}
}
return(out)
}
#' Get a Literature Distribution
#'
#'
#' @description `r lifecycle::badge("stable")`
#' Search a data frame for a distribution and return it in the format expected
#' by `delay_opts()` and the `generation_time` argument of `epinow` and
#' `estimate_infections`.
#'
#' @param data A `data.table` in the format of `generation_times`.
#'
#' @param disease A character string indicating the disease of interest.
#'
#' @param source A character string indicating the source of interest.
#'
#' @param max_value Numeric, the maximum value to allow. Defaults to 15 days.
#'
#' @param fixed Logical, defaults to `FALSE`. Should distributions be supplied
#' as fixed values (vs with uncertainty)?
#'
#' @return A list defining a distribution
#'
#' @author Sam Abbott
#' @export
#' @examples
#' get_dist(
#' EpiNow2::generation_times, disease = "SARS-CoV-2", source = "ganyani"
#' )
get_dist <- function(data, disease, source, max_value = 15, fixed = FALSE) {
target_disease <- disease
target_source <- source
data <- data[disease == target_disease][source == target_source]
dist <- as.list(data[, .(mean, mean_sd, sd, sd_sd, max = max_value, dist)])
if (fixed) {
dist$mean_sd <- 0
dist$sd_sd <- 0
}
return(do.call(dist_spec, dist))
}
#' Get a Literature Distribution for the Generation Time
#'
#' @description `r lifecycle::badge("stable")`
#' Extracts a literature distribution from `generation_times`.
#'
#' @inheritParams get_dist
#' @inherit get_dist
#' @export
#' @author Sam Abbott
#' @examples
#' get_generation_time(disease = "SARS-CoV-2", source = "ganyani")
get_generation_time <- function(disease, source, max_value = 15,
fixed = FALSE) {
dist <- get_dist(EpiNow2::generation_times,
disease = disease, source = source,
max_value = max_value, fixed = fixed
)
return(dist)
}
#' Get a Literature Distribution for the Incubation Period
#'
#' @description `r lifecycle::badge("stable")`
#' Extracts a literature distribution from `incubation_periods`.
#'
#' @inheritParams get_dist
#' @inherit get_dist
#' @author Sam Abbott
#' @export
#' @examples
#' get_incubation_period(disease = "SARS-CoV-2", source = "lauer")
get_incubation_period <- function(disease, source, max_value = 15,
fixed = FALSE) {
dist <- get_dist(EpiNow2::incubation_periods,
disease = disease, source = source,
max_value = max_value, fixed = fixed
)
return(dist)
}
#' Get Regions with Most Reported Cases
#'
#' @description `r lifecycle::badge("stable")`
#' Extract a vector of regions with the most reported cases in a set time
#' window.
#'
#' @param time_window Numeric, number of days to include from latest date in
#' data. Defaults to 7 days.
#'
#' @param no_regions Numeric, number of regions to return. Defaults to 6.
#'
#' @inheritParams regional_epinow
#'
#' @return A character vector of regions with the highest reported cases
#'
#' @author Sam Abbott
#' @importFrom data.table copy setorderv
#' @importFrom lubridate days
#' @export
get_regions_with_most_reports <- function(reported_cases,
time_window = 7,
no_regions = 6) {
most_reports <- data.table::copy(reported_cases)
most_reports <-
most_reports[,
.SD[date >= (max(date, na.rm = TRUE) - lubridate::days(time_window))
],
by = "region"
]
most_reports <- most_reports[,
.(confirm = sum(confirm, na.rm = TRUE)), by = "region"
]
most_reports <- data.table::setorderv(
most_reports, cols = "confirm", order = -1
)
most_reports <- most_reports[1:no_regions][!is.na(region)]$region
return(most_reports)
}
##' Estimate seeding time from delays and generation time
##'
##' The seeding time is set to the mean of the specified delays, constrained
##' to be at least the maximum generation time
##' @param delays Delays as specified using `dist_spec`
##' @param generation_time Generation time as specified using `dist_spec`
##' @return An integer seeding time
##' @author Sebastian Funk
get_seeding_time <- function(delays, generation_time) {
# Estimate the mean delay -----------------------------------------------
seeding_time <- sum(mean(delays))
if (seeding_time < 1) {
seeding_time <- 1
} else {
seeding_time <- as.integer(seeding_time)
}
## make sure we have at least gt_max seeding time
seeding_time <- max(
seeding_time,
sum(generation_time$max) + sum(generation_time$np_pmf_max)
)
return(seeding_time)
}