Learn R Programming

rstan (version 2.9.0-3)

sampling-methods: sampling: draw samples from Stan model

Description

Draw samples from the model defined by class stanmodel. This method is a generic function of the S4 class stanmodel.

Usage

## S3 method for class 'stanmodel':
sampling(object, data = list(), pars = NA, 
    chains = 4, iter = 2000, warmup = floor(iter/2), thin = 1,
    seed = sample.int(.Machine$integer.max, 1), init = 'random', 
    check_data = TRUE, sample_file = NULL, diagnostic_file = NULL, verbose = FALSE, 
    algorithm = c("NUTS", "HMC", "Fixed_param"),    control = NULL, include = TRUE, cores = getOption("mc.cores", 1L),
    open_progress = interactive() && !isatty(stdout()) &&
                    !identical(Sys.getenv("RSTUDIO"), "1"),
    show_messages = TRUE, ...)

Arguments

object
An object of class stanmodel.
data
A named list or environment providing the data for the model or a character vector for all the names of objects used as data. See the notes in stan.
pars
A vector of character string specifying parameters of interest; defaults to NA indicating all parameters in the model. If include = TRUE, only samples for parameters given in pars are stored in the fitted re
chains
A positive integer specifying number of chains; defaults to 4.
iter
A positive integer specifying how many iterations for each chain (including warmup). The default is 2000.
warmup
A positive integer specifying the number of warmup (aka burnin) iterations. If step-size adaptation is on (which it is by default), this also controls the number of iterations for which adaptation is run (and hence the samples should not be
thin
A positive integer specifying the period for saving samples; defaults to 1.
seed
The seed for random number generation. The default is generated from 1 to the maximum integer supported by Ron the machine. Even if multiple chains are used, only one seed is needed, with other chains having seeds derived from that of the f
init
One of digit 0, string "0" or "random", a function that returns a list, or a list of initial parameter values with which to indicate how the initial values of parameters are specified. "0":
check_data
Logical: if TRUE, the data would be preprocessed; otherwise not. If the data is not checked and preprocessed, it is safe to leave it to be the default TRUE. See the notes in stan
sample_file
A character string of file name for specifying where to write samples for all parameters and other saved quantities. If not provided, files are not created. When the folder specified is not writable, tempdir() is used.
diagnostic_file
A character string of file name for specifying where to write diagnostics data for all parameters. If not provided, files are not created. When the folder specified is not writable, tempdir() is used. When there
verbose
TRUE or FALSE: flag indicating whether to print intermediate output from Stan on the console, which might be helpful for model debugging.
algorithm
One of algorithms that are implemented in Stan such as the No-U-Turn sampler (NUTS, Hoffman and Gelman 2011), static HMC, or Fixed_param.
control
See the argument control of function stan.
include
Logical scalar defaulting to TRUE indicating whether to include or exclude the parameters given by the pars argument. If FALSE, only entire multidimensional parameters can be excluded, rather than partic
cores
Number of cores to use when executing the chains in parallel, which defaults to 1 but we recommend setting the mc.cores option to be as many processors as the hardware and RAM allow (up to the number of chains).
open_progress
Logical scalar that only takes effect if cores > 1 but is recommended to be TRUE in interactive use so that the progress of the chains will be redirected to a file that is automatically opened for inspection. For ver
show_messages
Either a logical scalar (defaulting to TRUE) indicating whether to print the summary of Informational Messages to the screen after a chain is finished or a character string naming a path where the summary is stored. Setting to
...
Additional arguments can be chain_id, init_r, test_grad, append_samples, refresh, enable_random_init. See the document in stan

Value

  • An object of S4 class stanfit representing the fitted results. Slot mode for this object indicates if the sampling is done or not.

See Also

stanmodel, stanfit, stan

Examples

Run this code
m <- stan_model(model_code = 'parameters {real y;} model {y ~ normal(0,1);}')
f <- sampling(m, iter = 100)

Run the code above in your browser using DataLab