<?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: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/595011#M171099</link>
    <description>&lt;P&gt;I would follow SAS. Since SAS is standard tool in statistic field .&lt;/P&gt;
&lt;P&gt;And the new method maybe better than old one ,but it has to be admitted by most of statistics in the world .&lt;/P&gt;</description>
    <pubDate>Wed, 09 Oct 2019 11:19:41 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-10-09T11:19:41Z</dc:date>
    <item>
      <title>Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594611#M170868</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Problem&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose you have a dice. You wish to test if the dice is fair for the side 6. You test this by rolling the dice 235 times and noting it lands on a 6, 51 times. You then wish to compare this to the expected rate of 1/6 = 0.1666667.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore, let us test this by conducting the test in R and SAS, but note the p-value for the R code is 0.04375 and the p-value for the SAS code is 0.0531.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;R Code&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;binom.test(51, 235, (1/6), alternative = "two.sided")&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;SAS Code&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA DICEROLL;
INPUT ROLL $ FREQQ; 
DATALINES; 
NO 184
YES 51
; 

PROC FREQ DATA = DATAROLL;
WEIGHT FREQQ; 
TABLES ROLL / BINOMIAL(ALL LEVEL = "YES" P = 0.1666667) ALPHA = 0.05;
EXACT BINOMIAL;
RUN; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Additional Notes &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is strange is that the one sided tests p-values are equivalent, but I cannot get the two sided p-values to match. Can someone please help me and point out how to get these equivalent? I am learning SAS and appreciate your patience and assistance in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;References&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;A href="https://en.wikipedia.org/wiki/Binomial_test" target="_self"&gt;https://en.wikipedia.org/wiki/Binomial_test&lt;/A&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 03:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594611#M170868</guid>
      <dc:creator>hyates</dc:creator>
      <dc:date>2019-10-08T03:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594702#M170921</link>
      <description>&lt;P&gt;If you check ASE , you will see 0.038 which is close to 0.04 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test of H0: Proportion = 0.1666666667&lt;/P&gt;
&lt;P&gt;ASE under H0 0.0243&lt;BR /&gt;Z 2.0713&lt;BR /&gt;One-sided Pr &amp;gt; Z 0.0192&lt;BR /&gt;Two-sided Pr &amp;gt; |Z| &lt;STRONG&gt;0.0383&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Exact Test&lt;BR /&gt;One-sided Pr &amp;gt;= P 0.0265&lt;BR /&gt;Two-sided = 2 * One-sided 0.0531&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And also try R code: and see the result&lt;/P&gt;
&lt;PRE&gt;binom.test(51, 235, 0.16666666666667, alternative = "two.sided")&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 11:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594702#M170921</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-08T11:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594764#M170961</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Additional Findings:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe it has to do with how p-values are being calculated. Consider the following R code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;binom.exact(51, 235, (1/6), tsmethod = "central")&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;This will give you the same result as SAS with a p-value of 0.05309.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if you use&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;binom.exact(51, 235, 1/6, tsmethod = "minlike")&lt;/PRE&gt;&lt;P&gt;This will give you the same results as binom.test() and a p-value of 0.04375.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As far as I can tell, minlike: sum of probabilities of outcomes with likelihoods less than or equal to observed. central: is 2 times the minimum of the one-sided p-values bounded by 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I have two follow up questions:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Statistically, which is the appropriate way to calculate the p-value and why would an algorithm like binom.test have minlike as a default?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. How can we get PROC FREQ to use minlike calculation of the p-value? I think it is based on Hirji 2006 probability based method.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Reference:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://cran.r-project.org/web/packages/exactci/exactci.pdf" target="_self"&gt;https://cran.r-project.org/web/packages/exactci/exactci.pdf&lt;/A&gt; (this discusses the tsmethods as described above)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 14:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594764#M170961</guid>
      <dc:creator>hyates</dc:creator>
      <dc:date>2019-10-08T14:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594787#M170972</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226777"&gt;@hyates&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Statistically, which is the appropriate way to calculate the p-value and why would an algorithm like binom.test have minlike as a default?&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a question for the R developer.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 15:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/594787#M170972</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-08T15:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/595011#M171099</link>
      <description>&lt;P&gt;I would follow SAS. Since SAS is standard tool in statistic field .&lt;/P&gt;
&lt;P&gt;And the new method maybe better than old one ,but it has to be admitted by most of statistics in the world .&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 11:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/595011#M171099</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-09T11:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Inquiry: Obtaining equivalent results between R code binom.test and SAS code using PROC FREQ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/595178#M171176</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226777"&gt;@hyates&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226777"&gt;@hyates&lt;/a&gt;&amp;nbsp;wrote (in the initial post):&lt;BR /&gt;
&lt;P&gt;What is strange is that the one sided tests p-values are equivalent, but I cannot get the two sided p-values to match.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think the equivalence of &lt;EM&gt;one-&lt;/EM&gt;sided &lt;EM&gt;exact&lt;/EM&gt; p-values is due to the fact that their definition is uncontroversial:&lt;/P&gt;
&lt;PRE&gt;p = min(cdf('binom',k,p0,n), 1-cdf('binom',k-1,p0,n))&lt;/PRE&gt;
&lt;P&gt;(using SAS notation; cf. section "Binomial Tests," subsection "Equality Test," in the &lt;A href="https://documentation.sas.com/?docsetId=statug&amp;amp;docsetTarget=statug_freq_details37.htm&amp;amp;docsetVersion=14.3&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PROC FREQ documentation "Binomial Proportion"&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For two-sided exact p-values, however, different definitions have been proposed. I guess the most commonly used method is the symmetrical one used by PROC FREQ ("equal-tailed test").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for providing the reference exactci.pdf, which contains a valuable hint as to the method used by R's binom.test function (i.e. "minlike"). Being unfamiliar with R, I had been searching a while for an official documentation of binom.test &lt;EM&gt;including methods/formulas&lt;/EM&gt; (unlike&amp;nbsp;&lt;A href="https://www.rdocumentation.org/packages/mosaic/versions/1.5.0/topics/binom.test" target="_blank" rel="noopener"&gt;https://www.rdocumentation.org/packages/mosaic/versions/1.5.0/topics/binom.test&lt;/A&gt;), until I found at least the source code in&amp;nbsp;&lt;A href="https://svn.r-project.org/R/trunk/src/library/stats/R/binom.test.R" target="_blank" rel="noopener"&gt;https://svn.r-project.org/R/trunk/src/library/stats/R/binom.test.R&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, this "minlike" method appears to be the first of &lt;EM&gt;three&lt;/EM&gt; alternative methods for computing two-sided exact p-values presented (in addition to the symmetrical approach) in&amp;nbsp;&lt;A href="http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471526290.html" target="_blank" rel="nofollow noopener noreferrer"&gt;Fleiss et al. (2003), Statistical Methods for Rates and Proportions, 3rd ed.&lt;/A&gt;&lt;SPAN&gt;, section 2.7: Point Probability Method, Tail Probability Method and Likelihood Ratio Method.&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226777"&gt;@hyates&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;1. Statistically, which is the appropriate way to calculate the p-value and why would an algorithm like binom.test have minlike as a default?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;None of the four above-mentioned methods is superior to all others. So, ideally, the choice should be based on statistical and subject-matter considerations. For example, if the Clopper-Pearson confidence interval is reported, it makes sense to use the corresponding&amp;nbsp;equal-tailed test. Fleiss et al. (2003) mention that the alternative methods "will usually improve a bit" "in terms of statistical power and length of confidence intervals." Maybe this was the motivation of the R developer. In some cases there might even be a reason to apportion the type I error rate between the two tails of the distribution neither symmetrically (SAS method) nor data-driven (the three alternatives), so that yet another method would be preferred.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, the method should be specified in advance, i.e., before the data to be analyzed are available. In particular, possible differences in terms of statistical significance (with the data at hand) must not influence the decision.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;2. How can we get PROC FREQ to use minlike calculation of the p-value?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think we can get that p-value from PROC FREQ, but we can easily calculate it, e.g., in a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let p0=1/6;
%let relErr=1.0000001;

data want(keep=n k p);
set diceroll end=last;
n+freqq;
if roll='YES' then k+freqq;
if last;
p0=&amp;amp;p0;
pk=pdf('binom',k,p0,n);
do j=0 to n;
  pj=pdf('binom',j,p0,n);
  p+(pj&amp;lt;=pk*&amp;amp;relErr)*pj;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For massive calculations (e.g. in a loop over a large number of parameter combinations) using the &lt;EM&gt;C&lt;/EM&gt;DF function would be more efficient, but requires a bit more code. One could implement the algorithm in a SAS macro or user-defined function (PROC FCMP) in order to make it readily available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC PRINT result for your sample dataset (rounded):&lt;/P&gt;
&lt;PRE&gt; n      k       p0          p

235    51    0.16667    0.043748&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The purpose of the factor &lt;FONT face="courier new,courier"&gt;relErr&lt;/FONT&gt; (=1+1E-7, taken from the R code) is to avoid errors due to numeric representation issues (rounding errors): It happens that binomial probabilities which are mathematically equal are delivered by the PDF function in slightly different internal binary representations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
d=pdf('binom',0,1/7,9)-pdf('binom',2,1/7,9);
put d=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result (SAS 9.4M5 on Windows 7):&lt;/P&gt;
&lt;PRE&gt;d=2.775558E-16&lt;/PRE&gt;
&lt;P&gt;In fact, both values should equal (6/7)**9, hence d=0. (I was a bit surprised to find even an example where this happened to a number with a &lt;EM&gt;finite&lt;/EM&gt; binary representation: p&lt;FONT size="1 2 3 4 5 6 7"&gt;j&lt;/FONT&gt;=27/64 with n=3, p0=1/4, j=0, 1.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a consequence, the p-value for n=9, p0=1/7 and k=2 (i.e. NO 7, YES 2) would be calculated incorrectly as 0.37566... without the "fuzz factor"&amp;nbsp;&lt;FONT face="courier new,courier"&gt;relErr&lt;/FONT&gt;, whereas the correct result, obtained with the above DATA step, is 0.62539... (note the Boolean expression &lt;CODE class=" language-sas"&gt;(pj&amp;lt;=pk*&amp;amp;relErr)&amp;nbsp;&lt;/CODE&gt;in the code!).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be mentioned that in certain (rather artificial) cases it can be &lt;EM&gt;vice versa&lt;/EM&gt;, i.e., the fuzz factor &lt;EM&gt;causes&lt;/EM&gt; an incorrect result.&lt;/P&gt;
&lt;P&gt;Example: n=20, p0=0.3527785166, k=4 (found in&amp;nbsp;&lt;A href="https://stat.ethz.ch/pipermail/r-help/2007-April/129113.html" target="_blank" rel="noopener"&gt;https://stat.ethz.ch/pipermail/r-help/2007-April/129113.html&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 19:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inquiry-Obtaining-equivalent-results-between-R-code-binom-test/m-p/595178#M171176</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-09T19:29:03Z</dc:date>
    </item>
  </channel>
</rss>

