Generate correlated random numbers from binomial distributions

Arnaud picture Arnaud · May 10, 2012 · Viewed 9.4k times · Source

I am trying to find a way to generate correlated random numbers from several binomial distributions.

I know how to do it with normal distributions (using MASS::mvrnorm), but I did not find a function applicable to binomial responses.

Answer

Greg Snow picture Greg Snow · May 10, 2012

You can generate correlated uniforms using the copula package, then use the qbinom function to convert those to binomial variables. Here is one quick example:

library(copula)

tmp <- normalCopula( 0.75, dim=2 )
x <- rcopula(tmp, 1000)
x2 <- cbind( qbinom(x[,1], 10, 0.5), qbinom(x[,2], 15, 0.7) )

Now x2 is a matrix with the 2 columns representing 2 binomial variables that are correlated.