spmrf: Fit Bayesian nonparametric adaptive smoothing model

Description Usage Arguments Details Value References See Also

View source: R/spmrf.r


Fit Bayesian nonparametric adaptive temporal smoothing models with the shrinkage-prior Markov random fields (SPMRF) method via Hamiltonian Monte Carlo with the stan engine from the rstan package.


spmrf(prior = "horseshoe", likelihood = "normal", order = 1,
  zeta = 0.01, fit = NA, data, pars = NA, chains = 4, iter = 2000,
  warmup = floor(iter/2), thin = 1, control = list(adapt_delta = 0.95,
  max_treedepth = 12), ...)



A character string specifying which prior to use on order-k differences. Choices are "horseshoe", "laplace", and "normal". Note that "laplace" priors are currently not available for coalescent likelihoods.


A character string specifying the probability distribution of the observation variable. Current choices are "normal", "poisson","binomial", and "coalescent".


Numeric value specifying order of differencing (1, 2, or 3). Note that order 3 is currently not available for coalescent likelihoods.


The hyperparameter for the global smoothing parameter gamma. This is the scale parameter of a half-Cauchy distribution. Smaller values will result in more smoothing depending on the prior specification and the strength of the data. Values must be > 0.


An instance of S4 class stanfit derived from a previous fit; defaults to NA. If fit is not NA, the compiled model associated with the fitted result is re-used; thus the time that would otherwise be spent recompiling the C++ code for the model can be saved.


A named list providing the data for the model. See details below.


A vector of character strings 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 results. Conversely, if include = FALSE, samples for all parameters except those given in pars are stored in the fitted results.


A positive integer specifying number of chains; defaults to 4.


A positive integer specifying how many iterations for each chain (including warmup). The default is 2000.


A positive integer specifying number of warmup (aka burnin) iterations. This also specifies the number of iterations used for stepsize adaptation, so warmup samples should not be used for inference. The number of warmup should not be larger than iter and the default is iter/2.


A positive integer specifying the period for saving sample; defaults to 1.


Additional arguments passed to rstan::stan. Do not include file, model_code, or init in this list.


This function first internally creates stan model code and a function to generate initial parameter seeds using the information specified in the prior, likelihood, order, and zeta arguments. It then passes these to the stan function. The spmrf function will take additional arguments passed to stan except for the arguments file, model_code, and init. These arguments are not accepted because they may conflict with model forms expected by spmrf. See the stan function documentation for more details.

stan does all of the work of fitting a Stan model and returning the results as an instance of stanfit. First, it translates the Stan model to C++ code. Second, the C++ code is compiled into a binary shared object, which is loaded into the current R session (an object of S4 class stanmodel is created). Finally, samples are drawn and wrapped in an object of S4 class stanfit, which provides functions such as print, summary, and plot to inspect and retrieve the results of the fitted model.

spmrf can also be used to sample again from a fitted model under different settings (e.g., different iter) by providing argument fit. In this case, the compiled C++ code for the model is reused.

For any data that is not coalescent data, the list specified by the data argument must contain an element named y, which is the vector of the response or observation variable. If the observation variable is binary or binomial, a vector of counts representing the number of 'trials' per observation must also be present and named sizeN and of the same length as y. If the observations are on an equally spaced grid with a single observation per grid location, then the xvar1 element can be left unspecified. However, if there are more than one observation per grid location, or if the grid locations are unequally spaced, or if the response is being modeled as a function of a continuous covariate, then an a vector of grid locations or covariate values for each observation must be specified with element name xvar1 and must be of the same length as y.

For coalescent data, there are several critical elements that must be included in the the list specified by the data argument. These elements are automatically included when the function make_coalescent_data is used to generate the data list. See the description of the make_coalescent_data function for details.


An object of class stanfit. See stanfit and stan for more details.


Faulkner, J. R., and V. N. Minin. 2018. Locally adaptive smoothing with Markov random fields and shrinkage priors. Bayesian Analysis 13(1):225–252.

See Also

stan, stanfit, get_model, get_init, set_zeta, and make_coalescent_data

faulknerjam/bnps documentation built on Oct. 18, 2018, 12:03 p.m.