Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
correlated random variables
## make it reproducible
set.seed(8675309)
## two normally distributed variables
X1 <- rnorm(100, mean = 0, sd = 1)
X2 <- rnorm(100, mean = 0, sd = 1)
plot(X1, X2)
# set approximate R-square value
Rsq <- 0.90
# create Y and Z
Y <- X1
Z <- Rsq * X1 + sqrt(1 - Rsq) * X2
# evaluate
cor(Y,Z)
plot(Y, Z)
### -----------------------------------------------------------------------------------------
## a binomial variable between 1 and 10 and a normally distributed variable
set.seed(8675309)
X1 <- rbinom(100, 10, 0.5)
X2 <- rnorm(100, mean = 0, sd = 1)
plot(X1, X2)
# set approximate R-square value
Rsq <- 0.90
# create Y and Z
Y <- X1
Z <- Rsq * X1 + sqrt(1 - Rsq) * X2
# evaluate
cor(Y,Z)
plot(Y, Z)
### -----------------------------------------------------------------------------------------
## two uniform variables between 1 and 10
set.seed(8675309)
X1 <- sample(1:10, 100, replace = TRUE)
X2 <- sample(1:10, 100, replace = TRUE)
plot(X1, X2)
# set approximate R-square value
Rsq <- 0.80
# create Y and Z
Y <- X1
Z <- Rsq * X1 + sqrt(1 - Rsq) * X2
Z <- floor(Z)
# evaluate
cor(Y,Z)
plot(Y, Z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.