Skip to contents

Extracts posterior draws of the parametric reporting-delay distribution from a fit (e.g. a delay-only fit, see the delay estimation vignette) and returns the discretised delay probability mass function for each draw. This supports comparing the estimated distribution against a known truth and plotting posterior samples rather than only summaries.

Usage

enw_posterior_delay(fit, max_delay, distribution = "lognormal", draws = NULL)

Arguments

fit

A cmdstanr fit object (the fit[[1]] element of an epinowcast output).

max_delay

Maximum delay (number of delay slots, delays 0:(max_delay - 1)).

distribution

The parametric distribution used in enw_reference(), one of "lognormal", "gamma", "exponential", or "loglogistic". Defaults to "lognormal". A cmdstanr fit does not record which distribution was used (the model_refp id is Stan data, not saved to the draws), so this must match the distribution passed to enw_reference(). An incorrect value silently yields the wrong PMF.

draws

Optional integer; if supplied, a random subset of this many posterior draws is returned (useful for plotting). The same draws are used across all reference-design rows.

Value

A long data.table with columns .draw, delay, and pmf, plus an integer row column when the delay model has more than one reference-design row.

Details

The function reads the full refp_mean and refp_sd vectors saved by the Stan model (one entry per unique combination of parametric reference-date covariates) and discretises a delay PMF for each. For an intercept-only delay (the common case, refp_mean of length one) the output is a long data.table with columns .draw, delay, and pmf. For a delay model with reference-date covariates, random effects, or time- or group-varying delays (refp_mean of length greater than one) an additional integer row column identifies the reference-design row, and each row may have a different PMF. The mapping from row to reference date and group is set by the reference module's fixed-effect design (see enw_reference() and enw_formula_as_data_list()'s refp_findex); it is not recovered here because a cmdstanr fit does not retain that design on its own.

Examples

if (FALSE) { # interactive()
fit <- enw_example("nowcast")
# Intercept-only delay
enw_posterior_delay(fit$fit[[1]], max_delay = 20, draws = 50)

# A delay model with a reference-date covariate yields one PMF per
# reference-design row, identified by the `row` column:
if (FALSE) { # \dontrun{
pobs <- enw_example("preprocessed")
covariate_fit <- epinowcast(
  pobs,
  reference = enw_reference(~ 1 + day_of_week, data = pobs),
  obs = enw_obs(delay_only = TRUE, data = pobs)
)
enw_posterior_delay(covariate_fit$fit[[1]], max_delay = 20, draws = 50)
} # }
}