<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Simulate bivariate normal data for multiple Sample size for two groups in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647082#M5095</link>
    <description>&lt;P&gt;Great! Many thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 12 May 2020 13:10:11 GMT</pubDate>
    <dc:creator>SWEETSAS</dc:creator>
    <dc:date>2020-05-12T13:10:11Z</dc:date>
    <item>
      <title>Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647061#M5090</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I am trying to simulate bivariate normal data for two groups with different covariance matrix. I need to generate this data for different sample sizes, e.g., sample=20, 40 etc. and 2000 replicates. I want the ratio of sample size in group 1: group 2 to be 1:2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the code I have for one group. For some reasons, the CREATE statement is creating two separate MVN datasets, and the last datasets overrides the initial one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help will be appreciated.&lt;/P&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;proc iml;&lt;BR /&gt;Numsamples=10;&lt;BR /&gt;/*specify population mean and covariance:grp1*/&lt;BR /&gt;mean1={6.0 6.0};&lt;BR /&gt;Cov1={0.5280563&amp;nbsp; 0.502445,&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.502445&amp;nbsp;&amp;nbsp; 0.5280563&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;/*specify population mean and covariance:grp2*/&lt;BR /&gt;mean2={6.2499 5.7399};&lt;BR /&gt;Cov2={0.6280563&amp;nbsp; 0.200978,&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.200978&amp;nbsp;&amp;nbsp; 0.401956&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;BR /&gt;&amp;nbsp;call randseed(132);&lt;BR /&gt;&amp;nbsp;do N=5 to 10 by 5;&lt;BR /&gt;&amp;nbsp;X=RandNormal(N*Numsamples,Mean2,Cov2);&lt;BR /&gt;&amp;nbsp;ID=colvec(repeat(T(1:Numsamples),1,N));&lt;BR /&gt;&amp;nbsp;Z=ID||X;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;create MVN from Z[c={"ID" "y0" "y1" }];&lt;BR /&gt;&amp;nbsp;append from Z;&lt;BR /&gt;&amp;nbsp;*end;&lt;BR /&gt;&amp;nbsp;close MVN;&lt;BR /&gt;&amp;nbsp;end;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;quit;&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 12 May 2020 11:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647061#M5090</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-05-12T11:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647062#M5091</link>
      <description>&lt;P&gt;Yes, each time it creates a data set named MVN (the exact same name each time, so it overwrites the previous version of MVN). That's how the program is written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This article from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;explains how you can overcome this&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2015/02/09/array-of-matrices.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2015/02/09/array-of-matrices.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 11:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647062#M5091</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-12T11:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647073#M5092</link>
      <description>&lt;P&gt;As Paige says you are overwritting the same data set in the loop.&amp;nbsp; An alternative would be to build one data set with successive appends as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create MVN var {"N" "ID" "y0" "y1" };

do N=5 to 10 by 5;
 X=RandNormal(N*Numsamples,Mean2,Cov2);
 ID=colvec(repeat(T(1:Numsamples),1,N));
 Z = j(nrow(X),1,N)||ID||X;
 append from Z;
end;
  
close MVN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have added the loop variable N to the output data set which you can use in WHERE or BY statements in other SAS PROCs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 12:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647073#M5092</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2020-05-12T12:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647074#M5093</link>
      <description>&lt;P&gt;It looks like you've already read the article &lt;A href="https://blogs.sas.com/content/iml/2013/04/10/generate-multiple-mvn-samples.html" target="_self"&gt;"How to generate multiple samples from the multivariate normal distribution in SAS."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For simulation studies, it can be convenient to &lt;A href="https://blogs.sas.com/content/iml/2019/05/13/write-results-inside-iml-loop.html" target="_self"&gt;write each sample to a data set from within a SAS/IML loop.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;So put the CREATE statement before the loop and the CLOSE statement after the loop. Inside the loop, you create both samples and then use the APPEND statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear to me how you want a 1:2 ratio when the sample size is not divisible by 3. For example, when N = 20, do you want 6 and 14 as the sample sizes, or do you want 20 and 40. In the following program, I've used the second option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect you will also need a second ID variable to identify which observations come from the first distribution and which from the second. I called that the GROUP variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
Numsamples=10;
/*specify population mean and covariance:grp1*/
mean1={6.0 6.0};
Cov1={0.5280563  0.502445,
      0.502445   0.5280563
  };
/*specify population mean and covariance:grp2*/
mean2={6.2499 5.7399};
Cov2={0.6280563  0.200978,
      0.200978   0.401956
  };
call randseed(132);

Z = {. . . .};    /* tell IML Z is numeric */
create MVN from Z[c={"Group" "ID" "y0" "y1" }];

do N=5 to 10 by 5;
   N1 = N;      /* or N1 = floor(N/3); ? */
   X=RandNormal(N1*Numsamples,Mean2,Cov2);
   ID=colvec(repeat(T(1:Numsamples),1,N1));
   Group = j(nrow(ID), 1, 1);  
   Z=Group||ID||X;
   append from Z;

   N2 = 2*N;    /* or N2 = N - N1; ? */
   X=RandNormal(N2*Numsamples,Mean2,Cov2);
   ID=colvec(repeat(T(1:Numsamples),1,N2));
   Group = j(nrow(ID), 1, 2);  
   Z=Group||ID||X;
   append from Z;
end;

close MVN;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 May 2020 12:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647074#M5093</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-05-12T12:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647081#M5094</link>
      <description>&lt;P&gt;Excellent!! Many thanks! Yes, I did read the referenced resource.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to add column for sample size (N) to Allow analysis by ID N?&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 13:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647081#M5094</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-05-12T13:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647082#M5095</link>
      <description>&lt;P&gt;Great! Many thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 13:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647082#M5095</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-05-12T13:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Simulate bivariate normal data for multiple Sample size for two groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647098#M5096</link>
      <description>&lt;P&gt;Yes, of course. Just add an additional column to Z. You'll want to modify the Z= assignments and the CREATE statement.&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 13:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulate-bivariate-normal-data-for-multiple-Sample-size-for-two/m-p/647098#M5096</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-05-12T13:58:54Z</dc:date>
    </item>
  </channel>
</rss>

