SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 684|回复: 0
打印 上一主题 下一主题

Automatically Run Fisher's Exact Test When the Chi-square Te

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-11-4 21:21:13 | 只看该作者

Automatically Run Fisher's Exact Test When the Chi-square Te

From LCChien's blog on blogspot

Link:&nbsp;<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>&nbsp;data test;&nbsp;<br />&nbsp;&nbsp; input r c ct;&nbsp;<br />&nbsp;&nbsp; datalines;&nbsp;<br />&nbsp;&nbsp; 1 1 12&nbsp;<br />&nbsp;&nbsp; 1 2 5&nbsp;<br />&nbsp;&nbsp; 1 3 6&nbsp;<br />&nbsp;&nbsp; 2 1 13&nbsp;<br />&nbsp;&nbsp; 2 2 2&nbsp;<br />&nbsp;&nbsp; 2 3 4&nbsp;<br />&nbsp;&nbsp; ;&nbsp;<br />&nbsp;run; &nbsp;&nbsp;<br />%run_fishers(data=test,&nbsp;row=r,&nbsp;col=c,&nbsp;count=ct);</code></pre>由於此資料會出現warning訊息,所以Fisher's exact test的結果就會直接顯示在報表最下方。如果今天資料變成:<br /><pre><code>data test;&nbsp;<br />input r c ct;&nbsp;<br />datalines;&nbsp;<br />1 1 12&nbsp;<br />1 2 50&nbsp;<br />1 3 60&nbsp;<br />2 1 13&nbsp;<br />2 2 20&nbsp;<br />2 3 40&nbsp;<br />;&nbsp;<br />run; &nbsp;&nbsp;<br />%run_fishers(data=test,&nbsp;row=r,&nbsp;col=c,&nbsp;count=ct);</code></pre>報表最下方就不會出現Fisher's exact test的結果。<br /><br />最後是這個macro的原始碼:<br /><pre><code>%macro run_fishers (version, data=, row=, col=, count=);&nbsp;<br />%let _version=1.0;&nbsp;<br />%if &amp;version ne %then %put RUN_FISHERS macro Version &amp;_version;&nbsp;<br />%let opts = %sysfunc(getoption(notes)) &nbsp;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_last_=%sysfunc(getoption(_last_));&nbsp;<br />%if &amp;version ne debug %then %str(options nonotes;);&nbsp;<br />&nbsp;<br />/* Check for newer version */&nbsp;<br />%if %sysevalf(&amp;sysver &gt;= 8.2) %then %do;&nbsp;<br />%let _notfound=0;&nbsp;<br />&nbsp;<br />filename ver url 'http://ftp.sas.com/techsup/download/stat/versions.dat' termstr=crlf;&nbsp;<br />&nbsp;data _null_;&nbsp;<br />&nbsp;&nbsp; &nbsp;infile ver end=_eof;&nbsp;<br />&nbsp;&nbsp; &nbsp;input name:$15. ver;&nbsp;<br />&nbsp;&nbsp; &nbsp;if upcase(name)="&amp;sysmacroname" then do;&nbsp;<br />&nbsp;&nbsp; &nbsp; &nbsp; call symput("_newver",ver); stop;&nbsp;<br />&nbsp;&nbsp; &nbsp;end;&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;&nbsp; &nbsp;if _eof then call symput("_notfound",1);&nbsp;<br />run;&nbsp;<br />&nbsp;&nbsp;%if &amp;syserr ne 0 or &amp;_notfound=1 %then&nbsp;<br />&nbsp;&nbsp; &nbsp;%put &amp;sysmacroname: Unable to check for newer version;&nbsp;<br />&nbsp;&nbsp;%else %if %sysevalf(&amp;_newver &gt; &amp;_version) %then %do;&nbsp;<br />&nbsp;&nbsp; &nbsp;%put &amp;sysmacroname: A newer version of the &amp;sysmacroname macro is available.;&nbsp;<br />&nbsp;&nbsp; &nbsp;%put %str( &nbsp; &nbsp; &nbsp; &nbsp; ) You can get the newer version at this location:;&nbsp;<br />&nbsp;&nbsp; &nbsp;%put %str( &nbsp; &nbsp; &nbsp; &nbsp; ) <!-- m --><a class="postlink" href="http://support.sas.com/ctx/samples/index.jsp;&amp;nbsp;">http://support.sas.com/ctx/samples/index.jsp;&amp;nbsp;</a><!-- m --><br />&nbsp;&nbsp;%end;&nbsp;<br />&nbsp;%end;&nbsp;<br />&nbsp;&nbsp;<br />proc freq data=&amp;data noprint;&nbsp;<br />&nbsp;&nbsp;%if &amp;count ne %then %str(weight &amp;count / zeros;);&nbsp;<br />&nbsp;&nbsp;tables &amp;row*&amp;col / sparse outexpect out=_out1;&nbsp;<br />run;&nbsp;<br />&nbsp;<br />proc means data=_out1 noprint;&nbsp;<br />&nbsp;&nbsp;var count;&nbsp;<br />&nbsp;&nbsp;output out=_out2;&nbsp;<br />run;&nbsp;<br />&nbsp;<br />data _null_;&nbsp;<br />&nbsp;&nbsp;set _out1;&nbsp;<br />&nbsp;&nbsp;if expected&lt;=5 then warn+1;&nbsp;<br />&nbsp;&nbsp;if _n_=1 then set _out2;&nbsp;<br />&nbsp;&nbsp;pct_lt5=warn/_freq_;&nbsp;<br />&nbsp;&nbsp;if _freq_=_n_;&nbsp;<br />&nbsp;&nbsp;warning=(pct_lt5&gt;=.2);&nbsp;<br />&nbsp;&nbsp;call symput('warning',warning);&nbsp;<br />run;&nbsp;<br />&nbsp;<br />options &amp;opts;&nbsp;<br />&nbsp;<br />proc freq data=_out1;&nbsp;<br />&nbsp;&nbsp;weight count / zeros;&nbsp;<br />&nbsp;&nbsp;tables &amp;row*&amp;col / chisq;&nbsp;<br />&nbsp;&nbsp;%if &amp;warning=1 %then %do;&nbsp;<br />&nbsp;&nbsp;exact fisher;&nbsp;<br />&nbsp;&nbsp;%end;&nbsp;<br />run;&nbsp;<br />&nbsp;<br />%mend;&nbsp;</code></pre><br /><b>CONTACT INFORMATION&nbsp;</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>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-3 16:37 , Processed in 0.065975 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表