enw_sample: Fit a CmdStan model using NUTS

View source: R/model-tools.R

enw_sampleR Documentation

Fit a CmdStan model using NUTS

Description

Fit a CmdStan model using NUTS

Usage

enw_sample(
  data,
  model = epinowcast::enw_model(),
  init = NULL,
  init_method = c("prior", "pathfinder"),
  init_method_args = list(),
  diagnostics = TRUE,
  ...
)

Arguments

data

A list of data as produced by model modules (for example enw_expectation(), enw_obs(), etc.) and as required for use the model being used.

model

A cmdstanr model object as loaded by enw_model() or as supplied by the user.

init

A list of initial values or a function to generate initial values. If not provided, the model will attempt to generate initial values

init_method

The method to use for initializing the model. Defaults to "prior" which samples initial values from the prior. "pathfinder", which uses the pathfinder algorithm (enw_pathfinder()) to initialize the model.

init_method_args

A list of additional arguments to pass to the initialization method.

diagnostics

Logical, defaults to TRUE. Should fitting diagnostics be returned as a data.frame.

...

Additional parameters passed to the sample method of cmdstanr.

Value

A data.frame containing the cmdstanr fit, the input data, the fitting arguments, and optionally summary diagnostics.

See Also

Functions used to help convert models into the format required for stan enw_formula_as_data_list(), enw_get_cache(), enw_model(), enw_pathfinder(), enw_priors_as_data_list(), enw_replace_priors(), enw_set_cache(), enw_stan_to_r(), enw_unset_cache(), remove_profiling(), write_stan_files_no_profile()

Examples


pobs <- enw_example("preprocessed")

nowcast <- epinowcast(pobs,
 expectation = enw_expectation(~1, data = pobs),
 fit = enw_fit_opts(enw_sample, pp = TRUE),
 obs = enw_obs(family = "poisson", data = pobs),
)

summary(nowcast)

# Use pathfinder initialization
nowcast_pathfinder <- epinowcast(pobs,
 expectation = enw_expectation(~1, data = pobs),
 fit = enw_fit_opts(enw_sample, pp = TRUE, init_method = "pathfinder"),
 obs = enw_obs(family = "poisson", data = pobs),
)

summary(nowcast_pathfinder)


seabbs/epinowcast documentation built on Sept. 20, 2024, 2:39 a.m.