Learn R Programming

trafficCAR (version 0.1.0)

fit_car: Fit Gaussian CAR / ICAR regression via Gibbs sampling

Description

Fits a Gaussian regression with a CAR/ICAR latent effect: \(y = X\beta + x + \epsilon\) with \(\epsilon \sim N(0,\sigma^2 I)\) and \(x \sim N(0, Q^{-1})\), where \(Q = \tau (D - \rho A)\) and \(D = diag(A 1)\). For ICAR, \(\rho = 1\).

Usage

fit_car(
  y,
  A,
  X = NULL,
  type = c("icar", "proper"),
  rho = 0.99,
  tau = 1,
  n_iter = 2000,
  burn_in = floor(n_iter/2),
  thin = 1,
  beta_init = NULL,
  x_init = NULL,
  sigma2_init = NULL,
  b0 = NULL,
  B0 = NULL,
  a0 = 2,
  b0_sigma = 1,
  center_icar = TRUE,
  verbose = FALSE
)

Value

A list of class "trafficCAR_fit" with elements:

draws

List with MCMC draws x, beta, sigma2.

keep

Iteration indices that were saved.

type, rho, tau

Model hyperparameters used.

y = X beta + x + epsilon

Arguments

y

Numeric response vector of length n.

A

Square n x n adjacency/weight matrix (base matrix or Matrix). Diagonal entries are ignored.

X

Optional n x p design matrix. If NULL, no regression is fit.

type

Either "icar" or "proper".

rho

Spatial dependence parameter for proper CAR. Ignored for ICAR.

tau

Positive scalar precision multiplier.

n_iter

Total MCMC iterations.

burn_in

Number of initial iterations to discard.

thin

Keep every thin-th draw after burn-in.

beta_init

Optional initial \(\beta\) (length p).

x_init

Optional initial latent field \(x\) (length n).

sigma2_init

Optional initial \(\sigma^2\) (positive scalar).

b0

Prior mean for \(\beta\) (length p). Default is zero vector.

B0

Prior covariance for \(\beta\) (p x p). Default is large diagonal.

a0

Shape parameter for inverse-gamma prior on \(\sigma^2\).

b0_sigma

Scale parameter for inverse-gamma prior on \(\sigma^2\).

center_icar

Logical; if TRUE and type="icar", center \(x\) to sum-to-zero within each connected component.

verbose

Logical; print coarse progress updates.

Details

The sampler updates \(x\), \(\beta\) (if X is provided), and \(\sigma^2\) using Gibbs steps.