-
Notifications
You must be signed in to change notification settings - Fork 31
/
get.R
184 lines (144 loc) · 6.42 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
#' Get Folders with Nowcast Results
#'
#' @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.
#' @export
get_regions <- function(results_dir) {
# Regions to include - based on folder names
regions <- list.files(results_dir, recursive = FALSE)
## Put into alphabetical order
regions <- regions[order(regions)]
names(regions) <- regions
return(regions)
}
#' Get a Single Raw Result
#'
#' @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
#' @export
#' @return An R object read in from the targeted .rds file
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
#' @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 forecast Logical, defaults to `FALSE`. Should forecast results be returned.
#' @return A list of estimates, forecasts and estimated cases by date of report.
#' @export
#' @importFrom purrr map safely
#' @importFrom data.table rbindlist
#' @examples
#'
#'
#' \donttest{
#' # Construct example distributions
#' generation_time <- list(mean = EpiNow2::covid_generation_times[1, ]$mean,
#' mean_sd = EpiNow2::covid_generation_times[1, ]$mean_sd,
#' sd = EpiNow2::covid_generation_times[1, ]$sd,
#' sd_sd = EpiNow2::covid_generation_times[1, ]$sd_sd,
#' max = 30)
#'
#' incubation_period <- list(mean = EpiNow2::covid_incubation_period[1, ]$mean,
#' mean_sd = EpiNow2::covid_incubation_period[1, ]$mean_sd,
#' sd = EpiNow2::covid_incubation_period[1, ]$sd,
#' sd_sd = EpiNow2::covid_incubation_period[1, ]$sd_sd,
#' max = 30)
#'
#' reporting_delay <- list(mean = log(10),
#' mean_sd = 0.8,
#' sd = log(2),
#' sd_sd = 0.1,
#' max = 30)
#'
#' # Uses example case vector from EpiSoon
#' cases <- EpiNow2::example_confirmed[1:30]
#'
#' cases <- data.table::rbindlist(list(
#' data.table::copy(cases)[, region := "testland"],
#' cases[, region := "realland"]))
#'
#' # Run basic nowcasting pipeline
#' regional_out <- regional_epinow(reported_cases = cases,
#' generation_time = generation_time,
#' incubation_period = incubation_period,
#' reporting_delay = reporting_delay,
#' samples = 2000, warmup = 200, cores = ifelse(interactive(), 4, 1),
#' adapt_delta = 0.95, chains = 4, verbose = TRUE,
#' summary = FALSE)
#'
#' get_regional_results(regional_out$regional, forecast = TRUE)
#'
#' }
get_regional_results <- function(regional_output,
results_dir, date,
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 <- list.files(results_dir, recursive = FALSE)
names(regions) <- regions
load_data <- purrr::safely(EpiNow2::get_raw_result)
# Get estimates -----------------------------------------------------------
get_estimates_file <- function(samples, summarised) {
samples <- purrr::map(regions, ~ load_data(samples, .,
result_dir = results_dir,
date = date)[[1]])
samples <- data.table::rbindlist(samples, idcol = "region")
## Get incidence values and combine
summarised <- purrr::map(regions, ~ load_data(summarised, .,
result_dir = results_dir,
date = date)[[1]])
summarised <- data.table::rbindlist(summarised, idcol = "region")
out <- list()
out$samples <- samples
out$summarised <- summarised
return(out)
}
out <- list()
out$estimates <- get_estimates_file(samples = "estimate_samples.rds",
summarised = "summarised_estimates.rds")
if (forecast) {
out$forecast <- get_estimates_file(samples = "forecast_samples.rds",
summarised = "summarised_forecast.rds")
out$estimated_reported_cases <- get_estimates_file(samples = "estimated_reported_cases_samples.rds",
summarised = "summarised_estimated_reported_cases.rds")
}
}else{
get_estimates_data <- function(data) {
samples <- purrr::map(regional_output, ~ .[[data]]$samples)
samples <- data.table::rbindlist(samples, idcol = "region")
## Get incidence values and combine
summarised <- purrr::map(regional_output, ~ .[[data]]$summarised)
summarised <- data.table::rbindlist(summarised, idcol = "region")
out <- list()
out$samples <- samples
out$summarised <- summarised
return(out)
}
out <- list()
out$estimates <- get_estimates_data("estimates")
if (forecast) {
out$forecast <- get_estimates_data("forecasts")
out$estimated_reported_cases <- get_estimates_data("estimated_reported_cases")
}
}
return(out)
}