Suppose X~exp(.67)
, Y~exp(.45)
and Z~exp(.8)
. Now X
is correlated with Y
with a correlation coefficient -0.6. Again, X
is correlated with Z
with a correlation coefficient -0.6. How can I incorporate this correlations to generate random variables X
, Y
and Z
? I know if there were no correlation among them, then I could simply generate data by X <- rexp(n=10, rate=.67)
, Y <- rexp(10, .45)
and Z <- rexp(10, .8)
.
Yes, and to add to all previous answers and comments, you can easily simulate the accidence. Out of 1000 random draws there was a case when two independent normal random variables had a (weak) correlation of ±0.34.
Correlation between two random variables, ρ(X,Y) is the covariance of the two. variables normalized by the variance of each variable. This normalization cancels the units out and. normalizes the measure so that it is always in the range [0, 1]:
The answer is a sum of independent exponentially distributed random variables, which is an Erlang(n, λ) distribution. The Erlang distribution is a special case of the Gamma distribution. The difference between Erlang and Gamma is that in a Gamma distribution, n can be a non-integer.
To do this, you can use the Iman and Conover method from the package mc2d
.
First, create your settings. I have assumed that Y and Z are uncorrelated, given the absence of a stated correlation above. (If they are not, just change the correlation matrix accordingly.)
library(mc2d)
x1 <- rexp(n = 1000, rate = 0.67)
x2 <- rexp(n = 1000, rate = 0.45)
x3 <- rexp(n = 1000, rate = 0.8)
mat <- cbind(x1, x2, x3)
corr <- matrix(c(1, -0.6, -0.6, -0.6, 1, 0, -0.6, 0, 1), ncol=3)
We can now test the actual correlations of the random samples ... that all seem independent, as expected.
cor(mat, method="spearman")
... which generates:
x1 x2 x3
x1 1.00000000 0.01602557 -0.0493488
x2 0.01602557 1.00000000 0.0124209
x3 -0.04934880 0.01242090 1.0000000
We now apply the Iman and Conover method to the data.
matc <- cornode(mat, target=corr, result=TRUE)
Doing so yields the following correlations:
Spearman Rank Correlation Post Function
x1 x2 x3
x1 1.0000000 -0.59385975 -0.56201396
x2 -0.5938597 1.00000000 -0.04115543
x3 -0.5620140 -0.04115543 1.00000000
Finally, by running head(matc)
, we see the initial rows of your revised sample:
x1 x2 x3
[1,] 1.1375395 0.3081750 2.26850817
[2,] 2.9387996 0.4434207 0.08940867
[3,] 1.0918648 0.4175625 2.29498679
[4,] 10.0273879 1.1478072 0.16099230
[5,] 1.5093832 0.4023230 2.57870672
[6,] 0.9474039 2.1134685 0.95268424
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With