Skip to contents

Stan is probabilistic programming language that can be used to specify most types of statistical linear and non-linear regression models. Stan provides full Bayesian inference for continuous-variable models through Markov chain Monte Carlo methods such as the No-U-Turn sampler, an adaptive form of Hamiltonian Monte Carlo sampling. Stan code has to be written separately and this function acts as compiler to build the stan-model. Requires the "cmdstanr" package to be installed!


  chains = 4,
  iter = 2000,
  warmup = floor(iter/2),
  init = "random",
  cores = getOption("ibis.nthread"),
  algorithm = "sampling",
  control = list(adapt_delta = 0.95),
  type = "response",



distribution() (i.e. BiodiversityDistribution) object.


A positive integer specifying the number of Markov chains (Default: 4 chains).


A positive integer specifying the number of iterations for each chain (including warmup). (Default: 2000).


A positive integer specifying the number of warmup (aka burnin) iterations per chain. If step-size adaptation is on (Default: TRUE), this also controls the number of iterations for which adaptation is run (and hence these warmup samples should not be used for inference). The number of warmup iterations should be smaller than iter and the default is iter/2.


Initial values for parameters (Default: 'random'). Can also be specified as list (see: "rstan::stan")


If set to NULL take values from specified ibis option getOption('ibis.nthread').


Mode used to sample from the posterior. Available options are "sampling", "optimize", or "variational". See "cmdstanr" package for more details. (Default: "sampling").


See "rstan::stan" for more details on specifying the controls.


The mode used for creating posterior predictions. Either summarizing the linear "predictor" or "response" (Default: "response").


Other variables


An Engine.


By default the posterior is obtained through sampling, however stan also supports approximate inference forms through penalized maximum likelihood estimation (see Carpenter et al. 2017).


The function obj$stancode() can be used to print out the stancode of the model.


  • Jonah Gabry and Rok Češnovar (2021). cmdstanr: R Interface to 'CmdStan'.,

  • Carpenter, B., Gelman, A., Hoffman, M. D., Lee, D., Goodrich, B., Betancourt, M., ... & Riddell, A. (2017). Stan: A probabilistic programming language. Journal of statistical software, 76(1), 1-32.

  • Piironen, J., & Vehtari, A. (2017). Sparsity information and regularization in the horseshoe and other shrinkage priors. Electronic Journal of Statistics, 11(2), 5018-5051.

See also

rstan, cmdstanr

Other engine: engine_bart(), engine_breg(), engine_gdb(), engine_glmnet(), engine_inlabru(), engine_inla(), engine_xgboost()


if (FALSE) {
# Add Stan as an engine
x <- distribution(background) |> engine_stan(iter = 1000)