Generating a quasi Poisson distribution, version 2

November 10, 2010

(This article was first published on Freakonometrics - Tag - R-english, and kindly contributed to R-bloggers)

and there, I mentioned two codes to generated quasiPoisson random
variables. And in both of them, the negative binomial approximation
seems to be wrong. Recall that the negative binomial distribution is


and in R, a negative binomial distribution can be parametrized using
two parameters, out of the following ones

  • the size
  • the probability
  • the mean

Recall also that the variance for a negative binomial distribution
should be

Here, we consider a distribution such that
and In the previous posts, I used


rqpois = function(n, lambda, phi) {
mu = lambda
k = mu/(phi * mu - 1)
r1 = rnbinom(n, mu = mu, size = k)
r2 = rnbinom(n, size=phi*mu/(phi-1),prob=1/phi)
k = mu/phi/(1-1/phi)
r3 = rnbinom(n, mu = mu, size = k)
r4 = rnbinom(n, size=mu/phi/(1-1/phi),prob=1/phi)
r = cbind(r1,r2,r3,r4)

but as we can see below, none of those two functions work,

> N=rqpois(1000000,2,4)
> mean(N[,1])
[1] 2.001992
> mean(N[,2])
[1] 8.000033
> var(N[,1])/ mean(N[,1])
[1] 7.97444
> var(N[,2])/ mean(N[,2])
[1] 4.002022

with the first one, the expected value is correct, while it is the
overdispersion parameter for the second. Now, if we do the maths
correctly, it comes

which should now work,

> mean(N[,3])
[1] 2.001667
> mean(N[,4])
[1] 2.002776
> var(N[,3])/ mean(N[,3])
[1] 3.999318
> var(N[,4])/ mean(N[,4])
[1] 4.009647

So, finally it is better when we do the maths well.

To leave a comment for the author, please follow the link and comment on their blog: Freakonometrics - Tag - R-english. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)