BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
superbug
Quartz | Level 8

I want to simulate two parameters a* and b satisfying the following conditions. How should I simulate these two parameter so that the correlation of them is 0.8. Thanks much for the help!

 

superbug_1-1617803411370.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I'm very sorry. That was a mistake on my part because I copied/pasted from another source.

From your follow-up post, I assume that N(0,2) means "mean=0 and stdDev=2." If so, the correct covariance matrix should be

COV  a  0.015  0.196
COV  b  0.196  4

which is the matrix in the second part of the problem. I will edit my solution. Thanks.

 

View solution in original post

17 REPLIES 17
superbug
Quartz | Level 8

@PaigeMiller 

Thanks much for the information.

Flowing example you provided, I made up my outcorr data, which looks like following table

superbug_0-1617807171815.png

I use the following code 

proc import datafile="....\outcorr.csv"
dbms=csv out=outcorr replace; run;

%let N = 60;            /* sample size */
%let NumSamples = 100;  /* number of samples */
proc simnormal data=work.outcorr outsim=SimMVN
               numreal = %sysevalf(&N*&NumSamples) 
               seed = 12345;          /* random number seed */
   var a b;
run;

However, there is following error message

superbug_1-1617807530964.png

 

I am seeing the "outcorr" data has been normally read in. Do you have any idea of what caused this error message?

Again thanks much!

PaigeMiller
Diamond | Level 26

How was OUTCORR created? Can you show us (screen capture is okay) OUTCORR?

--
Paige Miller
superbug
Quartz | Level 8

@PaigeMiller 

I manually created outcorr.csv, which looks like following

superbug_0-1617807996844.png

 

PaigeMiller
Diamond | Level 26

I will throw this question over to @Rick_SAS to see if he has any ideas why you are getting an error.

--
Paige Miller
superbug
Quartz | Level 8

@PaigeMiller 

Thanks!

I'll keep you updated once I hear from him. 

 

Rick_SAS
SAS Super FREQ

You can either use a TYPE=CORR or a TYPE=COV matrix as an input matrix to PROC SIMNORMAL. Here is both ways:

 

data outcorr(type=CORR);
input _TYPE_ $ 1-4 _NAME_ $ 5-6 a b;
datalines ;
MEAN    0      0
STD     0.1225 2
CORR a  1      0.8
CORR b  0.8    1
run;

proc simnormal data=work.outcorr outsim=SimMVN
               numreal = %sysevalf(&N*&NumSamples) 
               seed = 12345;          /* random number seed */
   var a b;
run;

Or (the harder way, because you have to form the covariance matrix yourself😞

data outcorr(type=COV);
input _TYPE_ $ 1-4 _NAME_ $ 5-6 a b;
datalines ;
MEAN    0      0
COV  a  0.015  0.196
COV  b  0.196  4
run;

%let N = 60;            /* sample size */
%let NumSamples = 100;  /* number of samples */
proc simnormal data=work.outcorr outsim=SimMVN
               numreal = %sysevalf(&N*&NumSamples) 
               seed = 12345;          /* random number seed */
   var a b;
run;

 

I want to make sure that the OP wants bivariate normal data. The initial post states

a ~ (0, 0.1225)

b ~ (0,2)

I do not know what that notation means. People seem to be interpreting it as

a ~ N(0, 0.1225)

b ~ N(0,2)

which seems likely, but worth verifying with the instructor of the class.

 

superbug
Quartz | Level 8

@Rick_SAS 

Thank you SOOO much for your quick response!

Yes, your code worked. The standard expression should be the following as you pointed out

a ~ N(0, 0.1225)

b ~ N(0, 2)

Could you please explain how did you get the numbers in the covariance matrix as following? 

COV  a  1.915  0.3873
COV  b  0.3873 4.321

 

Rick_SAS
SAS Super FREQ

I'm very sorry. That was a mistake on my part because I copied/pasted from another source.

From your follow-up post, I assume that N(0,2) means "mean=0 and stdDev=2." If so, the correct covariance matrix should be

COV  a  0.015  0.196
COV  b  0.196  4

which is the matrix in the second part of the problem. I will edit my solution. Thanks.

 

superbug
Quartz | Level 8

@Rick_SAS 

Thank you!

when I changed it to

COV  a  0.015  0.196
COV  b  0.196  4

 

and run the code, it has the following error message

superbug_0-1617818674360.png

 

superbug
Quartz | Level 8

@Rick_SAS 

Please ignore my last message. the error message resolved.

I very much appreciate your help and expertise. Again thanks much! 

PaigeMiller
Diamond | Level 26

@Rick_SAS wrote:

Here is the correct DATA step to create the input matrix for PROC SIMNORMAL:

data outcorr(type=COV);
input _TYPE_ $ 1-4 _NAME_ $ 5-6 a b;
datalines ;
MEAN    0      0
STD     0.1225 2
COV  a  1.915  0.3873
COV  b  0.3873 4.321
run;

%let N = 60;            /* sample size */
%let NumSamples = 100;  /* number of samples */
proc simnormal data=work.outcorr outsim=SimMVN
               numreal = %sysevalf(&N*&NumSamples) 
               seed = 12345;          /* random number seed */
   var a b;
run;

I want t make sure that the OP wants bivariate normal data. The initial post states

a ~ (0, 0.1225)

b ~ (0,2)

I do not know what that notation means. People seem to be interpreting it as

a ~ N(0, 0.1225)

b ~ N(0,2)

which seems likely, but worth verifying with the instructor of the class.

 


Well, that seems like an obvious error to me, now that you have pointed it out. COV matrix not CORR matrix. Thanks, @Rick_SAS 

--
Paige Miller
Reeza
Super User
Do you have SAS IML? If so, this could this be considered a multivariate normal distribution?
https://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution.html
Note the bottom note about using a correlation matrix instead of a covariance matrix.
superbug
Quartz | Level 8

@Reeza 

Thanks much!

I don't have SAS IML.

superbug_3-1617804835704.png

I don't know how to do this step as well.

 

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 17 replies
  • 993 views
  • 11 likes
  • 5 in conversation