Learn R Programming

mcmcsae (version 0.5.0)

create_sampler: Create a sampler object

Description

This sets up a sampler object, based on the specification of a model. The object contains functions to draw a set of model parameters from their prior and conditional posterior distributions, and to generate starting values for the MCMC simulation. The functions share a common environment containing precomputed quantities such as design matrices based on the model and the data. The sampler object is the main input for the MCMC simulation function MCMCsim.

Usage

create_sampler(
  formula,
  family = "gaussian",
  ny = NULL,
  ry,
  r.mod,
  sigma.fixed = NULL,
  sigma.mod = NULL,
  Q0 = NULL,
  formula.V = NULL,
  linpred = NULL,
  data = NULL,
  compute.weights = FALSE,
  block = compute.weights,
  prior.only = FALSE,
  control = NULL
)

Arguments

formula

formula to specify the response variable and additive model components. The model components form the linear predictor part of the model. A model component on the right hand side can currently be either a regression term specified with reg(...) or a generic random effect term specified with gen(...). See for details the help pages for these model component creation functions. Other terms in the formula are interpreted as ordinary regression effects (treated in the same way as reg(...) terms, but without the option to change e.g. the prior), An offset can be specified as offset(...).

family

character string describing the data distribution. The default is 'gaussian'. Other options are 'binomial' for the binomial distribution and 'negbinomial' for the negative binomial distribution. For the binomial and negative binomial distributions a logistic link function is used. In the case of binary data a probit link function is also supported for the binomial/Bernoulli distribution.

ny

in case family="binomial" the (vector of) numbers of trials. It can be either a numeric vector or the name of a variable in data. Defaults to a vector of 1s.

ry

in case family="negbinomial" the overdispersion parameter or vector of overdispersion parameters. Use r.mod instead if the (scalar) overdispersion parameter should be inferred from the data. If neither ry nor r.mod is specified a default inverse chi-squared prior with 1 degree of freedom is assumed.

r.mod

in case family="negbinomial", a prior for the (inverse) overdispersion parameter. In this case the (scalar) overdispersion parameter is inferred from the data. The prior can be specified by a call to a prior specification function. Currently only pr_invchisq is supported, which may be used to specify an inverse chi-squared or beta-prime prior for the inverse overdispersion parameter.

sigma.fixed

for Gaussian models, if TRUE the residual standard deviation parameter 'sigma_' is fixed at 1. In that case argument sigma.mod is ignored. This is convenient for Fay-Herriot type models with (sampling) variances assumed to be known. Default is FALSE.

sigma.mod

prior for the variance parameter of a gaussian sampling distribution. This can be specified by a call to one of the prior specification functions pr_invchisq and pr_exp for an inverse chi-squared or exponential prior distribution, respectively. The default is an improper prior pr_invchisq(df=0, scale=1). A half-t prior on the standard deviation can be specified using pr_invchisq with a chi-squared distributed scale parameter.

Q0

n x n data-level precision matrix for a Gaussian model. It defaults to the unit matrix. If an n-vector is provided it will be expanded to a (sparse) diagonal matrix with Q0 on its diagonal. If a name is supplied it will be looked up in data and subsequently expanded to a diagonal matrix.

formula.V

a formula specifying the terms of a variance model in the case of a Gaussian likelihood. Currently two types of terms are supported: a regression term for the log-variance specified with vreg(...), and a term vfac(...) for multiplicative modeled factors at a certain level specified by a factor variable. By using unit-level inverse-chi-squared factors the marginal sampling distribution becomes a Student-t distribution, and by using unit-level exponential factors it becomes a Laplace or double exponential distribution.

linpred

a list of matrices defining (possibly out-of-sample) linear predictors to be simulated. This allows inference on e.g. (sub)population totals or means. The list must be of the form list(name_1=X_1, ...) where the names refer to the model component names and predictors are computed by summing X_i %*% p[[name_i]]. Alternatively, X_i may be the name of an object stored in mod[[name_i]], typically "X" corresponding to the design matrix. Finally, linpred="fitted" can be used if the linear predictor corresponds to fitted values.

data

data frame with n rows in which the variables specified in model components (if any) can be found.

compute.weights

if TRUE weights are computed for each element of linpred. Note that for a large dataset in combination with vector-valued linear predictors the weights can take up a lot of memory. By default only means are stored in the simulation carried out using MCMCsim.

block

if TRUE all coefficients are sampled in a single block. Alternatively, a list of character vectors indicating which coefficients should be sampled together in blocks.

prior.only

whether a sampler is set up only for sampling from the prior or for sampling from both prior and posterior distributions. Default FALSE. If TRUE there is no need to specify a response in formula. This is used by generate_data, which samples from the prior predictive distribution.

control

a list with further computational options, see details section.

Value

A sampler object, which is the main input for the MCMC simulation function MCMCsim. The sampler object is an environment with precomputed quantities and functions. The main functions are rprior, which returns a sample from the prior distributions, draw, which returns a sample from the full conditional posterior distributions, and start, which returns a list with starting values for the Gibbs sampler. If prior.only is TRUE, functions draw and start are not created.

Details

The right hand side of the formula argument to create_sampler can be used to specify additive model components. Currently two specialized model components are supported, reg(...) and gen(...) for regression and generic random effects components, respectively.

For gaussian models, formula.V can be used to specify the variance structure of the model. Currently two specialized variance model components are supported, vreg(...) for regression effects predicting the log-variance and vfac(...) for modeled variance factors.

Further computational options can be set using the control parameter, which should be passed as a list with possible elements

add.outer.R

whether to add the outer product of the constraint matrix for a better conditioned solve system for blocks. This is done by default when using blocked Gibbs sampling for blocks with constraints.

recompute.e

when FALSE residuals or linear predictors are only computed at the start of the simulation. This may give a modest speedup but in some cases may be less accurate due to roundoff error accumulation. Default is TRUE.

References

J.H. Albert and S. Chib (1993). Bayesian analysis of binary and polychotomous response data. Journal of the American statistical Association 88(422), 669-679.

D. Bates, M. Maechler, B. Bolker and S.C. Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software 67(1), 1-48.

N. Polson, J.G. Scott and J. Windle (2013). Bayesian Inference for Logistic Models Using Polya-Gamma Latent Variables. Journal of the American Statistical Association 108(504), 1339-1349.

H. Rue and L. Held (2005). Gaussian Markov Random Fields. Chapman & Hall/CRC.

M. Zhou and L. Carin (2015). Negative Binomial Process Count and Mixture Modeling. IEEE Transactions on Pattern Analysis and Machine Intelligence 37(2), 307-320.

Examples

Run this code
# NOT RUN {
# first generate some data
n <- 200
x <- rnorm(n)
y <- 0.5 + 2*x + 0.3*rnorm(n)
# create a sampler for a simple linear regression model
sampler <- create_sampler(y ~ x)
sim <- MCMCsim(sampler)
(summary(sim))

y <- rbinom(n, 1, 1 / (1 + exp(-(0.5 + 2*x))))
# create a sampler for a binary logistic regression model
sampler <- create_sampler(y ~ x, family="binomial")
sim <- MCMCsim(sampler)
(summary(sim))

# }

Run the code above in your browser using DataLab