# [Rd] Random Count Generation with rnbinom

Achim Zeileis Achim.Zeileis at uibk.ac.at
Sun Feb 23 03:16:14 CET 2014

```On Sun, 23 Feb 2014, Dario Strbenac wrote:

> The documentation states :
>
> An alternative parametrization (often used in ecology) is by the mean ?mu?, and ?size?, the dispersion parameter.
>
> However, this fails :
>
>> rnbinom(10, mu = 100, size = 0)
> [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
> Warning message:
> In rnbinom(10, mu = 100, size = 0) : NAs produced
>
> For dispersion set to 0, it should work like drawing from a Poisson
> distribution.

No, Poisson is the case for size converging to Inf. Compare:

R> dnbinom(1:4, mu = 1, size = 1e9)
[1] 0.36787945 0.18393972 0.06131324 0.01532831
R> dpois(1:4, lambda = 1)
[1] 0.36787944 0.18393972 0.06131324 0.01532831

This is easy to see from the explanation in the manual page:

The variance is 'mu + mu^2/size' in this parametrization.

So for size = Inf this yields mu, i.e., equidispersion. And for finite
positive size, there is always overdispersion.

> It also produces incorrect draws for non-zero dispersions :
>
>> rnbinom(10, mu = 100, size = 0.000001)
> [1] 0 0 0 0 0 0 0 0 0 0

This conforms with the documentation

The case 'size == 0' is the distribution concentrated at zero.
This is the limiting distribution for 'size' approaching zero,
even if 'mu' rather than 'prob' is held constant. Notice though,
that the mean of the limit distribution is 0, whatever the value
of 'mu'.

> It also fails for underdispersed count models :
>
>> rnbinom(10, mu = 100, size = -0.1)
> [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
> Warning message:
> In rnbinom(10, mu = 100, size = -0.1) : NAs produced

And here the manual explains:

size: target for number of successful trials, or dispersion
parameter (the shape parameter of the gamma mixing
distribution).  Must be strictly positive, need not be
integer.

>> sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C
> [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8
> [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8
> [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C
> [11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_3.0.2
>
> --------------------------------------
> Dario Strbenac
> PhD Student
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

```