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,
vb = FALSE,
...
)
A networkModel
.
A positive integer specifying the number of iterations for each chain (including warmup). The default is 2000.
A positive integer specifying the number of Markov chains. The default is 4.
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).
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.
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).
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).
Boolean, if TRUE will use rstan::vb
for a quick approximate
sampling of the posterior. Important note from ?rstan::vb
:
"This is still considered an experimental feature. We recommend calling
stan
or sampling
for final inferences and only using ‘vb’ to
get a rough idea of the parameter distributions."
Arguments passed to `rstan::sampling` (e.g. iter, chains).
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).
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) { # \dontrun{
# 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)
} # }