# dBetaBinom: A beta binomial distribution and beta function for use in... In nimbleEcology: Distributions for Ecological Models in 'nimble'

## Description

`dBetaBinom` and `dBetaBinom_One` provide a beta binomial distribution that can be used directly from R or in `nimble` models. These are also used by beta binomial variations of dNmixture distributions. `nimBetaFun` is the beta function.

## Usage

 ```1 2 3 4 5 6 7 8 9``` ```nimBetaFun(a, b, log) dBetaBinom(x, N, shape1, shape2, log = 0) dBetaBinom_One(x, N, shape1, shape2, log = 0) rBetaBinom(n, N, shape1, shape2) rBetaBinom_One(n, N, shape1, shape2) ```

## Arguments

 `a` shape1 argument of the beta function nimBetaFun. `b` shape2 argument of the beta function nimBetaFun. `log` TRUE or 1 to return log probability. FALSE or 0 to return probability. `x` vector of integer counts. `N` number of trials, sometimes called "size". `shape1` shape1 parameter of the beta-binomial distribution. `shape2` shape2 parameter of the beta-binomial distribution. `n` number of random draws, each returning a vector of length `len`. Currently only `n = 1` is supported, but the argument exists for standardization of "`r`" functions.

## Details

These nimbleFunctions provide distributions that can be used directly in R or in `nimble` hierarchical models (via `nimbleCode` and `nimbleModel`). They were originally written for the beta binomial N-mixture extensions.

The beta binomial distribution is equivalent to a binomial distribution whose probability is itself a beta distributed random variable.

The probability mass function of the beta binomial is ```choose(N, x) * B(x + shape1, N - x + shape2) / B(shape1, shape2)```, where `B(shape1, shape2)` is the beta function.

The beta binomial distribution is provided in two forms. `dBetaBinom` and `rBetaBinom` are used when `x` is a vector (i.e. `length(x) > 1`), in which case the parameters `alpha` and `beta` must also be vectors. When `x` is scalar, `dBetaBinom_One` and `rBetaBinom_One` are used.

## Author(s)

Ben Goldstein and Perry de Valpine

For beta binomial N-mixture models, see `dNmixture`. For documentation on the beta function, use `?stats::dbeta`
 ```1 2 3 4 5``` ```# Calculate a beta binomial probability dBetaBinom(x = c(4, 0, 0, 3), N = 10, shape1 = c(0.5, 0.5, 0.3, 0.5), shape2 = c(0.2, 0.4, 1, 1.2)) # Same for case with one observation dBetaBinom_One(x = 3, N = 10, shape1 = 0.5, shape2 = 0.5, log = TRUE) ```