Skip to content
Prev 306495 / 398506 Next

Generating an autocorrelated binary variable

I have no idea what your code is doing, nor why you want correlated binary
variables.  Correlation makes little or no sense in the context of 
binary random
variables --- or more generally in the context of discrete random variables.

Be that as it may, it is an easy calculation to show that if X and Y are 
binary
random variables both with success probability of 0.5 then cor(X,Y) = 0.2 if
and only if Pr(X=1 | Y = 1) = 0.6.  So just generate X and Y using that 
fact:

set.seed(42)
X <- numeric(1000)
Y <- numeric(1000)
for(i in 1:1000) {
    Y[i] <- rbinom(1,1,0.5)
    X[i] <- if(Y[i]==1) rbinom(1,1,0.6) else rbinom(1,1,0.4)
}

# Check:
cor(X,Y) # Get 0.2012336

Looks about right.  Note that the sample proportions are 0.484 and
0.485 for X and Y respectively.  These values do not differ significantly
from 0.5.

     cheers,

             Rolf Turner
On 28/09/12 08:26, Simon Zehnder wrote: