btf_nb: MCMC Sampler for Bayesian Trend Filtering with Negative...

View source: R/nb_btf.R

btf_nbR Documentation

MCMC Sampler for Bayesian Trend Filtering with Negative Binomial Observation Model

Description

Run the MCMC for Bayesian trend filtering with a penalty on first (D = 1) or second (D = 2) differences of the log conditional expectation of a Negative Binomial distribution. The penalty is determined by the prior on the evolution errors, currently only the following options are available:

  • the dynamic horseshoe prior ('DHS');

  • the horseshoe prior ('HS');

In each case, the evolution error is a scale mixture of Gaussians. Sampling is accomplished with a (parameter-expanded) Gibbs sampler, mostly relying on a dynamic linear model representation.

Usage

btf_nb(
  y,
  evol_error = "DHS",
  D = 2,
  nsave = 1000,
  nburn = 1000,
  nskip = 4,
  mcmc_params = list("mu", "ypred", "evol_sigma_t2", "r", "dhs_phi", "dhs_mean"),
  r_init = NULL,
  r_sample = TRUE,
  step = 1,
  evol0_sample = FALSE,
  evol0_sd = 10,
  sigma_e = 1/sqrt(Nt),
  chol0 = NULL,
  computeDIC = TRUE,
  offset = 0,
  verbose = TRUE
)

Arguments

y

the length T vector of time series observations; expected to be in the support of a Negative Binomial distribution

evol_error

the evolution error distribution; must be one of 'DHS' (dynamic horseshoe prior; the default), 'HS' (horseshoe prior), 'BL' (Bayesian lasso), or 'NIG' (normal-inverse-gamma prior)

D

degree of differencing (D = 0, D = 1, or D = 2)

nsave

number of MCMC iterations to record

nburn

number of MCMC iterations to discard (burnin)

nskip

number of MCMC iterations to skip between saving iterations, i.e., save every (nskip + 1)th draw

mcmc_params

character list of parameters for which we store the MCMC output; must be one or more of:

  • 'mu' (conditional mean)

  • 'ypred' (posterior predictive distribution)

  • 'evol_sigma_t2' (evolution error variance)

  • 'r' (overdispersion)

  • 'dhs_phi' (DHS AR(1) coefficient)

  • 'dhs_mean' (DHS AR(1) unconditional mean)

Defaults to everything.

r_init

numeric; initial value of the overdispersion parameter, or NULL to use the default of 5. Must be a positive integer whenever the 'int_mh' sampler is active (that is, r_sample is TRUE or 'int_mh'), because the initial auxiliary variables are drawn with pgdraw::pgdraw(), which requires integer input. Non-integer values are permitted when r is held fixed (r_sample = FALSE) or with the 'slice' and 'mh' samplers.

r_sample

logical or character; controls whether and how the overdispersion r is sampled. If TRUE (default), r is sampled with the 'int_mh' sampler; if FALSE, it is held fixed at r_init. A character value selects the sampler explicitly:

  • 'int_mh': random walk Metropolis-Hastings on an integer grid. This keeps r integer valued, which allows the faster Polya-Gamma sampler (pgdraw::pgdraw()) to be used for the auxiliary variables.

  • 'slice': slice sampler on continuous r. Experimental.

  • 'mh': random walk Metropolis-Hastings on continuous r. Experimental.

'slice' and 'mh' leave r continuous, so the auxiliary variables fall back to BayesLogit::rpg(), which is substantially slower. Both are experimental and have seen little use; 'int_mh' is recommended. The character form is only available when calling btf_nb() directly. Through dsp_spec() this argument is not user settable: it is driven by r_user, which fixes r at the supplied value.

step

numeric (defaults to 1); step length of proposal distribution for Metropolis-Hasting sampling of overdispersion parameter; ignored if r_sample is FALSE

evol0_sample

logical; if TRUE (default), the prior variance of the initial D values of mu is sampled, else it is fixed at evol0_sd

evol0_sd

numeric; initial value (defaults to 10) or the fixed prior standard deviation for the initial D values of mu

sigma_e

numeric; scale value (defaults to 1/sqrt(T)) for half-Cauchy prior on global variance parameter

chol0

logical; If anything except NULL (the default), the Cholesky term of the log volatility is precomputed

computeDIC

logical; if TRUE, compute the deviance information criterion DIC and the effective number of parameters p_d

offset

length T vector of offset values for the log conditional expectation

verbose

logical; If TRUE (the default), time remaining is printed to console

Value

A named list with the following. One named element for each of the parameters specified in mcmc_params containing the nsave posterior draws as a vector for scalar parameters and matrix for multivariate parameters. An element named loglike containing a vector of the nsave evaluations of the log likelihood of the data y with a Negative Binomial distribution defined by the current iteration conditional expectation + offset and overdispersion. If computeDIC is TRUE, a named element for the computed value of DIC and effective parameters p_d for the data y with a Negative Binomial distribution defined by the posterior mean of the conditional expectation + offset and posterior mean overdispersion.


dsp documentation built on July 22, 2026, 1:07 a.m.