<?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: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517670#M4458</link>
    <description>Thank you Ksharp</description>
    <pubDate>Fri, 30 Nov 2018 21:16:23 GMT</pubDate>
    <dc:creator>SimRock</dc:creator>
    <dc:date>2018-11-30T21:16:23Z</dc:date>
    <item>
      <title>Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516552#M4452</link>
      <description>&lt;P&gt;Hi, I have been trying to simulate some correlated data in SAS Proc IML but I am having some troubles. Any help is appreciated. I saw the post by Rick Wicklin (&lt;SPAN class="login-bold"&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684" target="_self"&gt;Rick_SAS&lt;/A&gt;). I tried to modify the code provided here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Statistical-Procedures/Generate-correlated-random-variables-that-follow-beta/td-p/204878" target="_blank"&gt;https://communities.sas.com/t5/Statistical-Procedures/Generate-correlated-random-variables-that-follow-beta/td-p/204878&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;but I have only had little success. Any help would be greatly appreciated. Here's my code below. By I am having some error messages.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;I am trying to simulate the variable Age&amp;nbsp; (continuous) and Male (binary) using their means, standard deviation, and covariance as seen in&amp;nbsp;below&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;SAS Output&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Covariance Matrix, DF = 529&amp;nbsp; AGE MALEAGEMALE &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0.0689125085&lt;/TD&gt;&lt;TD&gt;0.0015515212&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.0015515212&lt;/TD&gt;&lt;TD&gt;0.2501159183&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;SAS Output&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Simple StatisticsVariable N Mean Std Dev Sum Minimum MaximumAGEMALE &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;530&lt;/TD&gt;&lt;TD&gt;11.23208&lt;/TD&gt;&lt;TD&gt;0.26251&lt;/TD&gt;&lt;TD&gt;5953&lt;/TD&gt;&lt;TD&gt;10.50000&lt;/TD&gt;&lt;TD&gt;12.00000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;530&lt;/TD&gt;&lt;TD&gt;0.48113&lt;/TD&gt;&lt;TD&gt;0.50012&lt;/TD&gt;&lt;TD&gt;255.00000&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1.00000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS Output&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Pearson Correlation Coefficients, N = 530Prob &amp;gt; |r| under H0: Rho=0&amp;nbsp; AGE MALEAGEMALE &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1.00000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;TD&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0.01182&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.7861&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0.01182&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.7861&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;TD&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1.00000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Here's the code I was trying to write but there some errors&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	call randseed(1);
	mu_age=11.23208;
  	sigma_age=0.26251;
 	p_male=0.48113;
	N = 1000;
	Mean = {11.23208,	0.48113};
	Cov = 	{0.0689125085	0.0015515212, 
			0.0015515212	0.2501159183};


  Z = RandNormal(N, Mean, Cov);
  

  U = cdf("Normal", Z);  

  Age= quantile("normal", U[,1], mu_age, sigma_age);

  Male= quantile("binomial", U[,2], p_male,1);

  mean_Age=mean(Age);
  std_Age=std(Age);
  mean_Male=mean(Male);
  covZ = cov(Z);
  covZ1 =cov(Age, Male);
  
 print mean_Age std_Age, mean_Male covZ covz1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am planning on getting the book Simulating data with by Rick Wicklin, but I am hoping someone could help me in the meantime&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 03:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516552#M4452</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2018-11-28T03:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516555#M4453</link>
      <description>Try this reference instead: &lt;A href="https://blogs.sas.com/content/iml/2013/04/10/generate-multiple-mvn-samples.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/04/10/generate-multiple-mvn-samples.html&lt;/A&gt;</description>
      <pubDate>Wed, 28 Nov 2018 03:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516555#M4453</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-28T03:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516808#M4454</link>
      <description>&lt;P&gt;Thank you, Reeza. But I couldn't find how to simulate the variables from the quantile distribution&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 17:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/516808#M4454</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2018-11-28T17:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517048#M4455</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;wrote a blog about simulating data from its quantiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my code .Also check Rick's blog.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let n=10000;
proc iml;
/* quantiles of total cholesterol from NHANES study
http://blogs.sas.com/content/iml/2014/06/18/distribution-from-quantiles.html
 */
Quantile = {0 , .01, .05, .10, .25, .50, .75, .90, .95, .99, 1};
Estimate = {80, 128, 151, 162, 184, 209, 236, 265, 284, 333, 727};
n=nrow(Quantile);
bin_value=j(n-1,1);
bin_width=j(n-1,1);
p=j(n-1,1);
do i=1 to n-1;
 bin_value[i]=Estimate[i];
 bin_width[i]=Estimate[i+1]-Estimate[i];
 p[i]=Quantile[i+1]-Quantile[i];
end;
print Quantile Estimate bin_value bin_width p;
call randseed(123456789);
_bin_value=sample(bin_value,&amp;amp;n,'replace',p);
_bin_width=j(&amp;amp;n,1);
do j=1 to &amp;amp;n;
 _bin_width[j]=bin_width[loc(bin_value=_bin_value[j])];
end;
eps=randfun(&amp;amp;n,'uniform');
value=_bin_value`+ceil(_bin_width#eps);
call histogram(value) label="Simulation" ;
call qntl(SimEst, value, Quantile);
print Quantile[F=percent6.] Estimate SimEst[F=5.1];
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Nov 2018 14:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517048#M4455</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-11-29T14:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517337#M4456</link>
      <description>&lt;P&gt;In your example, the variables are uncorrelated (p=0.7861), so you can use independent&amp;nbsp;normal and Bernoulli variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assume, however, you want to see how to simulate the data if the target correlation is larger, such as 0.3.&lt;/P&gt;
&lt;P&gt;See Chapter 9 in &lt;EM&gt;Simulating Data with SAS&lt;/EM&gt; (2013), the section on copulas. Although in the chapter I show how to use copulas for continuous distributions, the idea can apply to continuous-discrete distributions. The tricky part (as I discuss in the book) is that if your TARGET correlation is 0.3, you need to generate normal data from an "intermediate" distribution that has a larger correlation, such as 0.37. Here's some code&amp;nbsp;you can play around with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	call randseed(1);
	mu_age=11.23208;
  	sigma_age=0.26251;
 	p_male=0.48113;
	N = 1000;

/* let's try to simulate data that has correlation {1 0.3, 0.3 1} */
R = {1    0.37,  /* the intermediate correlation matrix must be inflated if you use Pearson corr */
     0.37 1};
Z = randnormal(N, {0 0}, R);

X1 = mu_age + sigma_age*Z[,1];           /* scale to N(mu, sigma) */
U2 = cdf("normal", Z[,2]);               /* ~ U(0,1) */
B2 = quantile("Bernoulli", U2, 0.48113); /* ~ Bern(p=0.48) */
X = X1 || B2;

mean = mean(X);
corr = corr(X);
cov = cov(X);
print mean, corr, cov;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2018 03:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517337#M4456</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-11-30T03:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517669#M4457</link>
      <description>&lt;P&gt;Thank you so much, Rick. It worked like a charm.&lt;/P&gt;&lt;P&gt;Two follow-up questions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Is there a particular reason why we should use the correlation matrix instead of the covariance matrix for these types of copula simulations especially since the argument for &lt;CODE class="command function"&gt;RANDNORMAL&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;(N, Mean, Cov )&amp;nbsp; asks for a covariance? Any clarification would be great&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2) In the subsequent simulations after generating the randnormal, could we interchange the Z[,1] and Z[,2] or does SAS know that the first Z[,1] is for Age and Z[,2] for the other variable Male?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you so much&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2018 21:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517669#M4457</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2018-11-30T21:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517670#M4458</link>
      <description>Thank you Ksharp</description>
      <pubDate>Fri, 30 Nov 2018 21:16:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517670#M4458</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2018-11-30T21:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517715#M4459</link>
      <description>&lt;P&gt;1. No reason, other than my preference.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. You can use the first column to simulate age, if you prefer.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Dec 2018 03:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517715#M4459</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-12-01T03:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Simulating normal and Bernoulli multivariate distribution from correlated data in Proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517813#M4461</link>
      <description>Thank you very much, Rick</description>
      <pubDate>Sun, 02 Dec 2018 05:52:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Simulating-normal-and-Bernoulli-multivariate-distribution-from/m-p/517813#M4461</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2018-12-02T05:52:29Z</dc:date>
    </item>
  </channel>
</rss>

