btf_bspline: MCMC Sampler for B-spline Bayesian Trend Filtering

View source: R/mcmc_samplers.R

btf_bsplineR Documentation

MCMC Sampler for B-spline Bayesian Trend Filtering

Description

Run the MCMC for B-spline fitting with a Bayesian trend filtering model on the coefficients, i.e., a penalty on zeroth (D=0), first (D=1), or second (D=2) differences of the B-spline basis coefficients. The penalty is determined by the prior on the evolution errors, which include:

  • the dynamic horseshoe prior ('DHS');

  • the static horseshoe prior ('HS');

  • the Bayesian lasso ('BL');

  • the normal stochastic volatility model ('SV');

  • the normal-inverse-gamma prior ('NIG').

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_bspline(
  y,
  x = NULL,
  num_knots = NULL,
  evol_error = "DHS",
  D = 2,
  nsave = 1000,
  nburn = 1000,
  nskip = 4,
  mcmc_params = list("mu", "yhat", "beta", "evol_sigma_t2", "obs_sigma_t2", "dhs_phi",
    "dhs_mean"),
  computeDIC = TRUE,
  verbose = TRUE
)

Arguments

y

the T x 1 vector of time series observations

x

the T x 1 vector of observation points; if NULL, assume equally spaced

num_knots

the number of knots; if NULL, use the default of max(20, min(ceiling(T/4), 150))

evol_error

the evolution error distribution; must be one of 'DHS' (dynamic horseshoe prior), '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 (burin-in)

nskip

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

mcmc_params

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

  • "mu" (conditional mean)

  • "beta" (B-spline basis coefficients)

  • "yhat" (posterior predictive distribution)

  • "evol_sigma_t2" (evolution error variance)

  • "obs_sigma_t2" (observation error variance)

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

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

computeDIC

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

verbose

logical; should R report extra information on progress?

Value

A named list of the nsave MCMC samples for the parameters named in mcmc_params

Note

The data y may contain NAs, which will be treated with a simple imputation scheme via an additional Gibbs sampling step. In general, rescaling y to have unit standard deviation is recommended to avoid numerical issues.

The primary advantages of btf_bspline over btf are

  1. Unequally-spaced points are handled automatically and

  2. Computations are linear in the number of basis coefficients, which may be substantially fewer than the number of time points.

Examples

# Example 1: Blocks data
simdata = simUnivariate(signalName = "blocks", T = 1000, RSNR = 3, include_plot = TRUE)
y = simdata$y
out = btf_bspline(y, D = 1)
plot_fitted(y, mu = colMeans(out$mu), postY = out$yhat, y_true = simdata$y_true)

## Not run: 
# Example 2: motorcycle data (unequally-spaced points)
library(MASS)
y = scale(mcycle$accel) # Center and Scale for numerical stability
x = mcycle$times
plot(x, y, xlab = 'Time (ms)', ylab='Acceleration (g)', main = 'Motorcycle Crash Data')
out = btf_bspline(y = y, x = x)
plot_fitted(y, mu = colMeans(out$mu), postY = out$yhat, t01 = x)

## End(Not run)


drkowal/dsp documentation built on July 19, 2023, 11:42 a.m.