|
|
楼主

楼主 |
发表于 2010-11-4 21:21:13
|
只看该作者
Automatically Run Fisher's Exact Test When the Chi-square Te
From LCChien's blog on blogspot
Link: <a href="https://docs.google.com/viewer?url=http%3A%2F%2Fsupport.sas.com%2Fresources%2Fpapers%2Fproceedings10%2F096-2010.pdf"><!-- m --><a class="postlink" href="http://support.sas.com/resources/papers/proceedings10/096-2010.pdf">http://support.sas.com/resources/papers ... 6-2010.pdf</a><!-- m --></a><br /><br />在使用PROC FREQ執行卡方檢定時,如果看到列聯表下面出現warning的訊息,就表示這個列聯表有50%以上的期望值小於五,因此程式會建議使用Fisher's exact test。然後使用者必須回到程式裡面,在tables statement後面加上一個exact的選項,再重跑一次PROC FREQ才能得到Fisher's exact test的結果。Wei Xu於SAS GLOBAL FORUM 2010發表了一個macro程式,讓電腦自動幫使用者判斷要不要進行Fisher's exact test,若需要的話會順便把Fisher's exact test的結果跑出來,使用者不需再去重新執行PROC FREQ一次。<br /><br /><a name='more'></a><br /><br />這個macro名叫%RUN_FISHERS,裡面只有四個參數須要設定:<br /><br /><ul><li>Data= 要進行卡方檢定的資料名稱</li><li>Row= 放在行的變數名稱</li><li>Col= 放在列的變數名稱</li><li>Count= 如果資料是已經整理好的列聯資料,則會多一個計數變數來表示某一欄位的個數,此時需要把這個變數定義在這個參數裡面。如果不是列聯資料的話,可在此留空白。</li></ul>範例:<br /><pre><code> data test; <br /> input r c ct; <br /> datalines; <br /> 1 1 12 <br /> 1 2 5 <br /> 1 3 6 <br /> 2 1 13 <br /> 2 2 2 <br /> 2 3 4 <br /> ; <br /> run; <br />%run_fishers(data=test, row=r, col=c, count=ct);</code></pre>由於此資料會出現warning訊息,所以Fisher's exact test的結果就會直接顯示在報表最下方。如果今天資料變成:<br /><pre><code>data test; <br />input r c ct; <br />datalines; <br />1 1 12 <br />1 2 50 <br />1 3 60 <br />2 1 13 <br />2 2 20 <br />2 3 40 <br />; <br />run; <br />%run_fishers(data=test, row=r, col=c, count=ct);</code></pre>報表最下方就不會出現Fisher's exact test的結果。<br /><br />最後是這個macro的原始碼:<br /><pre><code>%macro run_fishers (version, data=, row=, col=, count=); <br />%let _version=1.0; <br />%if &version ne %then %put RUN_FISHERS macro Version &_version; <br />%let opts = %sysfunc(getoption(notes)) <br /> _last_=%sysfunc(getoption(_last_)); <br />%if &version ne debug %then %str(options nonotes;); <br /> <br />/* Check for newer version */ <br />%if %sysevalf(&sysver >= 8.2) %then %do; <br />%let _notfound=0; <br /> <br />filename ver url 'http://ftp.sas.com/techsup/download/stat/versions.dat' termstr=crlf; <br /> data _null_; <br /> infile ver end=_eof; <br /> input name:$15. ver; <br /> if upcase(name)="&sysmacroname" then do; <br /> call symput("_newver",ver); stop; <br /> end; <br /> <br /> <br /> if _eof then call symput("_notfound",1); <br />run; <br /> %if &syserr ne 0 or &_notfound=1 %then <br /> %put &sysmacroname: Unable to check for newer version; <br /> %else %if %sysevalf(&_newver > &_version) %then %do; <br /> %put &sysmacroname: A newer version of the &sysmacroname macro is available.; <br /> %put %str( ) You can get the newer version at this location:; <br /> %put %str( ) <!-- m --><a class="postlink" href="http://support.sas.com/ctx/samples/index.jsp;&nbsp;">http://support.sas.com/ctx/samples/index.jsp;&nbsp;</a><!-- m --><br /> %end; <br /> %end; <br /> <br />proc freq data=&data noprint; <br /> %if &count ne %then %str(weight &count / zeros;); <br /> tables &row*&col / sparse outexpect out=_out1; <br />run; <br /> <br />proc means data=_out1 noprint; <br /> var count; <br /> output out=_out2; <br />run; <br /> <br />data _null_; <br /> set _out1; <br /> if expected<=5 then warn+1; <br /> if _n_=1 then set _out2; <br /> pct_lt5=warn/_freq_; <br /> if _freq_=_n_; <br /> warning=(pct_lt5>=.2); <br /> call symput('warning',warning); <br />run; <br /> <br />options &opts; <br /> <br />proc freq data=_out1; <br /> weight count / zeros; <br /> tables &row*&col / chisq; <br /> %if &warning=1 %then %do; <br /> exact fisher; <br /> %end; <br />run; <br /> <br />%mend; </code></pre><br /><b>CONTACT INFORMATION </b><br />Wei Xu<br />Boston Scientific<br />100 Boston Scientific Way<br />Marlborough, MA 01752-1234<br />(508) 683-4264<br /><!-- e --><a href="mailto:wei.xu@bsci.com">wei.xu@bsci.com</a><!-- e --><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-1413090291924213562?l=sugiclub.blogspot.com' alt='' /></div> |
|