Friday, February 23, 2018

Simulating correlated variables

Measures collected in paired/related measure experimental designs are...correlated!

There is a lot of utility in simulating experimental outcomes...for example, when doing a priori power analysis by Monte Carlo, when generating bootstrap distributions, etc.

To simulate paired observations appropriately its important to account for their paired correlations.

Here's a simple R script that accomplishes that, including a check for accuracy, illustrated for a case when 80% correlation is expected.

#simulate random variables
x1 <- rnorm(1000, 0, 1)
x2 <- rnorm(1000, 0, 1)
plot(x1,x2)
#correlate them
r=0.8
k<- sqrt(1-r^2)
x2 <- r*x1+k*x2
#confirm
plot(x1, x2)
cor(x1, x2)
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment