SAS中文论坛

标题: A Grad Student ‘How-To’ Paper on Grant Prep: Preliminary Dat [打印本页]

作者: shiyiming    时间: 2011-4-30 14:38
标题: A Grad Student ‘How-To’ Paper on Grant Prep: Preliminary Dat
From LCChien's blog on blogspot

原文載點:http://support.sas.com/resources/papers/proceedings10/274-2010.pdf<br /><br />SAS 語法百百種,身為研究生的你/妳,有沒有想過有什麼是一定要學起來並且用在報告或論文上面?這篇技術文件整合了許多你一定得用在報告和論文的SAS程式!<br /><br /><a name='more'></a><b>1. PRELIMINARY ANALYSIS I &nbsp;</b><br /><br /><b>Code1.1:將變數製作成聚集變數</b><br />這一步驟是用於一次要算幾百個變數的平均數的研究,讓使用者免去一個個輸入變數名稱的痛苦。但如果你只要算兩三個變數,那這段可以跳過。此程式所有的變數會被 PROC SQL 程序定義在一個名為 LIST 的巨集變數裡面。<br /><pre><code>PROC SQL noprint;<br />&nbsp;select distinct NAME<br />&nbsp;into :LIST<br />&nbsp;separated by " "<br />&nbsp;from work.cells_1;<br />quit;</code></pre><b>Code1.2:計算平均數並用ODS OUTPUT輸出</b><br />此例的變數名稱就直接用 &amp;LIST 把上面整合好的變數名稱一次叫進來。同樣地,如果你只有少量變數,那直接輸入即可。此程式會將算好的基本統計量另存在 basic_measures 的新資料集裡面。<br /><pre><code>ODS OUTPUT BASICMEASURES=work.basic_measures;<br />proc univariate data=work.&amp;input.;<br />class day;<br />var &amp;LIST.;<br />run;<br />ODS OUTPUT CLOSE;</code></pre><b>Code1.3:重整結果以生成繪圖所需的資料</b><br />重新編輯資料集 basic_measures 以算出額外兩個新變數 High 和 Low。這兩個變數被定義為平均數正負一個變異數的大小。但我建議正規的作法應該要去算 95% 信賴區間比較好。這部份各位可以自行修改。<br /><pre><code>data work.basic_measures_all;<br />set work.basic_measures (rename=( LocValue=Mean));<br />if LOCMEASURE in ("Mean");<br />High=mean+varvalue;<br />Low=mean-varvalue;<br />Keep varname day mean high low;<br />run;</code></pre>做出來的繪圖資料如下圖所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/1911325897.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/1911325897.jpg" /></a></div><b>Code1.4:將變數和標籤製作成聚集變數</b><br />這一段是要將剛剛做出來的繪圖資料裡面的變數以及標籤製作成巨集變數以用於繪圖程式當中。如果你打算手動輸入,此段也可以省略。<br /><pre><code>proc sql noprint;<br />&nbsp;&nbsp;select NAME as VAR into:VAR1- :VAR&amp;SYSMAXLONG.<br />&nbsp;&nbsp;from work.cells_1;<br />&nbsp;&nbsp;quit;<br />proc sql noprint;<br />&nbsp;&nbsp;select LABEL as LABEL into:LABEL1- :LABEL&amp;SYSMAXLONG.<br />&nbsp;&nbsp;from work.cells_1;<br />&nbsp;&nbsp;quit;</code></pre><b>Code1.5:繪圖並輸出</b><br />用 PROC SGPLOT 進行曲線圖的製作,並將結果用 ODS RTF 輸出到外部文件檔案。如果你想要製作別種圖,那就需要對 PROC SGPLOT 部分進行調整。<br /><pre><code>ods output;<br />ods RTF file="C:\documents and settings\desktop\GRAPHS_MEANS.RTF" &nbsp;;<br />%do j=1 %to &amp;SQLOBS.;<br />data work.values;<br />set work.basic_measures_all;<br />if varname="&amp;&amp;VAR&amp;J.";<br />run;<br />proc sgplot data=work.values noautolegend;<br />&nbsp;xaxis type=discrete;<br />&nbsp;series x=day y=mean;<br />&nbsp;scatter x=day y=mean /<br />&nbsp;markerattrs=(size=0)<br />&nbsp;yerrorlower=low<br />&nbsp;yerrorupper=high;<br />run;<br />%end;</code></pre>製作出來的圖型如下:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/0461979911.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/0461979911.jpg" /></a></div><b>Code1.6:用 PROC MIXED 程序進行分析並估計所需的組間差異</b><br />此例由於是在做重複觀測值的模型估計,所以用到 PROC MIXED 程序。這部份可視使用者的情況來調整。<br /><pre><code>ods proclabel "&amp;&amp;VAR&amp;K. &amp;&amp;LABEL&amp;K.";<br />ods output &nbsp;Estimates=work.est &nbsp;SolutionF=work.sol tests3=work.tests3 ;<br />proc mixed data=work.mixed1 COVTEST cl NOCLPRINT NOITPRINT ;<br />&nbsp;&nbsp;class patient_id day_numeric;<br />&nbsp;&nbsp;model &amp;&amp;var&amp;k. =day_numeric /solution noint cl residual ;<br />&nbsp;&nbsp;repeated day_numeric / subject=patient_id type=ar(1) rcorr r;<br />&nbsp;&nbsp;estimate '-4 vs 0' &nbsp; &nbsp; &nbsp;day_numeric &nbsp; -1 1 0 0 0 0 0 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 4' &nbsp;day_numeric &nbsp; -0.5 -0.5 1 0 0 0 0 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 8' &nbsp;day_numeric &nbsp; -0.5 -0.5 0 1 0 0 0 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 12' day_numeric &nbsp; -0.5 -0.5 0 0 1 0 0 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 15' day_numeric &nbsp; -0.5 -0.5 0 0 0 1 0 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 16' day_numeric &nbsp; -0.5 -0.5 0 0 0 0 1 0;<br />&nbsp;&nbsp;estimate '-4 &amp; 0 vs 22' day_numeric &nbsp; -0.5 -0.5 0 0 0 0 0 1;<br />run;<br />quit;<br />ods output close;</code></pre><br /><b>Code1.7:格式化結果</b><br />這段只是單純把上面用 PROC MIXED 跑出來的結果整理一下。<br /><pre><code>data work.est;<br />length variable $32 variable_label $32 variable_subset $32;<br />set work.est;<br />variable="&amp;&amp;var&amp;k.";<br />run;<br />PROC APPEND BASE=work.est_all_&amp;source. DATA=work.est force;<br />RUN;</code></pre>最後的表格就可以直接輸出到報告和論文上,如下所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/0261622099.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/0261622099.jpg" /></a></div><br /><br /><b>2. PRELIMINARY ANALYSIS II &nbsp;</b><br /><br /><b>Code2.1:繪製個人曲線圖</b><br />注意此處的巨集變數都是從 Code1.4 生成出來的。如果跳過那步驟的人,就自己手動輸入變數名稱吧!<br /><pre><code>axis1 label=(angle=90);<br />symbol i=j repeat=100 w=5;<br />proc gplot data=work.mixed2;<br />&nbsp;&nbsp; plot &amp;&amp;var&amp;i.*week_numeric=patient_id/vaxis=axis1 nolegend;<br />run;<br />quit;</code></pre><br />圖型如下所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/9198697077.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/9198697077.png" /></a></div><b>Code2.2:PROC SGPANAL</b><br />這段是在畫另一種圖,但我覺得不是很重要,可以省略。<br /><pre><code>proc sgpanel data=work.data_transpose_2;<br />&nbsp;&nbsp;where _NAME_ in ("&amp;&amp;VAR&amp;I.");<br />&nbsp;&nbsp;panelby _LABEL_/novarname ;<br />&nbsp;&nbsp;colaxis type=discrete values=(-7,0,1,3,7,10,14,21,28);<br />&nbsp;&nbsp;rowaxis label="log (Count)";<br />&nbsp;&nbsp;series x=day y=_1 /markers LINEATTRS = (THICKNESS = 3);<br />&nbsp;&nbsp;series x=day y=_2 /markers LINEATTRS = (THICKNESS = 3);<br />&nbsp;&nbsp;series x=day y=_3 /markers LINEATTRS = (THICKNESS = 3);<br />&nbsp;&nbsp;series x=day y=_4 /markers LINEATTRS = (THICKNESS = 3);<br />&nbsp;&nbsp;series x=day y=_5 /markers LINEATTRS = (THICKNESS = 3);<br />&nbsp;&nbsp;series x=day y=_6 /markers LINEATTRS = (THICKNESS = 3);<br />run;<br />quit;</code></pre><br />圖型如下:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/3080925228.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/3080925228.jpg" /></a></div><b>Code2.3:用 PROC MIXED 估計參數並用 ODS OUTPUT 輸出共變異數矩陣裡的參數</b><br /><pre><code>ODS OUTPUT "Covariance Parameter Estimates"=work.parameters;<br /><br />proc mixed data=mixed2 COVTEST cl NOCLPRINT NOITPRINT ;<br />&nbsp;&nbsp;class patient_id ;<br />&nbsp;&nbsp;model &amp;&amp;var&amp;i. =day_numeric /solution cl ;<br />&nbsp;&nbsp;random intercept / subject=patient_id ;<br />&nbsp;&nbsp;run;<br />&nbsp;&nbsp;quit;<br />ODS OUTPUT CLOSE;</code></pre><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/2304240514.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/2304240514.jpg" /></a></div><b>Code2.4:整理共變異數並計算 ICC</b><br />這段程式看起來非常冗長,但其實簡單來講就是把 Code2.3 做出來的資料整理後算出 ICC(藍色部分),不過原作者還算了很多其他的估計值。如果你只是想要求出 ICC,那綠色的程式碼可以不用寫。<br /><pre><code>data work.parameters_between;<br />&nbsp;set work.parameters;<br />if covparm="Intercept" then do;<br />&nbsp;&nbsp; Between_Subject_Variance=Estimate;<br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Between_p_value=ProbZ;&nbsp;</span><br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Between_Lower=Lower;&nbsp;</span><br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Between_Upper=Upper;&nbsp;</span><br />&nbsp;end;<br />&nbsp;if covparm="Intercept" ;<br />&nbsp;keep Between_Subject_Variance <span class="Apple-style-span" style="color: #38761d;">Between_P_Value Between_Lower Between_Upper</span>;<br />&nbsp;run;<br />&nbsp;data work.parameters_within;<br />&nbsp;set work.parameters;<br />&nbsp;if covparm="Residual" then do;<br />&nbsp;&nbsp; Within_Subject_Variance=Estimate;<br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Within_p_value=ProbZ;&nbsp;</span><br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Within_Lower=Lower;&nbsp;</span><br /><span class="Apple-style-span" style="color: #38761d;">&nbsp;&nbsp; Within_Upper=Upper;&nbsp;</span><br />&nbsp;&nbsp; end;<br />&nbsp;if covparm="Residual" ;<br />&nbsp;keep Within_Subject_Variance <span class="Apple-style-span" style="color: #38761d;">WITHIN_P_VALUE Within_Lower Within_Upper</span>;<br />&nbsp;run;<br />&nbsp;data work.parameters_all;<br />&nbsp;merge work.parameters_between work.parameters_within;<br /><span class="Apple-style-span" style="color: blue;">&nbsp;ICC=between_subject_variance/(within_subject_variance+between_subject_variance);&nbsp;</span><br />&nbsp;variable="&amp;&amp;var&amp;i."; variable_label="&amp;&amp;label&amp;i."; data_source="&amp;source.";<br />&nbsp;run;<br />&nbsp;quit;<br />&nbsp;proc print data=work.parameters_all noobs;<br />&nbsp;run;</code></pre>最後用 PROC PRINT 列印出來的結果如下:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/3454488330.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/418/3454488330.jpg" /></a></div><br /><br /><b>3. POWER SIMULATION</b><br />最後重頭戲是 power analysis。不過此處用到模擬的原因是因為,作者想要做 power analysis 的對象是 ICC 和組間變異數,而非我們一般常看到的假設檢定。而由於 ICC 和組間變異數的 power 計算並沒有封閉形式(close form)的解,所以必須用模擬的方式來算出。<br /><b>Code3.1:生成種子(seed)</b><br />用亂數去生成種子讓每一次的模擬都能隨機。種子的數量取得於母體大小乘上需要模擬的次數。本例的種子數目高達一千五百萬。<br /><pre><code>%MACRO INITIALIZE ();<br />%Global Initial; &nbsp;<br />%Let Initial=0;<br />data Initialseed (keep=id InitialSeed);<br />&nbsp;&nbsp; &nbsp; &nbsp;do I = 1 to 15000000; &nbsp; <br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InitialSeed =int(10000*(ranuni(0)));<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id=I;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output;<br />&nbsp;&nbsp; &nbsp; &nbsp;end;<br />run;<br />data SIM.InitialSeed;<br />&nbsp;set work.InitialSeed;<br />run;<br />%MEND INITIALIZE;</code></pre><br /><b>Code3.2:製作依序取得種子的巨集程式</b><br /><pre><code>%LET Initial=35;<br />%MACRO SEED ();<br />%let Initial=%EVAL(&amp;Initial. +1);<br />&nbsp;data work.seed ;<br />&nbsp;&nbsp;set sim.initialseed (Firstobs=&amp;Initial. obs=&amp;Initial.);;<br />&nbsp;run;<br />&nbsp;data _null_;<br />&nbsp;&nbsp;set work.seed;<br />&nbsp;&nbsp;call symputx ('InitialSeed',InitialSeed);<br />&nbsp;run;<br />%put Seed is # &amp;Initial. and is valued &amp;InitialSeed. ;<br />%MEND SEED;</code></pre><br /><b>Code3.3:模擬資料的巨集程式</b><br />原文中 Code3.3~Code3.9 都是在講資料的生成。我將他們放在一起。簡單來講,先生成 baseline 資料(work.test),再生成其他時間點的資料(work.test_2),然後用 PROC TRANSPOSE 把生成好的結果轉置(直的變成橫的),接著就是用 PROC MIXED 不斷重複地去估計共變異數矩陣的參數,把這重複估計出來的參數存成一個新檔案(work.mixed),然後去算有多少顯著的比例。最後把結果整理成報表。<br /><pre><code>%MACRO TEST(mean=,between_var=,within_var=,simulation=,population=,timecount=);<br /><br />data work.test;<br />*first loop defines the simulation number*;<br />do i= 1 to &amp;simulation.;<br />*second loop defines the number of people in the study*;<br />&nbsp;do j=1 to &amp;population.;<br />&nbsp;&nbsp; TIME_0=&amp;mean.+sqrt(&amp;between_var.)*rannor(&amp;initialseed.);<br />&nbsp;&nbsp;Simulation_ID=i;<br />&nbsp;&nbsp;Person_ID=J; <br />&nbsp;&nbsp;output;<br />&nbsp;end;<br />end;<br />drop I j;<br />run;<br /><br />data work.test_2;<br />set work.test;<br />array time (*) TIME_1-TIME_&amp;timecount.;<br />&nbsp;&nbsp;do k=1 to &amp;timecount.;<br />&nbsp;&nbsp; Time[k]=TIME_0 + sqrt(&amp;within_var.)*rannor(&amp;initialseed.);<br />&nbsp;end;<br />&nbsp;&nbsp;output;<br />drop k;<br />run;<br /><br />proc transpose data=work.test_2 out=work.test_3 prefix=value;<br />&nbsp;by simulation_ID person_id;<br />run;<br />data work.test_3;<br />set work.test_3;<br />&nbsp;TIME= input(scan(_NAME_,2,'_'),2.);<br />&nbsp;TIME2=TIME;<br />run;<br /><br />ODS LISTING CLOSE;<br />ODS OUTPUT COVPARMS=work.mixed;<br />proc mixed data=work.test_3 covtest;<br />&nbsp;&nbsp;by simulation_ID;<br />&nbsp;&nbsp;model value1=time;<br />&nbsp;&nbsp;random intercept/ sub=person_id;<br />run;<br />ODS OUTPUT CLOSE;<br /><br />data work.mixed_1;<br />set work.mixed;<br />if Covparm="Intercept";<br />if probz&lt;0.1 then P_point1=1;<br />if probz&lt;0.05 then P_point05=1;<br />if probz&lt;0.01 then P_point01=1;<br />if probz&lt;0.001 then P_point001=1;<br />if probz=. then do;<br />&nbsp;p_point1=.;<br />&nbsp;p_point05=.;<br />&nbsp;p_point01=.;<br />&nbsp;p_point001=.;<br />&nbsp;&nbsp;end;<br />run;<br /><br />ODS OUTPUT onewayfreqs=work.mixed_freqs_1;<br />ODS LISTING;<br />proc freq data=work.mixed_1 ;<br />table p_point1 p_point05 p_point01 &nbsp;p_point001/missing;<br />run;<br />ODS LISTING CLOSE;<br />ODS OUTPUT CLOSE;<br /><br />proc append Base=work._mixed_freqs_all data=work._mixed_freqs_all_&amp;initial. FORCE;<br />run;<br />%MEND;</code></pre><b>Code3.10:開始模擬</b><br />最後就是呼叫前面寫好的巨集程式,輸入不同的組間變異數和組內變異數的數值,定義總共要模擬的次數、樣本大小、時間點數量以及平均數。<br /><pre><code>%test(mean=-0.2740, between_var=0.01,within_var=0.67,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.02,within_var=0.66,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.03,within_var=0.65,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.04,within_var=0.64,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.05,within_var=0.63,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.06,within_var=0.62,simulation=1000,population=20,timecount=12);<br />%test(mean=-0.2740, between_var=0.50,within_var=0.16,simulation=1000,population=20,timecount=12);</code></pre><br /><b>Code3.11:整理繪圖資料</b><br />模擬完後的資料存在 work._mixed_freqs_all 裡面,把不同組間變異數相對應的 ICC 放進去,並且把變數的標籤寫好。<br /><pre><code>data work.plots ;<br />set work._mixed_freqs_all;<br />&nbsp;if between_var= &nbsp;0.01 then ICC = &nbsp;0.01 ;<br />&nbsp;if between_var= &nbsp;0.06 then ICC = &nbsp;0.09 ;<br />&nbsp;if between_var= &nbsp;0.11 then ICC = &nbsp;0.16 ;<br />&nbsp;.……<br />label percent="Power" population="Number of Subjects" timecount="Number of Time &nbsp;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Points:";<br />run;</code></pre><br /><b>Code3.12:繪圖</b><br /><pre><code>proc sort data=work.plots;<br />&nbsp;by population;<br />run;<br />symbol i=j width=5;<br />ODS LISTING;<br />proc gplot data=work.plots;<br />&nbsp;by population ;<br />&nbsp;where table ="P_point01" and mixed=2;<br />&nbsp;plot percent*ICC=timecount;<br />run;<br />quit;</code></pre>最後圖型如下:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/418/4177901899.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="348" src="http://easycaptures.com/fs/uploaded/418/4177901899.jpg" width="640" /></a></div><br /><b>CONTACT INFORMATION&nbsp;</b><br /><br />Your comments and questions are valued and encouraged. Contact the author at:<br />Elisa L. Priest, MPH <br /><!-- e --><a href="mailto:elisapriest@hotmail.com">elisapriest@hotmail.com</a><!-- e --><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-6567097650628863902?l=sugiclub.blogspot.com' alt='' /></div>




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2