Here you will find daily news and tutorials about R, contributed by over 573 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

Most of regression methods assume that response variables follow some exponential distribution families, e.g. Guassian, Poisson, Gamma, etc. However, this assumption was frequently violated in real world by, for example, zero-inflated overdispersion
problem. A number of methods were developed to deal with such problem, and among them, Quasi-Poisson and Negative Binomial are the most
popular methods perhaps due to that major statistical softwares contain such functions. Unlike Negative Binomial distribution, there is no function for generating Quasi-Poisson distributed random variable in R.
In this blog, I will show how to generate Quasi-Poisson distributed variable using Negative Binomial distribution.

Let variable follows Quasi-Poisson distribution, then the variance of should have a linear relationship with
the mean of :

where, is called the disperision parameter, and for overdispersion variables , should greater than 1.

If variable follows Negative Binomial distribution, the variance of should have quadratic relationship with the mean of .

Random Negative Binomial variable can be generated in R using function rnbinom:

rnbinom

12345

> x <- rnbinom(n =10000, size =8, mu =5)> mean(x)[1]4.9674> var(x)[1]7.874925

If we can find the relationship between and , then we can use the Negative Binomial distribution
to generate Quasi-Poisson distributed random variable. The proof process is listed as the following:

So, we can define such a function in R:

rqpois

123

rqpois <-function(n, mu, theta){ rnbinom(n = n, mu = mu, size = mu/(theta-1))}

Take an example to diagnose the performance of the above function: and . According to the relationship
, the generated variable should have variance arround 15.

test

123456

> set.seed(0)> x <- rqpois(n =10000, mu =3, theta =5);> mean(x)[1]2.9718> var(x)[1]14.66027