3

(This question is related to how to generate a dataset of correlated variables with different distributions?)

In Stata, say that I create a random variable following a Uniform[0,1] distribution:

set seed 100
gen random1 = runiform()

I now want to create a second random variable that is correlated with the first (the correlation should be .75, say), but is bounded by 0 and 1. I would like this second variable to also be more-or-less Uniform[0,1]. How can I do this?

1
  • 1
    Statistical comment: it's hard for me to make sense of what data generation model you're imagining here as linear relationships and bounds on variables are going to fight each other. If you're imagining nonlinear relationships, it's hard to see that correlations are natural summaries.
    – Nick Cox
    Sep 24, 2015 at 8:20

1 Answer 1

4

This won't be exact, but the NORTA/copula method should be pretty close and easy to implement.

The relevant citation is:

Cario, Marne C., and Barry L. Nelson. Modeling and generating random vectors with arbitrary marginal distributions and correlation matrix. Technical Report, Department of Industrial Engineering and Management Sciences, Northwestern University, Evanston, Illinois, 1997.

The paper can be found here.

The general recipe to generate correlated random variables from any distribution is:

  1. Draw two (or more) correlated variables from a joint standard normal distribution using corr2data
  2. Calculate the univariate normal CDF of each of these variables using normal()
  3. Apply the inverse CDF of any distribution to simulate draws from that distribution.

The third step is pretty easy with the [0,1] uniform: you don't even need it. Typically, the magnitude of the correlations you get will be less than the magnitudes of the original (normal) correlations, so it might be useful to bump those up a bit.

Stata Code for 2 uniformish variables that have a correlation of 0.75:

clear

// Step 1
matrix C = (1, .75 \ .75, 1)
corr2data x y, n(10000) corr(C) double
corr x y, means

// Steps 2-3
replace x = normal(x)
replace y = normal(y)

// Make sure things worked
corr x y, means
stack x y, into(z) clear
lab define vars 1 "x" 2 "y"
lab val _stack vars
capture ssc install bihist
bihist z, by(_stack) density tw1(yline(-1 0 1))

If you want to improve the approximation for the uniform case, you can transform the correlations like this (see section 5 of the linked paper):

matrix C = (1,2*sin(.75*_pi/6)\2*sin(.75*_pi/6),1)

This is 0.76536686 instead of the 0.75.


Code for the question in the comments

The correlation matrix C written more compactly, and I am applying the transformation:

clear
matrix C = ( 1, ///
             2*sin(-.46*_pi/6), 1,  ///
             2*sin(.53*_pi/6),  2*sin(-.80*_pi/6),  1,  ///
             2*sin(0*_pi/6),    2*sin(-.41*_pi/6),  2*sin(.48*_pi/6),   1 )
corr2data v1 v2 v3 v4, n(10000) corr(C) cstorage(lower)
forvalues i=1/4 {
    replace v`i' = normal(v`i')
}
4
  • Wow - thanks so much! It works great! I know I didn't ask this originally, but quick follow-up: Let's say that I need to create multiple variables: var2, which is correlated with var1 (what your code does), but then also var3, which has a correlation with var2; var4, which has a correlation with var 3; and so on. Is it easy to extend your solution to this case? Ideally, we wouldn't expand the matrix and create x, y, z, etc., but we would instead keep x and y and create z based on the existing y.
    – bill999
    Sep 24, 2015 at 2:57
  • 1
    @bill999 I added a four variable example with an arbitrary correlation matrix.
    – dimitriy
    Sep 24, 2015 at 5:04
  • I know this question isn't new, but I'd like to know if you could expand on step (3). I am a bit confused by "any distribution" and "that distribution". Do you mean generating data through any given inverse CDF and then using the data as input to each individual PDF?
    – Jxson99
    Jan 2 at 19:17
  • @Jxon99 You can use the inverse CDF of the K distribution to get a draw from that K distribution for K = normal, beta, zeta, etc. Does that make sense?
    – dimitriy
    Jan 2 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.