Run a MCMC sampler on a network model using Stan

run_mcmc(
  model,
  iter = 2000,
  chains = 4,
  method = "matrix_exp",
  euler_control = list(),
  cores = NULL,
  stanfit = FALSE,
  ...
)

Arguments

model

A networkModel.

iter

A positive integer specifying the number of iterations for each chain (including warmup). The default is 2000.

chains

A positive integer specifying the number of Markov chains. The default is 4.

method

A character string indicating the method to use to solve ODE in the Stan model; available methods are "matrix_exp" and "euler". The default is "matrix_exp", which uses matrix exponential and is reasonably fast for small networks. For large networks, the "euler" method can be used. It implements a simple forward Euler method to solve the ODE and can be faster than the matrix exponential approach, but extra caution must be taken to check for numerical accuracy (e.g. testing different dt time step values, ensuring that the product between dt and the largest transfer rates expected from the priors is always very small compared to 1).

euler_control

An optional list containing extra parameters when using method = "euler". Allowed list elements are "dt" and "grid_size", which are respectively the time step size for trajectory calculations ("dt") or the number of points for the calculation ("grid_size"). Only one of "dt" or "grid_size" can be specified, not both. If none is provided, a default grid size of 256 steps is used.

cores

Number of cores to use for parallel use. Default is NULL, which means to use the value stored in options()[["mc.cores"]] (or 1 if this value is not set).

stanfit

If TRUE, returns a `stanfit` object instead of the more classical `mcmc.list` object. Note that when an `mcmc.list` object is returned, the original `stanfit` object is still accessible as an attribute of that object (see Examples).

...

Arguments passed to `rstan::sampling` (e.g. iter, chains).

Value

An object of class `stanfit` returned by `rstan::sampling` if

stanfit = TRUE, otherwise the result of converting this

stanfit object with stanfit_to_named_mcmclist (i.e. an object of class networkModelStanfit and mcmc.list, which still carries the original `stanfit` object stored as an attribute).

Examples

aquarium_mod
#> # A tibble: 1 × 4
#>   topology           initial          observations      parameters      
#>   <list>             <list>           <list>            <list>          
#> 1 <topology [3 × 3]> <tibble [3 × 3]> <tibble [15 × 4]> <tibble [8 × 2]>
if (FALSE) {
  # The 'aquarium_run' object is shipped with the package, so you don't
  # actually need to run the line below to obtain it
  aquarium_run <- run_mcmc(aquarium_mod)

  plot(aquarium_run)
  summary(aquarium_run)

  # The original stanfit object returned by Stan
  sfit <- attr(aquarium_run, "stanfit")
  sfit

  # The stanfit object can be used for diagnostics, LOO cross-validation, etc.
  rstan::loo(sfit)
}