NegBinomial              package:stats              R Documentation

_T_h_e _N_e_g_a_t_i_v_e _B_i_n_o_m_i_a_l _D_i_s_t_r_i_b_u_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Density, distribution function, quantile function and random
     generation for the negative binomial distribution with parameters
     'size' and 'prob'.

_U_s_a_g_e:

     dnbinom(x, size, prob, mu, log = FALSE)
     pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
     qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
     rnbinom(n, size, prob, mu)

_A_r_g_u_m_e_n_t_s:

       x: vector of (non-negative integer) quantiles.

       q: vector of quantiles.

       p: vector of probabilities.

       n: number of observations. If 'length(n) > 1', the length is
          taken to be the number required.

    size: target for number of successful trials, or dispersion
          parameter (the shape parameter of the gamma mixing
          distribution).

    prob: probability of success in each trial.

      mu: alternative parametrization via mean: see Details

log, log.p: logical; if TRUE, probabilities p are given as log(p).

lower.tail: logical; if TRUE (default), probabilities are P[X <= x],
          otherwise, P[X > x].

_D_e_t_a_i_l_s:

     The negative binomial distribution with 'size' = n and 'prob' = p
     has density

             p(x) = Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x

     for x = 0, 1, 2, ...

     This represents the number of failures which occur in a sequence
     of Bernoulli trials before a target number of successes is
     reached.

     A negative binomial distribution can arise as a mixture of Poisson
     distributions with mean distributed as a gamma ('pgamma')
     distribution with scale parameter '(1 - prob)/prob' and shape
     parameter 'size'.  (This definition allows non-integer values of
     'size'.) In this model 'prob' = 'scale/(1+scale)', and the mean is
     'size * (1 - prob)/prob)'

     The alternative parametrization (often used in ecology) is by the
     _mean_ 'mu', and 'size', the _dispersion parameter_, where 'prob'
     = 'size/(size+mu)'.  In this parametrization the variance is 'mu +
     mu^2/size'.

     If an element of 'x' is not integer, the result of 'dnbinom' is
     zero, with a warning.

     The quantile is defined as the smallest value x such that F(x) >=
     p, where F is the distribution function.

_V_a_l_u_e:

     'dnbinom' gives the density, 'pnbinom' gives the distribution
     function, 'qnbinom' gives the quantile function, and 'rnbinom'
     generates random deviates.

_S_e_e _A_l_s_o:

     'dbinom' for the binomial, 'dpois' for the Poisson and 'dgeom' for
     the geometric distribution, which is a special case of the
     negative binomial.

_E_x_a_m_p_l_e_s:

     x <- 0:11
     dnbinom(x, size = 1, prob = 1/2) * 2^(1 + x) # == 1
     126 /  dnbinom(0:8, size  = 2, prob  = 1/2) #- theoretically integer

     ## Cumulative ('p') = Sum of discrete prob.s ('d');  Relative error :
     summary(1 - cumsum(dnbinom(x, size = 2, prob = 1/2)) /
                       pnbinom(x, size  = 2, prob = 1/2))

     x <- 0:15
     size <- (1:20)/4
     persp(x,size, dnb <- outer(x,size,function(x,s)dnbinom(x,s, pr= 0.4)),
           xlab = "x", ylab = "s", zlab="density", theta = 150)
     title(tit <- "negative binomial density(x,s, pr = 0.4)  vs.  x & s")

     image  (x,size, log10(dnb), main= paste("log [",tit,"]"))
     contour(x,size, log10(dnb),add=TRUE)

     ## Alternative parametrization
     x1 <- rnbinom(500, mu = 4, size = 1)
     x2 <- rnbinom(500, mu = 4, size = 10)
     x3 <- rnbinom(500, mu = 4, size = 100)
     h1 <- hist(x1, breaks = 20, plot = FALSE)
     h2 <- hist(x2, breaks = h1$breaks, plot = FALSE)
     h3 <- hist(x3, breaks = h1$breaks, plot = FALSE)
     barplot(rbind(h1$counts, h2$counts, h3$counts),
             beside = TRUE, col = c("red","blue","cyan"),
             names.arg = round(h1$breaks[-length(h1$breaks)]))

