How to simulate correlated binary data with R?

Suppose I want 2 binary data vectors with specified phi coefficients, how did I model it with R?

For example, how can I create two vectors of type x and y specified vector length with a coefficient of 0.79

 > x = c(1, 1, 0, 0, 1, 0, 1, 1, 1) > y = c(1, 1, 0, 0, 0, 0, 1, 1, 1) > cor(x,y) [1] 0.7905694 
+4
source share
1 answer

The bindata strong> package is good for generating binary data using these and more complex correlation structures. ( Here is a link to a working document (warning, pdf) that sets out the theory behind the approach made by the authors of the package.)

In your case, assuming the independent probabilities x and y are 0.5:

 library(bindata) ## Construct a binary correlation matrix rho <- 0.7905694 m <- matrix(c(1,rho,rho,1), ncol=2) ## Simulate 10000 xy pairs, and check that they have the specified ## correlation structure x <- rmvbin(1e5, margprob = c(0.5, 0.5), bincorr = m) cor(x) # [,1] [,2] # [1,] 1.0000000 0.7889613 # [2,] 0.7889613 1.0000000 
+10
source

All Articles