BookmarkSubscribeRSS Feed
H
Pyrite | Level 9 H
Pyrite | Level 9

I am trying to simulate two correlated lognormal variables. The variables should be right skewed somewhat and are are left bound by zero. I don't have any moment information beyond the following, which were superficially provided from a colleague:

 

X: mean = 7.5; std = 6.5

Y: mean = 1; std =  1.5

rho = 0.5

 

Ideally the code will allow me to play around with the above parameters, and I will desire to eventually simulate 1,000s of samples given the parameters. My fruitless attempt is below:

 

data have;
call streaminit(4321);
sigma1 = 0.78;      /* shape or log-scale parameter    */
zeta1 = 1.72;         /* scale or log-location parameter */
do id=1 to 10000;
 X1 = rand("Normal", zeta1, sigma1);  /* Y ~ N(zeta, sigma)    */
 X2 = exp(X1);
output;
end;
run;
%let rho=0.45;
data want;
 set have;
 call streaminit(1234);
	sigma2 = 0.00000000000000000000000000000000001;      /* shape or log-scale parameter    */
	zeta2 = 0.000000000000000000000000000000000001;         /* scale or log-location parameter */
 y1=&rho*x2+sqrt(1-&rho**2)*rand("Normal", zeta2, sigma2);
 Y2=exp(y1);
run;
proc corr data=want;
 var x2;
 with y2;
run;
proc univariate data=want;      /* visualize simulated data and check fit */
   	histogram x2 y2/ lognormal; 
run;

 

Obviously my issue is with the second variable. I was getting deserate and thinking of just sorting each variable independently and pseudo sorting them and merging them together get a semblance of my target sample. That or the X variable is not that skewed appearing, so I could simulate Y (log normal) and correlate X (normal). There could be an easy work around, say simulate normal variable and then transform them after the simulation process. Not sure 😞

 

P.S., This seems to be easiily done in R or Laavan!

6 REPLIES 6
Ksharp
Super User

You should take on SAS/IML .

Calling @Rick_SAS

 

 

data have;
call streaminit(123456789);
do i=1 to 10000;
 y=exp(rand('normal'));
 z=exp(rand('normal'));

 x=0.5*y+sqrt(1-0.5**2)*z;output;
end;
run;

proc standard data=have mean=7.5 std=6.5 out=have1;
var x;
run;
proc standard data=have1 mean=1 std=1.5 out=have2; 
var y;
run;

proc means data=have2 mean std;
var x y;
run;
proc corr data=have2 ;
var x y;
run;
proc univariate data=have2;
histogram x/kernel;
histogram y/kernel;
run;

 

Rick_SAS
SAS Super FREQ

I devote Chapter 9 of Simulating Data with  SAS to this kind of problem, which is simulating multivariate data that has a specified correlation and specified marginal distributions. In SAS, the easiest way to do this is to use PROC COPULA in SAS/ETS software and fit a normal copula, as shown in the example on p. 169-172 of my book. The same chapter also shows how to fit copulas with SAS/IML. 

 

There are infinitely many bivariate distributions that have the required correlation and marginals. Some, like KSharp's solution, have a  strange bivariate distribution, as shown by a scatter plot of his variables.

H
Pyrite | Level 9 H
Pyrite | Level 9

Sharp K's response seemed to be exactly what I was looking for in regards to the parameters. The use of the STANDARDIZED approach appears to be what I was missing. Clever. Thanks.

 

Rick, thank you for the feedback. Yes, I had seen that section in your book, but due to my initiial lack of knowledge with COPULA, I strayed away from that option. I will have to go back and examine it in more depth when I have time. Though, I was using these data in a sample size calculation, so I did tap into that portion of your book. Thanks.

Ksharp
Super User

To be honest, I want @Rick_SAS to say some more words to clarify this problem. 

Better give us an example of IML code .

H
Pyrite | Level 9 H
Pyrite | Level 9

Hmm, I have a tendency to get a little too excited without fully exploring the code.  Ksharp's code works near as instructed, but is not completely what I had originally wanted.

 

Problem with the code in regards to using it with sample size calculation is that it constrains all of the means and std to the a priori parameters with no variability in those parameters only in the values in the sample distribution. Though, I needed the variabiliity which would have been seen in the sampling distribution in parameters. The above code also allowed for negative values, since the standardiization occurred post hoc to data generation process, which would not be possible in my  particular context.

 

It ended up turning out that I don't have to corelate variables since the study design will be a little different than orginally expected. Which is easy enough to simulate two independent lognormal variables.

Ksharp
Super User

H,

I totally I agree with you. That is the reason I call Rick to clarify it more .

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 967 views
  • 3 likes
  • 3 in conversation