BookmarkSubscribeRSS Feed
thanoon
Calcite | Level 5

hi everyone

i have a problem with simulation using sas/iml and i cannot get on data just x1-x10 without data anyone help me please to correct this program.

thanks alot

%include "RandMVOrd.sas";

proc iml;

load module=_all_;                     /* load the modules */

    /* P1    P2     P3       p4     p5      p6       p7      p8     p9      p10 */

P = {0.35 0.50 0.20 0.10   0.10   0.30    0.20 0.25 0.15 0.20,

     0.30 0.10 0.20 0.30   0.20   0.20    0.40 0.05 0.45    0.10,

     0.15 0.20    0.35 0.25   0.50   0.20    0.25 0.55 0.10 0.10,

     0.20 0.20 0.25 0.35   0.20   0.30    0.15 0.15 0.30    0.60};

/* expected values and variance for each ordinal variable */

Expected = Mean(P) // Var(P);

varNames = "X1":"X10";

print Expected[r={"Mean" "Var"} c=varNames];

/* test the RandMVOrd function */

Delta = {1  0.45 0.4   0.35 0.3    0.25 0.2    0.15 0.1    0.05,

        0.45 1 0.45 0.4    0.35 0.3    0.25 0.2    0.15 0.1,

        0.4  0.45 1   0.45    0.4    0.35 0.3    0.25 0.2    0.15,

        0.35  0.4 0.45 1    0.45 0.4    0.35 0.3    0.25 0.2,

        0.3  0.35 0.4   0.45     1    0.45 0.4    0.35 0.3    0.25,

        0.25  0.3 0.35 0.4    0.45   1    0.45 0.4    0.35 0.3,

        0.2  0.25 0.3   0.35    0.4    0.45 1    0.45 0.4    0.35,

        0.15  0.2 0.25 0.3   0.35 0.4    0.45   1    0.45 0.4,

        0.1  0.15 0.2   0.25   0.3    0.35 0.4    0.45   1    0.45,

        0.05  0.1 0.15 0.2 0.25 0.3    0.35 0.4   0.45     1};

call randseed(54321);

X = RandMVOrdinal(1000, P, Delta);

/* print results */

varNames = ('x1':'x10');

create MVO var {X1 X2 X3 X4 X5 X6 X7 X8 X9 X10};  append from var;  close MVO; /* write data */

run;              

26 REPLIES 26
thanoon
Calcite | Level 5

Hi

I am waiting anyone help me to correct the previous program please.

Regards

Rick_SAS
SAS Super FREQ

As I commented in another thread (https://communities.sas.com/thread/52019), it is extremely hard to invent means and covariances that are valid.  Try using smaller correlations. The following program works:

d = 1 || do(0.4, 0.04, -0.04);
Delta = toeplitz(d);

v = {0.05 0.05 0.05 0.05 0.10 0.10 0.10 0.10 0.2 0.20}`;
P = j(10,10);
P[,1] = v;
call randseed(1);
do i = 2 to 10;
   P[,i] = v[ranperm(10)];
end;
print P, Delta;
X = RandMVOrdinal(1000, P, Delta);

thanoon
Calcite | Level 5

thank you so much for your help i dont have any problem when i used small number of variables but in my research i need to simulate at least 10 variables so i forgot to put quotation marks in include statement after that i got on a results. thanks alot

thanoon
Calcite | Level 5

HI

when i simulating data i found some outliers but i dont write any condition to put outliers in data how can i solve this problem.

thanks in advance

Rick_SAS
SAS Super FREQ

If by "outlier" you mean "an observation from an area in which the probability density is low," then this isn't necessarily a problem. You expect to get a few points in those regions. For example, if you simulate 1 million observations from a univariate standard normal distribution, you should expect to get 63 observations such that |x|>4:

P = 2*1e6*Cdf("Normal", -4);

print P;

They are outliers, yes, but they are supposed to be there.  The same is true for other distributions.  It is only a "problem" if you are observing these low-probability regions more often than theory predicts.

thanoon
Calcite | Level 5

hi

if i want to simulate multi sample data can i just change the rand seed for each one.

thanks in advance

Rick_SAS
SAS Super FREQ

If you want to generate many samples in one program, you can use a loop or just generate one long matrix:

/* loop approach */

NumSamples = 10;

call randseed(54321);

do i = 1 to NumSamples;

   X = RandMVOrdinal(1000, P, Delta);

  /* do something with each sample  */

end;

/* or */

/* each block of 1000 is a sample */

X = RandMVOrdinal(NumSamples*1000, P, Delta);

thanoon
Calcite | Level 5

thanks dr. rick

i want to simulate different samples like different cultures or different countries.

thanks alot

thanoon
Calcite | Level 5

this code is ok now ?

proc iml;

load module=_all_;                     /* load the modules */

    /* P1    P2     P3       p4     p5      p6       p7      p8     p9      p10 */

P = {0.35 0.50 0.20 0.10   0.10   0.30    0.20 0.25 0.15 0.20,

     0.30 0.10 0.20 0.30   0.20   0.20    0.40 0.05 0.45    0.10,

     0.15 0.20    0.35 0.25   0.50   0.20    0.25 0.55 0.10 0.10,

     0.20 0.20 0.25 0.35   0.20   0.30    0.15 0.15 0.30    0.60};

/* expected values and variance for each ordinal variable */

Expected = Mean(P) // Var(P);

varNames = "X1":"X10";

print Expected[r={"Mean" "Var"} c=varNames];

/* test the RandMVOrd function */

Delta = {1  0.45 0.4   0.35 0.3    0.25 0.2    0.15 0.1    0.05,

        0.45 1 0.45 0.4    0.35 0.3    0.25 0.2    0.15 0.1,

        0.4  0.45 1   0.45    0.4    0.35 0.3    0.25 0.2    0.15,

        0.35  0.4 0.45 1    0.45 0.4    0.35 0.3    0.25 0.2,

        0.3  0.35 0.4   0.45     1    0.45 0.4    0.35 0.3    0.25,

        0.25  0.3 0.35 0.4    0.45   1    0.45 0.4    0.35 0.3,

        0.2  0.25 0.3   0.35    0.4    0.45 1    0.45 0.4    0.35,

        0.15  0.2 0.25 0.3   0.35 0.4    0.45   1    0.45 0.4,

        0.1  0.15 0.2   0.25   0.3    0.35 0.4    0.45   1    0.45,

        0.05  0.1 0.15 0.2 0.25 0.3    0.35 0.4   0.45     1};

/* loop approach */

NumSamples = 10;

call randseed(54321);

do i = 1 to NumSamples;

   X = RandMVOrdinal(1000, P, Delta);

  /* do something with each sample  */

/* print results */

c=varNames;

create MVO from X[colname=c];  append from X;  close MVO;

quit;

because i receive this warning

WARNING: DO group statements were not executed due to missing END at the time of QUIT.

thanoon
Calcite | Level 5

dear dr.

i still have some errors after putting the previous code can you correct it to me please

proc iml;

load module=_all_;                     /* load the modules */

    /* P1    P2     P3       p4     p5      p6       p7      p8     p9      p10 */

P = {0.35 0.50 0.20 0.10   0.10   0.30    0.20 0.25 0.15 0.20,

     0.30 0.10 0.20 0.30   0.20   0.20    0.40 0.05 0.45    0.10,

     0.15 0.20    0.35 0.25   0.50   0.20    0.25 0.55 0.10 0.10,

     0.20 0.20 0.25 0.35   0.20   0.30    0.15 0.15 0.30    0.60};

/* expected values and variance for each ordinal variable */

Expected = Mean(P) // Var(P);

varNames = "X1":"X10";

print Expected[r={"Mean" "Var"} c=varNames];

/* test the RandMVOrd function */

Delta = {1  0.45 0.4   0.35 0.3    0.25 0.2    0.15 0.1    0.05,

        0.45 1 0.45 0.4    0.35 0.3    0.25 0.2    0.15 0.1,

        0.4  0.45 1   0.45    0.4    0.35 0.3    0.25 0.2    0.15,

        0.35  0.4 0.45 1    0.45 0.4    0.35 0.3    0.25 0.2,

        0.3  0.35 0.4   0.45     1    0.45 0.4    0.35 0.3    0.25,

        0.25  0.3 0.35 0.4    0.45   1    0.45 0.4    0.35 0.3,

        0.2  0.25 0.3   0.35    0.4    0.45 1    0.45 0.4    0.35,

        0.15  0.2 0.25 0.3   0.35 0.4    0.45   1    0.45 0.4,

        0.1  0.15 0.2   0.25   0.3    0.35 0.4    0.45   1    0.45,

        0.05  0.1 0.15 0.2 0.25 0.3    0.35 0.4   0.45     1};

/* loop approach */

NumSamples = 2;

call randseed(54321);

do i = 1 to NumSamples;

   X = RandMVOrdinal(500, P, Delta);

  /* do something with each sample  */

   end;

/* print results */

c=varNames;

create MVO from X[colname=c];  append from X;  close MVO;

quit;

many thanks

Rick_SAS
SAS Super FREQ

Every time you exit SAS, you need to say

%include "RandMVOrd.sas";

again.

thanoon
Calcite | Level 5

thanks alot dr. wicklin

i got on result but the results are one sample only i need 2 samples (means two different data) and when i changed number of samples in sas program i got on 1 sample also  how can i simulate two different sample such n=500 , n1=250 , n2=250.

thanks again

thanoon
Calcite | Level 5

hi

i still wait anyone tell me how can i get on a two different data by using the above command.

regards

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 26 replies
  • 2051 views
  • 0 likes
  • 3 in conversation