how to simulate correlated binary data with R? -


this question has answer here:

supposing want 2 vectors of binary data specified phi coefficients, how simulate r?

for example, how can create 2 vectors x , y of specified vector length cor efficient 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 

the bindata package nice generating binary data , more complicated correlation structures. (here's link working paper (warning, pdf) lays out theory underlying approach taken package authors.)

in case, assuming independent probabilities of x , y both 0.5:

library(bindata)  ## construct binary correlation matrix rho <- 0.7905694 m <- matrix(c(1,rho,rho,1), ncol=2)     ## simulate 10000 x-y pairs, , check have 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 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -