Conditions a composed model on an observed time series and samples from the posterior with NUTS.

fit(model, y, method = nuts(), dates = NULL, seed = NULL)

Arguments

model

A composed model from IDModel(), or a julia() expression that builds one.

y

Numeric vector of observations, one per time point. Missing observations are not supported, and neither is a matrix of stratified observations.

method

Sampler settings from nuts().

dates

Optional vector of dates (Date or POSIXt), one per observation, used when plotting. Forecasts continue at the spacing of the last two dates.

seed

Optional integer seed for the Julia random number generator.

Value

An object of class cidm_fit with elements

draws

Posterior draws of the model parameters, a posterior::draws_df.

sampler

Sampler diagnostics per draw (e.g. numerical_error for divergent transitions), a posterior::draws_df.

generated

Named list of draws x time matrices of generated quantities: Z_t (the latent process), I_t (infections), expected_y_t (expected observations), predicted_y_t (posterior predictive observations) and, for renewal models, Rt. It also holds generated_y_t, which is the observed series repeated for every draw, because the model was conditioned on it. In expected_y_t and predicted_y_t, time points the model does not score, such as those before a reporting delay has elapsed, are NaN.

model, y, dates, method

The inputs.

Examples

if (FALSE) { # \dontrun{
model <- IDModel(
  Renewal(generation_time = Gamma(shape = 6.5, scale = 0.62), rt = AR()),
  NegativeBinomialError(cluster_factor = HalfNormal(0.1))
)
y <- simulate(model, n = 40, seed = 1)$generated_y_t[1, ]
fitted <- fit(model, y, method = nuts(draws = 250, warmup = 250))
fitted
plot(fitted, type = "Rt")
} # }