Nothing
#' Hurdle Poisson distribution
#'
#' Probability mass function, distribution function, and random generation for
#' the hurdle (zero-altered) Poisson distribution.
#'
#' @details
#' This implementation allows for automatic differentiation with \code{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
#' \eqn{p_0} for \code{zeroprob},
#' \deqn{P(X = 0) = p_0, \qquad
#' P(X = x) = (1 - p_0)\,\frac{P_{\mathrm{Pois}}(x;\,\lambda)}{1 - \pi_0},
#' \quad x = 1, 2, \ldots}
#' where \eqn{\pi_0 = P_{\mathrm{Pois}}(0;\,\lambda)} is the probability of a zero
#' under the ordinary Poisson.
#'
#' This differs from the zero-inflated Poisson, where the zeros are a mixture of
#' structural zeros and zeros generated by the Poisson itself. In a hurdle model
#' \code{zeroprob} is exactly the probability of observing a zero, so it can be
#' larger \emph{or} smaller than the Poisson would give on its own; zero-inflation
#' can only ever add zeros. The two coincide with the ordinary Poisson when
#' \code{zeroprob} equals \eqn{\pi_0}.
#'
#' @references
#' Mullahy, J. (1986) Specification and testing of some modified count data models.
#' Journal of Econometrics, 33, 341-365.
#'
#' Rigby, R. A., Stasinopoulos, D. M., Heller, G. Z., and De Bastiani, F. (2019) Distributions for modeling location, scale, and shape: Using GAMLSS in R, Chapman and Hall/CRC,
#' doi:10.1201/9780429298547. An older version can be found in https://www.gamlss.com/.
#'
#' @seealso [hbinom], [hnbinom], [hnbinom2], [zipois], [ztpois]
#'
#' @param x,q integer vector of counts
#' @param n number of random values to return.
#' @param lambda vector of (non-negative) means of the underlying Poisson
#' @param zeroprob probability of a zero, between 0 and 1
#' @param log,log.p logical; return log-density if TRUE
#' @param lower.tail logical; if \code{TRUE}, probabilities are \eqn{P[X \le x]}, otherwise, \eqn{P[X > x]}.
#'
#' @return
#' \code{dhpois} gives the probability mass function, \code{phpois} gives the distribution function, and \code{rhpois} generates random deviates.
#'
#' @examples
#' set.seed(123)
#' x <- rhpois(5, lambda = 2, zeroprob = 0.4)
#' d <- dhpois(x, lambda = 2, zeroprob = 0.4)
#' p <- phpois(x, lambda = 2, zeroprob = 0.4)
#' @name hpois
NULL
#' @rdname hpois
#' @export
#' @importFrom RTMB dpois logspace_add
dhpois <- function(x, lambda, zeroprob = 0.5, log = FALSE) {
if(!ad_context()) {
args <- as.list(environment())
simulation_check(args) # informative error message if likelihood in wrong order
# ensure lambda >= 0, zeroprob in [0,1]
if (any(lambda < 0)) stop("lambda must be >= 0")
if (any(zeroprob < 0 | zeroprob > 1)) stop("zeroprob must be in [0,1]")
}
# potentially escape to RNG or CDF
if(inherits(x, "simref")){
return(dGenericSim("dhpois", x = x, lambda = lambda, zeroprob = zeroprob, log=log))
}
if(inherits(x, "osa")) {
return(dGenericOSA("dhpois", x = x, lambda = lambda, zeroprob = zeroprob, log=log))
}
# log_hurdle() combines the point mass at zero with the rescaled zero-truncated
# Poisson on the log scale, so either branch may be exactly zero without a NaN
logdens <- log_hurdle(x, dpois(x, lambda, log = TRUE), exp(-lambda), zeroprob)
if (log) return(logdens)
return(exp(logdens))
}
#' @rdname hpois
#' @export
phpois <- function(q, lambda, zeroprob = 0.5, lower.tail = TRUE, log.p = FALSE) {
if(!ad_context()) {
# ensure lambda >= 0, zeroprob in [0,1]
if (any(lambda < 0)) stop("lambda must be >= 0")
if (any(zeroprob < 0 | zeroprob > 1)) stop("zeroprob must be in [0,1]")
q <- floor(q) # make sure it's integer-valued
}
# zero mass plus the rescaled zero-truncated cdf, and zero below the support
p <- greater(q, -1) * (zeroprob + (1 - zeroprob) * pztpois(q, lambda))
if (!lower.tail) p <- 1 - p
if (log.p) p <- log(p)
return(p)
}
#' @rdname hpois
#' @export
#' @importFrom stats runif
rhpois <- function(n, lambda, zeroprob = 0.5) {
# ensure lambda >= 0, zeroprob in [0,1]
if (any(lambda < 0)) stop("lambda must be >= 0")
if (any(zeroprob < 0 | zeroprob > 1)) stop("zeroprob must be in [0,1]")
lambda <- rep_len(lambda, n)
zeroprob <- rep_len(zeroprob, n)
x <- numeric(n)
cleared <- runif(n) >= zeroprob # TRUE where the hurdle is cleared
if (any(cleared)) x[cleared] <- rztpois(sum(cleared), lambda[cleared])
return(x)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.