Density, distribution function, quantile function and random generation for the extended beta-binomial distribution.

dextbetabinom(x, size, prob, rho = 0,
     log = FALSE, forbycol = TRUE)
pextbetabinom(q, size, prob, rho = 0,
     lower.tail = TRUE, forbycol = TRUE)
qextbetabinom(p, size, prob, rho = 0,
     forbycol = TRUE)
rextbetabinom(n, size, prob, rho = 0)

Arguments

x, q

vector of quantiles.

p

vector of probabilities.

size

number of trials.

n

number of observations. Same as runif.

prob

the probability of success \(\mu\). Must be in the unit closed interval \([0,1]\).

rho

the correlation parameter \(\rho\), which may be negative for underdispersion or else be in the interval \([0, 1)\) for overdispersion. The default value of 0 corresponds to the usual binomial distribution with probability prob.

log, lower.tail

Same meaning as runif.

forbycol

Logical. A for loop cycles over either the rows or columns and this argument determines which. The rows are 1:length(x) and the columns are 0:max(size). The best choice is data set dependent.

Value

dextbetabinom gives the density, pextbetabinom gives the distribution function, qextbetabinom gives the quantile function and rextbetabinom generates random deviates.

Details

The extended beta-binomial distribution allows for a slightly negative correlation parameter between binary responses within a cluster (e.g., a litter). An exchangeable error structure with correlation \(\rho\) is assumed.

Note

Currently most of the code is quite slow. Speed improvements are a future project. Use forbycol optimally.

Warning

Setting rho = 1 is not recommended as NaN is returned, however the code may be modified in the future to handle this special case.

Examples

set.seed(1); rextbetabinom(10, 100, 0.5)
#>  [1] 52 46 60 49 52 51 63 52 47 38
set.seed(1);        rbinom(10, 100, 0.5)  # Same
#>  [1] 52 46 60 49 52 51 63 52 47 38

if (FALSE) N <- 9; xx <- 0:N; prob <- 0.5; rho <- -0.02
#> Error: object 'N' not found
dy <- dextbetabinom(xx, N, prob, rho)
#> Error: object 'xx' not found
barplot(rbind(dy, dbinom(xx, size = N, prob)),
  beside = TRUE, col = c("blue","green"), las = 1,
  main = paste0("Beta-binom(size=", N, 
  ", prob=", prob, ", rho=", rho, ") (blue) vs\n",
  " Binom(size=", N, ", prob=", prob, ") (green)"),
  names.arg = as.character(xx), cex.main = 0.8)
#> Error: object 'dy' not found
sum(dy * xx)  # Check expected values are equal
#> Error: object 'dy' not found
sum(dbinom(xx, size = N, prob = prob) * xx)
#> Error: object 'xx' not found
cumsum(dy) - pextbetabinom(xx, N, prob, rho)  # 0?
#> Error: object 'dy' not found
 # \dontrun{}