-1

Suppose I have three variables with a correlation matrix like this:

    y1  y2  y3
y1 1.0 0.2 0.3
y2 0.2 1.0 0.4
y3 0.3 0.4 1.0

How could I draw a set of n random bernoulli samples of y1, y2, y3 under this specified correlation parameters (maybe also with specified probability for each of them)? such like:

  y1 y2 y3
1  1  1  0
2  0  1  1
3  0  0  0
4  0  0  1
5  1  0  0
6  0  1  0
....
n  1  0  0
David Z
  • 6,641
  • 11
  • 50
  • 101

1 Answers1

1

As in this post, you could use the rmvbin function:

# Correlation and marginal probabilities
cor.mat <- matrix(c(1, .2, .3, .2, 1, .4, .3, .4, 1), nrow=3)
marg.prob <- margprob <- c(.1, .2, .3)

# Generate random data
library(bindata)
set.seed(144)
res <- rmvbin(10000, margprob, bincorr=cor.mat)

# Check things are working properly
cor(res)
#           [,1]      [,2]      [,3]
# [1,] 1.0000000 0.1891508 0.2965542
# [2,] 0.1891508 1.0000000 0.3927966
# [3,] 0.2965542 0.3927966 1.0000000
colMeans(res)
# [1] 0.1013 0.1980 0.3029
Community
  • 1
  • 1
josliber
  • 43,891
  • 12
  • 98
  • 133