Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using R to honor correlations for LatinHypercube / Monte Carlo trials

Tags:

r

I am currently using python and RPY to use the functionality inside R.

How do I use R library to generate Monte carlo samples that honor the correlation between 2 variables.. e.g if variable A and B have a correlation of 85% (0.85), i need to generate all the monte carlo samples honoring that correlation between A & B.

Would appreciate if anyone can share ideas / snippets

Thanks

like image 436
Ira S Avatar asked Apr 15 '11 15:04

Ira S


1 Answers

The rank correlation method of Iman and Conover seems to be a widely used and general approach to producing correlated monte carlo samples for computer based experiments, sensitivity analysis etc. Unfortunately I have only just come across this and don't have access to the PDF so don't know how the authors actually implement their method, but you could follow this up.

Their method is more general because each variable can come from a different distribution unlike the multivariate normal of @Dirk's answer.

Update: I found an R implementation of the above approach in package mc2d, in particular you want the cornode() function.

Here is an example taken from ?cornode

> require(mc2d)
> x1 <- rnorm(1000)
> x2 <- rnorm(1000)
> x3 <- rnorm(1000)
> mat <- cbind(x1, x2, x3)
> ## Target
> (corr <- matrix(c(1, 0.5, 0.2, 0.5, 1, 0.2, 0.2, 0.2, 1), ncol=3))
     [,1] [,2] [,3]
[1,]  1.0  0.5  0.2
[2,]  0.5  1.0  0.2
[3,]  0.2  0.2  1.0
> ## Before
> cor(mat, method="spearman")
            x1         x2          x3
x1  1.00000000 0.01218894 -0.02203357
x2  0.01218894 1.00000000  0.02298695
x3 -0.02203357 0.02298695  1.00000000
> matc <- cornode(mat, target=corr, result=TRUE)
Spearman Rank Correlation Post Function
          x1        x2        x3
x1 1.0000000 0.4515535 0.1739153
x2 0.4515535 1.0000000 0.1646381
x3 0.1739153 0.1646381 1.0000000

The rank correlations in matc are now very close to the target correlations of corr.

The idea with this is that you draw the samples separately from the distribution for each variable, and then use the Iman & Connover approach to make the samples (as close) to the target correlations as possible.

like image 120
Gavin Simpson Avatar answered Nov 03 '22 00:11

Gavin Simpson