dbetabinom: Beta-binomial probability density

dbetabinomR Documentation

Beta-binomial probability density

Description

Functions for computing density and producing random samples from a beta-binomial probability distribution.

Usage

dbetabinom( x , size , prob , theta , shape1 , shape2 , log=FALSE )
rbetabinom( n , size , prob , theta , shape1 , shape2 )

Arguments

x

Integer values to compute probabilies of

size

Number of trials

prob

Average probability of beta distribution

theta

Dispersion of beta distribution

shape1

First shape parameter of beta distribution (alpha)

shape2

Second shape parameter of beta distribution (beta)

log

If TRUE, returns log-probability instead of probability

n

Number of random observations to sample

Details

These functions provide density and random number calculations for beta-binomial observations. The dbetabinom code is based on Ben Bolker's original code in the emdbook package.

Either prob and theta OR shape1 and shape2 must be provided. The two parameterizations are related by shape1 = prob * theta, shape2 = (1-prob) * theta.

The rbetabinom function generates random beta-binomial observations by using both rbeta and rbinom. It draws n values from a beta distribution. Then for each, it generates a random binomial observation.

Author(s)

Richard McElreath

Examples

## Not run: 
data(reedfrogs)
reedfrogs$pred_yes <- ifelse( reedfrogs$pred=="pred" , 1 , 0 )

# map model fit
# note exp(log_theta) to constrain theta to positive reals
m <- map(
    alist(
        surv ~ dbetabinom( density , p , exp(log_theta) ),
        logit(p) <- a + b*pred_yes,
        a ~ dnorm(0,10),
        b ~ dnorm(0,1),
        log_theta ~ dnorm(1,10)
    ),
    data=reedfrogs )

# map2stan model fit
# constraint on theta is passed via contraints list
m.stan <- map2stan(
    alist(
        surv ~ dbetabinom( density , p , theta ),
        logit(p) <- a + b*pred_yes,
        a ~ dnorm(0,10),
        b ~ dnorm(0,1),
        theta ~ dcauchy(0,1)
    ),
    data=reedfrogs,
    constraints=list(theta="lower=0") )

## End(Not run)

rmcelreath/rethinking documentation built on Aug. 26, 2024, 5:54 p.m.