spmrf: Fit Bayesian nonparametric adaptive smoothing model

Description Usage Arguments Details Value References See Also

View source: R/spmrf.r

Description

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.

Usage

1
2
3
4
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), ...)

Arguments

prior

A character string specifying which prior to use on order-k differences. Choices are "horseshoe", "laplace", and "normal".

likelihood

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

order

Numeric value specifying order of differencing (1, 2, or 3).

zeta

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.

fit

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.

data

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

pars

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.

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 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.

thin

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.

Details

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.

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.

Value

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

References

Faulkner, J. R., and V. N. Minin. 2017. Locally adaptive smoothing with Markov random fields and shrinkage priors. Bayesian Analysis advance publication online.

See Also

stan, stanfit, get_model, and get_init


faulknerjam/bnps documentation built on Dec. 4, 2017, 5:28 p.m.