From 17219e8e42ad4c2fcc00244afbddd4b492595f3c Mon Sep 17 00:00:00 2001 From: Adam Howes Date: Thu, 11 Jul 2024 15:41:30 +0100 Subject: [PATCH] Issue 149: Remove epinowcast package and functions (#150) * Remove epinowcast dependency, and switch to stable version of data.table * Remove epinowcast package and functions only --- DESCRIPTION | 4 +- NAMESPACE | 2 - R/fitting-and-postprocessing.R | 109 -------------------------- R/globals.R | 3 - man/add_natural_scale_mean_sd.Rd | 2 - man/correct_primary_censoring_bias.Rd | 2 - man/draws_to_long.Rd | 2 - man/extract_epinowcast_draws.Rd | 31 -------- man/extract_lognormal_draws.Rd | 2 - man/make_relative_to_truth.Rd | 2 - man/sample_epinowcast_model.Rd | 41 ---------- man/sample_model.Rd | 2 - man/summarise_draws.Rd | 2 - man/summarise_variable.Rd | 2 - 14 files changed, 1 insertion(+), 205 deletions(-) delete mode 100644 man/extract_epinowcast_draws.Rd delete mode 100644 man/sample_epinowcast_model.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b73e133bc..f99447de1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,6 @@ Imports: cli Suggests: bookdown, - epinowcast, testthat (>= 3.0.0), readxl, janitor, @@ -46,8 +45,7 @@ Suggests: roxyglobals Remotes: stan-dev/cmdstanr, - Rdatatable/data.table, - epinowcast/epinowcast + Rdatatable/data.table Config/Needs/website: r-lib/pkgdown, epinowcast/enwtheme diff --git a/NAMESPACE b/NAMESPACE index 9f0551e7c..232a2dad4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,7 +29,6 @@ export(epidist_stan_chunk) export(epidist_stancode) export(epidist_version_stanvar) export(event_to_incidence) -export(extract_epinowcast_draws) export(extract_lognormal_draws) export(filter_obs_by_obs_time) export(filter_obs_by_ptime) @@ -46,7 +45,6 @@ export(plot_mean_posterior_pred) export(plot_recovery) export(plot_relative_recovery) export(reverse_obs_at) -export(sample_epinowcast_model) export(sample_model) export(simulate_double_censored_pmf) export(simulate_exponential_cases) diff --git a/R/fitting-and-postprocessing.R b/R/fitting-and-postprocessing.R index d43931105..0f8333a85 100644 --- a/R/fitting-and-postprocessing.R +++ b/R/fitting-and-postprocessing.R @@ -54,76 +54,6 @@ sample_model <- function(model, data, scenario = data.table::data.table(id = 1), return(out[]) } -#' Sample from the posterior of an epinowcast model with additional diagnositics -#' -#' @inheritParams sample_model -#' @family postprocess -#' @export -sample_epinowcast_model <- function( - model, data, scenario = data.table::data.table(id = 1), - diagnostics = TRUE, ... -) { - - out <- data.table::copy(scenario) - - # Setup failure tolerant model fitting - fit_model <- function(model, data, ...) { - fit <- epinowcast::enw_model( - target_dir = here::here("data", "models") - )$sample(data = data, ...) - print(fit) - return(fit) - } - safe_fit_model <- purrr::safely(fit_model) - fit <- safe_fit_model(model, data, ...) - - if (!is.null(fit$error)) { - out[, error := list(fit$error[[1]])] - diagnostics <- FALSE - }else { - out[, fit := list(fit$result)] - fit <- fit$result - } - - if (diagnostics) { - diag <- fit$sampler_diagnostics(format = "df") - diagnostics <- data.table( - samples = nrow(diag), - max_rhat = round(max( - fit$summary( - variables = NULL, posterior::rhat, - .args = list(na.rm = TRUE) - )$`posterior::rhat`, - na.rm = TRUE - ), 0), - min_ess_bulk = round(min( - fit$summary( - variables = NULL, posterior::ess_bulk, - .args = list(na.rm = TRUE) - )$`posterior::ess_bulk`, - na.rm = TRUE - ), 2), - min_ess_tail = round(min( - fit$summary( - variables = NULL, posterior::ess_tail, - .args = list(na.rm = TRUE) - )$`posterior::ess_tail`, - na.rm = TRUE - ), 0), - divergent_transitions = sum(diag$divergent__), - per_divergent_transitions = sum(diag$divergent__) / nrow(diag), - max_treedepth = max(diag$treedepth__) - ) - diagnostics[, no_at_max_treedepth := sum(diag$treedepth__ == max_treedepth)] - diagnostics[, per_at_max_treedepth := no_at_max_treedepth / nrow(diag)] - out <- cbind(out, diagnostics) - - timing <- round(max(fit$metadata()$time$total), 1) - out[, run_time := timing] - } - return(out[]) -} - #' Add natural scale summary parameters for a lognormal distribution #' #' @param dt ... @@ -182,45 +112,6 @@ extract_lognormal_draws <- function(data, id_vars, from_dt = FALSE) { return(draws[]) } -#' Extract posterior samples for a lognormal epinowcast model -#' -#' @inheritParams extract_lognormal_draws -#' @family postprocess -#' @autoglobal -#' @export -extract_epinowcast_draws <- function( - data, id_vars, from_dt = FALSE -) { - if (from_dt) { - if (!any(colnames(data) %in% "fit")) { - return(id_vars[]) - } - draws <- data$fit[[1]]$draws( - variables = c("refp_mean_int[1]", "refp_sd_int[1]"), format = "draws_df" - ) - }else { - draws <- data$fit[[1]]$draws( - variables = c("refp_mean_int[1]", "refp_sd_int[1]"), format = "draws_df" - ) - } - - draws <- data.table::setDT(draws) - - data.table::setnames( - draws, c("refp_mean_int[1]", "refp_sd_int[1]"), c("meanlog", "sdlog"), - skip_absent = TRUE - ) - draws <- draws[, list(meanlog, sdlog)] - draws <- add_natural_scale_mean_sd(draws) - - if (!missing(id_vars)) { - draws <- merge( - draws[, id := id_vars$id], id_vars, by = "id" - ) - } - return(draws[]) -} - #' Primary event bias correction #' #' @param draws ... diff --git a/R/globals.R b/R/globals.R index 9841e25ca..c8430bce9 100644 --- a/R/globals.R +++ b/R/globals.R @@ -13,9 +13,6 @@ utils::globalVariables(c( "sdlog_log", # "meanlog", # "id", # - "meanlog", # - "sdlog", # - "id", # "true_value", # "value", # "rel_value", # diff --git a/man/add_natural_scale_mean_sd.Rd b/man/add_natural_scale_mean_sd.Rd index f9cd629e8..269283932 100644 --- a/man/add_natural_scale_mean_sd.Rd +++ b/man/add_natural_scale_mean_sd.Rd @@ -16,10 +16,8 @@ Add natural scale summary parameters for a lognormal distribution Other postprocess: \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} diff --git a/man/correct_primary_censoring_bias.Rd b/man/correct_primary_censoring_bias.Rd index 1b5718e1a..1b167e343 100644 --- a/man/correct_primary_censoring_bias.Rd +++ b/man/correct_primary_censoring_bias.Rd @@ -16,10 +16,8 @@ Primary event bias correction Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} diff --git a/man/draws_to_long.Rd b/man/draws_to_long.Rd index cf4b955fd..0c64dfdaf 100644 --- a/man/draws_to_long.Rd +++ b/man/draws_to_long.Rd @@ -16,10 +16,8 @@ Convert posterior lognormal samples to long format Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} diff --git a/man/extract_epinowcast_draws.Rd b/man/extract_epinowcast_draws.Rd deleted file mode 100644 index 8e3f2db06..000000000 --- a/man/extract_epinowcast_draws.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fitting-and-postprocessing.R -\name{extract_epinowcast_draws} -\alias{extract_epinowcast_draws} -\title{Extract posterior samples for a lognormal epinowcast model} -\usage{ -extract_epinowcast_draws(data, id_vars, from_dt = FALSE) -} -\arguments{ -\item{data}{...} - -\item{id_vars}{...} - -\item{from_dt}{...} -} -\description{ -Extract posterior samples for a lognormal epinowcast model -} -\seealso{ -Other postprocess: -\code{\link{add_natural_scale_mean_sd}()}, -\code{\link{correct_primary_censoring_bias}()}, -\code{\link{draws_to_long}()}, -\code{\link{extract_lognormal_draws}()}, -\code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, -\code{\link{sample_model}()}, -\code{\link{summarise_draws}()}, -\code{\link{summarise_variable}()} -} -\concept{postprocess} diff --git a/man/extract_lognormal_draws.Rd b/man/extract_lognormal_draws.Rd index 526d64a1c..d154b6abb 100644 --- a/man/extract_lognormal_draws.Rd +++ b/man/extract_lognormal_draws.Rd @@ -21,9 +21,7 @@ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} diff --git a/man/make_relative_to_truth.Rd b/man/make_relative_to_truth.Rd index 8a2eac6df..99573f12a 100644 --- a/man/make_relative_to_truth.Rd +++ b/man/make_relative_to_truth.Rd @@ -21,9 +21,7 @@ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} diff --git a/man/sample_epinowcast_model.Rd b/man/sample_epinowcast_model.Rd deleted file mode 100644 index db4cae915..000000000 --- a/man/sample_epinowcast_model.Rd +++ /dev/null @@ -1,41 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fitting-and-postprocessing.R -\name{sample_epinowcast_model} -\alias{sample_epinowcast_model} -\title{Sample from the posterior of an epinowcast model with additional diagnositics} -\usage{ -sample_epinowcast_model( - model, - data, - scenario = data.table::data.table(id = 1), - diagnostics = TRUE, - ... -) -} -\arguments{ -\item{model}{...} - -\item{data}{...} - -\item{scenario}{...} - -\item{diagnostics}{...} - -\item{...}{...} -} -\description{ -Sample from the posterior of an epinowcast model with additional diagnositics -} -\seealso{ -Other postprocess: -\code{\link{add_natural_scale_mean_sd}()}, -\code{\link{correct_primary_censoring_bias}()}, -\code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, -\code{\link{extract_lognormal_draws}()}, -\code{\link{make_relative_to_truth}()}, -\code{\link{sample_model}()}, -\code{\link{summarise_draws}()}, -\code{\link{summarise_variable}()} -} -\concept{postprocess} diff --git a/man/sample_model.Rd b/man/sample_model.Rd index d374b0608..fbb6a5e40 100644 --- a/man/sample_model.Rd +++ b/man/sample_model.Rd @@ -31,10 +31,8 @@ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{summarise_draws}()}, \code{\link{summarise_variable}()} } diff --git a/man/summarise_draws.Rd b/man/summarise_draws.Rd index 40edb763a..617082e01 100644 --- a/man/summarise_draws.Rd +++ b/man/summarise_draws.Rd @@ -23,10 +23,8 @@ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_variable}()} } diff --git a/man/summarise_variable.Rd b/man/summarise_variable.Rd index a36e94737..955897398 100644 --- a/man/summarise_variable.Rd +++ b/man/summarise_variable.Rd @@ -23,10 +23,8 @@ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, \code{\link{correct_primary_censoring_bias}()}, \code{\link{draws_to_long}()}, -\code{\link{extract_epinowcast_draws}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, -\code{\link{sample_epinowcast_model}()}, \code{\link{sample_model}()}, \code{\link{summarise_draws}()} }