Learn R Programming

emdbook (version 1.3.13)

dbetabinom: Beta-binomial distribution

Description

Density function and random variate generator for the beta-binomial function, parameterized in terms of probability and overdispersion

Usage

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

Value

A vector of probability densities or random deviates. If x is non-integer, the result is zero (and a warning is given).

Arguments

x

a numeric vector of integer values

prob

numeric vector: mean probability of underlying beta distribution

size

integer: number of samples

theta

overdispersion parameter

shape1

shape parameter of per-trial probability distribution

shape2

shape parameter of per-trial probability distribution

log

(logical) return log probability density?

n

integer number of random variates to return

Author

Ben Bolker

Details

The beta-binomial distribution is the result of compounding a beta distribution of probabilities with a binomial sampling process. The density function is $$p(x) = \frac{C(N,x) \mbox{Beta}(x+\theta p,N-x+\theta(1-p))}% {\mbox{Beta}(\theta p,\theta(1-p))}% $$ The parameters shape1 and shape2 are the more traditional parameterization in terms of the parameters of the per-trial probability distribution.

References

Morris (1997), American Naturalist 150:299-327; https://en.wikipedia.org/wiki/Beta-binomial_distribution

See Also

Examples

Run this code
  set.seed(100)
  n <- 9
  z <- rbetabinom(1000, 0.5, size=n, theta=4)
  par(las=1,bty="l")
  plot(table(z)/length(z),ylim=c(0,0.34),col="gray",lwd=4,
       ylab="probability")
  points(0:n,dbinom(0:n,size=n,prob=0.5),col=2,pch=16,type="b")
  points(0:n,dbetabinom(0:n,size=n,theta=4,
           prob=0.5),col=4,pch=17,type="b")
  ## correspondence with SuppDists 
  if (require(SuppDists)) {
    d1a <- dghyper(0:5,a=-5,N=-10,k=5)
    d1b <- dbetabinom(0:5,shape1=5,shape2=5,size=5)
    max(abs(d1a-d1b))
    p1a <- pghyper(0:5,a=-5,N=-10,k=5,lower.tail=TRUE)
    p1b <- cumsum(d1b)
    max(abs(p1a-p1b))
  } 

Run the code above in your browser using DataLab