<?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; non-parametric test</title>
	<atom:link href="http://www.r-statistics.com/tag/non-parametric-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>Correlation scatter-plot matrix for ordered-categorical data</title>
		<link>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/</link>
		<comments>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 21:37:26 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[correlation]]></category>
		<category><![CDATA[correlation matrix]]></category>
		<category><![CDATA[correlation scatter plot]]></category>
		<category><![CDATA[non-parametric]]></category>
		<category><![CDATA[non-parametric test]]></category>
		<category><![CDATA[nonparametric]]></category>
		<category><![CDATA[nonparametric test]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[scatter plot]]></category>
		<category><![CDATA[scatter plot matrix]]></category>
		<category><![CDATA[spearman correlation]]></category>
		<category><![CDATA[spearman test]]></category>
		<category><![CDATA[stackoverflow]]></category>
		<category><![CDATA[survey]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=256</guid>
		<description><![CDATA[When analyzing a questionnaire, one often wants to view the correlation between two or more Likert questionnaire item&#8217;s (for example: two ordered categorical vectors ranging from 1 to 5). When dealing with several such Likert variable&#8217;s, a clear presentation of all the pairwise relation&#8217;s between our variable can be achieved by inspecting the (Spearman) correlation matrix (easily achieved in R by using the &#8220;cor.test&#8221; command on a matrix of variables). Yet, a challenge appears once we wish to plot this [...]]]></description>
			<content:encoded><![CDATA[<p>When analyzing a questionnaire, one often wants to view the correlation between two or more <a href="http://en.wikipedia.org/wiki/Likert_scale">Likert questionnaire</a> item&#8217;s (for example: two ordered categorical vectors ranging from 1 to 5).</p>
<p>When dealing with several such Likert variable&#8217;s, a clear presentation of all the pairwise relation&#8217;s between our variable can be achieved by inspecting the (Spearman) correlation matrix (easily achieved in R by using the &#8220;cor.test&#8221; command on a matrix of variables).<br />
Yet, a challenge appears once we wish to plot this correlation matrix.  The challenge stems from the fact that the classic presentation for a correlation matrix is a <strong>scatter plot matrix</strong> &#8211; but scatter plots don&#8217;t (usually) work well for ordered categorical vectors since the dots on the scatter plot often overlap each other.</p>
<p>There are four solution for the point-overlap problem that I know of:</p>
<ol>
<li>Jitter the data a bit to give a sense of the &#8220;density&#8221; of the points</li>
<li>Use a color spectrum to represent when a point actually represent &#8220;many points&#8221;</li>
<li>Use different points sizes to represent when there are &#8220;many points&#8221; in the location of that point</li>
<li>Add a LOWESS (or LOESS) line to the scatter plot &#8211; to show the trend of the data</li>
</ol>
<p>In this post I will offer the code for the  a solution that uses solution 3-4 (and possibly 2, please read this post comments). Here is the output (click to see a larger image):</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2010/04/scatter-plot-correlation-matrix.png"><img class="alignnone size-full wp-image-257" title="scatter plot correlation matrix" src="http://www.r-statistics.com/wp-content/uploads/2010/04/scatter-plot-correlation-matrix.png" alt="" width="550"/></a></p>
<p>And here is the code to produce this plot:</p>
<p><span id="more-256"></span></p>
<h3>R code for producing a Correlation scatter-plot matrix &#8211; for ordered-categorical data</h3>
<p><strong>Note</strong> that this code will work fine for continues data points (although I might suggest to enlarge the &#8220;point.size.rescale&#8221; parameter to something bigger then 1.5 in the &#8220;panel.smooth.ordered.categorical&#8221; function)</p>

<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
</pre></td><td class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;"># -----------------</span>
<span style="color: #228B22;"># Functions</span>
<span style="color: #228B22;"># -----------------</span>
&nbsp;
panel.<span style="">cor</span>.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x, y, digits<span style="color: #080;">=</span><span style="color: #ff0000;">2</span>, prefix<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span>, cex.<span style="">cor</span><span style="color: #080;">&#41;</span> 
<span style="color: #080;">&#123;</span>
&nbsp;
    usr <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;usr&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">;</span> <span style="color: #0000FF; font-weight: bold;">on.<span style="">exit</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr <span style="color: #080;">=</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: #ff0000;">1</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
&nbsp;
    r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cor</span><span style="color: #080;">&#40;</span>x, y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notive we use spearman, non parametric correlation here</span>
    r.<span style="">no</span>.<span style="">abs</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">cor</span><span style="color: #080;">&#40;</span>x, y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
    txt <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">format</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>r.<span style="">no</span>.<span style="">abs</span> , <span style="color: #ff0000;">0.123456789</span><span style="color: #080;">&#41;</span>, digits<span style="color: #080;">=</span>digits<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> 
    txt <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span>prefix, txt, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">missing</span><span style="color: #080;">&#40;</span>cex.<span style="">cor</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> cex <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.8</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">strwidth</span><span style="color: #080;">&#40;</span>txt<span style="color: #080;">&#41;</span> 
&nbsp;
    test <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">cor.<span style="">test</span></span><span style="color: #080;">&#40;</span>x,y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #228B22;"># borrowed from printCoefmat</span>
    Signif <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">symnum</span><span style="color: #080;">&#40;</span>test$p.<span style="">value</span>, corr <span style="color: #080;">=</span> FALSE, na <span style="color: #080;">=</span> FALSE, 
                  cutpoints <span style="color: #080;">=</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: #ff0000;">0.001</span>, <span style="color: #ff0000;">0.01</span>, <span style="color: #ff0000;">0.05</span>, <span style="color: #ff0000;">0.1</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>,
                  <span style="color: #0000FF; font-weight: bold;">symbols</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;">&quot;***&quot;</span>, <span style="color: #ff0000;">&quot;**&quot;</span>, <span style="color: #ff0000;">&quot;*&quot;</span>, <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">text</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0.5</span>, <span style="color: #ff0000;">0.5</span>, txt, cex <span style="color: #080;">=</span> cex <span style="color: #080;">*</span> r<span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">text</span><span style="color: #080;">&#40;</span>.8, .8, Signif, cex<span style="color: #080;">=</span>cex, <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;
&nbsp;
&nbsp;
&nbsp;
panel.<span style="">smooth</span>.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span> <span style="color: #080;">&#40;</span>x, y, <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;col&quot;</span><span style="color: #080;">&#41;</span>, bg <span style="color: #080;">=</span> NA, pch <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;pch&quot;</span><span style="color: #080;">&#41;</span>, 
												cex <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span>, col.<span style="">smooth</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;red&quot;</span>, span <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span><span style="color: #080;">/</span><span style="color: #ff0000;">3</span>, iter <span style="color: #080;">=</span> <span style="color: #ff0000;">3</span>, 
												point.<span style="">size</span>.<span style="">rescale</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1.5</span>, ...<span style="color: #080;">&#41;</span> 
<span style="color: #080;">&#123;</span>
	<span style="color: #228B22;">#require(colorspace)</span>
    <span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#41;</span>
    z <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">merge</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>, melt<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">table</span><span style="color: #080;">&#40;</span>x ,y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">sort</span> <span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>$value
    <span style="color: #228B22;">#the.col &lt;- heat_hcl(length(x))[z]</span>
    z <span style="color: #080;">&lt;-</span> point.<span style="">size</span>.<span style="">rescale</span><span style="color: #080;">*</span>z<span style="color: #080;">/</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notice how we rescale the dots accourding to the maximum z could have gotten</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">symbols</span><span style="color: #080;">&#40;</span> x, y,  circles <span style="color: #080;">=</span> z,<span style="color: #228B22;">#rep(0.1, length(x)), #sample(1:2, length(x), replace = T) ,</span>
			inches<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span>, bg<span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;grey&quot;</span>,<span style="color: #228B22;">#the.col ,</span>
			fg <span style="color: #080;">=</span> bg, add <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
&nbsp;
    <span style="color: #228B22;"># points(x, y, pch = pch, col = col, bg = bg, cex = cex)</span>
    ok <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">finite</span></span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">finite</span></span><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">any</span><span style="color: #080;">&#40;</span>ok<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
        <span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span>stats<span style="color: #080;">::</span><span style="color: #0000FF; font-weight: bold;">lowess</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>ok<span style="color: #080;">&#93;</span>, y<span style="color: #080;">&#91;</span>ok<span style="color: #080;">&#93;</span>, f <span style="color: #080;">=</span> span, iter <span style="color: #080;">=</span> iter<span style="color: #080;">&#41;</span>, 
            <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> col.<span style="">smooth</span>, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
panel.<span style="">hist</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#123;</span>
    usr <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;usr&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">;</span> <span style="color: #0000FF; font-weight: bold;">on.<span style="">exit</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">1.5</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
    h <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">hist</span><span style="color: #080;">&#40;</span>x, <span style="color: #0000FF; font-weight: bold;">plot</span> <span style="color: #080;">=</span> FALSE, br <span style="color: #080;">=</span> <span style="color: #ff0000;">20</span><span style="color: #080;">&#41;</span>
    breaks <span style="color: #080;">&lt;-</span> h$breaks<span style="color: #080;">;</span> nB <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>breaks<span style="color: #080;">&#41;</span>
    y <span style="color: #080;">&lt;-</span> h$counts<span style="color: #080;">;</span> y <span style="color: #080;">&lt;-</span> y<span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">max</span><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">rect</span><span style="color: #080;">&#40;</span>breaks<span style="color: #080;">&#91;</span><span style="color: #080;">-</span>nB<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">0</span>, breaks<span style="color: #080;">&#91;</span><span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>, y, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;orange&quot;</span>, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
pairs.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>xx,...<span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#123;</span>
			<span style="color: #0000FF; font-weight: bold;">pairs</span><span style="color: #080;">&#40;</span>xx , 
					diag.<span style="">panel</span> <span style="color: #080;">=</span> panel.<span style="">hist</span> ,
					lower.<span style="">panel</span><span style="color: #080;">=</span>panel.<span style="">smooth</span>.<span style="">ordered</span>.<span style="">categorical</span>,
					upper.<span style="">panel</span><span style="color: #080;">=</span>panel.<span style="">cor</span>.<span style="">ordered</span>.<span style="">categorical</span>,
					cex.<span style="">labels</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1.5</span>, ...<span style="color: #080;">&#41;</span> 
		<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #228B22;"># -----------------</span>
<span style="color: #228B22;"># Example</span>
<span style="color: #228B22;"># -----------------</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">set.<span style="">seed</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">666</span><span style="color: #080;">&#41;</span>
a1 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sample</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">5</span>, <span style="color: #ff0000;">100</span>, <span style="color: #0000FF; font-weight: bold;">replace</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
a2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sample</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">5</span>, <span style="color: #ff0000;">100</span>, <span style="color: #0000FF; font-weight: bold;">replace</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
a3 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">jitter</span><span style="color: #080;">&#40;</span>a2, <span style="color: #ff0000;">7</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
	a3<span style="color: #080;">&#91;</span>a3 <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span> <span style="color: #080;">|</span> a3 <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span>
a4 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">6</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">jitter</span><span style="color: #080;">&#40;</span>a1, <span style="color: #ff0000;">7</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
	a4<span style="color: #080;">&#91;</span>a4 <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span> <span style="color: #080;">|</span> a4 <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span>
&nbsp;
aa <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>a1,a2,a3, a4<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># plotting :)		</span>
pairs.<span style="">ordered</span>.<span style="">categorical</span><span style="color: #080;">&#40;</span>aa<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<h3> Credits: </h3>
<ul>
<li>The original R code for the correlation matrix plot was taken from <a href="http://addictedtor.free.fr/graphiques/graphcode.php?graph=137">R Graph Gallery</a> (The differences are: 1) The use of spearman correlation;  2) The adding of hist panel and;  3) The changing of points sizes</li>
<li>The idea to use symbols for changing the point sizes was <a href="http://stackoverflow.com/questions/2593643/correlation-scatter-matrix-plot-with-different-point-size-in-r">offered</a> by <a href="http://www.linkedin.com/pub/doug-y-barbo/2/356/416">Doug Y&#8217;barbo</a>.<br />
And also to<a href="http://dirk.eddelbuettel.com/"> Dirk Eddelbuettel </a>for offering to use cex (although I ended up not using that)</li>
</ul>
<p>If you got ideas on how to improve this code (or reproducing it with ggplot2 or lattice), please do so in the comments (or on your own blog, but be sure to let me know <img src='http://www.r-statistics.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   )</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Siegel-Tukey: a Non-parametric test for equality in variability (R code)</title>
		<link>http://www.r-statistics.com/2010/02/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/</link>
		<comments>http://www.r-statistics.com/2010/02/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 21:13:51 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[non-parametric]]></category>
		<category><![CDATA[non-parametric test]]></category>
		<category><![CDATA[nonparametric]]></category>
		<category><![CDATA[nonparametric test]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[Siegel]]></category>
		<category><![CDATA[Siegel-Tukey]]></category>
		<category><![CDATA[Tukey]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=161</guid>
		<description><![CDATA[Daniel Malter just shared on the R mailing list (link to the thread) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability. Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied &#8220;yes&#8221;. From here on I copy his note at full. p.s: (The R function can be downloaded from here) * * * * Hi, I recently ran into the problem that I needed a Siegel-Tukey test [...]]]></description>
			<content:encoded><![CDATA[<p>Daniel Malter just shared on the R mailing list (<a href="http://n4.nabble.com/Siegel-Tukey-test-for-equal-variability-code-td1565053.html">link to the thread</a>) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability.<br />
Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied &#8220;yes&#8221;.<br />
From here on I copy his note at full.</p>
<p>p.s: (The R function can be <a href="http://www.r-statistics.com/wp-content/uploads/2010/02/siegel-tukey-non-parametric-test-for-equal-variance.r.txt">downloaded from here</a>)</p>
<p>*  *  *  *<br />
<span id="more-161"></span></p>
<p>Hi, I recently ran into the problem that I needed a Siegel-Tukey test for equal variability based on ranks. Maybe there is a package that has it implemented, but I could not find it. So I programmed an R function to do it. The Siegel-Tukey test requires to recode the ranks so that they express variability rather than ascending order. This is essentially what the code further below does. After the rank  transformation, a regular Mann-Whitney U test is applied. The &#8220;manual&#8221; and code are pasted below.</p>
<p><strong><span style="text-decoration: underline;">Description</span></strong>:  Non-parametric Siegel-Tukey test for equality in variability. The null hypothesis is that the variability of x is equal between two groups. A rejection of the null indicates that variability differs between<br />
the two groups.</p>
<p><strong><span style="text-decoration: underline;">Usage:</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="rsplus" style="font-family:monospace;">siegel.<span style="">tukey</span><span style="color: #080;">&#40;</span>x,y,id.<span style="">col</span><span style="color: #080;">=</span>FALSE,adjust.<span style="">median</span><span style="color: #080;">=</span>FALSE,rnd<span style="color: #080;">=</span><span style="color: #ff0000;">8</span>, ...<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><strong><span style="text-decoration: underline;">Arguments:</span></strong></p>
<p>x: a vector of data</p>
<p>y: Data of the second group (if id.col=FALSE) or group indicator (if id.col=TRUE). In the latter case, y MUST take 1 or 2 to indicate observations of group 1 and 2, respectively, and x must contain the data for both groups.</p>
<p>id.col: If FALSE (default), then x and y are the data columns for group 1 and 2, respectively. If TRUE, the y is the group indicator.</p>
<p>adjust.median: Should between-group differences in medians be leveled before performing the test? In certain cases, the Siegel-Tukey test is susceptible to median differences and may indicate significant differences in variability that, in reality, stem from differences in medians.</p>
<p>rnd: Should the data be rounded and, if so, to which decimal? The default (-1) uses the data as is. Otherwise, rnd must be a non-negative integer. Typically, this option is not needed. However, occasionally, differences in<br />
the precision with which certain functions return values cause the merging of two data frames to fail within the siegel.tukey function. Only then  rounding is necessary. This operation should not be performed if it affects<br />
the ranks of observations.</p>
<p>&#8230; arguments passed on to the Wilcoxon test. See ?wilcox.test</p>
<p><strong><span style="text-decoration: underline;">Value</span></strong>: Among other output, the function returns rank sums for the two groups, the associated Wilcoxon&#8217;s W, and the p-value for a Wilcoxon test on tie-adjusted Siegel-Tukey ranks (i.e., it performs and returns a<br />
Siegel-Tukey test). If significant, the group with the smaller rank sum has greater variability.</p>
<p><strong><span style="text-decoration: underline;">References</span></strong>: Sidney Siegel and John Wilder Tukey (1960) &#8220;A nonparametric sum of ranks procedure for relative spread in unpaired samples.&#8221; Journal of the<br />
American Statistical Association. See also, David J. Sheskin (2004) &#8221;Handbook of parametric and nonparametric statistical procedures.&#8221; 3rd<br />
edition. Chapman and Hall/CRC. Boca Raton, FL.</p>
<p><strong><span style="text-decoration: underline;">Notes</span></strong>: The Siegel-Tukey test has relatively low power and may, under certain conditions, indicate significance due to differences in medians rather than<br />
differences in variabilities (consider using the argument adjust.median).</p>
<p><strong><span style="text-decoration: underline;">Output</span></strong> (in this order)</p>
<p style="padding-left: 30px;">1. Group medians<br />
2. Wilcoxon-test for between-group differences in median (after the median<br />
adjustment if specified)<br />
3. Unique values of x and their tie-adjusted Siegel-Tukey ranks<br />
4. Xs of group 1 and their tie-adjusted Siegel-Tukey ranks<br />
5. Xs of group 2 and their tie-adjusted Siegel-Tukey ranks<br />
6. Siegel-Tukey test (Wilcoxon test on tie-adjusted Siegel-Tukey ranks)</p>
<p><strong>And here is the code:</strong></p>

<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
</pre></td><td class="code"><pre class="rsplus" style="font-family:monospace;">siegel.<span style="">tukey</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x,y,id.<span style="">col</span><span style="color: #080;">=</span>FALSE,adjust.<span style="">median</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span>,rnd<span style="color: #080;">=-</span><span style="color: #ff0000;">1</span>,alternative<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;two.sided&quot;</span>,mu<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>,paired<span style="color: #080;">=</span>FALSE,exact<span style="color: #080;">=</span>FALSE,correct<span style="color: #080;">=</span>TRUE,conf.<span style="">int</span><span style="color: #080;">=</span>FALSE,conf.<span style="">level</span><span style="color: #080;">=</span><span style="color: #ff0000;">0.95</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>id.<span style="">col</span><span style="color: #080;">==</span>FALSE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
   <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">rep</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;">1</span>,<span style="color: #ff0000;">2</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: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>y<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;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
	<span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>
   <span style="color: #080;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><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;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;y&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">order</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>rnd<span style="color: #080;">&gt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x,rnd<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>adjust.<span style="">median</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: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">-</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</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: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">-</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</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;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Median of group 1 = &quot;</span>,<span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Median of group 2 = &quot;</span>,<span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Test of median differences&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&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;">wilcox.<span style="">test</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
 a<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,each<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
 b<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rep</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;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
 rk.<span style="">up</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;">1</span>,<span style="color: #080;">&#40;</span>a<span style="color: #080;">*</span><span style="color: #ff0000;">4</span><span style="color: #080;">+</span>b<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<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;">&#93;</span>
 rk.<span style="">down</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rev</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>a<span style="color: #080;">*</span><span style="color: #ff0000;">4</span><span style="color: #080;">+</span>b<span style="color: #080;">-</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">floor</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<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;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Performing Siegel-Tukey rank transformation...&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 rks<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>rk.<span style="">up</span>,rk.<span style="">down</span><span style="color: #080;">&#41;</span>
 unqs<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">unique</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">sort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
 corr.<span style="">rks</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">tapply</span><span style="color: #080;">&#40;</span>rks,<span style="color: #0000FF; font-weight: bold;">data</span>$x,<span style="color: #0000FF; font-weight: bold;">mean</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>unqs,corr.<span style="">rks</span><span style="color: #080;">&#41;</span>
 rks.<span style="">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>unqs,corr.<span style="">rks</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span><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;">&quot;unique values of x&quot;</span>,<span style="color: #ff0000;">&quot;tie-adjusted Siegel-Tukey rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span>,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span><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;">&quot;unqs&quot;</span>,<span style="color: #ff0000;">&quot;corr.rks&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">merge</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>,rks.<span style="">data</span>,by.<span style="">x</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;x&quot;</span>,by.<span style="">y</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;unqs&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 rk1<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$corr.<span style="">rks</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
 rk2<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$corr.<span style="">rks</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;Tie-adjusted Siegel-Tukey ranks of group 1&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 group1<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,rk1<span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>group1<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;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>group1,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;Tie-adjusted Siegel-Tukey ranks of group 2&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 group2<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>,rk2<span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>group2<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;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>group2,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Siegel-Tukey test&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Siegel-Tukey rank transformation performed.&quot;</span>,<span style="color: #ff0000;">&quot;Tie adjusted ranks computed.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>adjust.<span style="">median</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: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Medians adjusted to equality.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</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;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Medians not adjusted.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Rank sum of group 1 =&quot;</span>, <span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>rk1<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;    Rank sum of group 2 =&quot;</span>,<span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>rk2<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">wilcox.<span style="">test</span></span><span style="color: #080;">&#40;</span>rk1,rk2,alternative<span style="color: #080;">=</span>alternative,mu<span style="color: #080;">=</span>mu,paired<span style="color: #080;">=</span>paired,exact<span style="color: #080;">=</span>exact,correct<span style="color: #080;">=</span>correct,conf.<span style="">int</span><span style="color: #080;">=</span>conf.<span style="">int</span>,conf.<span style="">level</span><span style="color: #080;">=</span>conf.<span style="">level</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">#Example:</span>
&nbsp;
x<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">4</span>,<span style="color: #ff0000;">4</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">6</span>,<span style="color: #ff0000;">6</span><span style="color: #080;">&#41;</span>
y<span style="color: #080;">=</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: #ff0000;">0</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">9</span>,<span style="color: #ff0000;">10</span>,<span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>
&nbsp;
siegel.<span style="">tukey</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<pre>
<strong>Here is the code output:</strong>
<div id="_mcePaste" style="padding-left: 30px;">Median of group 1 =  5</div>
<div id="_mcePaste" style="padding-left: 30px;">Median of group 2 =  5</div>
<div id="_mcePaste" style="padding-left: 30px;">Test of median differences</div>
<div id="_mcePaste" style="padding-left: 30px;">Wilcoxon rank sum test with continuity correction</div>
<div id="_mcePaste" style="padding-left: 30px;">data:  data$x[data$y == 1] and data$x[data$y == y]</div>
<div id="_mcePaste" style="padding-left: 30px;">W = 1, p-value = 0.4274</div>
<div id="_mcePaste" style="padding-left: 30px;">alternative hypothesis: true location shift is not equal to 0</div>
<div id="_mcePaste" style="padding-left: 30px;">Performing Siegel-Tukey rank transformation...</div>
<div id="_mcePaste" style="padding-left: 30px;">unique values of x tie-adjusted Siegel-Tukey rank</div>
<div id="_mcePaste" style="padding-left: 30px;">0                            2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">1                            5.0</div>
<div id="_mcePaste" style="padding-left: 30px;">4                            8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5                           11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6                            8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">9                            6.0</div>
<div id="_mcePaste" style="padding-left: 30px;">10                            2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Tie-adjusted Siegel-Tukey ranks of group 1</div>
<div id="_mcePaste" style="padding-left: 30px;">x rank</div>
<div id="_mcePaste" style="padding-left: 30px;">4  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">4  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5 11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5 11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Tie-adjusted Siegel-Tukey ranks of group 2</div>
<div id="_mcePaste" style="padding-left: 30px;">x rank</div>
<div id="_mcePaste" style="padding-left: 30px;">0  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">0  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">1  5.0</div>
<div id="_mcePaste" style="padding-left: 30px;">9  6.0</div>
<div id="_mcePaste" style="padding-left: 30px;">10  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">10  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Siegel-Tukey test</div>
<div id="_mcePaste" style="padding-left: 30px;">Siegel-Tukey rank transformation performed. Tie adjusted ranks computed.</div>
<div id="_mcePaste" style="padding-left: 30px;">Medians not adjusted.</div>
<div id="_mcePaste" style="padding-left: 30px;">Rank sum of group 1 = 57     Rank sum of group 2 = 21</div>
<div id="_mcePaste" style="padding-left: 30px;">Wilcoxon rank sum test with continuity correction</div>
<div id="_mcePaste" style="padding-left: 30px;">data:  rk1 and rk2</div>
<div id="_mcePaste" style="padding-left: 30px;">W = 36, p-value = 0.003601</div>
<div id="_mcePaste" style="padding-left: 30px;">alternative hypothesis: true location shift is not equal to 0</div>
<div id="_mcePaste" style="padding-left: 30px;">Warning message:</div>
<div id="_mcePaste" style="padding-left: 30px;">In wilcox.test.default(data$x[data$y == 1], data$x[data$y == y]) :</div>
<div id="_mcePaste" style="padding-left: 30px;">cannot compute exact p-value with ties</div>
<p style="padding-left: 30px;">Median of group 1 =  5 Median of group 2 =  5  Test of median differences
Wilcoxon rank sum test with continuity correction
data:  data$x[data$y == 1] and data$x[data$y == y] W = 1, p-value = 0.4274alternative hypothesis: true location shift is not equal to 0
Performing Siegel-Tukey rank transformation...   unique values of x tie-adjusted Siegel-Tukey rank                  0                            2.5                  1                            5.0                  4                            8.5                  5                           11.5                  6                            8.5                  9                            6.0                 10                            2.5
Tie-adjusted Siegel-Tukey ranks of group 1  x rank 4  8.5 4  8.5 5 11.5 5 11.5 6  8.5 6  8.5
Tie-adjusted Siegel-Tukey ranks of group 2   x rank  0  2.5  0  2.5  1  5.0  9  6.0 10  2.5 10  2.5
Siegel-Tukey test Siegel-Tukey rank transformation performed. Tie adjusted ranks computed. Medians not adjusted. Rank sum of group 1 = 57     Rank sum of group 2 = 21
Wilcoxon rank sum test with continuity correction
data:  rk1 and rk2 W = 36, p-value = 0.003601alternative hypothesis: true location shift is not equal to 0
Warning message:In wilcox.test.default(data$x[data$y == 1], data$x[data$y == y]) :  cannot compute exact p-value with ties
</pre>
<p>(The R function can be <a href="http://www.r-statistics.com/wp-content/uploads/2010/02/siegel-tukey-non-parametric-test-for-equal-variance.r.txt">downloaded from here</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/02/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
