Simulate Correlated Lognormal (Skewed) Data

Frequent Contributor
Posts: 122

Simulate Correlated Lognormal (Skewed) Data

[ Edited ]

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!

Super User
Posts: 10,787

Re: Simulate Correlated Lognormal (Skewed) Data

[ Edited ]

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;``````

SAS Super FREQ
Posts: 4,245

Re: Simulate Correlated Lognormal (Skewed) Data

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.

Frequent Contributor
Posts: 122

Re: Simulate Correlated Lognormal (Skewed) Data

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.

Super User
Posts: 10,787

Re: Simulate Correlated Lognormal (Skewed) Data

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

Better give us an example of IML code .

Frequent Contributor
Posts: 122

Re: Simulate Correlated Lognormal (Skewed) Data

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.

Super User
Posts: 10,787

Re: Simulate Correlated Lognormal (Skewed) Data

H,

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

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