1

I have two normal distributions X and Y and with a given covariance between them and variances for both X and Y, I want to simulate (say 200 points) of pairs of points from the joint distribution, but I can't seem to find a command/way to do this. I want to eventually plot these points in a scatter plot.

so far, I have

set obs 100
set seed 1

gen y = 64*rnormal(1, 5/64)
gen x = 64*rnromal(1, 5/64)
matrix D = (1, .5 | .5, 1)

drawnorm x, y, cov(D)

but this makes an error saying that x and y already exist.

Also, once I have a sample, how would I plot the drawnorm output as a scatter?

Nick Cox
  • 35,529
  • 6
  • 31
  • 47

2 Answers2

1

A related approach for generating correlated data is to use the corr2data command:

clear

set obs 100
set seed 1

matrix D = (1, .5 \ .5, 1)

drawnorm x1 y1, cov(D)
corr2data x2 y2, cov(D)


. summarize x*

Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      x1 |        100    .0630304    1.036762  -2.808194   2.280756
      x2 |        100    1.83e-09           1  -2.332422   2.238905

. summarize y*

Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      y1 |        100   -.0767662    .9529448  -2.046532   2.726873
      y2 |        100    3.40e-09           1  -2.492884   2.797518

It is important to note that unlike drawnorm, the corr2data approach does not generate data that is a sample from an underlying population.


You can then create a scatter plot as follows:

scatter x1 y1 

Or to compare the two approaches in a single graph:

twoway scatter x1 y1 || scatter x2 y2

EDIT:

For specific means and variances you need to specify the mean vector μ and covariance matrix Σ in drawnorm. For example, to draw two random variables that are jointly normally distributed with means of 8 and 12, and variances 5 and 8 respectively, you type:

matrix mu = (8, 12)
scalar cov = 0.4 * sqrt(5 * 8) // assuming a correlation of 0.4
matrix sigma = (5, cov \ cov, 8)

drawnorm double x y, means(mu) cov(sigma)

The mean and cov options of drawnorm are both documented in the help file.

0

Here is an almost minimal example:

. clear 

. set obs 100
number of observations (_N) was 0, now 100

. set seed 1

. matrix D = (1, .5 \ .5, 1)

. drawnorm x y, cov(D)

As the help for drawnorm explains, you must supply new variable names. As x and y already exist, drawnorm threw you out. You also had a superfluous comma that would have triggered a syntax error.

help scatter tells you about scatter plots.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47