<?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: Random sampling from with covariance in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956408#M42968</link>
    <description>&lt;P&gt;The first step for any data simulation is to write down the model from which you want to generate the data. You must do this first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like you might be modeling these data as a repeated-measures model where a random effect dictates the intercept and slope for each individual?&lt;/P&gt;
&lt;P&gt;Several sources show how to simulate from mixed models in SAS. I suggest you start by reading&amp;nbsp;Gibbs and Kiernan (2020), &lt;A href="https://support.sas.com/resources/papers/proceedings20/4400-2020.pdf" target="_self"&gt;"Simulating Data for Complex Linear Models."&lt;/A&gt;&amp;nbsp;That paper discusses several mixed models and how to simulate them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have real data, you might already have an appropriate model that you have fit by using a SAS procedure. If so, the output of the SAS procedure provides estimates of the parameters and variance terms that are necessary to build a simulation that reflects your real data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After you know the model from which you wish to simulate, write back if you need help with the actual simulation step.&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jan 2025 10:50:35 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2025-01-17T10:50:35Z</dc:date>
    <item>
      <title>Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939579#M42204</link>
      <description>&lt;P&gt;Hello everyone SAS!&lt;BR /&gt;&lt;BR /&gt;Please could you help me to do problem on random sampling.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;How do I sample a quadruplet of values from the distribution shown in figure? Note that two samples (in blue) demonstrate covariance. Namely if x1 is high in distribution (u1,s1), then x2, x3 and x4 will also be high in the respective distribution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all!&lt;BR /&gt;ll87&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="figure.png" style="width: 567px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99343iDA1E5BF9CA4C0DFD/image-size/large?v=v2&amp;amp;px=999" role="button" title="figure.png" alt="figure.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2024 10:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939579#M42204</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2024-08-16T10:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939583#M42205</link>
      <description>&lt;P&gt;Most of us, including me, will not download Microsoft Office documents, they can be a security threat. Please include screen captures of what you want to show us by clicking on the "Insert Photos" icon. Do NOT attach files.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 879px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91641i24282B42CFE94122/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2024 09:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939583#M42205</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-16T09:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939590#M42206</link>
      <description>Thank you PaigeMiller. I added plot as you suggest</description>
      <pubDate>Fri, 16 Aug 2024 10:18:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939590#M42206</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2024-08-16T10:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939591#M42207</link>
      <description>&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2024 10:25:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/939591#M42207</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-16T10:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942446#M42303</link>
      <description>&lt;P&gt;Thank you PaigeMiller I appreciate! &lt;BR /&gt;&lt;BR /&gt;But look at this picture how does the sampling you suggest give me the cyan line? Or lower blue line? I want these quadruplet points from sampling. The sampling you suggests, give me the distribution but not give me a vector of 4 values at values x1, x2, x3, x4.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Hope this clear and thanking you for help!&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="figure.png" style="width: 567px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99343iDA1E5BF9CA4C0DFD/image-size/large?v=v2&amp;amp;px=999" role="button" title="figure.png" alt="figure.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 10:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942446#M42303</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2024-09-04T10:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942449#M42304</link>
      <description>&lt;P&gt;Please explain in detail what the cyan line and the blue line represent.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 11:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942449#M42304</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-04T11:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942473#M42307</link>
      <description>&lt;P&gt;The line with 4 points represents data from a single person. The subject has an outcome (y) measured at fixed concentrations (x1, x2, x3, x4). So for a subject you have pairs of points (x1,y1)... (x4,y4) for a subject. I basically want to randomly sample a subjects data [(x1,y1)...(x4,y4)]. If the subject has high response y1, it tends to stay high for all concentrations (as picture shows).&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 14:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942473#M42307</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2024-09-04T14:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942481#M42308</link>
      <description>&lt;P&gt;So for a subject, the four Y values are measured, the four X values are fixed. It seems as if the Y values are correlated with the X values, am I right about that? Are the Ys correlated with each other beyond the correlation with X?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 14:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942481#M42308</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-04T14:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942663#M42312</link>
      <description>&lt;P&gt;Yes the Xi are correlated with Yi. And the Yi's are correlated with one another.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 08:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942663#M42312</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2024-09-05T08:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942672#M42314</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes the Xi are correlated with Yi. And the Yi's are correlated with one another.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This really doesn't answer the question. Are the Yi's correlated with one another because of their relationship to X? Or is there additional correlation amongst the Yi's that have nothing to do with X?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 10:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/942672#M42314</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-05T10:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956346#M42963</link>
      <description>&lt;P&gt;The Yi are correlated with one another because each individual person has a unique response profile over the Xs. If that answer question? So it have nothing to do with X really, it is just variation in the strength of the response. But currently sampling approach does not account for this.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 21:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956346#M42963</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2025-01-16T21:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956408#M42968</link>
      <description>&lt;P&gt;The first step for any data simulation is to write down the model from which you want to generate the data. You must do this first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like you might be modeling these data as a repeated-measures model where a random effect dictates the intercept and slope for each individual?&lt;/P&gt;
&lt;P&gt;Several sources show how to simulate from mixed models in SAS. I suggest you start by reading&amp;nbsp;Gibbs and Kiernan (2020), &lt;A href="https://support.sas.com/resources/papers/proceedings20/4400-2020.pdf" target="_self"&gt;"Simulating Data for Complex Linear Models."&lt;/A&gt;&amp;nbsp;That paper discusses several mixed models and how to simulate them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have real data, you might already have an appropriate model that you have fit by using a SAS procedure. If so, the output of the SAS procedure provides estimates of the parameters and variance terms that are necessary to build a simulation that reflects your real data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After you know the model from which you wish to simulate, write back if you need help with the actual simulation step.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2025 10:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956408#M42968</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-01-17T10:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling from with covariance</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956448#M42975</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;But currently sampling approach does not account for this.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you referring to &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892" target="_blank" rel="noopener"&gt;PaigeMiller&lt;/A&gt;'s suggestion of sampling from the multivariate normal distribution? I think that this model could have the characteristics shown in your sample plot. Below&amp;nbsp;is an example using only Base SAS and SAS/STAT (as I don't have a SAS/IML license). But, really, you should follow &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684" target="_blank" rel="noopener"&gt;Rick_SAS&lt;/A&gt;'s advice and precisely define the statistical model you are starting with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Define mean vector and covariance matrix */

data cov(type=COV);
input _type_ $ _name_ $ y1-y4;
datalines;
COV    y1  0.14   0.158  0.167  0.183
COV    y2  0.158  0.22   0.235  0.263
COV    y3  0.167  0.235  0.31   0.351
COV    y4  0.183  0.263  0.351  0.49
MEAN   .   1      1.72   2.42   3.52
;

/* Simulate data from the corresponding 4-dimensional normal distribution */

proc simnorm data=cov outsim=want numreal=100000 seed=2718;
var y1-y4;
run;

/* Prepare density plot */

proc transpose data=want out=trans;
by rnum;
run;

/* Prepare annotation to display the data of two simulated vectors:
   rnum=46651 (representative of the  5% quantile of y1),
   rnum=16395 (representative of the 95% quantile of y1). */

%sganno

data sgannodata;
%sgoval(x1=24.0, y1=76.1, height=2, width=2.6, linecolor="CX3F48CC", fillcolor="CX3F48CC", display="fill");
%sgoval(x1=31.3, y1=52.2);
%sgoval(x1=38.4, y1=28.3);
%sgoval(x1=50.7, y1=4.4);
%sgline(x1=24.0, x2=31.3, y1=76.1, y2=52.2, linethickness=1);
%sgline(x1=31.3, x2=38.4, y1=52.2, y2=28.3);
%sgline(x1=38.4, x2=50.7, y1=28.3, y2=4.4);
%sgoval(x1=38.1, y1=76.1, height=2, width=2.6, linecolor="CX00A2E8", fillcolor="CX00A2E8", display="fill");
%sgoval(x1=47.0, y1=52.2);
%sgoval(x1=56.0, y1=28.3);
%sgoval(x1=69.0, y1=4.4);
%sgline(x1=38.1, x2=47.0, y1=76.1, y2=52.2, linethickness=1);
%sgline(x1=47.0, x2=56.0, y1=52.2, y2=28.3);
%sgline(x1=56.0, x2=69.0, y1=28.3, y2=4.4);
run;

/* Create the annotated density plot */

proc sgpanel data=trans sganno=sgannodata noautolegend;
panelby _name_ / rows=4 columns=1 onepanel novarname;
colaxis display=(nolabel);
density col1 / type=kernel lineattrs=(color=black);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Density_panel_plot.png" style="width: 480px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103774iAFF2815A25779C86/image-size/large?v=v2&amp;amp;px=999" role="button" title="Density_panel_plot.png" alt="Density_panel_plot.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2025 17:33:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Random-sampling-from-with-covariance/m-p/956448#M42975</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-01-17T17:33:09Z</dc:date>
    </item>
  </channel>
</rss>

