ZINB | R Documentation |
Fits a zero-inflated negative binomial (ZINB) model using JAGS, with an optional design matrix of covariates and full inprod for mean structure, and can generate posterior predictive counts for new covariate data.
ZINB(
cases,
pop = NULL,
covariates_count = NULL,
covariates_zero = NULL,
covariatespred_count = NULL,
covariatespred_zero = NULL,
poppred = NULL,
casespred = NULL,
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("beta", "delta", "r")
)
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. |
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. |
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")) |
A list with MCMC summary, samples, DIC, and if prediction data provided: pred_matrix, pred_mean, mae, rmse
# ---- tiny example for users & CRAN (< 5s) ----
set.seed(5)
n <- 100
base <- rnbinom(n, size = 5, mu = 7)
zeros <- rbinom(n, 1, 0.25) # add extra zeros
cases <- ifelse(zeros == 1, 0L, base)
# ---- actually fit the model, but only when JAGS is available ----
fit <- ZINB(
cases = cases,
# optional priors if your API exposes them, e.g.:
# beta_prior_mean = 0, beta_prior_sd = 5,
# r_prior_shape = 2, r_prior_rate = 0.5,
n_iter = 400, # keep fast
n_burnin = 200,
n_chains = 1,
n_thin = 2
)
print(fit)
# ---- longer user-facing demo (skipped on checks) ----
if (nzchar(Sys.which("jags")) && requireNamespace("R2jags", quietly = TRUE)) {
x <- sin(2*pi*seq_len(n)/12) # simple seasonal regressor
fit2 <- ZINB(
cases = cases,
covariates_count = cbind(x),
covariates_zero = cbind(x),
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 <- ZINB(
cases = cases,
n_iter = 100000,
n_burnin = 10000,
n_chains = 4,
n_thin = 5
)
print(fit_full)
}
## End(Not run)
if (interactive()) {
# e.g., plot(fit)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.