Skip to contents

This function calculates the log predictive mass function (log-PMF) and the log cumulative distribution function (log-CDF) for each observation from a fitted negative binomial model (fitted using brms). The function extracts posterior samples for the distributional parameters and evaluates the predictive distributions across all posterior draws.

Usage

log_pred_dist_NB(fit)

Arguments

fit

A fitted brms negative binomial model object. The model must include the distributional parameters mu (mean parameter) and shape (dispersion parameter).

Value

A list with the following components:

lpmf_hat

A matrix of log-PMF values (posterior samples × observations).

lcdf_hat

A matrix of log-CDF values (posterior samples × observations).

zero_id

Indices of observations with zero counts.

Details

For each posterior draw and each observation, the function computes:

  • lpmf_hat: Log predictive mass function values using dnbinom().

  • lcdf_hat: Log cumulative distribution function values using pnbinom() with lower.tail = FALSE.

The function also identifies indices of zero-valued observations, which can be useful for model diagnostic procedures such as Z-residuals or posterior predictive checks.

Examples

if (FALSE) { # \dontrun{
# Example usage:
fit <- brm(bf(y ~ x1 + x2), family = negbinomial(), data = mydata)
pred_dist <- log_pred_dist_NB(fit)
str(pred_dist)
} # }