From 0023abafa1e82ea9424fd7932229318a3390ce18 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 4 May 2023 16:39:55 +0100 Subject: [PATCH 01/31] updated gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7f855a336..31cb7c5c2 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,6 @@ CRAN-RELEASE # C++ object files inst/include/*.o # avoid rstantools generated files -src \ No newline at end of file +src + +.DS_Store From 6d1ecccc9dbc48feadecc11e3f1a5aa58806add9 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 4 May 2023 16:40:30 +0100 Subject: [PATCH 02/31] replaced ifelse with fcase in map_prob_change() --- R/utilities.R | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/R/utilities.R b/R/utilities.R index 102fb97cb..932093bd7 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -77,6 +77,7 @@ make_conf <- function(value, CrI = 90, reverse = FALSE) { #' "Likely decreasing" (< 0.95), "Decreasing" (<= 1) #' @param var Numeric variable to be categorised #' +#' @importFrom data.table fcase #' @return A character variable. #' @export #' @examples @@ -85,17 +86,15 @@ make_conf <- function(value, CrI = 90, reverse = FALSE) { #' #' map_prob_change(var) map_prob_change <- function(var) { - # nolint start - var <- ifelse(var < 0.05, "Increasing", - ifelse(var < 0.4, "Likely increasing", - ifelse(var < 0.6, "Stable", - ifelse(var < 0.95, "Likely decreasing", - "Decreasing" - ) - ) - ) - ) - # nolint end + + var <- data.table::fcase( + var < 0.05, "Increasing", + var >= 0.05 & var < 0.4, "Likely increasing", + var >= 0.4 & var < 0.6, "Stable", + var >= 0.6 & var < 0.95, "Likely decreasing", + var >= 0.95 & var <= 1, "Decreasing" + ) + var <- factor(var, levels = c( "Increasing", "Likely increasing", "Stable", "Likely decreasing", "Decreasing" From 7e915288959f958785a54b9e88c830d0e1234221 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 4 May 2023 22:48:43 +0100 Subject: [PATCH 03/31] revised explicit breakpoints for the categories --- R/utilities.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/utilities.R b/R/utilities.R index 932093bd7..b15725407 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -89,10 +89,10 @@ map_prob_change <- function(var) { var <- data.table::fcase( var < 0.05, "Increasing", - var >= 0.05 & var < 0.4, "Likely increasing", - var >= 0.4 & var < 0.6, "Stable", - var >= 0.6 & var < 0.95, "Likely decreasing", - var >= 0.95 & var <= 1, "Decreasing" + var < 0.4, "Likely increasing", + var < 0.6, "Stable", + var < 0.95, "Likely decreasing", + var <= 1, "Decreasing" ) var <- factor(var, levels = c( From c8db992c407cc0ef760a0c709e2108fbbd3f2eb1 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 13:58:40 +0100 Subject: [PATCH 04/31] replace nested ifelse() with data.table::fcase() in create_backcalc_data() --- R/create.R | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/R/create.R b/R/create.R index 6ef9ce2fc..04f2fe050 100644 --- a/R/create.R +++ b/R/create.R @@ -263,6 +263,7 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL, #' define the back calculation. Defaults to `backcalc_opts()`. #' #' @seealso backcalc_opts +#' @importFrom data.table fcase #' @return A list of settings defining the Gaussian process #' @export #' @author Sam Abbott @@ -282,18 +283,19 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL, #' #' # custom lengthscale #' create_gp_data(gp_opts(ls_mean = 14), data) -create_backcalc_data <- function(backcalc = backcalc_opts) { +create_backcalc_data <- function(backcalc = backcalc_opts()) { data <- list( - rt_half_window = as.integer((backcalc$rt_window - 1) / 2), - # nolint start - backcalc_prior = ifelse(backcalc$prior == "none", 0, - ifelse(backcalc$prior == "reports", 1, - ifelse(backcalc$prior == "infections", 2, 0) - ) - ) - # nolint end - ) - return(data) + rt_half_window = as.integer((backcalc$rt_window - 1) / 2), + # nolint start + backcalc_prior = data.table::fcase( + backcalc$prior == "none", 0, + backcalc$prior == "reports", 1, + backcalc$prior == "infections", 2, + default = 0 + ) + ) + # nolint end + return(data) } #' Create Gaussian Process Data #' From 7b63d9bf79b1c30ae15df7780e2a013b33223927 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 15:29:55 +0100 Subject: [PATCH 05/31] set the default samples argument to 1000 and added a warning message --- R/dist.R | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/R/dist.R b/R/dist.R index 35e5dd0b2..6b3f670ea 100644 --- a/R/dist.R +++ b/R/dist.R @@ -158,7 +158,8 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model, #' `stan`. #' @param values Numeric vector of values #' -#' @param samples Numeric, number of samples to take +#' @param samples Numeric, number of samples to take. Must be >= 1000. Defaults +#' to 1000. #' #' @param dist Character string, which distribution to fit. Defaults to #' exponential (`"exp"`) but gamma (`"gamma"`) and lognormal (`"lognormal"`) are @@ -192,21 +193,23 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model, #' ) #' #' # integer adjusted lognormal model -#' dist_fit(rlnorm(1:100, log(5), 0.2), -#' samples = 1000, dist = "lognormal", -#' cores = ifelse(interactive(), 4, 1), verbose = TRUE -#' ) +# dist_fit(rlnorm(1:100, log(5), 0.2), +# samples = 1000, dist = "lognormal", +# cores = ifelse(interactive(), 4, 1), verbose = TRUE +# ) #' } -dist_fit <- function(values = NULL, samples = NULL, cores = 1, +dist_fit <- function(values = NULL, samples = 1000, cores = 1, chains = 2, dist = "exp", verbose = FALSE) { - if (is.null(samples)) { - samples <- 1000 - } if (samples < 1000) { samples <- 1000 } + warning(sprintf("%s %s", "`samples` must be at least 1000.", + "Now setting it to 1000 internally." + ) + ) + # model parameters lows <- values - 1 lows <- ifelse(lows <= 0, 1e-6, lows) From 6a8a1158676c45601a90495fce510b88398bad63 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 15:40:23 +0100 Subject: [PATCH 06/31] replaced ifelse with data.table::fcase in create_gp_data() --- R/create.R | 56 +++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/R/create.R b/R/create.R index 04f2fe050..f0fa085a9 100644 --- a/R/create.R +++ b/R/create.R @@ -307,26 +307,28 @@ create_backcalc_data <- function(backcalc = backcalc_opts()) { #' Gaussian process. #' @param data A list containing the following numeric values: #' `t`, `seeding_time`, `horizon`. +#' @importFrom data.table fcase #' @seealso gp_opts #' @return A list of settings defining the Gaussian process #' @export #' @author Sam Abbott #' @examples #' # define input data required -#' data <- list( -#' t = 30, -#' seeding_time = 7, -#' horizon = 7 -#' ) -#' -#' # default gaussian process data -#' create_gp_data(data = data) -#' -#' # settings when no gaussian process is desired -#' create_gp_data(NULL, data) -#' -#' # custom lengthscale -#' create_gp_data(gp_opts(ls_mean = 14), data) +# data <- list( +# t = 30, +# seeding_time = 7, +# horizon = 7 +# ) +# +# # default gaussian process data +# create_gp_data(data = data) +# +# # settings when no gaussian process is desired +# create_gp_data(NULL, data) +# +# # custom lengthscale +# create_gp_data(gp_opts(ls_mean = 14), data) + create_gp_data <- function(gp = gp_opts(), data) { # Define if GP is on or off if (is.null(gp)) { @@ -349,20 +351,22 @@ create_gp_data <- function(gp = gp_opts(), data) { # map settings to underlying gp stan requirements gp_data <- list( - fixed = as.numeric(fixed), - M = M, - L = gp$boundary_scale, - ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd), - ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd), - ls_min = gp$ls_min, - ls_max = data$t - data$seeding_time - data$horizon, - alpha_sd = gp$alpha_sd, - # nolint start - gp_type = ifelse(gp$kernel == "se", 0, - ifelse(gp$kernel == "matern", 1, 0) + fixed = as.numeric(fixed), + M = M, + L = gp$boundary_scale, + ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd), + ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd), + ls_min = gp$ls_min, + ls_max = data$t - data$seeding_time - data$horizon, + alpha_sd = gp$alpha_sd, + # nolint start + gp_type = data.table::fcase( + gp$kernel == "se", 0, + gp$kernel == "matern", 1, + default = 0 ) - # nolint end ) + # nolint end gp_data <- c(data, gp_data) return(gp_data) From e27fe705dcf53800e7cba1d346d20f728a40c0e0 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 15:58:41 +0100 Subject: [PATCH 07/31] replaced ifelse with fcase in create_initial_conditions() --- R/create.R | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/R/create.R b/R/create.R index f0fa085a9..9bd92816b 100644 --- a/R/create.R +++ b/R/create.R @@ -544,6 +544,7 @@ create_stan_data <- function(reported_cases, generation_time, #' @return An initial condition generating function #' @importFrom purrr map2_dbl #' @importFrom truncnorm rtruncnorm +#' @importFrom data.table fcase #' @export # @author Sam Abbott # @author Sebastian Funk @@ -587,13 +588,13 @@ create_initial_conditions <- function(data) { meanlog = data$ls_meanlog, sdlog = ifelse(data$ls_sdlog > 0, data$ls_sdlog * 0.1, 0.01) )) - # nolint start - out$rho <- ifelse(out$rho > data$ls_max, data$ls_max - 0.001, - ifelse(out$rho < data$ls_min, data$ls_min + 0.001, - out$rho + + out$rho <- data.table::fcase( + out$rho > data$ls_max, data$ls_max - 0.001, + out$rho < data$ls_min, data$ls_min + 0.001, + default = out$rho ) - ) - # nolint end + out$alpha <- array( truncnorm::rtruncnorm(1, a = 0, mean = 0, sd = data$alpha_sd) ) From ba2572d3d996cc4a31fcdb19ec53d9bb6e6fc302 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 16:15:24 +0100 Subject: [PATCH 08/31] moved warning message into rightful place (inside the if() block) --- R/dist.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/dist.R b/R/dist.R index 6b3f670ea..ca0789cee 100644 --- a/R/dist.R +++ b/R/dist.R @@ -203,12 +203,12 @@ dist_fit <- function(values = NULL, samples = 1000, cores = 1, if (samples < 1000) { samples <- 1000 - } - warning(sprintf("%s %s", "`samples` must be at least 1000.", - "Now setting it to 1000 internally." - ) - ) + warning(sprintf("%s %s", "`samples` must be at least 1000.", + "Now setting it to 1000 internally." + ) + ) + } # model parameters lows <- values - 1 From 9274e7ac1e4d15662065168077e95509d65c2988 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 16:48:22 +0100 Subject: [PATCH 09/31] replaced ifelse with data.table::fcase in regional_summary() --- R/summarise.R | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/R/summarise.R b/R/summarise.R index b6f2e1e7d..9f3c31d2d 100644 --- a/R/summarise.R +++ b/R/summarise.R @@ -162,7 +162,7 @@ summarise_results <- function(regions, #' @inheritParams epinow #' @importFrom purrr map_chr compact #' @importFrom ggplot2 coord_cartesian guides guide_legend ggsave ggplot_build -#' @importFrom data.table setDT +#' @importFrom data.table setDT fcase #' @importFrom futile.logger flog.info #' @examples #' \donttest{ @@ -336,10 +336,11 @@ regional_summary <- function(regional_output = NULL, ) } save_ggplot(summary_plot, "summary_plot.png", - width = ifelse(length(regions) > 60, - ifelse(length(regions) > 120, 36, 24), # nolint - 12 - ) + width = data.table::fcase( + length(regions) > 60 & length(regions) > 120, 36, + length(regions) > 60 & !(length(regions) > 120), 24, + default = 12 + ) ) } # extract regions with highest number of reported cases in the last week @@ -370,10 +371,11 @@ regional_summary <- function(regional_output = NULL, } if (all_regions) { - # nolint start - plots_per_row <- ifelse(length(regions) > 60, - ifelse(length(regions) > 120, 8, 5), 3 - ) + plots_per_row <- data.table::fcase( + length(regions) > 60 & length(regions) > 120, 8, + length(regions) > 60 & ! (length(regions) > 120), 5, + default = 3 + ) # nolint end plots <- report_plots( @@ -669,7 +671,7 @@ calc_CrIs <- function(samples, summarise_by = NULL, CrIs = c(0.2, 0.5, 0.9)) { # nolint start order_CrIs <- c( paste0("lower_", rev(scale_CrIs)), paste0("upper_", scale_CrIs) - ) + ) # nolint end with_CrIs <- data.table::dcast( with_CrIs, ... ~ factor(CrI, levels = order_CrIs), From 188a79a34470564e3ea3c3f8a79e3fc78ad662b7 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 17:07:05 +0100 Subject: [PATCH 10/31] replaced ifelse with data.table::fcase in format_fit() --- R/estimate_infections.R | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/R/estimate_infections.R b/R/estimate_infections.R index 731f88fe2..c67d940a7 100644 --- a/R/estimate_infections.R +++ b/R/estimate_infections.R @@ -678,7 +678,7 @@ fit_model_with_vb <- function(args, future = FALSE, id = "stan") { #' @param start_date Date, earliest date with data. #' #' @inheritParams calc_summary_measures -#' @importFrom data.table fifelse rbindlist +#' @importFrom data.table fcase rbindlist #' @importFrom lubridate days #' @importFrom futile.logger flog.info #' @return A list of samples and summarised posterior parameter estimates. @@ -698,15 +698,12 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, # nolint start format_out$samples <- format_out$samples[ , - type := data.table::fifelse( - date > (max(date, na.rm = TRUE) - horizon), - "forecast", - data.table::fifelse( - date > (max(date, na.rm = TRUE) - horizon - shift), - "estimate based on partial data", - "estimate" + type := data.table::fcase( + date > (max(date, na.rm = TRUE) - horizon), "forecast", + date > (max(date, na.rm = TRUE) - horizon - shift), + "estimate based on partial data", + default = "estimate" ) - ) ] # nolint end From 72e04deb9d227d4516560b201e4dbf8659996428 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Fri, 5 May 2023 17:44:09 +0100 Subject: [PATCH 11/31] linting: removed nolint tags and unnecessary whitespace, and added proper comment tags in examples --- R/create.R | 28 ++++++++++++++-------------- R/summarise.R | 3 +-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/R/create.R b/R/create.R index 9bd92816b..05ada8e96 100644 --- a/R/create.R +++ b/R/create.R @@ -314,20 +314,20 @@ create_backcalc_data <- function(backcalc = backcalc_opts()) { #' @author Sam Abbott #' @examples #' # define input data required -# data <- list( -# t = 30, -# seeding_time = 7, -# horizon = 7 -# ) -# -# # default gaussian process data -# create_gp_data(data = data) -# -# # settings when no gaussian process is desired -# create_gp_data(NULL, data) -# -# # custom lengthscale -# create_gp_data(gp_opts(ls_mean = 14), data) +#' data <- list( +#' t = 30, +#' seeding_time = 7, +#' horizon = 7 +#' ) +#' +#' # default gaussian process data +#' create_gp_data(data = data) +#' +#' # settings when no gaussian process is desired +#' create_gp_data(NULL, data) +#' +#' # custom lengthscale +#' create_gp_data(gp_opts(ls_mean = 14), data) create_gp_data <- function(gp = gp_opts(), data) { # Define if GP is on or off diff --git a/R/summarise.R b/R/summarise.R index 9f3c31d2d..22264dd33 100644 --- a/R/summarise.R +++ b/R/summarise.R @@ -373,10 +373,9 @@ regional_summary <- function(regional_output = NULL, if (all_regions) { plots_per_row <- data.table::fcase( length(regions) > 60 & length(regions) > 120, 8, - length(regions) > 60 & ! (length(regions) > 120), 5, + length(regions) > 60 & !(length(regions) > 120), 5, default = 3 ) - # nolint end plots <- report_plots( summarised_estimates = results$estimates$summarised, From b3ced53e9f06ddd4f8650e27b5f9794d9de600ce Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Tue, 9 May 2023 14:53:17 +0100 Subject: [PATCH 12/31] Revert "set the default samples argument to 1000 and added a warning message" This reverts commit 7b63d9bf79b1c30ae15df7780e2a013b33223927. --- R/dist.R | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/R/dist.R b/R/dist.R index ca0789cee..bb6b11c5d 100644 --- a/R/dist.R +++ b/R/dist.R @@ -158,8 +158,7 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model, #' `stan`. #' @param values Numeric vector of values #' -#' @param samples Numeric, number of samples to take. Must be >= 1000. Defaults -#' to 1000. +#' @param samples Numeric, number of samples to take #' #' @param dist Character string, which distribution to fit. Defaults to #' exponential (`"exp"`) but gamma (`"gamma"`) and lognormal (`"lognormal"`) are @@ -193,23 +192,21 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model, #' ) #' #' # integer adjusted lognormal model -# dist_fit(rlnorm(1:100, log(5), 0.2), -# samples = 1000, dist = "lognormal", -# cores = ifelse(interactive(), 4, 1), verbose = TRUE -# ) +#' dist_fit(rlnorm(1:100, log(5), 0.2), +#' samples = 1000, dist = "lognormal", +#' cores = ifelse(interactive(), 4, 1), verbose = TRUE +#' ) #' } -dist_fit <- function(values = NULL, samples = 1000, cores = 1, +dist_fit <- function(values = NULL, samples = NULL, cores = 1, chains = 2, dist = "exp", verbose = FALSE) { - - if (samples < 1000) { + if (is.null(samples)) { samples <- 1000 - - warning(sprintf("%s %s", "`samples` must be at least 1000.", - "Now setting it to 1000 internally." - ) - ) } + if (samples < 1000) { + samples <- 1000 + } + # model parameters lows <- values - 1 lows <- ifelse(lows <= 0, 1e-6, lows) From f2a2a36df7ab15511a3f4837727a367e354f8297 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Tue, 9 May 2023 21:24:11 +0100 Subject: [PATCH 13/31] register fcase import --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index ba4559ebd..87c81213e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -102,6 +102,7 @@ importFrom(data.table,as.data.table) importFrom(data.table,copy) importFrom(data.table,data.table) importFrom(data.table,dcast) +importFrom(data.table,fcase) importFrom(data.table,fifelse) importFrom(data.table,frollmean) importFrom(data.table,frollsum) From ff9412c948e0fc2f832ef34fb6d5c5e08fa4a730 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Tue, 9 May 2023 21:24:42 +0100 Subject: [PATCH 14/31] redoc create_backcalc_data --- man/create_backcalc_data.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/create_backcalc_data.Rd b/man/create_backcalc_data.Rd index d5118d2bf..adc4eb680 100644 --- a/man/create_backcalc_data.Rd +++ b/man/create_backcalc_data.Rd @@ -4,7 +4,7 @@ \alias{create_backcalc_data} \title{Create Back Calculation Data} \usage{ -create_backcalc_data(backcalc = backcalc_opts) +create_backcalc_data(backcalc = backcalc_opts()) } \arguments{ \item{backcalc}{A list of options as generated by \code{backcalc_opts()} to From f8a5cb25353317c62128d6d5795c39112b190257 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Tue, 9 May 2023 22:11:42 +0100 Subject: [PATCH 15/31] make rho an array --- R/create.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/create.R b/R/create.R index 05ada8e96..32ccf9466 100644 --- a/R/create.R +++ b/R/create.R @@ -589,11 +589,11 @@ create_initial_conditions <- function(data) { sdlog = ifelse(data$ls_sdlog > 0, data$ls_sdlog * 0.1, 0.01) )) - out$rho <- data.table::fcase( + out$rho <- array(data.table::fcase( out$rho > data$ls_max, data$ls_max - 0.001, out$rho < data$ls_min, data$ls_min + 0.001, default = out$rho - ) + )) out$alpha <- array( truncnorm::rtruncnorm(1, a = 0, mean = 0, sd = data$alpha_sd) From 0d79c79a731a17d2288035411729da536b1b14d8 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Wed, 10 May 2023 10:23:39 +0100 Subject: [PATCH 16/31] linting: remove whitespace --- R/dist.R | 1 - R/stanmodels.R | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/R/dist.R b/R/dist.R index bb6b11c5d..cdadcd4f2 100644 --- a/R/dist.R +++ b/R/dist.R @@ -206,7 +206,6 @@ dist_fit <- function(values = NULL, samples = NULL, cores = 1, if (samples < 1000) { samples <- 1000 } - # model parameters lows <- values - 1 lows <- ifelse(lows <= 0, 1e-6, lows) diff --git a/R/stanmodels.R b/R/stanmodels.R index 6fcdd36a3..120f51678 100644 --- a/R/stanmodels.R +++ b/R/stanmodels.R @@ -17,7 +17,7 @@ Rcpp::loadModule("stan_fit4tune_inv_gamma_mod", what = TRUE) # instantiate each stanmodel object stanmodels <- sapply(stanmodels, function(model_name) { # create C++ code for stan model - stan_file <- if (dir.exists("stan")) "stan" else file.path("inst", "stan") + stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan") stan_file <- file.path(stan_file, paste0(model_name, ".stan")) stanfit <- rstan::stanc_builder(stan_file, allow_undefined = TRUE, From a173a03770d8a63f0acac1083228e958e6bfb934 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Wed, 10 May 2023 17:17:14 +0100 Subject: [PATCH 17/31] added a condition for when date is NA --- R/estimate_infections.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/estimate_infections.R b/R/estimate_infections.R index c67d940a7..812ba8e67 100644 --- a/R/estimate_infections.R +++ b/R/estimate_infections.R @@ -702,6 +702,7 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, date > (max(date, na.rm = TRUE) - horizon), "forecast", date > (max(date, na.rm = TRUE) - horizon - shift), "estimate based on partial data", + is.na(date), NA_character_, default = "estimate" ) ] From 7d30e3b4afe52e94a552656b711cc9edc542dfea Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 18:18:43 +0100 Subject: [PATCH 18/31] removed linting gates --- R/create.R | 5 +---- R/estimate_infections.R | 2 -- R/summarise.R | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/R/create.R b/R/create.R index 32ccf9466..aa7601a45 100644 --- a/R/create.R +++ b/R/create.R @@ -286,7 +286,6 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL, create_backcalc_data <- function(backcalc = backcalc_opts()) { data <- list( rt_half_window = as.integer((backcalc$rt_window - 1) / 2), - # nolint start backcalc_prior = data.table::fcase( backcalc$prior == "none", 0, backcalc$prior == "reports", 1, @@ -294,7 +293,6 @@ create_backcalc_data <- function(backcalc = backcalc_opts()) { default = 0 ) ) - # nolint end return(data) } #' Create Gaussian Process Data @@ -359,14 +357,13 @@ create_gp_data <- function(gp = gp_opts(), data) { ls_min = gp$ls_min, ls_max = data$t - data$seeding_time - data$horizon, alpha_sd = gp$alpha_sd, - # nolint start + gp_type = data.table::fcase( gp$kernel == "se", 0, gp$kernel == "matern", 1, default = 0 ) ) - # nolint end gp_data <- c(data, gp_data) return(gp_data) diff --git a/R/estimate_infections.R b/R/estimate_infections.R index 812ba8e67..c575c49a7 100644 --- a/R/estimate_infections.R +++ b/R/estimate_infections.R @@ -695,7 +695,6 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, format_out$samples <- format_out$samples[, strat := NA] } # add type based on horizon - # nolint start format_out$samples <- format_out$samples[ , type := data.table::fcase( @@ -706,7 +705,6 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, default = "estimate" ) ] - # nolint end # remove burn in period if specified if (burn_in > 0) { diff --git a/R/summarise.R b/R/summarise.R index 22264dd33..bd17e6d97 100644 --- a/R/summarise.R +++ b/R/summarise.R @@ -667,11 +667,11 @@ calc_CrIs <- function(samples, summarise_by = NULL, CrIs = c(0.2, 0.5, 0.9)) { with_CrIs <- data.table::rbindlist(with_CrIs) scale_CrIs <- round(CrIs * 100, 1) - # nolint start + order_CrIs <- c( paste0("lower_", rev(scale_CrIs)), paste0("upper_", scale_CrIs) ) - # nolint end + with_CrIs <- data.table::dcast( with_CrIs, ... ~ factor(CrI, levels = order_CrIs), value.var = "value" From 84491c87d41e697e635317994584b320bf0e3c48 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 18:30:24 +0100 Subject: [PATCH 19/31] fixed indentation --- R/estimate_infections.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/estimate_infections.R b/R/estimate_infections.R index c575c49a7..f49076327 100644 --- a/R/estimate_infections.R +++ b/R/estimate_infections.R @@ -704,7 +704,7 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, is.na(date), NA_character_, default = "estimate" ) - ] + ] # remove burn in period if specified if (burn_in > 0) { From 610fb6d5069cc09f27973d7974baf7e508de3217 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 18:42:42 +0100 Subject: [PATCH 20/31] added James Azam as contributor --- DESCRIPTION | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 83b39ccde..fbd7f8fde 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -67,6 +67,11 @@ Authors@R: family = "Chapman", role = "ctb", email = "lloyd.chapman1@lshtm.ac.uk "), + person(given = "James M.", + family = "Azam", + role = "ctb", + email = "james.azam@lshtm.ac.uk", + comment = c(ORCID = "0000-0001-5782-7330")), person(given = "EpiForecasts", role = "aut"), person(given = "Sebastian", From bf9459c363c6804400445599532ccb4fa3174296 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 18:46:04 +0100 Subject: [PATCH 21/31] removed a space --- R/create.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/create.R b/R/create.R index aa7601a45..527b76804 100644 --- a/R/create.R +++ b/R/create.R @@ -357,7 +357,6 @@ create_gp_data <- function(gp = gp_opts(), data) { ls_min = gp$ls_min, ls_max = data$t - data$seeding_time - data$horizon, alpha_sd = gp$alpha_sd, - gp_type = data.table::fcase( gp$kernel == "se", 0, gp$kernel == "matern", 1, From 276af42c192613a78ea60e995716a9443c5d8c69 Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 18:54:43 +0100 Subject: [PATCH 22/31] incremented the version and added a news item --- DESCRIPTION | 2 +- NEWS.md | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index fbd7f8fde..3d8e87732 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: EpiNow2 Title: Estimate Real-Time Case Counts and Time-Varying Epidemiological Parameters -Version: 1.3.6.2000 +Version: 1.3.6.2001 Authors@R: c(person(given = "Sam", family = "Abbott", diff --git a/NEWS.md b/NEWS.md index 109d63530..10234a997 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ -# EpiNow2 1.3.6.2000 +# EpiNow2 (development version) + +# EpiNow2 1.3.6.2001 This release is in development. For a stable release install 1.3.5 from CRAN. @@ -10,6 +12,8 @@ This release is in development. For a stable release install 1.3.5 from CRAN. * Added a GitHub Action to build the README when it is altered. * Added handling of edge case where we sample from the negative binomial with mean close or equal to 0. By @sbfnk in #366. +* Replaced use of nested `ifelse()` and `data.table::fifelse()` in the + code base with `data.table::fcase()`. # EpiNow2 1.3.5 From 3841eaac8e4c97229f259d906a3c372059519a3a Mon Sep 17 00:00:00 2001 From: jamesmbaazam Date: Thu, 11 May 2023 19:30:29 +0100 Subject: [PATCH 23/31] removed a space --- R/dist.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dist.R b/R/dist.R index cdadcd4f2..5966a7cb3 100644 --- a/R/dist.R +++ b/R/dist.R @@ -205,7 +205,7 @@ dist_fit <- function(values = NULL, samples = NULL, cores = 1, if (samples < 1000) { samples <- 1000 - } + } # model parameters lows <- values - 1 lows <- ifelse(lows <= 0, 1e-6, lows) From 6109f24518cf9d88c0eee658f3726cc546b3f5fa Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Sat, 13 May 2023 16:33:35 +0100 Subject: [PATCH 24/31] linting: removed whitespace and added lint gates --- R/summarise.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/summarise.R b/R/summarise.R index bd17e6d97..22264dd33 100644 --- a/R/summarise.R +++ b/R/summarise.R @@ -667,11 +667,11 @@ calc_CrIs <- function(samples, summarise_by = NULL, CrIs = c(0.2, 0.5, 0.9)) { with_CrIs <- data.table::rbindlist(with_CrIs) scale_CrIs <- round(CrIs * 100, 1) - + # nolint start order_CrIs <- c( paste0("lower_", rev(scale_CrIs)), paste0("upper_", scale_CrIs) ) - + # nolint end with_CrIs <- data.table::dcast( with_CrIs, ... ~ factor(CrI, levels = order_CrIs), value.var = "value" From 74f699bb7f0bfc443be60576241c57980f29922f Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Sat, 13 May 2023 16:34:30 +0100 Subject: [PATCH 25/31] redoc'd package --- man/EpiNow2-package.Rd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/EpiNow2-package.Rd b/man/EpiNow2-package.Rd index e3f954023..3f75b2a6c 100644 --- a/man/EpiNow2-package.Rd +++ b/man/EpiNow2-package.Rd @@ -43,6 +43,7 @@ Other contributors: \item Peter Ellis \email{peter.ellis2013nz@gmail.com} [contributor] \item Pietro Monticone \email{pietro.monticone@edu.unito.it} [contributor] \item Lloyd Chapman \email{lloyd.chapman1@lshtm.ac.uk } [contributor] + \item James M. Azam \email{james.azam@lshtm.ac.uk} (\href{https://orcid.org/0000-0001-5782-7330}{ORCID}) [contributor] } } From b853c9875b1cdbbee8909c5e125d27cb4e400949 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Tue, 16 May 2023 21:24:31 +0100 Subject: [PATCH 26/31] incremented dev version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3d8e87732..60aefe145 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: EpiNow2 Title: Estimate Real-Time Case Counts and Time-Varying Epidemiological Parameters -Version: 1.3.6.2001 +Version: 1.3.6.3000 Authors@R: c(person(given = "Sam", family = "Abbott", From 37786f08efbcbde670b46975e30a6d34dbd53aeb Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Tue, 16 May 2023 21:26:26 +0100 Subject: [PATCH 27/31] changed package version in NEWS --- NEWS.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 10234a997..c2e10588c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,4 @@ -# EpiNow2 (development version) - -# EpiNow2 1.3.6.2001 +# EpiNow2 1.3.6 This release is in development. For a stable release install 1.3.5 from CRAN. From b286eba30d9cc794633f81800bb966b99d66478e Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Tue, 16 May 2023 21:35:00 +0100 Subject: [PATCH 28/31] Updated news entry to link PR --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index c2e10588c..58a63432c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,7 +11,7 @@ This release is in development. For a stable release install 1.3.5 from CRAN. * Added handling of edge case where we sample from the negative binomial with mean close or equal to 0. By @sbfnk in #366. * Replaced use of nested `ifelse()` and `data.table::fifelse()` in the - code base with `data.table::fcase()`. + code base with `data.table::fcase()`. By @jamesmbaazam in #383 and reviewed by @seabbs. # EpiNow2 1.3.5 From e8836198bdd9a0882cc7a8d7a24065c5141410cb Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Tue, 16 May 2023 22:59:57 +0100 Subject: [PATCH 29/31] fixed indentation --- R/create.R | 18 +++++++++--------- R/estimate_infections.R | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/R/create.R b/R/create.R index 527b76804..75a0adaa3 100644 --- a/R/create.R +++ b/R/create.R @@ -349,15 +349,15 @@ create_gp_data <- function(gp = gp_opts(), data) { # map settings to underlying gp stan requirements gp_data <- list( - fixed = as.numeric(fixed), - M = M, - L = gp$boundary_scale, - ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd), - ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd), - ls_min = gp$ls_min, - ls_max = data$t - data$seeding_time - data$horizon, - alpha_sd = gp$alpha_sd, - gp_type = data.table::fcase( + fixed = as.numeric(fixed), + M = M, + L = gp$boundary_scale, + ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd), + ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd), + ls_min = gp$ls_min, + ls_max = data$t - data$seeding_time - data$horizon, + alpha_sd = gp$alpha_sd, + gp_type = data.table::fcase( gp$kernel == "se", 0, gp$kernel == "matern", 1, default = 0 diff --git a/R/estimate_infections.R b/R/estimate_infections.R index f49076327..eaf86b959 100644 --- a/R/estimate_infections.R +++ b/R/estimate_infections.R @@ -698,13 +698,14 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date, format_out$samples <- format_out$samples[ , type := data.table::fcase( - date > (max(date, na.rm = TRUE) - horizon), "forecast", + date > (max(date, na.rm = TRUE) - horizon), + "forecast", date > (max(date, na.rm = TRUE) - horizon - shift), "estimate based on partial data", is.na(date), NA_character_, default = "estimate" ) - ] + ] # remove burn in period if specified if (burn_in > 0) { From 70483ea9c478a0a80acb04d47e1c3244f553923c Mon Sep 17 00:00:00 2001 From: Sam Abbott Date: Wed, 17 May 2023 11:34:01 +0100 Subject: [PATCH 30/31] remove space --- R/dist.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/dist.R b/R/dist.R index 5966a7cb3..35e5dd0b2 100644 --- a/R/dist.R +++ b/R/dist.R @@ -206,6 +206,7 @@ dist_fit <- function(values = NULL, samples = NULL, cores = 1, if (samples < 1000) { samples <- 1000 } + # model parameters lows <- values - 1 lows <- ifelse(lows <= 0, 1e-6, lows) From 2f2a765553586f4d43c49f6dd407d4c5c9940a27 Mon Sep 17 00:00:00 2001 From: Sam Abbott Date: Wed, 17 May 2023 11:35:24 +0100 Subject: [PATCH 31/31] fix space --- R/create.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/create.R b/R/create.R index 75a0adaa3..c7b6c2922 100644 --- a/R/create.R +++ b/R/create.R @@ -326,7 +326,6 @@ create_backcalc_data <- function(backcalc = backcalc_opts()) { #' #' # custom lengthscale #' create_gp_data(gp_opts(ls_mean = 14), data) - create_gp_data <- function(gp = gp_opts(), data) { # Define if GP is on or off if (is.null(gp)) {