<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>R-statistics blog &#187; Fisher&#8217;s Exact test</title>
	<atom:link href="http://www.r-statistics.com/tag/fishers-exact-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r-statistics.com</link>
	<description>Writing about statistics with R, and open source stuff (software, data, community)</description>
	<lastBuildDate>Thu, 29 Jul 2010 01:51:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Barnard&#8217;s exact test &#8211; a powerful alternative for Fisher&#8217;s exact test (implemented in R)</title>
		<link>http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/</link>
		<comments>http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 10:12:10 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[Barnard's test]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[contingency tables]]></category>
		<category><![CDATA[Fisher's Exact test]]></category>
		<category><![CDATA[non-parametric]]></category>
		<category><![CDATA[non-parametric test]]></category>
		<category><![CDATA[nuisance parameter]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[Wald statistic]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=75</guid>
		<description><![CDATA[(The R code for Barnard&#8217;s exact test is at the end of the article, and you could also just download it from here) About Barnard&#8217;s exact test About half a year ago, I was studying various statistical methods to employ on contingency tables. I came across a promising method for 2×2 contingency tables called &#8220;Barnard&#8217;s exact test&#8220;. Barnard&#8217;s test is a non-parametric alternative to Fisher&#8217;s exact test which can be more powerful (for 2×2 tables) but is also more time-consuming to compute (References can be [...]]]></description>
			<content:encoded><![CDATA[<p><em>(The R code for Barnard&#8217;s exact test is at the end of the article, and you could also just <a href="http://www.r-statistics.com/wp-content/uploads/2010/02/Barnardtest.R.txt">download it from here</a>)</em></p>
<p><em><a href="http://www.r-statistics.com/wp-content/uploads/2010/02/Barnards-exact-test-p-value-based-on-the-nuisance-parameter.png"><img title="Barnards exact test - p-value based on the nuisance parameter" src="http://www.r-statistics.com/wp-content/uploads/2010/02/Barnards-exact-test-p-value-based-on-the-nuisance-parameter.png" alt="" width="500" /></a></em></p>
<h3>About Barnard&#8217;s exact test</h3>
<p>About half a year ago, I was studying various statistical methods to employ on contingency tables. I came across a promising method for 2×2 contingency tables called &#8220;<strong>Barnard&#8217;s exact test</strong>&#8220;. Barnard&#8217;s test is a non-parametric alternative to <a title="Fisher's exact test" href="http://en.wikipedia.org/wiki/Fisher%27s_exact_test">Fisher&#8217;s exact test</a> which can be more powerful (for 2×2 tables) but is also more time-consuming to compute (References can be found in the <a href="http://en.wikipedia.org/wiki/Barnard%27s_test">Wikipedia article</a> on the subject).</p>
<p>The test was first published by <a title="George Alfred Barnard" href="http://en.wikipedia.org/wiki/George_Alfred_Barnard">George Alfred Barnard</a> (1945). <a href="http://www.cytel.com/Papers/twobinomials.pdf">Mehta and Senchaudhuri (2003)</a> explain why Barnard&#8217;s test can be more powerful than Fisher&#8217;s under certain conditions:</p>
<blockquote><p>When comparing Fisher’s and Barnard’s exact tests, the loss of power due to the greater discreteness of the Fisher statistic is somewhat offset by the requirement that Barnard’s exact test must maximize over all possible p-values, by choice of the nuisance parameter, π. <strong>For 2 × 2 tables </strong>the loss of power due to the discreteness dominates over the loss of power due to the maximization, resulting in<strong> greater power for Barnard’s exact test</strong>. But as the number of rows and columns of the observed table increase, the maximizing factor will tend to dominate, and Fisher’s exact test will achieve greater power than Barnard’s.</p></blockquote>
<h3>About the R implementation of Barnard&#8217;s exact test</h3>
<p>After finding about Barnard&#8217;s test I was sad to discover that (at the time) there had been no R implementation of it. But last week, I received a surprising e-mail with good news. The sender, <strong>Peter Calhoun</strong>, currently a graduate student at the University of Florida, had implemented the algorithm in R. Peter had  found my posting on the R mailing list (from almost half a year ago) and was so kind as to share with me (and the rest of the R community) his R code for computing Barnard&#8217;s exact test. Here is some of what Peter wrote to me about his code:</p>
<blockquote><p>On a side note, I believe <strong>there are more efficient codes than this one</strong>.  For example, I&#8217;ve seen codes in Matlab that run faster and display nicer-looking graphs.  However, this code will still provide accurate results and a plot that gives the p-value based on the nuisance parameter.  I did not come up with the idea of this code, I simply translated Matlab code into R, occasionally using different methods to get the same result.  The code was translated from:</p>
<p>Trujillo-Ortiz, A., R. Hernandez-Walls, A. Castro-Perez, L. Rodriguez-Cardozo. Probability Test.  A MATLAB file. URL</p>
<p>http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=6198</p>
<p>My goal was to make this test accessible to everyone.  Although there are many ways to run this test through Matlab, I hadn&#8217;t seen any code to implement this test in R.  I hope it is useful for you, and if you have any questions or ways to improve this code, please contact me at calhoun.peter@gmail.com</p></blockquote>
<p>p.s: I added some minor cosmetics to the code, like allowing the input to be a table/matrix and the output to be a list.</p>
<p><em><span id="more-75"></span></em></p>
<h3>The R function for Barnard&#8217;s exact test</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
</pre></td><td class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;"># published on: </span>
<span style="color: #228B22;"># http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/</span>
&nbsp;
Barnardextest<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>Ta,Tb <span style="color: #080;">=</span>NULL,Tc <span style="color: #080;">=</span>NULL,Td <span style="color: #080;">=</span>NULL, to.<span style="">print</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">F</span>, to.<span style="">plot</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#123;</span>
<span style="color: #228B22;"># The first argument (Ta) can be either a table or a matrix of 2X2.</span>
<span style="color: #228B22;"># Or instead, the values of the table can be entered one by one to the function</span>
&nbsp;
<span style="color: #228B22;">#Barnard's test calculates the probabilities for contingency tables.  It has been shown that for 2x2 tables, Barnard's test</span>
<span style="color: #228B22;">#has a higher power than Fisher's Exact test.  Barnard's test is a non-parametric test that relies upon a computer to generate</span>
<span style="color: #228B22;">#the distribution of the Wald statistic.  Using a computer program, one could find the nuisance parameter that maximizes the </span>
<span style="color: #228B22;">#probability of the observations displayed from a table.</span>
<span style="color: #228B22;">#Despite giving lower p-values for 2x2 tables, Barnard's test hasn't been used as often as Fisher's test because of its</span>
<span style="color: #228B22;">#computational difficulty.  This code gives the Wald statistic, the nuisance parameter, and the p-value for any 2x2 table.</span>
<span style="color: #228B22;">#The table can be written as:</span>
<span style="color: #228B22;">#			Var.1</span>
<span style="color: #228B22;">#		 ---------------</span>
<span style="color: #228B22;">#		   a		b	 r1=a+b</span>
<span style="color: #228B22;">#	Var.2</span>
<span style="color: #228B22;">#		   c		d	 r2=c+d</span>
<span style="color: #228B22;">#		 ---------------</span>
<span style="color: #228B22;">#		 c1=a+c   c2=b+d	 n=c1+c2</span>
&nbsp;
<span style="color: #228B22;">#One example would be </span>
<span style="color: #228B22;">#				Physics</span>
<span style="color: #228B22;">#			 Pass	     Fail</span>
<span style="color: #228B22;">#			 ---------------</span>
<span style="color: #228B22;">#		Crane	   8		14</span>
<span style="color: #228B22;">#  Collage	</span>
<span style="color: #228B22;">#		Egret	   1		3</span>
<span style="color: #228B22;">#			 ---------------</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#After implementing the function, simply call it by the command:</span>
<span style="color: #228B22;">#Barnardextest(8,14,1,3)</span>
<span style="color: #228B22;">#This will display the results:</span>
&nbsp;
<span style="color: #228B22;">#&quot;The contingency table is:&quot;</span>
<span style="color: #228B22;">#      [,1] [,2]</span>
<span style="color: #228B22;">#[1,]    8   14</span>
<span style="color: #228B22;">#[2,]    1    3</span>
<span style="color: #228B22;">#&quot;Wald Statistic:&quot;</span>
<span style="color: #228B22;">#0.43944</span>
<span style="color: #228B22;">#&quot;Nuisance parameter:&quot;</span>
<span style="color: #228B22;">#0.9001</span>
<span style="color: #228B22;">#&quot;The 1-tailed p-value:&quot;</span>
<span style="color: #228B22;">#0.4159073</span>
&nbsp;
<span style="color: #228B22;">#On a side note, I believe there are more efficient codes than this one.  For example, I've seen codes in Matlab that run</span>
<span style="color: #228B22;">#faster and display nicer-looking graphs.  However, this code will still provide accurate results and a plot that gives the</span>
<span style="color: #228B22;">#p-value based on the nuisance parameter.  I did not come up with the idea of this code, I simply translated Matlab code </span>
<span style="color: #228B22;">#into R, occasionally using different methods to get the same result.  The code was translated from:</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#Trujillo-Ortiz, A., R. Hernandez-Walls, A. Castro-Perez, L. Rodriguez-Cardozo. Probability Test.  A MATLAB file. URL</span>
<span style="color: #228B22;">#http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=6198</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#My goal was to make this test accessible to everyone.  Although there are many ways to run this test through Matlab, I hadn't</span>
<span style="color: #228B22;">#seen any code to implement this test in R.  I hope it is useful for you, and if you have any questions or ways to improve</span>
<span style="color: #228B22;">#this code, please contact me at calhoun.peter@gmail.com.</span>
&nbsp;
&nbsp;
	<span style="color: #228B22;"># Tal edit: choosing if to work with a 2X2 table or with 4 numbers:</span>
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">is.<span style="">null</span></span><span style="color: #080;">&#40;</span>Tb<span style="color: #080;">&#41;</span> <span style="color: #080;">|</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">null</span></span><span style="color: #080;">&#40;</span>Tc<span style="color: #080;">&#41;</span> <span style="color: #080;">|</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">null</span></span><span style="color: #080;">&#40;</span>Td<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#123;</span>
		<span style="color: #228B22;"># If one of them is null, then Ta should have an entire table, and we can take it's values</span>
		<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">is.<span style="">table</span></span><span style="color: #080;">&#40;</span>Ta<span style="color: #080;">&#41;</span> <span style="color: #080;">|</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">matrix</span></span><span style="color: #080;">&#40;</span>Ta<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#123;</span>
			<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dim</span><span style="color: #080;">&#40;</span>Ta<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
			<span style="color: #080;">&#123;</span>
				Tb <span style="color: #080;">&lt;-</span> Ta<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
				Tc <span style="color: #080;">&lt;-</span> Ta<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
				Td <span style="color: #080;">&lt;-</span> Ta<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
				Ta <span style="color: #080;">&lt;-</span> Ta<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>		
			<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The table is not 2X2, please check it again...&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#125;</span>		
		<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;We are missing value in the table&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#125;</span>		
	<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
	c1<span style="color: #080;">&lt;-</span>Ta<span style="color: #080;">+</span>Tc
	c2<span style="color: #080;">&lt;-</span>Tb<span style="color: #080;">+</span>Td
	n<span style="color: #080;">&lt;-</span>c1<span style="color: #080;">+</span>c2
	pao<span style="color: #080;">&lt;-</span>Ta<span style="color: #080;">/</span>c1
	pbo<span style="color: #080;">&lt;-</span>Tb<span style="color: #080;">/</span>c2
	pxo<span style="color: #080;">&lt;-</span><span style="color: #080;">&#40;</span>Ta<span style="color: #080;">+</span>Tb<span style="color: #080;">&#41;</span><span style="color: #080;">/</span>n
	TXO<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span>pao<span style="color: #080;">-</span>pbo<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span>pxo<span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span>pxo<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c1<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	n1<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>c1<span style="color: #080;">&#41;</span>
	n2<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>c2<span style="color: #080;">&#41;</span>
&nbsp;
	P<span style="color: #080;">&lt;-</span><span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
	<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span> p <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">:</span><span style="color: #ff0000;">99</span><span style="color: #080;">+</span>.01<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#123;</span>
		TX<span style="color: #080;">&lt;-</span><span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
		S<span style="color: #080;">&lt;-</span><span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
		<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span> i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">:</span>c1<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#123;</span>
			<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span> j <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">:</span>c2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
			<span style="color: #080;">&#123;</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>i<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>fac1<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>fac1<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>i<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>fac2<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>fac2<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>c1<span style="color: #080;">-</span>i<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>fac3<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>fac3<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>c1<span style="color: #080;">-</span>i<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>c2<span style="color: #080;">-</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>fac4<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>fac4<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">prod</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>c2<span style="color: #080;">-</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
&nbsp;
				small.<span style="">s</span><span style="color: #080;">&lt;-</span><span style="color: #080;">&#40;</span>n1<span style="color: #080;">*</span>n2<span style="color: #080;">*</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">^</span><span style="color: #080;">&#40;</span>i<span style="color: #080;">+</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span>p<span style="color: #080;">&#41;</span><span style="color: #080;">^</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">-</span><span style="color: #080;">&#40;</span>i<span style="color: #080;">+</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span>fac1<span style="color: #080;">*</span>fac2<span style="color: #080;">*</span>fac3<span style="color: #080;">*</span>fac4<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
				S<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>S,small.<span style="">s</span><span style="color: #080;">&#41;</span>
				pa<span style="color: #080;">&lt;-</span> i<span style="color: #080;">/</span>c1
				pb<span style="color: #080;">&lt;-</span>j<span style="color: #080;">/</span>c2
				px <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>i<span style="color: #080;">+</span>j<span style="color: #080;">&#41;</span><span style="color: #080;">/</span>n
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">is.<span style="">nan</span></span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>pa<span style="color: #080;">-</span>pb<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span>px<span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span>px<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c1<span style="color: #080;">&#41;</span><span style="color: #080;">+</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
					<span style="color: #080;">&#123;</span>
						tx<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">0</span>
					<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
						tx <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>pa<span style="color: #080;">-</span>pb<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span>px<span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span>px<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c1<span style="color: #080;">&#41;</span><span style="color: #080;">+</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>c2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
					<span style="color: #080;">&#125;</span>
				TX<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>TX,tx<span style="color: #080;">&#41;</span>
			<span style="color: #080;">&#125;</span>
		<span style="color: #080;">&#125;</span>
&nbsp;
		P<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>P,<span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>S<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>TX<span style="color: #080;">&gt;=</span>TXO<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
&nbsp;
	np<span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>P<span style="color: #080;">==</span><span style="color: #0000FF; font-weight: bold;">max</span><span style="color: #080;">&#40;</span>P<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	p <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">:</span><span style="color: #ff0000;">99</span><span style="color: #080;">+</span>.01<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">100</span>
	nuisance<span style="color: #080;">&lt;-</span>p<span style="color: #080;">&#91;</span>np<span style="color: #080;">&#93;</span>
	pv<span style="color: #080;">&lt;-</span>P<span style="color: #080;">&#91;</span>np<span style="color: #080;">&#93;</span>
&nbsp;
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">print</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#123;</span> 
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The contingency table is:&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>Ta,Tc,Tb,Td<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Wald Statistic:&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>TXO<span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Nuisance parameter:&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>nuisance<span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The 1-tailed p-value:&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>pv<span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">plot</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#123;</span>
		<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>p,P,type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>,main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Barnard's exact P-value&quot;</span>, xlab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Nuisance parameter&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;P-value&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">points</span><span style="color: #080;">&#40;</span>nuisance,pv,<span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>	
			<span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>
					contingency.<span style="">table</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">as.<span style="">table</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>Ta,Tc,Tb,Td<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,
					Wald.<span style="">Statistic</span> <span style="color: #080;">=</span> TXO,
					Nuisance.<span style="">parameter</span> <span style="color: #080;">=</span> nuisance,
					p.<span style="">value</span>.<span style="">one</span>.<span style="">tailed</span> <span style="color: #080;">=</span> pv					
				<span style="color: #080;">&#41;</span>
			<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
Barnardextest<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">8</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">14</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">fisher.<span style="">test</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">8</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">14</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
Convictions <span style="color: #080;">&lt;-</span>
<span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>, <span style="color: #ff0000;">10</span>, <span style="color: #ff0000;">15</span>, <span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,
		   <span style="color: #0000FF; font-weight: bold;">nrow</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>,
		   <span style="color: #0000FF; font-weight: bold;">dimnames</span> <span style="color: #080;">=</span>
		   <span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Dizygotic&quot;</span>, <span style="color: #ff0000;">&quot;Monozygotic&quot;</span><span style="color: #080;">&#41;</span>,
				<span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Convicted&quot;</span>, <span style="color: #ff0000;">&quot;Not convicted&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
Convictions
<span style="color: #0000FF; font-weight: bold;">fisher.<span style="">test</span></span><span style="color: #080;">&#40;</span>Convictions, alternative <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;less&quot;</span><span style="color: #080;">&#41;</span>
Barnardextest<span style="color: #080;">&#40;</span>Convictions<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<blockquote><p>Fisher&#8217;s Exact Test for Count Data</p>
<p>data:  Convictions<br />
p-value = 0.0004652<br />
alternative hypothesis: true odds ratio is less than 1<br />
95 percent confidence interval:<br />
0.0000000 0.2849601<br />
sample estimates:<br />
odds ratio<br />
0.04693661</p></blockquote>
<blockquote><p>$contingency.table<br />
A  B<br />
A  2 15<br />
B 10  3</p>
<p>$Wald.Statistic<br />
[1] 3.609941</p>
<p>$Nuisance.parameter<br />
[1] 0.4401</p>
<p>$p.value.one.tailed<br />
[1] 0.0001528846</p></blockquote>
<p><strong>Final note</strong>: I would like to thank <strong>Peter Calhoun</strong> again for sharing his code with the rest of us &#8211; Thanks Peter!</p>
<p><strong>Update (21.04.2010)</strong>: In case you are facing a table with structural zeros (that is, missing values in the table), the package <a href="http://cran.r-project.org/web/packages/aylmer/index.html">aylmer</a> might be able to help you (it offers a generalization of Fisher&#8217;s exact test)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
