Is there a way to generate two correlated variables with a Bernoulli distribution where the sample distribution is exactly the same as the population distribution?
I can easily generate a pair of correlated variables with a Bernoulli distribution. Here is an example where the two variables have a correlation of 0.5, and the probability is 0.5.
# simulate normally distributed data
data = MASS::mvrnorm(n=1000,
mu=c(0,0),
Sigma=matrix(data = c(1 , 0.5,
0.5, 1),
nrow = 2,
byrow = TRUE),
empirical=TRUE)
# transform to a Bernoulli distribution
data2 = apply(data,2,function(X){
p <- stats::pnorm(X, 0, 1)
p2<-stats::qbinom(p, 1, 0.5 )
return(p2)
})
cor(data2)
## [,1] [,2]
##[1,] 1.0000000 0.3561253
##[2,] 0.3561253 1.0000000
Note though that in this sample, the probability is close, but not exactly 0.5.
sum(data2[,1]/dim(data2)[1])
## [1] 0.507
sum(data2[,2]/dim(data2)[1])
## [1] 0.497
Normally this isn't an issue, and is probably even a feature! Is there a way though to generate correlated variables with a Bernoulli distribution where the sample probability is exactly the same as the population probability? Where the number in each group is exactly what you'd expect? Obviously this is trivial with a single variable (at least when the probability is 0.5).
x1 = sample(rep(c(0,1),1000/2),replace = F,1000)
sum(x1)/1000
##[1] 0.5
But I don't know how I would generate a second variable with the same distribution that has the desired correlation with the first.
Thanks in advance!
Edit While prior questions have asked about simulating Bernoulli data with a known population correlation, this question is about attaining a specific correlation within a sample. I have already accepted an answer, but I think the question is different enough that it is inaccurate to mark it as a duplicate.