EpiNow2 icon indicating copy to clipboard operation
EpiNow2 copied to clipboard

error with fixed delays in `estimate_secondary()` + `forecast_secondary()` workflow

Open avallecam opened this issue 8 months ago • 0 comments

Summary: fixed delays generate error in estimate_secondary() + forecast_secondary() workflow, but not with uncertain delays

Description: Fixed delays generate an error in the forecast_secondary() function when running the estimate_secondary() + forecast_secondary() workflow. I compared this with the same workflow but with an uncertain distribution, and it worked fine. I detected this error when trying to reproduce a tutorial step that worked with the previous version using incidence2::covidregionaldataUK

I compared this with the uncertain distribution because in the documentation it is as default input https://epiforecasts.io/EpiNow2/reference/estimate_secondary.html

Reproducible Steps:

This uses data from the {cfr} package

library(EpiNow2)
#> 
#> Attaching package: 'EpiNow2'
#> The following object is masked from 'package:stats':
#> 
#>     Gamma
library (cfr)
library(tidyverse)

# data --------------------------------------------------------------------

reported_cases_deaths <- cfr::ebola1976 %>% 
  dplyr::rename(primary = cases, secondary = deaths)

# Estimate from day 1 to day 35 of this data
cases_to_estimate <- reported_cases_deaths %>%
  slice(1:35)

# Forecast from day 36 to day 73
cases_to_forecast <- reported_cases_deaths %>%
  dplyr::slice(36:73) %>% 
  dplyr::mutate(value = primary)



# works: uncertain delay workflow -----------------------------------------


# delay
delay_uncertain <- EpiNow2::Gamma(
  mean = EpiNow2::Normal(mean = 14, sd = 0.5),
  sd = EpiNow2::Normal(mean = 5, sd = 0.5),
  max = 30
)
#> Warning in new_dist_spec(params, "gamma"): Uncertain gamma distribution
#> specified in terms of parameters that are not the "natural" parameters of the
#> distribution (shape, rate). Converting using a crude and very approximate
#> method that is likely to produce biased results. If possible, it is preferable
#> to specify the distribution directly in terms of the natural parameters.


# estimate secondary cases
secondary_uncertain <- EpiNow2::estimate_secondary(
  data = cases_to_estimate,
  delays = EpiNow2::delay_opts(delay_uncertain)
)
#> WARN [2024-06-22 01:49:44] estimate_secondary (chain: 1): Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess -


# forecast secondary cases
forecast_uncertain <- EpiNow2::forecast_secondary(
  estimate = secondary_uncertain,
  primary = cases_to_forecast
)

summary(forecast_uncertain)
#>             Length Class      Mode
#> samples      3     data.table list
#> forecast    10     data.table list
#> predictions 12     data.table list


# ISSUE: fixed delay workflow ---------------------------------------------


# delay
delay_fixed <- EpiNow2::Gamma(
  mean = 14,
  sd = 5,
  max = 30
)

# estimate secondary cases
secondary_fixed <- EpiNow2::estimate_secondary(
  data = cases_to_estimate,
  delays = EpiNow2::delay_opts(delay_fixed)
)

# forecast secondary cases
EpiNow2::forecast_secondary(
  estimate = secondary_fixed,
  primary = cases_to_forecast
)
#> Error in eval(expr, envir, enclos) : 
#>   Exception: variable does not exist; processing stage=data initialization; variable name=delay_params; base type=double (in 'string', line 431, column 2 to column 52)
#> failed to create the sampler; sampling not done
#> Error in `fit_model_with_nuts()`:
#> ! model fitting was timed out or failed

Created on 2024-06-22 with reprex v2.1.0

EpiNow2 Version:

packageVersion(pkg = "EpiNow2")
#> [1] '1.5.2'

avallecam avatar Jun 22 '24 00:06 avallecam