<?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; tables</title>
	<atom:link href="http://www.r-statistics.com/tag/tables/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>Mon, 30 Jan 2012 07:45:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Printing nested tables in R &#8211; bridging between the {reshape} and {tables} packages</title>
		<link>http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/</link>
		<comments>http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 09:41:38 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[aggregate]]></category>
		<category><![CDATA[aggregation]]></category>
		<category><![CDATA[cast]]></category>
		<category><![CDATA[cast_df]]></category>
		<category><![CDATA[data processing]]></category>
		<category><![CDATA[data.frame]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[melt]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[reshape]]></category>
		<category><![CDATA[reshaping]]></category>
		<category><![CDATA[Rnw]]></category>
		<category><![CDATA[Sweave]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[tabular]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=921</guid>
		<description><![CDATA[This post shows how to print a prettier nested pivot table, created using the {reshape} package (similar to what you would get with Microsoft Excel), so you could print it either in the R terminal or as a LaTeX table. This task is done by bridging between the cast_df object produced by the {reshape} package, [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:right;"><div class="socialize-in-button socialize-in-button-right"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/&amp;layout=box_count&amp;show_faces=false&amp;width=50&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/"></g:plusone></div></div><p><strong>This post shows how to print a <u>prettier</u> nested pivot table, created using the {reshape} package (similar to what you would get with Microsoft Excel), so you could print it either in the R terminal or as a LaTeX table.</strong>  This task is done by bridging between the cast_df object produced by the {reshape} package, and the tabular function introduced by the new {tables} package.</p>
<p>Here is an example of the type of output we wish to produce in the R terminal:</p>

<div class="wp_codebox"><table><tr id="p9214"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p921code4"><pre class="rsplus" style="font-family:monospace;">       ozone       solar.<span style="">r</span>        wind         temp       
 month <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>    <span style="color: #0000FF; font-weight: bold;">sd</span>     <span style="color: #0000FF; font-weight: bold;">mean</span>   <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>   
 <span style="color: #ff0000;">5</span>     <span style="color: #ff0000;">23.62</span> <span style="color: #ff0000;">22.22</span> <span style="color: #ff0000;">181.3</span>   <span style="color: #ff0000;">115.08</span> <span style="color: #ff0000;">11.623</span> <span style="color: #ff0000;">3.531</span> <span style="color: #ff0000;">65.55</span> <span style="color: #ff0000;">6.855</span>
 <span style="color: #ff0000;">6</span>     <span style="color: #ff0000;">29.44</span> <span style="color: #ff0000;">18.21</span> <span style="color: #ff0000;">190.2</span>    <span style="color: #ff0000;">92.88</span> <span style="color: #ff0000;">10.267</span> <span style="color: #ff0000;">3.769</span> <span style="color: #ff0000;">79.10</span> <span style="color: #ff0000;">6.599</span>
 <span style="color: #ff0000;">7</span>     <span style="color: #ff0000;">59.12</span> <span style="color: #ff0000;">31.64</span> <span style="color: #ff0000;">216.5</span>    <span style="color: #ff0000;">80.57</span>  <span style="color: #ff0000;">8.942</span> <span style="color: #ff0000;">3.036</span> <span style="color: #ff0000;">83.90</span> <span style="color: #ff0000;">4.316</span>
 <span style="color: #ff0000;">8</span>     <span style="color: #ff0000;">59.96</span> <span style="color: #ff0000;">39.68</span> <span style="color: #ff0000;">171.9</span>    <span style="color: #ff0000;">76.83</span>  <span style="color: #ff0000;">8.794</span> <span style="color: #ff0000;">3.226</span> <span style="color: #ff0000;">83.97</span> <span style="color: #ff0000;">6.585</span>
 <span style="color: #ff0000;">9</span>     <span style="color: #ff0000;">31.45</span> <span style="color: #ff0000;">24.14</span> <span style="color: #ff0000;">167.4</span>    <span style="color: #ff0000;">79.12</span> <span style="color: #ff0000;">10.180</span> <span style="color: #ff0000;">3.461</span> <span style="color: #ff0000;">76.90</span> <span style="color: #ff0000;">8.356</span></pre></td></tr></table></div>

<p>Or in a latex document:</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2012/01/cast_df-to-tabular1.png"><img src="http://www.r-statistics.com/wp-content/uploads/2012/01/cast_df-to-tabular1-300x89.png" alt="" title="cast_df to tabular" width="300" height="89" class="aligncenter size-medium wp-image-929" /></a></p>
<p><span id="more-921"></span></p>
<h3>Motivation: creating pretty nested tables</h3>
<p>In a recent post we learned <a href="http://www.r-statistics.com/2012/01/aggregation-and-restructuring-data-from-r-in-action/">how to use the {reshape} package</a> (by <a href="http://had.co.nz/">Hadley Wickham</a>) in order to aggregate and reshape data (in R) using the melt and cast functions.</p>
<p>The cast function is wonderful but it has one problem &#8211; the format of the output.  As opposed to a pivot table in (for example) MS excel, the output of a nested table created by cast is very &#8220;flat&#8221;.  That is, there is only one row for the header, and only one column for the row names.  So for both the R terminal, or an Sweave document, when we deal with a more complex reshaping/aggregating, the result is <strong>not </strong>something you would be proud to send to a journal.</p>
<h3>The opportunity: the {tables} package</h3>
<p>The good news is that Duncan Murdoch have <a href="http://www.mail-archive.com/r-help@r-project.org/msg154819.html">recently</a> released a new package to CRAN called <a href="http://cran.r-project.org/web/packages/tables/">{tables}</a>.  The {tables} package can compute and display complex tables of summary statistics and turn them into nice looking tables in Sweave (LaTeX) documents.  For using the full power of this package, you are invited to read through its detailed (and well written) 23 pages <a href="http://cran.r-project.org/web/packages/tables/vignettes/tables.pdf">Vignette</a>.  However, some of us might have preferred to keep using the syntax of the {reshape} package, while also benefiting from the great formatting that is offered by the new {tables} package.  For this purpose, I devised a function that bridges between cast_df (from {reshape}) and the tabular function (from {tables}).</p>
<h3>The bridge: between the {tables} and the {reshape} packages</h3>
<p>The code for the function is available on my github (link: <a href="https://github.com/talgalili/R-code-snippets/blob/master/tabular.cast_df.r">tabular.cast_df.r on github</a>)  and it seems to works fine as far as I can see (though I wouldn&#8217;t run it on larger data files since it relies on melting a cast_df object.)</p>
<p>Here is an example for how to load and use the function:</p>

<div class="wp_codebox"><table><tr id="p9215"><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
</pre></td><td class="code" id="p921code5"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">######################</span>
<span style="color: #228B22;"># Loading the functions</span>
<span style="color: #228B22;">######################</span>
<span style="color: #228B22;"># Making sure we can source code from github</span>
<span style="color: #0000FF; font-weight: bold;">source</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;http://www.r-statistics.com/wp-content/uploads/2012/01/source_https.r.txt&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># Reading in the function for using tabular on a cast_df object:</span>
source_https<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;https://raw.github.com/talgalili/R-code-snippets/master/tabular.cast_df.r&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #228B22;">######################</span>
<span style="color: #228B22;"># example:</span>
<span style="color: #228B22;">######################</span>
&nbsp;
<span style="color: #228B22;">############</span>
<span style="color: #228B22;"># Loading and preparing some data</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>
<span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span><span style="color: #CC9900; font-weight: bold;">airquality</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">tolower</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span><span style="color: #CC9900; font-weight: bold;">airquality</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
airquality2 <span style="color: #080;">&lt;-</span> <span style="color: #CC9900; font-weight: bold;">airquality</span>
airquality2$temp2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">ifelse</span><span style="color: #080;">&#40;</span>airquality2$temp <span style="color: #080;">&gt;</span> <span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span>airquality2$temp<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;hot&quot;</span>, <span style="color: #ff0000;">&quot;cold&quot;</span><span style="color: #080;">&#41;</span>
aqm <span style="color: #080;">&lt;-</span> melt<span style="color: #080;">&#40;</span>airquality2, id<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;month&quot;</span>, <span style="color: #ff0000;">&quot;day&quot;</span>,<span style="color: #ff0000;">&quot;temp2&quot;</span><span style="color: #080;">&#41;</span>, na.<span style="">rm</span><span style="color: #080;">=</span>TRUE<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">colnames</span><span style="color: #080;">&#40;</span>aqm<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;variable2&quot;</span>	<span style="color: #228B22;"># because otherwise the function is having problem when relying on the melt function of the cast object</span>
<span style="color: #0000FF; font-weight: bold;">head</span><span style="color: #080;">&#40;</span>aqm,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#  month day temp2 variable2 value</span>
<span style="color: #228B22;">#1     5   1  cold     ozone    41</span>
<span style="color: #228B22;">#2     5   2  cold     ozone    36</span>
<span style="color: #228B22;">#3     5   3  cold     ozone    12</span>
&nbsp;
<span style="color: #228B22;">############</span>
<span style="color: #228B22;"># Running the example:</span>
tabular.<span style="">cast_df</span><span style="color: #080;">&#40;</span>cast<span style="color: #080;">&#40;</span>aqm, month ~ variable2, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span>,<span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
tabular<span style="color: #080;">&#40;</span>cast<span style="color: #080;">&#40;</span>aqm, month ~ variable2, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span>,<span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notice how we turned tabular to be an S3 method that can deal with a cast_df object</span>
Hmisc<span style="color: #080;">::</span><span style="">latex</span><span style="color: #080;">&#40;</span>tabular<span style="color: #080;">&#40;</span>cast<span style="color: #080;">&#40;</span>aqm, month ~ variable2, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span>,<span style="color: #0000FF; font-weight: bold;">sd</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: #228B22;"># this is what we would have used for an Sweave document</span></pre></td></tr></table></div>

<p>And here are the results in the terminal:</p>

<div class="wp_codebox"><table><tr id="p9216"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p921code6"><pre class="rsplus" style="font-family:monospace;"><span style="color: #080;">&gt;</span> 
<span style="color: #080;">&gt;</span> tabular.<span style="">cast_df</span><span style="color: #080;">&#40;</span>cast<span style="color: #080;">&#40;</span>aqm, month ~ variable2, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span>,<span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
       ozone       solar.<span style="">r</span>        wind         temp       
 month <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>    <span style="color: #0000FF; font-weight: bold;">sd</span>     <span style="color: #0000FF; font-weight: bold;">mean</span>   <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>   
 <span style="color: #ff0000;">5</span>     <span style="color: #ff0000;">23.62</span> <span style="color: #ff0000;">22.22</span> <span style="color: #ff0000;">181.3</span>   <span style="color: #ff0000;">115.08</span> <span style="color: #ff0000;">11.623</span> <span style="color: #ff0000;">3.531</span> <span style="color: #ff0000;">65.55</span> <span style="color: #ff0000;">6.855</span>
 <span style="color: #ff0000;">6</span>     <span style="color: #ff0000;">29.44</span> <span style="color: #ff0000;">18.21</span> <span style="color: #ff0000;">190.2</span>    <span style="color: #ff0000;">92.88</span> <span style="color: #ff0000;">10.267</span> <span style="color: #ff0000;">3.769</span> <span style="color: #ff0000;">79.10</span> <span style="color: #ff0000;">6.599</span>
 <span style="color: #ff0000;">7</span>     <span style="color: #ff0000;">59.12</span> <span style="color: #ff0000;">31.64</span> <span style="color: #ff0000;">216.5</span>    <span style="color: #ff0000;">80.57</span>  <span style="color: #ff0000;">8.942</span> <span style="color: #ff0000;">3.036</span> <span style="color: #ff0000;">83.90</span> <span style="color: #ff0000;">4.316</span>
 <span style="color: #ff0000;">8</span>     <span style="color: #ff0000;">59.96</span> <span style="color: #ff0000;">39.68</span> <span style="color: #ff0000;">171.9</span>    <span style="color: #ff0000;">76.83</span>  <span style="color: #ff0000;">8.794</span> <span style="color: #ff0000;">3.226</span> <span style="color: #ff0000;">83.97</span> <span style="color: #ff0000;">6.585</span>
 <span style="color: #ff0000;">9</span>     <span style="color: #ff0000;">31.45</span> <span style="color: #ff0000;">24.14</span> <span style="color: #ff0000;">167.4</span>    <span style="color: #ff0000;">79.12</span> <span style="color: #ff0000;">10.180</span> <span style="color: #ff0000;">3.461</span> <span style="color: #ff0000;">76.90</span> <span style="color: #ff0000;">8.356</span>
<span style="color: #080;">&gt;</span> tabular<span style="color: #080;">&#40;</span>cast<span style="color: #080;">&#40;</span>aqm, month ~ variable2, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span>,<span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notice how we turned tabular to be an S3 method that can deal with a cast_df object</span>
&nbsp;
       ozone       solar.<span style="">r</span>        wind         temp       
 month <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>    <span style="color: #0000FF; font-weight: bold;">sd</span>     <span style="color: #0000FF; font-weight: bold;">mean</span>   <span style="color: #0000FF; font-weight: bold;">sd</span>    <span style="color: #0000FF; font-weight: bold;">mean</span>  <span style="color: #0000FF; font-weight: bold;">sd</span>   
 <span style="color: #ff0000;">5</span>     <span style="color: #ff0000;">23.62</span> <span style="color: #ff0000;">22.22</span> <span style="color: #ff0000;">181.3</span>   <span style="color: #ff0000;">115.08</span> <span style="color: #ff0000;">11.623</span> <span style="color: #ff0000;">3.531</span> <span style="color: #ff0000;">65.55</span> <span style="color: #ff0000;">6.855</span>
 <span style="color: #ff0000;">6</span>     <span style="color: #ff0000;">29.44</span> <span style="color: #ff0000;">18.21</span> <span style="color: #ff0000;">190.2</span>    <span style="color: #ff0000;">92.88</span> <span style="color: #ff0000;">10.267</span> <span style="color: #ff0000;">3.769</span> <span style="color: #ff0000;">79.10</span> <span style="color: #ff0000;">6.599</span>
 <span style="color: #ff0000;">7</span>     <span style="color: #ff0000;">59.12</span> <span style="color: #ff0000;">31.64</span> <span style="color: #ff0000;">216.5</span>    <span style="color: #ff0000;">80.57</span>  <span style="color: #ff0000;">8.942</span> <span style="color: #ff0000;">3.036</span> <span style="color: #ff0000;">83.90</span> <span style="color: #ff0000;">4.316</span>
 <span style="color: #ff0000;">8</span>     <span style="color: #ff0000;">59.96</span> <span style="color: #ff0000;">39.68</span> <span style="color: #ff0000;">171.9</span>    <span style="color: #ff0000;">76.83</span>  <span style="color: #ff0000;">8.794</span> <span style="color: #ff0000;">3.226</span> <span style="color: #ff0000;">83.97</span> <span style="color: #ff0000;">6.585</span>
 <span style="color: #ff0000;">9</span>     <span style="color: #ff0000;">31.45</span> <span style="color: #ff0000;">24.14</span> <span style="color: #ff0000;">167.4</span>    <span style="color: #ff0000;">79.12</span> <span style="color: #ff0000;">10.180</span> <span style="color: #ff0000;">3.461</span> <span style="color: #ff0000;">76.90</span> <span style="color: #ff0000;">8.356</span></pre></td></tr></table></div>

<p>And in an Sweave document:</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2012/01/cast_df-to-tabular1.png"><img src="http://www.r-statistics.com/wp-content/uploads/2012/01/cast_df-to-tabular1-300x89.png" alt="" title="cast_df to tabular" width="300" height="89" class="aligncenter size-medium wp-image-929" /></a></p>
<p>Here is an example for the Rnw file that produces the above table:<br />
<a href='http://www.r-statistics.com/wp-content/uploads/2012/01/cast_df-to-tabular.Rnw_.txt'>cast_df to tabular.Rnw</a></p>
<p>I will finish with saying that the tabular function offers more flexibility then the one offered by the function I provided.  If you find any bugs or have suggestions of improvement, you are invited to leave a comment here or <a href="https://github.com/talgalili/R-code-snippets/blob/master/tabular.cast_df.r">inside the code on github</a>.</p>
<p>(Link-tip goes to <a href="http://tonybreyal.wordpress.com/">Tony Breyal</a> for putting together <a href="http://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/">a solution</a> for sourcing r code from github.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2012/01/printing-nested-tables-in-r-bridging-between-the-reshape-and-tables-packages/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 [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:right;"><div class="socialize-in-button socialize-in-button-right"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/&amp;layout=box_count&amp;show_faces=false&amp;width=50&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.r-statistics.com/2010/02/barnards-exact-test-a-powerful-alternative-for-fishers-exact-test-implemented-in-r/"></g:plusone></div></div><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/Barnard.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><em><span id="more-75"></span></em></p>
<h3>Using the R function for Barnard&#8217;s exact test</h3>

<div class="wp_codebox"><table><tr id="p759"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p75code9"><pre class="rsplus" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">source</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;http://www.r-statistics.com/wp-content/uploads/2010/02/Barnard.R.txt&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># downloading the code from the website</span>
<span style="color: #228B22;">#Examples:</span>
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>
<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>
Barnard<span style="color: #080;">&#40;</span>Convictions<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>And here is the output</p>

<div class="wp_codebox"><table><tr id="p7510"><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
</pre></td><td class="code" id="p75code10"><pre class="rsplus" style="font-family:monospace;"><span style="color: #080;">&gt;</span> <span style="color: #228B22;">#Examples:</span>
<span style="color: #080;">&gt;</span> 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>
<span style="color: #080;">&gt;</span> <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>
&nbsp;
        Fisher<span style="color: #ff0000;">'s Exact Test for Count Data
&nbsp;
data:  Convictions 
p-value = 0.0004652
alternative hypothesis: true odds ratio is less than 1 
95 percent confidence interval:
 0.0000000 0.2849601 
sample estimates:
odds ratio 
0.04693661 
&nbsp;
&gt; Barnard(Convictions)
&nbsp;
 2x2 matrix Barnard'</span>s exact test<span style="color: #080;">:</span> <span style="color: #ff0000;">100</span> 13x19 tables were evaluated
 <span style="color: #080;">-----------------------------------------------------------</span>
 Wald statistic <span style="color: #080;">=</span>  <span style="color: #ff0000;">3.6099</span>
 Nuisance parameter <span style="color: #080;">=</span>  <span style="color: #ff0000;">0.44446</span>
 p<span style="color: #080;">-</span>values<span style="color: #080;">:</span>  <span style="color: #ff0000;">1</span><span style="color: #080;">-</span>tailed <span style="color: #080;">=</span>  <span style="color: #ff0000;">0.00015285</span> <span style="color: #ff0000;">2</span><span style="color: #080;">-</span>tailed <span style="color: #080;">=</span>  <span style="color: #ff0000;">0.00030569</span>
 <span style="color: #080;">-----------------------------------------------------------</span></pre></td></tr></table></div>

<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>
<p><strong>Update (16.06.2011)</strong>: A more updated (and faster) code is now available on the post (thanks goes, again, to Peter Calhoun).  The post is updated with a newer example.  The old R code for the function can still be <a href="http://www.r-statistics.com/wp-content/uploads/2010/02/Barnardtest.R.txt">found here</a>.</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>10</slash:comments>
		</item>
		<item>
		<title>Simple visualization of a 11X5 table (for WordPress 2.9 Features Vote Results)</title>
		<link>http://www.r-statistics.com/2009/07/simple-visualization-of-a-11x5-table-for-wordpress-2-9-features-vote-results/</link>
		<comments>http://www.r-statistics.com/2009/07/simple-visualization-of-a-11x5-table-for-wordpress-2-9-features-vote-results/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 23:24:21 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[WordPress and Statistics]]></category>
		<category><![CDATA[opensource community]]></category>
		<category><![CDATA[survey]]></category>
		<category><![CDATA[tables]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=20</guid>
		<description><![CDATA[I guess this is not the number one post I would like to start with on this blog, but I feel the time is right for it (community-wise). I&#8217;ll move on to the subject matter in a moment, but first a short intro: This blog is written by Tal Galili. I am an aspiring statistician [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:right;"><div class="socialize-in-button socialize-in-button-right"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.r-statistics.com/2009/07/simple-visualization-of-a-11x5-table-for-wordpress-2-9-features-vote-results/&amp;layout=box_count&amp;show_faces=false&amp;width=50&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.r-statistics.com/2009/07/simple-visualization-of-a-11x5-table-for-wordpress-2-9-features-vote-results/"></g:plusone></div></div><p style="text-align: center;"><a href="http://www.flickr.com/photos/44742295@N00/2496109349"><img class="aligncenter" title="Simply Something Sophisicated - a WordPress poster" src="http://farm4.static.flickr.com/3063/2496109349_949c62ec2b_m.jpg" border="0" alt="Simply Something Sophisicated - a WordPress poster" hspace="5" /></a></p>
<p>I guess this is not the number one post I would like to start with on this blog, but I feel the time is right for it (community-wise).</p>
<p>I&#8217;ll move on to the subject matter in a moment, but first a short intro: This blog is written by Tal Galili. I am an aspiring statistician who also loves to use <a title="R" href="http://www.r-statistics.com/2009/03/what-is-r/">R</a> for his work. At the same time I am also a <a title="WordPress " href="http://en.wikipedia.org/wiki/WordPress">WordPress</a> blogger, writing mainly at <a href="http://www.TalGalili.com">www.TalGalili.com</a> where I can use my native language (Hebrew) for self expression.</p>
<p>This combination of <strong>statistics </strong>and <strong>blogging</strong> will lead me to sometimes much less statistical, but more Web/Open-Source oriented posts like this one. So for the statisticians in the audience I extend my apologies and invite you to wait for future posts which will be more fully focused on <strong>Statistics and R</strong>.</p>
<p>And now for the topic at hand. . .</p>
<p>*         *         *         *         *<br />
<span id="more-20"></span>I have just noticed the nice article published on the <a href="http://wordpress.org/development/">wordpress development blog</a> titled &#8220;<a href="http://wordpress.org/development/2009/07/2-9-vote-results/">2.9 Features Vote Results</a>&#8220;. The post exemplifies a wonderful trend in the WordPress community (led by <a style="text-decoration: none; color: #777777; font-weight: normal; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dfdfdf;" href="http://jane.wordpress.com/">Jane Wells</a>) having to do with connecting between the core team and the WordPress user community. The way Jane does this is by giving surveys to WordPress users,  which in turn offers the WordPress core team an opportunity to understand the community needs.</p>
<p>In the post &#8220;<a href="http://wordpress.org/development/2009/07/2-9-vote-results/">2.9 Features Vote Results</a>&#8220;, Jane presented the results of such survey. The post had tables and barplots, but the barplots were only present for the one dimensional variables. In contrast, more elaborate data, such as that of question 2 (asking to rate each of 11 potential features on a scale of 1 to 5), was shown only with a table, such as this:</p>
<p><a href="http://wpdotorg.wordpress.com/files/2009/07/q2.png"><img class="alignnone size-medium wp-image-24" title="q2" src="http://www.r-statistics.com/wp-content/uploads/2009/07/q2-300x181.png" alt="q2" width="300" height="181" /></a></p>
<p>The table gives the full information (although I would love it if it was easily downloadable, instead of having to type in the numbers) &#8211; but its main limitation is that from a quick look, one can not easily get (let alone understand) anything.</p>
<p>For the goal of understanding more of the results with a quick glance, I offer two simple and well-known visualizations for the results.</p>
<p>1) Parallel barplots (click for bigger image)</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2009/07/q2-option1.png"><img class="alignnone size-medium wp-image-25" title="q2-option1" src="http://www.r-statistics.com/wp-content/uploads/2009/07/q2-option1-300x129.png" alt="q2-option1" width="300" height="129" /></a></p>
<p>This plot can be easily implemented in Excel (although I did it in R) and can allow us to compare the different ranking each potential feature received.</p>
<p>For example, this shows us that most answers were usually given rank 4 (&#8220;would be nice&#8221;) for each feature.</p>
<p>2) Mosaic plot (click for bigger image)</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2009/07/q2-option2.png"><img class="alignnone size-medium wp-image-26" title="q2-option2" src="http://www.r-statistics.com/wp-content/uploads/2009/07/q2-option2-300x129.png" alt="q2-option2" width="300" height="129" /></a></p>
<p>I don&#8217;t know if this can be done in Excel, but with R it is just a simple line of code.</p>
<p>(<em>mosaicplot((DataSet.table), las = 1, col = c(&#8220;gray&#8221;,&#8221;gray&#8221;,&#8221;blue&#8221;,3,&#8221;dark green&#8221;), main = &#8220;&#8221;)</em>)</p>
<p>The advantage of this plot is that it allows us to compare the different features easily, while not only comparing the top rank, but also combining different rankings for easy comparison (for example, comparing how many rank 4 or 5 each feature received).</p>
<p>So for example, the plot shows me that the most ranked with number 5 was the feature &#8220;easier embeds&#8221; but the most ranked &#8220;number 4 or 5&#8243; was the feature &#8220;custom image sizes&#8221;. The feature &#8220;media album&#8221; came close to these two, but didn&#8217;t top either.</p>
<p><strong><span style="text-decoration: underline;">Conclusions from this post</span></strong>:</p>
<ol>
<li>It would be nice (if possible) to publish the full data of the surveys, not just the results.</li>
<li>The second question of the survey gives different answer than the first question. But since the difference in percentage seems to be so small compared to the other options, I would guess that all of the top 4 features are more or less in the same level of interest to the community.</li>
</ol>
<p>p.s. to Jane &#8211; why do none of the numbers in this table add up to 3406 (the number of respondants) ?</p>
<p>p.p.s.  to Jane and the Dev team &#8211; great work people!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2009/07/simple-visualization-of-a-11x5-table-for-wordpress-2-9-features-vote-results/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

