Skip to contents

Probability mass function, distribution function and random generation for the hurdle (zero-altered) beta-binomial distribution.

Usage

dhbetabinom(x, size, shape1, shape2, zeroprob = 0.5, log = FALSE)

phbetabinom(
  q,
  size,
  shape1,
  shape2,
  zeroprob = 0.5,
  lower.tail = TRUE,
  log.p = FALSE
)

rhbetabinom(n, size, shape1, shape2, zeroprob = 0.5)

Arguments

x

integer vector of counts

size

number of trials (zero or more)

shape1, shape2

positive shape parameters of the mixing beta distribution

zeroprob

probability of a zero, between 0 and 1

log

logical; return log-density if TRUE

q

vector of quantiles.

lower.tail

logical; if TRUE (default), probabilities are \(P[X \le q]\), otherwise \(P[X > q]\).

log.p

logical; if TRUE, probabilities are returned on the log scale.

n

number of random values to return.

Value

dhbetabinom gives the probability mass function, phbetabinom gives the distribution function, and rhbetabinom generates random deviates.

Details

This implementation allows for automatic differentiation with RTMB.

A hurdle distribution models the zeros and the positive counts as two separate processes: the probability of a zero is a free parameter, and the positive counts follow the corresponding zero-truncated distribution. Writing \(p_0\) for zeroprob, $$P(X = 0) = p_0, \qquad P(X = x) = (1 - p_0)\,\frac{P_{\mathrm{BB}}(x;\,n,a,b)}{1 - \pi_0}, \quad x = 1, \ldots, n.$$ where \(\pi_0 = P_{\mathrm{BB}}(0;\,n,a,b)\) is the probability of a zero under the ordinary beta-binomial.

Unlike zero-inflation, which can only add zeros, zeroprob here is exactly the probability of observing a zero and may be larger or smaller than the beta-binomial would give on its own.

The distribution function has no closed form and is computed by summing the probability mass function over \(0, \ldots, q\). It is AD-compatible in the parameters, while q and size must be numeric data. This is also what one-step-ahead (OSA) residuals via RTMB::oneStepPredict need, so these are supported, e.g. with method = "cdf" and discrete = TRUE.

Examples

set.seed(123)
x <- rhbetabinom(5, size = 10, shape1 = 2, shape2 = 3, zeroprob = 0.4)
d <- dhbetabinom(x, size = 10, shape1 = 2, shape2 = 3, zeroprob = 0.4)
p <- phbetabinom(x, size = 10, shape1 = 2, shape2 = 3, zeroprob = 0.4)