Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Draw new variable with fixed correlation to an existing variable


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Draw new variable with fixed correlation to an existing variable
Date   Wed, 15 Aug 2007 18:32:30 -0400

Rachel--
If I understand correctly, you want the new variable(s) to have
*exactly* the specified correlation structure, like -corr2data-
instead of -drawnorm- for new variables?  I thought -corr2data- might
do this for you, but it does not:

clear
set obs 100
set seed 12345
matrix r=(1, .5 ,.5 \ .5, 1, .5 \ .5, .5 ,1)
corr2data x1 x2 x3, corr(r)
corr x*
more

clear
set obs 100
set seed 12345
matrix r=(1, .5 ,.5 \ .5, 1, .5 \ .5, .5 ,1)
mat rown r = x1 x2 x3
mat coln r = x1 x2 x3
g x1=invnorm(uniform())
corr2data  x2 x3, corr(r)
corr x*

I think -corr2data- should probably exit with an error in the second
piece of the above code, since it cannot generate two variables with
the desired correlations.

On 8/15/07, Paulo Guimaraes <[email protected]> wrote:
> Rachel,
>
> If you want a new normal variable that on average has, say 0.5
> correlation, then you can easily do that:
>
> clear
> set obs 10000
> local corr=0.5
> gen x1=invnorm(uniform())
> quietly sum x1
> local sz=r(sd)
> local mz=r(mean)
> local s2=`sz'^2*(1/`corr'^2-1)
> gen x2=x1+sqrt(`s2')*invnorm(uniform())
> corr x1 x2
>
> Paulo Guimaraes
>
> Rachel wrote:
> > Suppose I have an existing variable x1, which is distributed as a
> > standard normal.  I wish to create a new variable x2 (also standard
> > normal) such that corr(x1,x2)=0.5.  ?Is there a way to do this in
> > Stata?
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index