From efb2ec17818ec4f00925a70acbe61b12d5b9f5a9 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:23:48 +0000 Subject: [PATCH 01/17] Deprecate adjust_infection_to_report() --- R/adjust.R | 7 ++++++- tests/testthat/test-adjust_infection_to_report.R | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/R/adjust.R b/R/adjust.R index d1352689c..3dc7a64f2 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -1,6 +1,6 @@ #' Adjust from Case Counts by Infection Date to Date of Report #' -#' @description `r lifecycle::badge("stable")` +#' @description `r lifecycle::badge("deprecated")` #' Maps from cases by date of infection to date of report via date of #' onset. #' @param infections `` containing a `date` variable and a numeric @@ -25,6 +25,7 @@ #' @inheritParams sample_approx_dist #' @importFrom data.table setorder data.table data.table #' @importFrom lubridate wday +#' @importFrom lifecycle deprecate_warn #' @examples #' \donttest{ #' # define example cases @@ -82,6 +83,10 @@ adjust_infection_to_report <- function(infections, delay_defs, reporting_model, reporting_effect, type = "sample", truncate_future = TRUE) { + deprecate_warn( + "1.5.0", + "adjust_infection_to_report()" + ) # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/tests/testthat/test-adjust_infection_to_report.R b/tests/testthat/test-adjust_infection_to_report.R index 6f73aa227..cb0a8ab6c 100644 --- a/tests/testthat/test-adjust_infection_to_report.R +++ b/tests/testthat/test-adjust_infection_to_report.R @@ -38,3 +38,14 @@ test_that("adjust_infection_to_report can correctly handle a mapping with a day expect_true(all(!is.infinite(reports$cases))) expect_true(all(!is.na(reports$cases))) }) + +test_that("deprecation warnings are caught", { + # define example cases + cases <- data.table::copy(example_confirmed)[, cases := as.integer(confirm)] + expect_deprecated( + adjust_infection_to_report( + cases, + delay_defs = list(incubation_def, delay_def) + ) + ) +}) \ No newline at end of file From 228a8a07b9e7ace4fe08ab0d1d395a7545040a3d Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:24:48 +0000 Subject: [PATCH 02/17] Deprecate sample_approx_dist() --- R/dist.R | 7 ++++++- man/sample_approx_dist.Rd | 2 +- tests/testthat/test-dist.R | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/R/dist.R b/R/dist.R index 2611d7028..eaa781b1c 100644 --- a/R/dist.R +++ b/R/dist.R @@ -614,7 +614,7 @@ estimate_delay <- function(delays, ...) { #' Approximate Sampling a Distribution using Counts #' -#' @description `r lifecycle::badge("soft-deprecated")` +#' @description `r lifecycle::badge("deprecated")` #' Convolves cases by a PMF function. This function will soon be removed or #' replaced with a more robust stan implementation. #' @@ -645,6 +645,7 @@ estimate_delay <- function(delays, ...) { #' @export #' @importFrom data.table data.table setorder #' @importFrom lubridate days +#' @importFrom lifecycle deprecate_warn #' @examples #' \donttest{ #' cases <- example_confirmed @@ -706,6 +707,10 @@ sample_approx_dist <- function(cases = NULL, direction = "backwards", type = "sample", truncate_future = TRUE) { + deprecate_warn( + "1.5.0", + "sample_approx_dist()" + ) if (type == "sample") { if (direction == "backwards") { direction_fn <- rev diff --git a/man/sample_approx_dist.Rd b/man/sample_approx_dist.Rd index 817fbca38..a2adf55ef 100644 --- a/man/sample_approx_dist.Rd +++ b/man/sample_approx_dist.Rd @@ -42,7 +42,7 @@ after the first date reported in the data. Defaults to \code{TRUE}.} A \verb{} of cases by date of onset } \description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#soft-deprecated}{\figure{lifecycle-soft-deprecated.svg}{options: alt='[Soft-deprecated]'}}}{\strong{[Soft-deprecated]}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Convolves cases by a PMF function. This function will soon be removed or replaced with a more robust stan implementation. } diff --git a/tests/testthat/test-dist.R b/tests/testthat/test-dist.R index 0902f7183..57d9ca111 100644 --- a/tests/testthat/test-dist.R +++ b/tests/testthat/test-dist.R @@ -26,3 +26,17 @@ test_that("distributions are the same in R and stan", { expect_equal(pmf_r_lognormal, pmf_stan_lognormal) expect_equal(pmf_r_gamma, pmf_stan_gamma) }) + +test_that("deprecation warnins are caught", { + delay_fn <- function(n, dist, cum) { + pgamma(n + 0.9999, 2, 1) - pgamma(n - 1e-5, 2, 1) + } + expect_deprecated( + sample_approx_dist( + cases = example_confirmed[1:5], + dist_fn = delay_fn, + direction = "forwards", + type = "median" + ) + ) +}) \ No newline at end of file From f4e59050e16c3df6268318fc549dc177bbeaee0c Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:25:12 +0000 Subject: [PATCH 03/17] Deprecate report_cases() --- R/report.R | 7 ++++++- man/report_cases.Rd | 2 +- tests/testthat/test-report_cases.R | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/R/report.R b/R/report.R index ee32ea034..6eb3d7d83 100644 --- a/R/report.R +++ b/R/report.R @@ -1,6 +1,6 @@ #' Report case counts by date of report #' -#' @description `r lifecycle::badge("soft-deprecated")` +#' @description `r lifecycle::badge("deprecated")` #' Convolves latent infections to reported cases via an observation model. #' Likely to be removed/replaced in later releases by functionality drawing on #' the `stan` implementation. @@ -27,6 +27,7 @@ #' @inheritParams adjust_infection_to_report #' @importFrom data.table data.table rbindlist #' @importFrom future.apply future_lapply +#' @importFrom lifecycle deprecate_warn #' @examples #' \donttest{ #' # define example cases @@ -51,6 +52,10 @@ report_cases <- function(case_estimates, type = "sample", reporting_effect, CrIs = c(0.2, 0.5, 0.9)) { + deprecate_warn( + "1.5.0", + "report_cases()", + ) samples <- length(unique(case_estimates$sample)) # define delay distributions diff --git a/man/report_cases.Rd b/man/report_cases.Rd index e7d13a026..9e0cdc5b6 100644 --- a/man/report_cases.Rd +++ b/man/report_cases.Rd @@ -43,7 +43,7 @@ variables \code{sample}, \code{date} and \code{cases} with the second being summ across samples. } \description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#soft-deprecated}{\figure{lifecycle-soft-deprecated.svg}{options: alt='[Soft-deprecated]'}}}{\strong{[Soft-deprecated]}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Convolves latent infections to reported cases via an observation model. Likely to be removed/replaced in later releases by functionality drawing on the \code{stan} implementation. diff --git a/tests/testthat/test-report_cases.R b/tests/testthat/test-report_cases.R index 5378df768..ff5008535 100644 --- a/tests/testthat/test-report_cases.R +++ b/tests/testthat/test-report_cases.R @@ -19,3 +19,19 @@ test_that("report_cases can simulate infections forward", { expect_equal(class(reported_cases$summarised$median), "numeric") set.seed(Sys.time()) }) + +test_that("deprecated warnings are caught", { + cases <- example_confirmed[1:40] + # get example delays + #' # Instead of running them model we use example + #' # data for speed in this example. + cases <- cases[, cases := as.integer(confirm)] + cases <- cases[, confirm := NULL][, sample := 1] + expect_deprecated( + report_cases( + case_estimates = cases, + delays = delay_opts(example_incubation_period + example_reporting_delay), + type = "sample" + ) + ) +}) From 84034a1c2d3c9c9db3f2c24f590e65d9f9f260ec Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:25:29 +0000 Subject: [PATCH 04/17] Generate .Rd file --- man/adjust_infection_to_report.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/adjust_infection_to_report.Rd b/man/adjust_infection_to_report.Rd index 6501c1dfb..37e530b3e 100644 --- a/man/adjust_infection_to_report.Rd +++ b/man/adjust_infection_to_report.Rd @@ -42,7 +42,7 @@ A \code{data.table} containing a \code{date} variable (date of report) and a \code{reference} which indicates what the date variable refers to. } \description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Maps from cases by date of infection to date of report via date of onset. } From 426e55f623adea6b9463f6f6d6399c2d43c63d42 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:29:52 +0000 Subject: [PATCH 05/17] Add NEWS item --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 147392a55..e21b2e071 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ * `simulate_infections` has been renamed to `forecast_infections` in line with `simulate_secondary` and `forecast_secondary`. The terminology is: a forecast is done from a fit to existing data, a simulation from first principles. By @sbfnk in #544 and reviewed by @seabbs. * A new `simulate_infections` function has been added that can be used to simulate from the model from given initial conditions and parameters. By @sbfnk in #557 and reviewed by @jamesmbaazam. * The function `init_cumulative_fit()` has been deprecated. By @jamesmbaazam in #541 and reviewed by @sbfnk. +* The functions `sample_approx_dist()`, `report_cases()`, and `adjust_infection_to_reports()` have been deprecated as the functionality they provide can now be achieved with `simulate_secondary()`. See by @jamesmbaazam and reviewed by . ## Documentation From 056c7f7a7253b7cba20522cb0c2c97b67f47cd1d Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Mon, 4 Mar 2024 16:32:30 +0000 Subject: [PATCH 06/17] Fix wrong function name in NEWS --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index e21b2e071..110142a87 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,7 +9,7 @@ * `simulate_infections` has been renamed to `forecast_infections` in line with `simulate_secondary` and `forecast_secondary`. The terminology is: a forecast is done from a fit to existing data, a simulation from first principles. By @sbfnk in #544 and reviewed by @seabbs. * A new `simulate_infections` function has been added that can be used to simulate from the model from given initial conditions and parameters. By @sbfnk in #557 and reviewed by @jamesmbaazam. * The function `init_cumulative_fit()` has been deprecated. By @jamesmbaazam in #541 and reviewed by @sbfnk. -* The functions `sample_approx_dist()`, `report_cases()`, and `adjust_infection_to_reports()` have been deprecated as the functionality they provide can now be achieved with `simulate_secondary()`. See by @jamesmbaazam and reviewed by . +* The functions `sample_approx_dist()`, `report_cases()`, and `adjust_infection_reports()` have been deprecated as the functionality they provide can now be achieved with `simulate_secondary()`. See by @jamesmbaazam and reviewed by . ## Documentation From f4c9b4d51ae5d7de5ad2854c0332e1661ce888e6 Mon Sep 17 00:00:00 2001 From: James Azam Date: Mon, 4 Mar 2024 17:31:50 +0000 Subject: [PATCH 07/17] Linting: turn off missing_argument_linter for deprecation warning --- R/adjust.R | 2 ++ R/dist.R | 2 ++ R/report.R | 2 ++ 3 files changed, 6 insertions(+) diff --git a/R/adjust.R b/R/adjust.R index 3dc7a64f2..272112b99 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -83,10 +83,12 @@ adjust_infection_to_report <- function(infections, delay_defs, reporting_model, reporting_effect, type = "sample", truncate_future = TRUE) { + #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "adjust_infection_to_report()" ) + #nolint end # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/R/dist.R b/R/dist.R index eaa781b1c..5394eb9e4 100644 --- a/R/dist.R +++ b/R/dist.R @@ -707,10 +707,12 @@ sample_approx_dist <- function(cases = NULL, direction = "backwards", type = "sample", truncate_future = TRUE) { + #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "sample_approx_dist()" ) + #nolint end if (type == "sample") { if (direction == "backwards") { direction_fn <- rev diff --git a/R/report.R b/R/report.R index 6eb3d7d83..8e46efc21 100644 --- a/R/report.R +++ b/R/report.R @@ -52,10 +52,12 @@ report_cases <- function(case_estimates, type = "sample", reporting_effect, CrIs = c(0.2, 0.5, 0.9)) { + #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "report_cases()", ) + #nolint end samples <- length(unique(case_estimates$sample)) # define delay distributions From a6eb04361f4a058ff9af43af7b02307bd99c22df Mon Sep 17 00:00:00 2001 From: James Azam Date: Mon, 4 Mar 2024 17:32:24 +0000 Subject: [PATCH 08/17] Linting: use file.path() to construct paths instead of paste0. --- R/report.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/report.R b/R/report.R index 8e46efc21..1d842b98c 100644 --- a/R/report.R +++ b/R/report.R @@ -220,7 +220,7 @@ report_summary <- function(summarised_estimates, } if (!is.null(target_folder)) { - saveRDS(summary, paste0(target_folder, "/summary.rds")) + saveRDS(summary, file.path(target_folder, "summary.rds")) } return(summary) } From 20ef605e546c759ad61a6b4089e5df818203a17f Mon Sep 17 00:00:00 2001 From: James Azam Date: Tue, 5 Mar 2024 12:03:53 +0000 Subject: [PATCH 09/17] Remove unnecessary nolint tags --- R/adjust.R | 2 -- R/dist.R | 2 -- R/report.R | 2 -- 3 files changed, 6 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index 272112b99..3dc7a64f2 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -83,12 +83,10 @@ adjust_infection_to_report <- function(infections, delay_defs, reporting_model, reporting_effect, type = "sample", truncate_future = TRUE) { - #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "adjust_infection_to_report()" ) - #nolint end # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/R/dist.R b/R/dist.R index 5394eb9e4..eaa781b1c 100644 --- a/R/dist.R +++ b/R/dist.R @@ -707,12 +707,10 @@ sample_approx_dist <- function(cases = NULL, direction = "backwards", type = "sample", truncate_future = TRUE) { - #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "sample_approx_dist()" ) - #nolint end if (type == "sample") { if (direction == "backwards") { direction_fn <- rev diff --git a/R/report.R b/R/report.R index 1d842b98c..c5fe3dfe7 100644 --- a/R/report.R +++ b/R/report.R @@ -52,12 +52,10 @@ report_cases <- function(case_estimates, type = "sample", reporting_effect, CrIs = c(0.2, 0.5, 0.9)) { - #nolint start: missing_argument_linter deprecate_warn( "1.5.0", "report_cases()", ) - #nolint end samples <- length(unique(case_estimates$sample)) # define delay distributions From 4298599ef7a93a84669614eae9de751b403cc406 Mon Sep 17 00:00:00 2001 From: James Azam Date: Tue, 5 Mar 2024 12:04:12 +0000 Subject: [PATCH 10/17] Remove trailing comma --- R/report.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/report.R b/R/report.R index c5fe3dfe7..5a6b27433 100644 --- a/R/report.R +++ b/R/report.R @@ -54,7 +54,7 @@ report_cases <- function(case_estimates, CrIs = c(0.2, 0.5, 0.9)) { deprecate_warn( "1.5.0", - "report_cases()", + "report_cases()" ) samples <- length(unique(case_estimates$sample)) From 550674cd2cb6701f412e7fd43e0966361690233e Mon Sep 17 00:00:00 2001 From: James Azam Date: Tue, 5 Mar 2024 12:05:35 +0000 Subject: [PATCH 11/17] Add more details to deprecation warning --- R/adjust.R | 3 ++- R/dist.R | 3 ++- R/report.R | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index 3dc7a64f2..bc04c1daf 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -85,7 +85,8 @@ adjust_infection_to_report <- function(infections, delay_defs, truncate_future = TRUE) { deprecate_warn( "1.5.0", - "adjust_infection_to_report()" + "adjust_infection_to_report()", + "The function will be removed completely in version 2.0.0." ) # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/R/dist.R b/R/dist.R index eaa781b1c..e02509566 100644 --- a/R/dist.R +++ b/R/dist.R @@ -709,7 +709,8 @@ sample_approx_dist <- function(cases = NULL, truncate_future = TRUE) { deprecate_warn( "1.5.0", - "sample_approx_dist()" + "sample_approx_dist()", + "The function will be removed completely in version 2.0.0." ) if (type == "sample") { if (direction == "backwards") { diff --git a/R/report.R b/R/report.R index 5a6b27433..3a2cdb227 100644 --- a/R/report.R +++ b/R/report.R @@ -54,7 +54,8 @@ report_cases <- function(case_estimates, CrIs = c(0.2, 0.5, 0.9)) { deprecate_warn( "1.5.0", - "report_cases()" + "report_cases()", + "The function will be removed completely in version 2.0.0." ) samples <- length(unique(case_estimates$sample)) From c25965c4805fd97c6486226125fc0f34ec3a7a22 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Wed, 6 Mar 2024 12:07:28 +0000 Subject: [PATCH 12/17] Assign details argument explicitly --- R/adjust.R | 2 +- R/dist.R | 2 +- R/report.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index bc04c1daf..0ca5969b5 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -86,7 +86,7 @@ adjust_infection_to_report <- function(infections, delay_defs, deprecate_warn( "1.5.0", "adjust_infection_to_report()", - "The function will be removed completely in version 2.0.0." + details = "The function will be removed completely in version 2.0.0." ) # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/R/dist.R b/R/dist.R index e02509566..f29eeb73e 100644 --- a/R/dist.R +++ b/R/dist.R @@ -710,7 +710,7 @@ sample_approx_dist <- function(cases = NULL, deprecate_warn( "1.5.0", "sample_approx_dist()", - "The function will be removed completely in version 2.0.0." + details = "The function will be removed completely in version 2.0.0." ) if (type == "sample") { if (direction == "backwards") { diff --git a/R/report.R b/R/report.R index 3a2cdb227..c44789ebb 100644 --- a/R/report.R +++ b/R/report.R @@ -55,7 +55,7 @@ report_cases <- function(case_estimates, deprecate_warn( "1.5.0", "report_cases()", - "The function will be removed completely in version 2.0.0." + details = "The function will be removed completely in version 2.0.0." ) samples <- length(unique(case_estimates$sample)) From 128c553ac16e16cd4ced81bd9a6e2af7ce3db25c Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Fri, 15 Mar 2024 17:32:38 +0000 Subject: [PATCH 13/17] Add pointers to replacement functions in the deprecation warnings --- R/adjust.R | 11 ++++++++--- R/report.R | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index c297fc78b..b5bbd1317 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -84,9 +84,14 @@ adjust_infection_to_report <- function(infections, delay_defs, type = "sample", truncate_future = TRUE) { deprecate_warn( - "1.5.0", - "adjust_infection_to_report()", - details = "The function will be removed completely in version 2.0.0." + when = "1.5.0", + what = "adjust_infection_to_report()", + with = "simulate_secondary()", + details = c( + "See equivalent examples using `simulate_secondary()`", + "in ?adjust_infection_to_report.", + "This function will be removed completely in version 2.0.0." + ) ) # Reset DT Defaults on Exit set_dt_single_thread() diff --git a/R/report.R b/R/report.R index cc601d0b0..cfb392dd7 100644 --- a/R/report.R +++ b/R/report.R @@ -53,9 +53,14 @@ report_cases <- function(case_estimates, reporting_effect, CrIs = c(0.2, 0.5, 0.9)) { deprecate_warn( - "1.5.0", - "report_cases()", - details = "The function will be removed completely in version 2.0.0." + when = "1.5.0", + what = "report_cases()", + with = "simulate_secondary()", + details = c( + "See equivalent examples using `simulate_secondary()`", + "in ?adjust_infection_to_report.", + "This function will be removed completely in version 2.0.0." + ) ) samples <- length(unique(case_estimates$sample)) From 973760325f5dd3db1aa07422f7bc8b901c804754 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Fri, 15 Mar 2024 17:33:32 +0000 Subject: [PATCH 14/17] Revise example in report_cases to show use of simulate_secondary() --- R/report.R | 32 ++++++++++++++++++++++---------- man/report_cases.Rd | 32 ++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/R/report.R b/R/report.R index cfb392dd7..f45d10143 100644 --- a/R/report.R +++ b/R/report.R @@ -30,21 +30,33 @@ #' @importFrom lifecycle deprecate_warn #' @examples #' \donttest{ -#' # define example cases +#' # This function is deprecated and its functionality can now be accessed +#' # from [simulate_secondary()]. +#' # Here are some examples of how to use [simulate_secondary()] to replace +#' # report_cases(). +#' # Old (using report_cases()): +#' # Define case data #' cases <- example_confirmed[1:40] -#' -#' # get example delays -#' # Instead of running them model we use example -#' # data for speed in this example. #' cases <- cases[, cases := as.integer(confirm)] #' cases <- cases[, confirm := NULL][, sample := 1] -#' #' reported_cases <- report_cases( -#' case_estimates = cases, -#' delays = delay_opts(example_incubation_period + example_reporting_delay), -#' type = "sample" +#' case_estimates = cases, +#' delays = delay_opts(example_incubation_period + example_reporting_delay), +#' type = "sample" +#' ) +#' print(reported_cases$samples) +#' +#' # New (using simulate_secondary()): +#' cases <- example_confirmed[1:40] +#' cases <- cases[, primary := as.integer(confirm)] +#' report <- simulate_secondary( +#' cases, +#' delays = delay_opts( +#' fix_dist(example_incubation_period + example_reporting_delay) +#' ), +#' obs = obs_opts(family = "poisson") #' ) -#' print(reported_cases) +#' print(report) #' } report_cases <- function(case_estimates, case_forecast = NULL, diff --git a/man/report_cases.Rd b/man/report_cases.Rd index 9e0cdc5b6..64b2b921e 100644 --- a/man/report_cases.Rd +++ b/man/report_cases.Rd @@ -50,20 +50,32 @@ the \code{stan} implementation. } \examples{ \donttest{ -# define example cases +# This function is deprecated and its functionality can now be accessed +# from [simulate_secondary()]. +# Here are some examples of how to use [simulate_secondary()] to replace +# report_cases(). +# Old (using report_cases()): +# Define case data cases <- example_confirmed[1:40] - -# get example delays -# Instead of running them model we use example -# data for speed in this example. cases <- cases[, cases := as.integer(confirm)] cases <- cases[, confirm := NULL][, sample := 1] - reported_cases <- report_cases( - case_estimates = cases, - delays = delay_opts(example_incubation_period + example_reporting_delay), - type = "sample" + case_estimates = cases, + delays = delay_opts(example_incubation_period + example_reporting_delay), + type = "sample" +) +print(reported_cases$samples) + +# New (using simulate_secondary()): +cases <- example_confirmed[1:40] +cases <- cases[, primary := as.integer(confirm)] +report <- simulate_secondary( + cases, + delays = delay_opts( + fix_dist(example_incubation_period + example_reporting_delay) + ), + obs = obs_opts(family = "poisson") ) -print(reported_cases) +print(report) } } From e0fd67be40a8c6e370a46e6d308453d4ef513a9f Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Fri, 15 Mar 2024 17:43:47 +0000 Subject: [PATCH 15/17] Refer to the right function in warning message --- R/report.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/report.R b/R/report.R index f45d10143..80f4cefab 100644 --- a/R/report.R +++ b/R/report.R @@ -70,7 +70,7 @@ report_cases <- function(case_estimates, with = "simulate_secondary()", details = c( "See equivalent examples using `simulate_secondary()`", - "in ?adjust_infection_to_report.", + "in ?report_cases.", "This function will be removed completely in version 2.0.0." ) ) From 0570abc83d3daedb1c928f93389982da6bd5bf2b Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Fri, 15 Mar 2024 18:13:24 +0000 Subject: [PATCH 16/17] Revise examples in adjust_infection_to_reports to demonstrate use of simulate_secondary --- R/adjust.R | 67 ++++++++++++------------------- man/adjust_infection_to_report.Rd | 67 ++++++++++++------------------- 2 files changed, 52 insertions(+), 82 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index b5bbd1317..6f9c1dbe1 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -28,56 +28,41 @@ #' @importFrom lifecycle deprecate_warn #' @examples #' \donttest{ -#' # define example cases -#' cases <- data.table::copy(example_confirmed)[, cases := as.integer(confirm)] +#' # This function is deprecated and its functionality can now be accessed +#' # from [simulate_secondary()]. +#' # Here are some examples of how to use [simulate_secondary()] to replace +#' # adjust_infection_to_report(). #' -#' # define a single report delay distribution +#' # Old (using adjust_infection_to_report()): +#' # Define example case data +#' cases <- data.table::copy(example_confirmed) +#' cases <- cases[, cases := as.integer(confirm)] +#' # Define a simple reporting delay distribution #' delay_def <- lognorm_dist_def( -#' mean = 5, mean_sd = 1, sd = 3, sd_sd = 1, -#' max_value = 30, samples = 1, to_log = TRUE -#' ) -#' -#' # define a single incubation period -#' incubation_def <- lognorm_dist_def( -#' mean = incubation_periods[1, ]$mean, -#' mean_sd = incubation_periods[1, ]$mean_sd, -#' sd = incubation_periods[1, ]$sd, -#' sd_sd = incubation_periods[1, ]$sd_sd, -#' max_value = 30, samples = 1 +#' mean = 5, mean_sd = 1, sd = 3, sd_sd = 1, +#' max_value = 30, samples = 1, to_log = TRUE #' ) -#' -#' # simple mapping #' report <- adjust_infection_to_report( -#' cases, delay_defs = list(incubation_def, delay_def) +#' cases, +#' delay_defs = list(delay_def), +#' reporting_model = function(n) rpois(length(n), n) #' ) #' print(report) #' -#' # mapping with a weekly reporting effect -#' report_weekly <- adjust_infection_to_report( -#' cases, -#' delay_defs = list(incubation_def, delay_def), -#' reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95) -#' ) -#' print(report_weekly) -#' -#' # map using a deterministic median shift for both delays -#' report_median <- adjust_infection_to_report(cases, -#' delay_defs = list(incubation_def, delay_def), -#' type = "median" +#' # New (using simulate_secondary()): +#' cases <- data.table::copy(example_confirmed) +#' cases <- cases[, primary := as.integer(confirm)] +#' uncertain_delay <- LogNormal( +#' mean = Normal(5, 1), sd = Normal(3, 1), +#' max = 30 #' ) -#' print(report_median) -#' -#' # map with a weekly reporting effect and stochastic reporting model -#' report_stochastic <- adjust_infection_to_report( -#' cases, -#' delay_defs = list(incubation_def, delay_def), -#' reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95), -#' reporting_model = function(n) { -#' out <- suppressWarnings(rnbinom(length(n), as.integer(n), 0.5)) -#' out <- ifelse(is.na(out), 0, out) -#' } +#' delay <- fix_dist(uncertain_delay, strategy = "sample") +#' report <- simulate_secondary( +#' cases, +#' delays = delay_opts(delay), +#' obs = obs_opts(family = "poisson") #' ) -#' print(report_stochastic) +#' print(report) #' } adjust_infection_to_report <- function(infections, delay_defs, reporting_model, reporting_effect, diff --git a/man/adjust_infection_to_report.Rd b/man/adjust_infection_to_report.Rd index 37e530b3e..9c952d6de 100644 --- a/man/adjust_infection_to_report.Rd +++ b/man/adjust_infection_to_report.Rd @@ -48,55 +48,40 @@ onset. } \examples{ \donttest{ -# define example cases -cases <- data.table::copy(example_confirmed)[, cases := as.integer(confirm)] +# This function is deprecated and its functionality can now be accessed +# from [simulate_secondary()]. +# Here are some examples of how to use [simulate_secondary()] to replace +# adjust_infection_to_report(). -# define a single report delay distribution +# Old (using adjust_infection_to_report()): +# Define example case data +cases <- data.table::copy(example_confirmed) +cases <- cases[, cases := as.integer(confirm)] +# Define a simple reporting delay distribution delay_def <- lognorm_dist_def( - mean = 5, mean_sd = 1, sd = 3, sd_sd = 1, - max_value = 30, samples = 1, to_log = TRUE + mean = 5, mean_sd = 1, sd = 3, sd_sd = 1, + max_value = 30, samples = 1, to_log = TRUE ) - -# define a single incubation period -incubation_def <- lognorm_dist_def( - mean = incubation_periods[1, ]$mean, - mean_sd = incubation_periods[1, ]$mean_sd, - sd = incubation_periods[1, ]$sd, - sd_sd = incubation_periods[1, ]$sd_sd, - max_value = 30, samples = 1 -) - -# simple mapping report <- adjust_infection_to_report( - cases, delay_defs = list(incubation_def, delay_def) + cases, + delay_defs = list(delay_def), + reporting_model = function(n) rpois(length(n), n) ) print(report) -# mapping with a weekly reporting effect -report_weekly <- adjust_infection_to_report( - cases, - delay_defs = list(incubation_def, delay_def), - reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95) +# New (using simulate_secondary()): +cases <- data.table::copy(example_confirmed) +cases <- cases[, primary := as.integer(confirm)] +uncertain_delay <- LogNormal( + mean = Normal(5, 1), sd = Normal(3, 1), + max = 30 ) -print(report_weekly) - -# map using a deterministic median shift for both delays -report_median <- adjust_infection_to_report(cases, - delay_defs = list(incubation_def, delay_def), - type = "median" +delay <- fix_dist(uncertain_delay, strategy = "sample") +report <- simulate_secondary( + cases, + delays = delay_opts(delay), + obs = obs_opts(family = "poisson") ) -print(report_median) - -# map with a weekly reporting effect and stochastic reporting model -report_stochastic <- adjust_infection_to_report( - cases, - delay_defs = list(incubation_def, delay_def), - reporting_effect = c(1.1, rep(1, 4), 0.95, 0.95), - reporting_model = function(n) { - out <- suppressWarnings(rnbinom(length(n), as.integer(n), 0.5)) - out <- ifelse(is.na(out), 0, out) - } -) -print(report_stochastic) +print(report) } } From 5e6c3afe89c042597de2c66d281229f8b60c9d7f Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Fri, 15 Mar 2024 18:13:37 +0000 Subject: [PATCH 17/17] Remove trailing whitespace --- R/report.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/report.R b/R/report.R index 80f4cefab..afc5b34de 100644 --- a/R/report.R +++ b/R/report.R @@ -45,7 +45,7 @@ #' type = "sample" #' ) #' print(reported_cases$samples) -#' +#' #' # New (using simulate_secondary()): #' cases <- example_confirmed[1:40] #' cases <- cases[, primary := as.integer(confirm)]