Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to generate a variable with with fixed correlations to an existing variable which already has a uniform distribution

    Hi, I have generate a variable which I have called X, it has a uniform distribution like this: gen x=runiform(0,2). I want to create another variable, called it Y, that has a fixed correlation with X. For instance 0.5. I have already tried commands such as corr2data but they do not work the way I want to. I will sincerely appreciate a little help with my problem, thank you very much.

  • #2
    Welcome to Statalist.

    You've not told us what distribution you want the variable Y to have. If it too is intended to be uniform, then Dimitriy V. Masterov has answered that question in a different forum at

    https://stackoverflow.com/questions/...m0-1-variables

    Comment


    • #3
      The general procedure I outlined on SO should also work for the non-uniform case, though you will have to use the appropriate inverse CDF for the third step.

      Here's in example where Y~N[100,15^2]:

      Code:
      clear
      
      // Step 1
      matrix C = (1, .512 \ .512, 1) // useful to bump the correlation up from what you desire
      corr2data x y, n(10000) corr(C)
      
      // Steps 2-3
      replace x = normal(x) // step 3 not needed for uniform [0,1]
      replace y = 100 + 15*invnormal(normal(y))
      
      // Check that everything looks OK
      hist x, addplot(function y=1, range(0 1))
      hist y, normal
      corr x y, means
      Last edited by Dimitriy V. Masterov; 05 Feb 2019, 15:58.

      Comment

      Working...
      X