[R] Generating random variables with given pairwise correlations

Greg Snow 538280 at gmail.com
Tue May 27 02:13:21 CEST 2014


For numeric/continuous/normal values you can use the mvrnorm function
in the MASS package (set the empirical argument to TRUE to force the
exact correlation).  Some would argue that you should not compute
correlations with binary variables, but you could generate 4 normals,
then take the last 2 and dicotomize them to 0/1 based on
negative/positive (or other cut-off).  The correlation will not be
exactly the same, but you could do some trial and error to adjust the
starting correlations to see what the final ones are.  Or you could
wrap the whole process in a function to pass to optim to find the
starting correlations that will get you closest to your desired ones.

On Sat, May 24, 2014 at 10:35 AM, Sourav Ghosh <scottsourav at gmail.com> wrote:
> I want to generate 2 continuous random variables `Q1`, `Q2` (quantitative
> traits) and 2 binary random variables `Z1`, `Z2` (binary traits) with given
> pairwise correlations between all possible pairs of them.
> Say
>
>     (Q1,Q2):0.23
>     (Q1,Z1):0.55
>     (Q1,Z2):0.45
>     (Q2,Z1):0.4
>     (Q2,Z2):0.5
>     (Z1,Z2):0.47
>
> Please help me generate such data in R.
>
> --
> Sourav Ghosh
> 2nd Year M.Stat. Student
> Indian Statistical Institute, Kolkata
> India
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list