Learn R Programming

gratia (version 0.9.0)

post_draws: Low-level Functions to generate draws from the posterior distribution of model coefficients

Description

Low-level Functions to generate draws from the posterior distribution of model coefficients

Generate posterior draws from a fitted model

Usage

post_draws(model, ...)

# S3 method for default post_draws( model, n, method = c("gaussian", "mh", "inla", "user"), mu = NULL, sigma = NULL, n_cores = 1L, burnin = 1000, thin = 1, t_df = 40, rw_scale = 0.25, index = NULL, frequentist = FALSE, unconditional = FALSE, parametrized = TRUE, mvn_method = c("mvnfast", "mgcv"), draws = NULL, seed = NULL, ... )

generate_draws(model, ...)

# S3 method for gam generate_draws( model, n, method = c("gaussian", "mh", "inla"), mu = NULL, sigma = NULL, n_cores = 1L, burnin = 1000, thin = 1, t_df = 40, rw_scale = 0.25, index = NULL, frequentist = FALSE, unconditional = FALSE, mvn_method = c("mvnfast", "mgcv"), seed = NULL, ... )

Arguments

model

a fitted R model. Currently only models fitted by mgcv::gam() or mgcv::bam(), or return an object that inherits from such objects are supported. Here, "inherits" is used in a loose fashion; models fitted by scam::scam() are support even though those models don't strictly inherit from class "gam" as far as inherits() is concerned.

...

arguments passed to methods.

n

numeric; the number of posterior draws to take.

method

character; which algorithm to use to sample from the posterior. Currently implemented methods are: "gaussian" and "mh". "gaussian" calls gaussian_draws() which uses a Gaussian approximation to the posterior distribution. "mh" uses a simple Metropolis Hasting sampler which alternates static proposals based on a Gaussian approximation to the posterior, with random walk proposals. Note, setting t_df to a low value will result in heavier-tailed statistic proposals. See mgcv::gam.mh() for more details.

mu

numeric; user-supplied mean vector (vector of model coefficients). Currently ignored.

sigma

matrix; user-supplied covariance matrix for mu. Currently ignored.

n_cores

integer; number of CPU cores to use when generating multivariate normal distributed random values. Only used if mvn_method = "mvnfast" and method = "gaussian".

burnin

numeric; the length of any initial burn in period to discard. See mgcv::gam.mh().

thin

numeric; retain only thin samples. See mgcv::gam.mh().

t_df

numeric; degrees of freedom for static multivariate t proposal. See mgcv::gam.mh().

rw_scale

numeric; factor by which to scale posterior covariance matrix when generating random walk proposals. See mgcv::gam.mh().

index

numeric; vector of indices of coefficients to use. Can be used to subset the mean vector and covariance matrix extracted from model.

frequentist

logical; if TRUE, the frequentist covariance matrix of the parameter estimates is used. If FALSE, the Bayesian posterior covariance matrix of the parameters is used. See mgcv::vcov.gam().

unconditional

logical; if TRUE the Bayesian smoothing parameter uncertainty corrected covariance matrix is used, if available for model. See mgcv::vcov.gam().

parametrized

logical; use parametrized coefficients and covariance matrix, which respect the linear inequality constraints of the model. Only for scam::scam() model fits.

mvn_method

character; one of "mvnfast" or "mgcv". The default is uses mvnfast::rmvn(), which can be considerably faster at generate large numbers of MVN random values than mgcv::rmvn(), but which might not work for some marginal fits, such as those where the covariance matrix is close to singular.

draws

matrix; user supplied posterior draws to be used when method = "user".

seed

numeric; the random seed to use. If NULL, a random seed will be generated without affecting the current state of R's RNG.