GARMA_ZINB: Fit Zero-Inflated Negative Binomial GARMA Model with...

View source: R/Garma_zinb.R

GARMA_ZINBR Documentation

Fit Zero-Inflated Negative Binomial GARMA Model with Prediction

Description

This function fits a generalized autoregressive moving average (GARMA-ZINB) model for count data using a zero-inflated negative binomial distribution, allowing separate covariates for the count and zero-inflation parts, and optionally generates posterior predictive counts for future covariate inputs.

Usage

GARMA_ZINB(
  cases,
  pop = NULL,
  covariates_count = NULL,
  covariates_zero = NULL,
  p = 2,
  q = 2,
  c = 1,
  beta_init = NULL,
  delta_init = NULL,
  r_init = NULL,
  beta_prior_mean = 0,
  beta_prior_sd = 10,
  delta_prior_mean = 0,
  delta_prior_sd = 10,
  r_prior_shape = 1,
  r_prior_rate = 1,
  n_iter = 1e+05,
  n_burnin = 10000,
  n_chains = 3,
  n_thin = 1,
  save_params = c("r", "beta", "phi", "theta", "delta"),
  covariatespred_count = NULL,
  covariatespred_zero = NULL,
  poppred = NULL,
  casespred = NULL
)

Arguments

cases

Vector of observed counts (length N)

pop

Optional vector of population offsets (length N)

covariates_count

Optional numeric matrix (N x P) of covariates for the count component.

covariates_zero

Optional numeric matrix (N x Q) of covariates for the zero-inflation component.

p

Integer, autoregressive order

q

Integer, moving average order

c

Constant added before log (default 1)

beta_init

Optional list of length n_chains for beta, count coefficients initial values.

delta_init

Optional list of length n_chains for delta, zero-inflation coefficients.

r_init

Optional numeric vector of length n_chains for dispersion parameter.

beta_prior_mean

Mean for beta prior (default: 0)

beta_prior_sd

SD for beta prior (default: 10)

delta_prior_mean

Mean for delta prior (default: 0)

delta_prior_sd

SD for delta prior (default: 10)

r_prior_shape

Shape for r ~ dgamma (default: 1)

r_prior_rate

Rate for r ~ dgamma (default: 1)

n_iter

Total MCMC iterations (default: 100000)

n_burnin

Burn-in iterations (default: 10000)

n_chains

Number of chains (default: 3)

n_thin

Thinning interval (default: 1)

save_params

Character vector of parameters to save (default c("beta","delta","r"))

covariatespred_count

Optional numeric matrix (M x P) of new covariates for count prediction.

covariatespred_zero

Optional numeric matrix (M x Q) of new covariates for zero-inflation prediction.

poppred

Optional vector of population offsets (length M) for prediction.

casespred

Optional vector of true counts (length M) for prediction performance.

Value

A list with MCMC summary, samples, DIC, and if prediction data provided: pred_matrix, pred_mean, mae, rmse

Examples

# ---- tiny example for users & CRAN (< 5s) ----
set.seed(3)
n <- 100
# toy NB counts with extra zeros to mimic zero-inflation
base <- rnbinom(n, size = 5, mu = 6)
zeros <- rbinom(n, size = 1, prob = 0.30)
cases <- ifelse(zeros == 1, 0L, base)



# ---- actually fit the model, but only when JAGS is available ----

fit <- GARMA_ZINB(
  cases = cases,
  p = 1, q = 1,          # rename if your args are ar_order/ma_order
  # keep priors at defaults unless you need to tweak
  n_iter   = 100,        # keep fast for examples
  n_burnin = 10,
  n_chains = 1,
  n_thin   = 1
)
print(fit)


# ---- longer user-facing demo (skipped on checks) ----
if (nzchar(Sys.which("jags")) && requireNamespace("R2jags", quietly = TRUE)) {
  # simple seasonal covariate (use only if your function supports 'covariates')
  # x <- sin(2*pi*seq_along(cases)/12)
  fit2 <- GARMA_ZINB(
    cases = cases,
    p = 2, q = 1,
    # covariates = cbind(x),        # uncomment if supported
    # z_covariates = cbind(x),      # uncomment if zero-part covariates are supported
    n_iter   = 1000,
    n_burnin = 100,
    n_chains = 2,
    n_thin   = 2
  )
  print(fit2)
  # if a plot method exists:  # plot(fit2)
}


## Not run: 
# ---- time-consuming / full demo ----
if (nzchar(Sys.which("jags")) && requireNamespace("R2jags", quietly = TRUE)) {
  fit_full <- GARMA_ZINB(
    cases = cases,
    p = 2, q = 2,
    n_iter   = 100000,
    n_burnin = 10000,
    n_chains = 4,
    n_thin   = 1
  )
  print(fit_full)
}

## End(Not run)

if (interactive()) {
  # e.g., plot(fit)
}


sparsesurv documentation built on Sept. 11, 2025, 9:11 a.m.