<?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; code</title>
	<atom:link href="http://www.r-statistics.com/tag/code/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>How to upgrade R on windows 7</title>
		<link>http://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows-7/</link>
		<comments>http://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows-7/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 07:42:43 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[program files]]></category>
		<category><![CDATA[R 2.13.0]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[R upgrade]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[upgrading R]]></category>
		<category><![CDATA[windows 7]]></category>
		<category><![CDATA[windows vista]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=686</guid>
		<description><![CDATA[Background &#8211; time to upgrade to R 2.13.0 The news of the new release of R 2.13.0 is out, and the R blogosphere is buzzing. Bloggers posting excitedly about the new R compiler package that brings with it the hope to speed up our R code with up to 4 times improvement. So it is time [...]]]></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/2011/04/how-to-upgrade-r-on-windows-7/&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/2011/04/how-to-upgrade-r-on-windows-7/"></g:plusone></div></div><h3>Background &#8211; time to upgrade to R 2.13.0</h3>
<p>The news of the new release of <a href="http://cran.r-project.org/bin/windows/base/">R 2.13.0</a> is out, and the <a href="http://www.r-bloggers.com/">R blogosphere</a> is buzzing.  Bloggers <a href="http://blog.revolutionanalytics.com/2011/04/r-2130-released.html">posting excitedly </a>about the new R compiler package that brings with it the hope to <a href="http://dirk.eddelbuettel.com/blog/2011/04/12/#the_new_r_compiler_package">speed up our R code with up to 4 times improvement</a>.  So it is time to upgrade, and bloggers are here to help.  Some wrote how to upgrade R on <a href="http://nsaunders.wordpress.com/2011/04/15/r-2-12-to-2-13-package-upgrade/">Linux</a> and <a href="http://www.imachordata.com/?p=745">mac OSX</a> (based on <a href="http://onertipaday.blogspot.com/2011/04/r-2130-is-released.html">posts by Paolo</a>).  And it is now my turn, with suggestions on how to upgrade R on windows 7.</p>
<h3>Upgrading R on windows &#8211; the two strategies</h3>
<p>The classic description of how to upgrade R can be found in <a href="http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f">the R project FAQ page</a> (and also the <a href="http://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-install-R-for-Windows_003f">FAQ on how to install R on windows</a>)</p>
<p>There are basically two strategies for R upgrading on windows.  The first is to install a new R version and copy paste all the packages to the new R installation folder.  The second is to have a global R package folder, each time synced to the most current R installation (thus saving us the time of copying the package library each we upgrade R).</p>
<p>I described the second strategy in detail in a post I wrote a year ago titled: &#8220;<a href="http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/">How to upgrade R on windows XP – another strategy</a>&#8221; which explains how to upgrade R using the simple two-liner code:</p>

<div class="wp_codebox"><table><tr id="p6864"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p686code4"><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/04/upgrading-R-on-windows.r.txt&quot;</span><span style="color: #080;">&#41;</span>
New.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>p.s: If this is the <strong>first time </strong>you are upgrading R using this method, then first run the following two lines on your old R installation (<strong>before </strong>running the above code in the new R intallation):</p>

<div class="wp_codebox"><table><tr id="p6865"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p686code5"><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/04/upgrading-R-on-windows.r.txt&quot;</span><span style="color: #080;">&#41;</span>
Old.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>The above code should be enough.  However, there are some common pitfalls you might encounter when upgrading R on windows 7, bellow I outline the ones I know about, and how they can be solved.</p>
<p><span id="more-686"></span></p>
<h3>upgrade R on windows 7 &#8211; the issues</h3>
<p>Ideally, we would simply run the code above and go on doing our statistics.</p>
<p>But for windows 7 users there are several common issues when upgrading to a new version of R (compared to when<a title="How to upgrade R on windows XP – another strategy (and the R code to do it)" href="http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/"> upgrading R in windows XP</a>).</p>
<h4>Issue 1 &#8211; folder location when upgrading from 32 bit to a 64 bit R version</h4>
<p><strong>The first issue </strong>is that the folder in which R is installed might be different if you have installed the 32 bit version on win 7 and now started using the 64 bit version, in which case you will need to run the following commands (notice the use of the &#8220;global.library.folder&#8221; paramater), I here assume you&#8217;ve installed R on D:\R.</p>

<div class="wp_codebox"><table><tr id="p6866"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p686code6"><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/04/upgrading-R-on-windows.r.txt&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># in the old R</span>
Old.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span>global.<span style="">library</span>.<span style="">folder</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;D:<span style="color: #000099; font-weight: bold;">\\</span>R<span style="color: #000099; font-weight: bold;">\\</span>library&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># in the new R</span>
New.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span>global.<span style="">library</span>.<span style="">folder</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;D:<span style="color: #000099; font-weight: bold;">\\</span>R<span style="color: #000099; font-weight: bold;">\\</span>library&quot;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<h4>Issue 2 &#8211; folder permissions</h4>
<p><strong>The second issue</strong> is problem with permissions.  The default permissions of a regular user on windows 7 won&#8217;t let you create folders and files under the &#8220;c:\program files&#8221; directory (and, if I am not mistaken, for some other directories as well).   The result of this problem is that running the upgrade code I provided above, will often result with an error similar to this one:</p>
<blockquote><p>[1] &#8220;The path to the Global library ( d:\\R\\library\\ ) Didn&#8217;t exist &#8211; and was now created.&#8221;<br />
Error in file(file, ifelse(append, &#8220;a&#8221;, &#8220;w&#8221;)) :<br />
cannot open the connection<br />
In addition: Warning messages:<br />
1: In dir.create(global.library.folder) : &#8216;\library&#8217; already exists<br />
2: In dir.create(global.library.folder) :<br />
cannot create dir &#8216;\library&#8217;, reason &#8216;No error&#8217;<br />
3: In file(file, ifelse(append, &#8220;a&#8221;, &#8220;w&#8221;)) :<br />
cannot open file &#8216;D:/R/R-213~1.0\etc\Renviron.site&#8217;: Permission denied</p></blockquote>
<p>There are three solutions to this, the best one (I believe) is number 2 (maybe also combined with number 3):</p>
<p><strong>Solution 1</strong>: (which is easier to do, but I like less these days) is to run R with administrator privileges by following these steps:(<a href="http://superuser.com/questions/162680/having-a-shortcut-run-with-administrator-permissions-win-7">My thanks goes to superuser</a>):</p>
<ul>
<li> Right click on the R shortcut</li>
<li> Click on Properties</li>
<li> Select the Compatibility tab</li>
<li> At the bottom click &#8220;Change settings for all users&#8221;</li>
<li>Again at the bottom select to &#8220;Run this program as an administrator&#8221;</li>
</ul>
<p>The downside of this method is that every time you will start up R, you will get a nagging pop-up window asking you if you want to grant admin privileges to R, not a fun thing to deal with on a day to day basis.  Also, some other programs might have a hard time &#8220;doing things to&#8221; R, if it is run as administrator while they are not.</p>
<p><strong>Solution 2 <strong>(editors pick)</strong></strong>:  Change (grant) your own user permissions to the relevant R folders (for example: &#8220;D:\R&#8221;). In order to do this you can simply follow the steps described <a href="http://www.blogsdna.com/2159/how-to-take-ownership-grant-permissions-to-access-files-folder-in-windows-7.htm">here</a>.</p>
<p><strong>Solution 3: </strong>Install R in something like &#8220;D:\R&#8221; instead of &#8220;C:\program files\R&#8221; so to avoid permission problems.</p>
<p>It is advisable to restart your Rgui in order to have the changes (for example, file permission changes) take effect.  Also, sometimes you need to install the same package twice before the installation &#8220;catches&#8221; (e.g: that you&#8217;d get the &#8220;package &#8216;***&#8217; successfully unpacked and MD5 sums checked&#8221; massage).</p>
<h4>Issue 3 &#8211; antivirus file access restrictions</h4>
<p>One of the possible error massage you might come across when trying to upgrade (or install) your R packages is the following:</p>
<blockquote><p>massage:<br />
package &#8216;***&#8217; successfully unpacked and MD5 sums checked<br />
Warning: unable to move temporary installation &#8216;C:\Program Files\R\R-2.13.0\library\file70669f\***&#8217; to &#8216;C:\Program Files\R\R-2.13.0\library\***&#8217;</p></blockquote>
<p>After (too much) pocking around, I <strong>suspect</strong> that the source of this error was two things.  The first is my antivirus software.  It appears that the &#8220;real time file system protection&#8221; was blocking R from copying the files between folders once they were downloaded.  The second is the permission issues discussed above.<br />
<strong>The solution</strong> is to add the R directory path to the exception list in the antivirus software + fixing the user permission as discussed above in &#8220;solution 2&#8243;.<br />
p.s: I suspect that the reason this error happens with only some of the packages and not all of them is because of the *.dll the error prune packages have in them.  But I am not sure of that.<br />
<strong>Update</strong>: After disabling the antivirus while updating, I still found that I need to use the update function twice until it is able to properly install the package.  Another potential reason for this problem was <a href="http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg46356.html">posted in 2005</a> by Sean O&#8217;Riordain suggesting that:</p>
<blockquote><p>This is just an FYI documenting a conflict between R and<br />
Google-Desktop.  The solution was to click on the Google-Desktop icon<br />
in the systray and click on &#8220;Pause Indexing&#8221;.  I also temporarily<br />
suspended anti-virus scanning before successfully<br />
install.packages(&#8220;VR&#8221;) many times without getting an error message.</p>
<p>I was getting an intermittant failure when I tried to install a<br />
package or update a package under Win-XP-Pro-sp2.</p>
<p>There is 5gb of free space on the drive and I&#8217;m an admistrator on this<br />
machine and most times (but not every time) I tried to<br />
upgrade.packages() or install.packages() I got the following error<br />
message &#8220;unable to move temporary installation&#8221; message.</p>
<p>It appears to be a file-locking issue with the Google-Desktop search -<br />
ie. during the few seconds that install.packages() creates a fileNNNN<br />
directory tree, google-desktop starts reading these files and then<br />
prevents this tree from being moved to its correct place under<br />
\library</p>
<p>cheers,<br />
Sean</p></blockquote>
<p>&nbsp;</p>
<p>I wish R will have an &#8220;automatic update&#8221; mechanism one day, but until then I hope the above code will make your R upgrading experience a tiny bit easier&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Dumping functions from the global environment into an R script file</title>
		<link>http://www.r-statistics.com/2010/09/dumping-functions-from-the-global-environment-into-an-r-script-file/</link>
		<comments>http://www.r-statistics.com/2010/09/dumping-functions-from-the-global-environment-into-an-r-script-file/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 21:16:58 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=537</guid>
		<description><![CDATA[Looking at a project you didn&#8217;t touch for years poses many challenges. The less documentation and organization you had in your files, the more time you&#8217;ll have to spend tracing back what you did back when the code was written. I just opened up such a project, that was before I ever knew to split [...]]]></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/09/dumping-functions-from-the-global-environment-into-an-r-script-file/&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/09/dumping-functions-from-the-global-environment-into-an-r-script-file/"></g:plusone></div></div><p>Looking at a project you didn&#8217;t touch for years poses many challenges.  The less documentation and organization you had in your files, the more time you&#8217;ll have to spend tracing back what you did back when the code was written.</p>
<p>I just opened up such a project, that was before I ever knew to split my .r files to &#8220;data.r&#8221;, &#8220;functions.r&#8221;, &#8220;do.r&#8221;.  All I have are several versions of an old .RData file and many .r files with a mix of functions and commands (oh the shame!)</p>
<p>One idea I had for the tracing back was to take the latest version of .RData I had, and see what functions I had in it&#8217;s environment.  simply typing ls() wouldn&#8217;t work.  Also, I wanted to have <strong>a list of all the functions that where defined in my .RData environment</strong>.  Thanks to the code <a href="http://4dpiecharts.com/2010/09/14/which-functions-in-r-base-call-internal-code/">recently published by Richie Cotton</a>, I was able to create the &#8220;save.functions.from.env&#8221;.  This function will go through all your defined functions and write them into &#8220;d:\\temp.r&#8221;.</p>
<p>I hope this might be useful to one of you in the future, here is the code to do it:</p>

<div class="wp_codebox"><table><tr id="p5378"><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
</pre></td><td class="code" id="p537code8"><pre class="rsplus" style="font-family:monospace;">save.<span style="">functions</span>.<span style="">from</span>.<span style="">env</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">file</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;d:<span style="color: #000099; font-weight: bold;">\\</span>temp.r&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#123;</span>
	<span style="color: #228B22;"># This function will go through all your defined functions and write them into &quot;d:\\temp.r&quot;</span>
	<span style="color: #228B22;"># let's get all the functions from the envoirnement:</span>
	funs <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">Filter</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">is.<span style="">function</span></span>, <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ls</span><span style="color: #080;">&#40;</span> <span style="color: #ff0000;">&quot;.GlobalEnv&quot;</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF; font-weight: bold;">get</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
	<span style="color: #228B22;"># Let's </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;">seq_along</span><span style="color: #080;">&#40;</span>funs<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;">cat</span><span style="color: #080;">&#40;</span>	<span style="color: #228B22;"># number the function we are about to add</span>
			<span style="color: #0000FF; font-weight: bold;">paste</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;#------ Function number &quot;</span>, i , <span style="color: #ff0000;">&quot;-----------------------------------&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;">append</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, <span style="color: #0000FF; font-weight: bold;">file</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">file</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: #228B22;"># print the function into the file</span>
			<span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>funs<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> , <span style="color: #ff0000;">&quot;&lt;-&quot;</span>, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">capture.<span style="">output</span></span><span style="color: #080;">&#40;</span>funs<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>, collapse <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>, collapse <span style="color: #080;">=</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;">append</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, <span style="color: #0000FF; font-weight: bold;">file</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">file</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: #0000FF; font-weight: bold;">paste</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;#-----------------------------------------&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;">append</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, <span style="color: #0000FF; font-weight: bold;">file</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">file</span>
			<span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span> <span style="color: #228B22;"># writing at the end of the file how many new functions where added to it</span>
		<span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;# A total of &quot;</span>, <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>funs<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; Functions where written into&quot;</span>, <span style="color: #0000FF; font-weight: bold;">file</span><span style="color: #080;">&#41;</span>,
		<span style="color: #0000FF; font-weight: bold;">append</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, <span style="color: #0000FF; font-weight: bold;">file</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">file</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;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;A total of &quot;</span>, <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>funs<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; Functions where written into&quot;</span>, <span style="color: #0000FF; font-weight: bold;">file</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;"># save.functions.from.env() # this is how you run it</span></pre></td></tr></table></div>

<p><strong>Update:</strong>  <a href="http://blog.fosstrading.com/">Joshua Ulrich</a> gave <a href="http://stackoverflow.com/questions/3835714/r-turning-rdata-file-into-script-files">on stackoverflow</a> another solution for this challenge:</p>
<pre>
	newEnv <- new.env()
	load("myFunctions.Rdata", newEnv)
	dump(c(lsf.str(newEnv)), file="normalCodeFile.R", envir=newEnv)
</pre>
<p>And also suggested to look into ?prompt (which creates documentation files for objects) and / or ?package.skeleton.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/09/dumping-functions-from-the-global-environment-into-an-r-script-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to upgrade R on windows XP &#8211; another strategy (and the R code to do it)</title>
		<link>http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/</link>
		<comments>http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 22:45:14 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[R 2.11.0]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[R windows]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[upgrading R]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 7]]></category>
		<category><![CDATA[windows vista]]></category>
		<category><![CDATA[windows xp]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=300</guid>
		<description><![CDATA[Background &#8211; how I heard that there is more then one way to upgrade R If you didn&#8217;t hear it by now &#8211; R 2.11.0 is out with a bunch of new features. After Andrew Gelman recently lamented the lack of an easy upgrade process for R, a Stackoverflow thread (by JD Long) invited R [...]]]></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/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/&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/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/"></g:plusone></div></div><h3>Background &#8211; how I heard that there is more then one way to upgrade R</h3>
<p>If you didn&#8217;t <a href="http://www.statsravingmad.com/blog/infos/r-2-11-0-just-landed/">hear</a> <a href="http://blog.revolution-computing.com/2010/04/r-2110-released.html">it</a> <a href="http://onertipaday.blogspot.com/2010/04/r-2110-is-released.html">by </a>now &#8211; <a href="https://mailman.stat.ethz.ch/pipermail/r-announce/2010/000519.html">R 2.11.0 is out</a> with a bunch of new features.</p>
<p>After <a href="http://www.stat.columbia.edu/~cook/movabletype/archives/2009/08/upgrading_r.html">Andrew Gelman</a> recently lamented the lack of an easy upgrade process for R, a <a href="http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r">Stackoverflow thread</a> (by JD Long) invited R users to share their strategies for easily upgrading R.</p>
<h3>Upgrading strategy &#8211; moving to a global R library</h3>
<p>In that thread, <a href="http://dirk.eddelbuettel.com/blog/">Dirk Eddelbuettel</a> suggested another idea for upgrading R.  His idea is of using a folder for R&#8217;s packages which is <strong>outside </strong>the standard directory tree of the installation (a different strategy then the one <a href="http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f">offered on the R FAQ</a>).</p>
<p>The idea of this upgrading strategy is to save us steps in upgrading.  So when you wish to upgrade R, instead of doing the following three steps:</p>
<ul>
<li>download new R and install
</li>
<li>copy the &#8220;library&#8221; content from the old R to the new R
</li>
<li>upgrade all of the packages (in the library folder) to the new version of R.
</li>
</ul>
<p>You could instead just have steps 1 and 3, and <strong>skip step 2</strong> (thus, saving us time&#8230;).</p>
<p><strong>For example</strong>, under windows XP, you might have R installed on:<br />
<code>C:\Program Files\R\R-2.11.0\</code><br />
But (in this alternative model for upgrading) you will have your packages library on a &#8220;global library folder&#8221; (global in the sense of independent of a specific R version):<br />
<code>C:\Program Files\R\library</code></p>
<p>So in order to use this strategy, you will need to do the following steps (all of them are performed in an R code provided later in the post)-</p>
<ol>
<li><span style="font-size: 13.3333px;">In the OLD R installation (in the first time you move to the new system of managing the upgrade):</span>
<ol>
<li><span style="font-size: 13.3333px;">Create a new global library folder (if it doesn&#8217;t exist)</span></li>
<li><span style="font-size: 13.3333px;">Copy to the new &#8220;global library folder&#8221; all of your packages from the old R installation</span></li>
<li><span style="font-size: 13.3333px;">After you move to this system &#8211; the steps 1 and 2 would <span style="text-decoration: underline;"><strong>not</strong></span> need to be repeated. (hence the advantage)</span></li>
</ol>
</li>
<li><span style="font-size: 13.3333px;">In the NEW R installation:</span>
<ol>
<li><span style="font-size: 13.3333px;">Create a new global library folder (if it doesn&#8217;t exist &#8211; in case this is your first R installation)</span></li>
<li><span style="font-size: 13.3333px;">Premenantly point to the Global library folder whenever R starts</span></li>
<li><span style="font-size: 13.3333px;">(Optional) Delete from the &#8220;Global library folder&#8221; all the packages that already exist in the local library folder of the new R install (no need to have doubles)</span></li>
<li><span style="font-size: 13.3333px;">Update all packages.</span> (notice that you picked a mirror where the packages are up-to-date, you sometimes need to choose another mirror)</li>
</ol>
</li>
</ol>
<p>Thanks to <a href="http://stackoverflow.com/questions/2698269/how-do-you-change-library-location-in-r-under-windows-xp">help from Dirk</a>, David Winsemius and Uwe Ligges, I was able to write the following R code to perform all the tasks I described <img src='http://www.r-statistics.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>So first you will need to run the following code:<br />
<span id="more-300"></span></p>
<h3>Code for upgrading R &#8211; only two lines for each R installation (old/new)</h3>
<p>The code to perform all of the steps described above is available <a href="http://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt">here</a>, and can be accessed from within R using &#8220;source&#8221;.</p>
<p>In order to move your R upgrade to the new (simpler) system (assuming you already use R, and that it is your first time moving to the new system), do the following:<br />
1) Download and install the new version of R<br />
2) Open your old R and run &#8211; </p>
<pre>
source("http://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt")
Old.R.RunMe()
</pre>
<p>(wait until it finishes)<br />
3) Open your new R and run</p>

<div class="wp_codebox"><table><tr id="p30011"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p300code11"><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/04/upgrading-R-on-windows.r.txt&quot;</span><span style="color: #080;">&#41;</span>
New.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>(wait until it finishes) </p>
<p>Once you do this, then from now on, whenever you will upgrade to a new R in the future, all you will need to do are the following TWO (instead of three) steps:<br />
1) Download and install the new version of R<br />
2) Open your new R and run</p>

<div class="wp_codebox"><table><tr id="p30012"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p300code12"><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/04/upgrading-R-on-windows.r.txt&quot;</span><span style="color: #080;">&#41;</span>
New.<span style="">R</span>.<span style="">RunMe</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>(wait until it finishes) </p>
<p>And that is it.</p>
<p>Update: <strong>This post has a follow-up explaining how to <a href="http://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows/">upgrade R on windows 7</a> explaining how to deal with permission issues.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>The difference between &#8220;letters[c(1,NA)]&#8221; and &#8220;letters[c(NA,NA)]&#8220;</title>
		<link>http://www.r-statistics.com/2010/04/the-difference-between-lettersc1na-and-letterscnana/</link>
		<comments>http://www.r-statistics.com/2010/04/the-difference-between-lettersc1na-and-letterscnana/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 06:40:19 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coercion]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[NA]]></category>
		<category><![CDATA[recycling]]></category>
		<category><![CDATA[Zen]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=296</guid>
		<description><![CDATA[In David Smith&#8217;s latest blog post (which, in a sense, is a continued response to the latest public attack on R), there was a comment by Barry that caught my eye. Barry wrote: Even I get caught out on R quirks after 20 years of using it. Compare letters[c(12,NA)] and letters[c(NA,NA)] for the most recent [...]]]></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/04/the-difference-between-lettersc1na-and-letterscnana/&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/04/the-difference-between-lettersc1na-and-letterscnana/"></g:plusone></div></div><p>In David Smith&#8217;s <a href="http://blog.revolution-computing.com/2010/04/why-use-r-because-thats-what-the-pros-use.html">latest blog post</a> (which, in a sense, is a continued response to the <a href="http://www.r-statistics.com/2010/04/an-article-attacking-r-gets-responses-from-the-r-blogosphere-some-reflections/">latest public attack on R</a>), there was a comment by Barry that caught my eye.  Barry wrote:</p>
<blockquote><p>Even I get caught out on R quirks after 20 years of using it. <strong>Compare letters[c(12,NA)] and letters[c(NA,NA)]</strong> for the most recent thing that made me bang my head against the wall.</p></blockquote>
<p>So I did, and here&#8217;s the output:</p>

<div class="wp_codebox"><table><tr id="p29615"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p296code15"><pre class="rsplus" style="font-family:monospace;"><span style="color: #080;">&gt;</span> <span style="color: #0000FF; font-weight: bold;">letters</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,NA<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #ff0000;">&quot;l&quot;</span> NA 
<span style="color: #080;">&gt;</span>  <span style="color: #0000FF; font-weight: bold;">letters</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>NA,NA<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> 
 <span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
<span style="color: #080;">&gt;</span></pre></td></tr></table></div>

<p>Interesting isn&#8217;t it?<br />
I had no clue why this had happened but luckily for us, Barry gave a follow-up reply with an explanation.  And here is what he wrote:<br />
<span id="more-296"></span><br />
My example with &#8216;letters&#8217; comes from a collision of three features:</p>
<ol>
<li><span style="font-size: 13.3333px;">recycling of short subscripts</span></li>
<li><span style="font-size: 13.3333px;">silent coercion of types (boolean NA to numeric NA)</span></li>
<li><span style="font-size: 13.3333px;">and the existence of five different NA values that all print the same.</span></li>
</ol>
<p><span style="font-size: 13.3333px;">[...] to really understand that letters[c(1,NA)] is different from letters[c(NA,NA)] you have to see that:</span></p>
<ul>
<li><span style="font-size: 13.3333px;">in the first case, the NA is coerced to a numeric NA because it&#8217;s in a vector with a numeric &#8217;1&#8242;.</span></li>
<li><span style="font-size: 13.3333px;">in the first case, you are selecting elements by supplying a vector of indexes</span></li>
<li><span style="font-size: 13.3333px;">in the second case, your NAs are boolean (logical) NA values</span></li>
<li><span style="font-size: 13.3333px;">hence your subscript is a logical vector</span></li>
<li><span style="font-size: 13.3333px;">logical vectors are recycled</span></li>
<li><span style="font-size: 13.3333px;">now your subscript is a vector of TRUE/FALSE values (which are all NA) of the same length as &#8216;letters&#8217;.</span></li>
</ul>
<p>To make sure I understood Barry correctly, I tried the following code:</p>

<div class="wp_codebox"><table><tr id="p29616"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p296code16"><pre class="rsplus" style="font-family:monospace;"><span style="color: #080;">&gt;</span>  <span style="color: #0000FF; font-weight: bold;">letters</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">T</span>,NA<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> 
 <span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #ff0000;">&quot;a&quot;</span> NA  <span style="color: #ff0000;">&quot;c&quot;</span> NA  <span style="color: #ff0000;">&quot;e&quot;</span> NA  <span style="color: #ff0000;">&quot;g&quot;</span> NA  <span style="color: #ff0000;">&quot;i&quot;</span> NA  <span style="color: #ff0000;">&quot;k&quot;</span> NA  <span style="color: #ff0000;">&quot;m&quot;</span> NA  <span style="color: #ff0000;">&quot;o&quot;</span> NA  <span style="color: #ff0000;">&quot;q&quot;</span> NA  <span style="color: #ff0000;">&quot;s&quot;</span> NA  <span style="color: #ff0000;">&quot;u&quot;</span> NA  <span style="color: #ff0000;">&quot;w&quot;</span> NA  <span style="color: #ff0000;">&quot;y&quot;</span> NA</pre></td></tr></table></div>

<div>Barry gave this example to illustrate how R violates the Zen idea if: &#8220;Simple is better than complex&#8221;.  Since (so he claims), subscript recycling is shooting you in the foot.</div>
<div>To follow up on that discussion, head over to <a href="http://blog.revolution-computing.com/2010/04/why-use-r-because-thats-what-the-pros-use.html?cid=6a010534b1db25970b0133ecd8ce48970b#comment-6a010534b1db25970b0133ecd8ce48970b">Barry&#8217;s comment on the REvolution blog</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/04/the-difference-between-lettersc1na-and-letterscnana/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 [...]]]></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/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/&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/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/"></g:plusone></div></div><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_codebox"><table><tr id="p25618"><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" id="p256code18"><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>9</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[rank test]]></category>
		<category><![CDATA[Siegel]]></category>
		<category><![CDATA[Siegel-Tukey]]></category>
		<category><![CDATA[Tukey]]></category>
		<category><![CDATA[var.test]]></category>
		<category><![CDATA[variance test]]></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: [...]]]></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/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/&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/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/"></g:plusone></div></div><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_codebox"><table><tr id="p16121"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p161code21"><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/siegel-tukey-non-parametric-test-for-equal-variance.r.txt&quot;</span><span style="color: #080;">&#41;</span>
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_codebox"><table><tr id="p16122"><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
</pre></td><td class="code" id="p161code22"><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>
&nbsp;
	<span style="color: #228B22;"># rk.up=c(1,(a*4+b))[1:ceiling(length(data$x)/2)]	# this method would lead to mistakes in calculating the ranks, it was corrected</span>
	<span style="color: #228B22;"># rk.down=rev(c(a*4+b-2)[1:floor(length(data$x)/2)])</span>
		<span style="color: #228B22;">#Example for when the old code would fail with the ranking:</span>
			<span style="color: #228B22;">#x1 &lt;- c(85, 106)</span>
			<span style="color: #228B22;">#x2 &lt;- c(96, 105, 104, 108, 86) # 108 should get rank 7 and not 8</span>
			<span style="color: #228B22;">#iv &lt;- rep(1:2, c(length(x1), length(x2)))</span>
			<span style="color: #228B22;">#siegel.tukey(c(x1, x2), iv, id.col=TRUE)</span>
&nbsp;
	<span style="color: #228B22;"># Corrected code (17.12.10)</span>
	X <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data</span>$x
	N <span style="color: #080;">&lt;-</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: #228B22;"># N is the length of the combined data</span>
	TF <span style="color: #080;">&lt;-</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>TRUE, FALSE, FALSE, TRUE<span style="color: #080;">&#41;</span>, <span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span>N<span style="color: #080;">/</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	up <span style="color: #080;">&lt;-</span> TF<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">min</span><span style="color: #080;">&#40;</span>N, <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>TF<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
	Rup <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rank</span><span style="color: #080;">&#40;</span>X<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>up<span style="color: #080;">&#93;</span>
	Rdown <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rev</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">rank</span><span style="color: #080;">&#40;</span>X<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #080;">!</span>up<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;
<span style="color: #228B22;"># rks=c(rk.up,rk.down)</span>
 rks<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>Rup,Rdown<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;
&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>

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>5</slash:comments>
		</item>
		<item>
		<title>Post hoc analysis for Friedman&#8217;s Test  (R code)</title>
		<link>http://www.r-statistics.com/2010/02/post-hoc-analysis-for-friedmans-test-r-code/</link>
		<comments>http://www.r-statistics.com/2010/02/post-hoc-analysis-for-friedmans-test-r-code/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 09:08:14 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[ANOVA]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[friedman test]]></category>
		<category><![CDATA[friedman's test]]></category>
		<category><![CDATA[multiple comparisons]]></category>
		<category><![CDATA[nonparametric]]></category>
		<category><![CDATA[nonparametric test]]></category>
		<category><![CDATA[one way anova]]></category>
		<category><![CDATA[post hoc]]></category>
		<category><![CDATA[post hoc analysis]]></category>
		<category><![CDATA[posthoc]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[repeated measures]]></category>
		<category><![CDATA[repeated measures anova]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=150</guid>
		<description><![CDATA[My goal in this post is to give an overview of Friedman&#8217;s Test and then offer R code to perform post hoc analysis on Friedman&#8217;s Test results. (The R function can be downloaded from here) Preface: What is Friedman&#8217;s Test Friedman test is a non-parametric randomized block analysis of variance. Which is to say it [...]]]></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/post-hoc-analysis-for-friedmans-test-r-code/&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/post-hoc-analysis-for-friedmans-test-r-code/"></g:plusone></div></div><p>My goal in this post is to give an overview of Friedman&#8217;s Test and then offer R code to perform post hoc analysis on Friedman&#8217;s Test results. (The R function can be <a href="http://www.r-statistics.com/wp-content/uploads/2010/02/Friedman-Test-with-Post-Hoc.r.txt">downloaded from here</a>)</p>
<h3>Preface: What is Friedman&#8217;s Test</h3>
<p><strong>Friedman test</strong> is a non-parametric randomized block analysis of variance.  Which is to say it is a non-parametric version of a one way ANOVA with repeated measures. That means that while a simple ANOVA test requires the assumptions of a normal distribution and equal variances (of the residuals), the Friedman test is free from those restriction. The price of this parametric freedom is the loss of power (of Friedman&#8217;s test compared to the parametric ANOVa versions).</p>
<p>The hypotheses for the comparison across repeated measures are:</p>
<ul>
<li>H0: The distributions (whatever they are) are the same across repeated measures</li>
<li>H1: The distributions across repeated measures are different</li>
</ul>
<p>The test statistic for the Friedman&#8217;s test is a Chi-square with [(number of repeated measures)-1] degrees of freedom. A detailed explanation of the method for computing the Friedman test is available <a href="http://en.wikipedia.org/wiki/Friedman_test">on Wikipedia</a>.</p>
<p><strong>Performing Friedman&#8217;s Test in R</strong> is very simple, and is by using the &#8220;friedman.test&#8221; command.</p>
<h3>Post hoc analysis for the Friedman&#8217;s Test</h3>
<p>Assuming you performed Friedman&#8217;s Test and found a significant P value, that means that some of the groups in your data have different distribution from one another, but you don&#8217;t (yet) know which. Therefor, our next step will be to try and find out which pairs of our groups are significantly different then each other. But when we have N groups, checking all of their pairs will be to perform [n over 2] comparisons, thus the need to correct for multiple comparisons arise.<br />
<strong>The tasks:</strong><br />
<strong>Our first task</strong> will be to perform a post hoc analysis of our results (using R) &#8211; in the hope of finding out which of our groups are responsible that we found that the null hypothesis was rejected. While in the simple case of ANOVA, an R command is readily available (&#8220;TukeyHSD&#8221;), in the case of friedman&#8217;s test (until now) the code to perform the post hoc test was not as easily accessible.<br />
<strong>Our second task</strong> will be to visualize our results. While in the case of simple ANOVA, a boxplot of each group is sufficient, in the case of a repeated measures &#8211; a boxplot approach will be misleading to the viewer. Instead, we will offer two plots: one of parallel coordinates, and the other will be boxplots of the differences between all pairs of groups (in this respect, the post hoc analysis can be thought of as performing paired wilcox.test with correction for multiplicity).</p>
<h3>R code for Post hoc analysis for the Friedman&#8217;s Test</h3>
<p>The analysis will be performed using the function (I wrote) called &#8220;friedman.test.with.post.hoc&#8221;, based on the packages &#8220;coin&#8221; and &#8220;multcomp&#8221;. Just a few words about it&#8217;s arguments:</p>
<ul>
<li>formu &#8211; is a formula object of the shape: 	Y ~ X | block (where Y is the ordered (numeric) responce, X is a group indicator (factor), and block is the block (or subject) indicator (factor)</li>
<li>data &#8211; is a data frame with columns of Y, X and block (the names could be different, of course, as long as the formula given in &#8220;formu&#8221; represent that)</li>
<li>All the other parameters are to allow or suppress plotting of the results.</li>
</ul>

<div class="wp_codebox"><table><tr id="p15025"><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
</pre></td><td class="code" id="p150code25"><pre class="rsplus" style="font-family:monospace;">friedman.<span style="">test</span>.<span style="">with</span>.<span style="">post</span>.<span style="">hoc</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>formu, <span style="color: #0000FF; font-weight: bold;">data</span>, to.<span style="">print</span>.<span style="">friedman</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, to.<span style="">post</span>.<span style="">hoc</span>.<span style="">if</span>.<span style="">signif</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>,  to.<span style="">plot</span>.<span style="">parallel</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, to.<span style="">plot</span>.<span style="">boxplot</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, signif.<span style="">P</span> <span style="color: #080;">=</span> .05, color.<span style="">blocks</span>.<span style="">in</span>.<span style="">cor</span>.<span style="">plot</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span>, jitter.<span style="">Y</span>.<span style="">in</span>.<span style="">cor</span>.<span style="">plot</span> <span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#123;</span>
	<span style="color: #228B22;"># formu is a formula of the shape: 	Y ~ X | block</span>
	<span style="color: #228B22;"># data is a long data.frame with three columns:    [[ Y (numeric), X (factor), block (factor) ]]</span>
&nbsp;
	<span style="color: #228B22;"># Note: This function doesn't handle NA's! In case of NA in Y in one of the blocks, then that entire block should be removed.</span>
&nbsp;
&nbsp;
	<span style="color: #228B22;"># Loading needed packages</span>
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #080;">!</span><span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>coin<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;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;You are missing the package 'coin', we will now try to install it...&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">install.<span style="">packages</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;coin&quot;</span><span style="color: #080;">&#41;</span>		
		<span style="color: #0000FF; font-weight: bold;">library</span><span style="color: #080;">&#40;</span>coin<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><span style="color: #080;">!</span><span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>multcomp<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;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;You are missing the package 'multcomp', we will now try to install it...&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">install.<span style="">packages</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;multcomp&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">library</span><span style="color: #080;">&#40;</span>multcomp<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><span style="color: #080;">!</span><span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>colorspace<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;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;You are missing the package 'colorspace', we will now try to install it...&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">install.<span style="">packages</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;colorspace&quot;</span><span style="color: #080;">&#41;</span>
		<span style="color: #0000FF; font-weight: bold;">library</span><span style="color: #080;">&#40;</span>colorspace<span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #228B22;"># get the names out of the formula</span>
	formu.<span style="">names</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">all.<span style="">vars</span></span><span style="color: #080;">&#40;</span>formu<span style="color: #080;">&#41;</span>
	Y.<span style="">name</span> <span style="color: #080;">&lt;-</span> formu.<span style="">names</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
	X.<span style="">name</span> <span style="color: #080;">&lt;-</span> formu.<span style="">names</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
	block.<span style="">name</span> <span style="color: #080;">&lt;-</span> formu.<span style="">names</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dim</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;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&gt;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,<span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>Y.<span style="">name</span>,X.<span style="">name</span>,block.<span style="">name</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>	<span style="color: #228B22;"># In case we have a &quot;data&quot; data frame with more then the three columns we need. This code will clean it from them...</span>
&nbsp;
	<span style="color: #228B22;"># Note: the function doesn't handle NA's. In case of NA in one of the block T outcomes, that entire block should be removed.</span>
&nbsp;
	<span style="color: #228B22;"># stopping in case there is NA in the Y vector</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;">is.<span style="">na</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,Y.<span style="">name</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Function stopped: This function doesn't handle NA's. In case of NA in Y in one of the blocks, then that entire block should be removed.&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
	<span style="color: #228B22;"># make sure that the number of factors goes with the actual values present in the data:</span>
	<span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span> <span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">factor</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span> <span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,block.<span style="">name</span> <span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">factor</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,block.<span style="">name</span> <span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	number.<span style="">of</span>.<span style="">X</span>.<span style="">levels</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span> <span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>number.<span style="">of</span>.<span style="">X</span>.<span style="">levels</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span> <span style="color: #0000FF; font-weight: bold;">warning</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;'&quot;</span>,X.<span style="">name</span>,<span style="color: #ff0000;">&quot;'&quot;</span>, <span style="color: #ff0000;">&quot;has only two levels. Consider using paired wilcox.test instead of friedman test&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
&nbsp;
	<span style="color: #228B22;"># making the object that will hold the friedman test and the other.</span>
	the.<span style="">sym</span>.<span style="">test</span> <span style="color: #080;">&lt;-</span> symmetry_test<span style="color: #080;">&#40;</span>formu, <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: #228B22;">### all pairwise comparisons	</span>
						   teststat <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;max&quot;</span>,
						   xtrafo <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>Y.<span style="">data</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span> trafo<span style="color: #080;">&#40;</span> Y.<span style="">data</span>, factor_trafo <span style="color: #080;">=</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> <span style="color: #0000FF; font-weight: bold;">model.<span style="">matrix</span></span><span style="color: #080;">&#40;</span>~ x <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">%*%</span> <span style="color: #0000FF; font-weight: bold;">t</span><span style="color: #080;">&#40;</span>contrMat<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">table</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;Tukey&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#125;</span> <span style="color: #080;">&#41;</span> <span style="color: #080;">&#125;</span>,
						   ytrafo <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>Y.<span style="">data</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span> trafo<span style="color: #080;">&#40;</span>Y.<span style="">data</span>, numeric_trafo <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">rank</span>, block <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,block.<span style="">name</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&#41;</span> <span style="color: #080;">&#125;</span>
						<span style="color: #080;">&#41;</span>
	<span style="color: #228B22;"># if(to.print.friedman) { print(the.sym.test) }</span>
&nbsp;
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">post</span>.<span style="">hoc</span>.<span style="">if</span>.<span style="">signif</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>pvalue<span style="color: #080;">&#40;</span>the.<span style="">sym</span>.<span style="">test</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;</span> signif.<span style="">P</span><span style="color: #080;">&#41;</span>
			<span style="color: #080;">&#123;</span>
				<span style="color: #228B22;"># the post hoc test</span>
				The.<span style="">post</span>.<span style="">hoc</span>.<span style="">P</span>.<span style="">values</span> <span style="color: #080;">&lt;-</span> pvalue<span style="color: #080;">&#40;</span>the.<span style="">sym</span>.<span style="">test</span>, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;single-step&quot;</span><span style="color: #080;">&#41;</span>	<span style="color: #228B22;"># this is the post hoc of the friedman test</span>
&nbsp;
&nbsp;
				<span style="color: #228B22;"># plotting</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">plot</span>.<span style="">parallel</span> <span style="color: #080;">&amp;</span> to.<span style="">plot</span>.<span style="">boxplot</span><span style="color: #080;">&#41;</span>	<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow <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: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># if we are plotting two plots, let's make sure we'll be able to see both</span>
&nbsp;
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">plot</span>.<span style="">parallel</span><span style="color: #080;">&#41;</span>
				<span style="color: #080;">&#123;</span>
					X.<span style="">names</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>, X.<span style="">name</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
					X.<span style="">for</span>.<span style="">plot</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq_along</span><span style="color: #080;">&#40;</span>X.<span style="">names</span><span style="color: #080;">&#41;</span>
					plot.<span style="">xlim</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>.7 , <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>X.<span style="">for</span>.<span style="">plot</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span>.3<span style="color: #080;">&#41;</span>	<span style="color: #228B22;"># adding some spacing from both sides of the plot</span>
&nbsp;
					<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>color.<span style="">blocks</span>.<span style="">in</span>.<span style="">cor</span>.<span style="">plot</span><span style="color: #080;">&#41;</span> 
					<span style="color: #080;">&#123;</span>
						blocks.<span style="">col</span> <span style="color: #080;">&lt;-</span> rainbow_hcl<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;">levels</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,block.<span style="">name</span><span style="color: #080;">&#93;</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>
						blocks.<span style="">col</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;"># black</span>
					<span style="color: #080;">&#125;</span>					
&nbsp;
					data2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data</span>
					<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>jitter.<span style="">Y</span>.<span style="">in</span>.<span style="">cor</span>.<span style="">plot</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
						data2<span style="color: #080;">&#91;</span>,Y.<span style="">name</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">jitter</span><span style="color: #080;">&#40;</span>data2<span style="color: #080;">&#91;</span>,Y.<span style="">name</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
						par.<span style="">cor</span>.<span style="">plot</span>.<span style="">text</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;Parallel coordinates plot (with Jitter)&quot;</span>				
					<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
						par.<span style="">cor</span>.<span style="">plot</span>.<span style="">text</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;Parallel coordinates plot&quot;</span>
					<span style="color: #080;">&#125;</span>				
&nbsp;
					<span style="color: #228B22;"># adding a Parallel coordinates plot</span>
					<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#40;</span>data2,  idvar<span style="color: #080;">=</span>X.<span style="">name</span>, timevar<span style="color: #080;">=</span>block.<span style="">name</span>,
									 direction<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;wide&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>,<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>  , 
							type <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;l&quot;</span>,  lty <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span>, axes <span style="color: #080;">=</span> FALSE, ylab <span style="color: #080;">=</span> Y.<span style="">name</span>, 
							xlim <span style="color: #080;">=</span> plot.<span style="">xlim</span>,
							<span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> blocks.<span style="">col</span>,
							main <span style="color: #080;">=</span> par.<span style="">cor</span>.<span style="">plot</span>.<span style="">text</span><span style="color: #080;">&#41;</span>
					<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, at <span style="color: #080;">=</span> X.<span style="">for</span>.<span style="">plot</span> , <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> X.<span style="">names</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># plot X axis</span>
					<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># plot Y axis</span>
					<span style="color: #0000FF; font-weight: bold;">points</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">tapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,Y.<span style="">name</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#41;</span> ~ X.<span style="">for</span>.<span style="">plot</span>, <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;red&quot;</span>,pch <span style="color: #080;">=</span> <span style="color: #ff0000;">4</span>, cex <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>, lwd <span style="color: #080;">=</span> <span style="color: #ff0000;">5</span><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>to.<span style="">plot</span>.<span style="">boxplot</span><span style="color: #080;">&#41;</span>
				<span style="color: #080;">&#123;</span>
					<span style="color: #228B22;"># first we create a function to create a new Y, by substracting different combinations of X levels from each other.</span>
					subtract.<span style="">a</span>.<span style="">from</span>.<span style="">b</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>a.<span style="">b</span> , the.<span style="">data</span><span style="color: #080;">&#41;</span>
					<span style="color: #080;">&#123;</span>
						the.<span style="">data</span><span style="color: #080;">&#91;</span>,a.<span style="">b</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">-</span> the.<span style="">data</span><span style="color: #080;">&#91;</span>,a.<span style="">b</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
					<span style="color: #080;">&#125;</span>
&nbsp;
					temp.<span style="">wide</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>,  idvar<span style="color: #080;">=</span>X.<span style="">name</span>, timevar<span style="color: #080;">=</span>block.<span style="">name</span>,
									 direction<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;wide&quot;</span><span style="color: #080;">&#41;</span> 	<span style="color: #228B22;">#[,-1]</span>
					wide.<span style="">data</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">t</span><span style="color: #080;">&#40;</span>temp.<span style="">wide</span><span style="color: #080;">&#91;</span>,<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: #0000FF; font-weight: bold;">colnames</span><span style="color: #080;">&#40;</span>wide.<span style="">data</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> temp.<span style="">wide</span><span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
&nbsp;
					Y.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">apply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>,<span style="color: #0000FF; font-weight: bold;">combn</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span>, subtract.<span style="">a</span>.<span style="">from</span>.<span style="">b</span>, the.<span style="">data</span> <span style="color: #080;">=</span>wide.<span style="">data</span><span style="color: #080;">&#41;</span>
					names.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">apply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>,<span style="color: #0000FF; font-weight: bold;">combn</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span>,X.<span style="">name</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>a.<span style="">b</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span>a.<span style="">b</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>,a.<span style="">b</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot; - &quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>
&nbsp;
					the.<span style="">ylim</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">range</span><span style="color: #080;">&#40;</span>Y.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span><span style="color: #080;">&#41;</span>
					the.<span style="">ylim</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> the.<span style="">ylim</span><span style="color: #080;">&#91;</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;">max</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#40;</span>Y.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>	<span style="color: #228B22;"># adding some space for the labels</span>
					is.<span style="">signif</span>.<span style="">color</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">ifelse</span><span style="color: #080;">&#40;</span>The.<span style="">post</span>.<span style="">hoc</span>.<span style="">P</span>.<span style="">values</span> <span style="color: #080;">&lt;</span> .05 , <span style="color: #ff0000;">&quot;green&quot;</span>, <span style="color: #ff0000;">&quot;grey&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
					<span style="color: #0000FF; font-weight: bold;">boxplot</span><span style="color: #080;">&#40;</span>Y.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span>,
						<span style="color: #0000FF; font-weight: bold;">names</span> <span style="color: #080;">=</span> names.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span> ,
						<span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> is.<span style="">signif</span>.<span style="">color</span>,
						main <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;Boxplots (of the differences)&quot;</span>,
						ylim <span style="color: #080;">=</span> the.<span style="">ylim</span>
						<span style="color: #080;">&#41;</span>
					<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;topright&quot;</span>, <span style="color: #0000FF; font-weight: bold;">legend</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span>names.<span style="">b</span>.<span style="">minus</span>.<span style="">a</span>.<span style="">combos</span>, <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot; ; PostHoc P.value:&quot;</span>, number.<span style="">of</span>.<span style="">X</span>.<span style="">levels</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span>The.<span style="">post</span>.<span style="">hoc</span>.<span style="">P</span>.<span style="">values</span>,<span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> , fill <span style="color: #080;">=</span>  is.<span style="">signif</span>.<span style="">color</span> <span style="color: #080;">&#41;</span>
					<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
				<span style="color: #080;">&#125;</span>
&nbsp;
				list.<span style="">to</span>.<span style="">return</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>Friedman.<span style="">Test</span> <span style="color: #080;">=</span> the.<span style="">sym</span>.<span style="">test</span>, PostHoc.<span style="">Test</span> <span style="color: #080;">=</span> The.<span style="">post</span>.<span style="">hoc</span>.<span style="">P</span>.<span style="">values</span><span style="color: #080;">&#41;</span>
				<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>to.<span style="">print</span>.<span style="">friedman</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>list.<span style="">to</span>.<span style="">return</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>				
				<span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>list.<span style="">to</span>.<span style="">return</span><span style="color: #080;">&#41;</span>
&nbsp;
			<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;">print</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The results where not significant, There is no need for a post hoc test&quot;</span><span style="color: #080;">&#41;</span>
					<span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>the.<span style="">sym</span>.<span style="">test</span><span style="color: #080;">&#41;</span>
				<span style="color: #080;">&#125;</span>					
	<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;"># Original credit (for linking online, to the package that performs the post hoc test) goes to &quot;David Winsemius&quot;, see:</span>
<span style="color: #228B22;"># http://tolstoy.newcastle.edu.au/R/e8/help/09/10/1416.html</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<h3>Example</h3>
<p>(The code for the example is given at the end of the post)</p>
<p>Let&#8217;s make up a little story: let&#8217;s say we have three types of wine (A, B and C), and we would like to know which one is the best one (in a scale of 1 to 7). We asked 22 friends to taste each of the three wines (in a blind fold fashion), and then to give a grade of 1 till 7 (for example sake, let&#8217;s say we asked them to rate the wines 5 times each, and then averaged their results to give a number for a persons preference for each wine. This number which is now an average of several numbers, will not necessarily be an integer).</p>
<p>After getting the results, we started by performing a simple boxplot of the ratings each wine got. Here it is:</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2010/02/comparing-wines-boxplot.png"><img class="alignnone size-full wp-image-154" title="comparing wines - boxplot" src="http://www.r-statistics.com/wp-content/uploads/2010/02/comparing-wines-boxplot.png" alt="" width="500" /></a></p>
<p>The plot shows us two things: 1) that the assumption of equal variances here might not hold. 2) That if we are to ignore the &#8220;within subjects&#8221; data that we have, we have no chance of finding any difference between the wines.</p>
<p>So we move to using the function &#8220;friedman.test.with.post.hoc&#8221; on our data, and we get the following output:</p>
<blockquote>
<div id="_mcePaste">$Friedman.Test</div>
<div id="_mcePaste">Asymptotic General Independence Test</div>
<div id="_mcePaste">data:  Taste by</div>
<div id="_mcePaste">Wine (Wine A, Wine B, Wine C)</div>
<div id="_mcePaste">stratified by Taster</div>
<div id="_mcePaste">maxT = 3.2404, <strong>p-value = 0.003421</strong></div>
<div></div>
<div id="_mcePaste">$PostHoc.Test</div>
<div id="_mcePaste">Wine B &#8211; Wine A 0.623935139</div>
<div id="_mcePaste"><strong>Wine C &#8211; Wine A 0.003325929</strong></div>
<div id="_mcePaste">Wine C &#8211; Wine B 0.053772757</div>
</blockquote>
<p><strong><span style="text-decoration: underline;">The conclusion</span></strong> is that once we take into account the within subject variable, we discover that there is a significant difference between our three wines (significant P value of about  0.0034). And the posthoc analysis shows us that the difference is due to the difference in tastes between Wine C and Wine A (P value 0.003). and maybe also with the difference between Wine C and Wine B (the P value is 0.053, which is just borderline significant).</p>
<p>Plotting our analysis will also show us the direction of the results, and the connected answers of each of our friends answers:</p>
<p><a href="http://www.r-statistics.com/wp-content/uploads/2010/02/posthoc-friedman-plots.png"><img class="alignnone size-full wp-image-153" title="posthoc friedman plots" src="http://www.r-statistics.com/wp-content/uploads/2010/02/posthoc-friedman-plots.png" alt="" width="500" /></a></p>
<p>Here is the code for the example:</p>

<div class="wp_codebox"><table><tr id="p15026"><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
</pre></td><td class="code" id="p150code26"><pre class="rsplus" style="font-family:monospace;">&nbsp;
<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/Friedman-Test-with-Post-Hoc.r.txt&quot;</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;"># loading the friedman.test.with.post.hoc function from the internet</span>
&nbsp;
	<span style="color: #228B22;">### Comparison of three Wine (&quot;Wine A&quot;, &quot;Wine B&quot;, and</span>
	<span style="color: #228B22;">###  &quot;Wine C&quot;) for rounding first base. </span>
	WineTasting <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>
		  Taste <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">5.40</span>, <span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.55</span>,
					<span style="color: #ff0000;">5.85</span>, <span style="color: #ff0000;">5.70</span>, <span style="color: #ff0000;">5.75</span>,
					<span style="color: #ff0000;">5.20</span>, <span style="color: #ff0000;">5.60</span>, <span style="color: #ff0000;">5.50</span>,
					<span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.40</span>,
					<span style="color: #ff0000;">5.90</span>, <span style="color: #ff0000;">5.85</span>, <span style="color: #ff0000;">5.70</span>,
					<span style="color: #ff0000;">5.45</span>, <span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.60</span>,
					<span style="color: #ff0000;">5.40</span>, <span style="color: #ff0000;">5.40</span>, <span style="color: #ff0000;">5.35</span>,
					<span style="color: #ff0000;">5.45</span>, <span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.35</span>,
					<span style="color: #ff0000;">5.25</span>, <span style="color: #ff0000;">5.15</span>, <span style="color: #ff0000;">5.00</span>,
					<span style="color: #ff0000;">5.85</span>, <span style="color: #ff0000;">5.80</span>, <span style="color: #ff0000;">5.70</span>,
					<span style="color: #ff0000;">5.25</span>, <span style="color: #ff0000;">5.20</span>, <span style="color: #ff0000;">5.10</span>,
					<span style="color: #ff0000;">5.65</span>, <span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.45</span>,
					<span style="color: #ff0000;">5.60</span>, <span style="color: #ff0000;">5.35</span>, <span style="color: #ff0000;">5.45</span>,
					<span style="color: #ff0000;">5.05</span>, <span style="color: #ff0000;">5.00</span>, <span style="color: #ff0000;">4.95</span>,
					<span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.40</span>,
					<span style="color: #ff0000;">5.45</span>, <span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.50</span>,
					<span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.55</span>, <span style="color: #ff0000;">5.35</span>,
					<span style="color: #ff0000;">5.45</span>, <span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.55</span>,
					<span style="color: #ff0000;">5.50</span>, <span style="color: #ff0000;">5.45</span>, <span style="color: #ff0000;">5.25</span>,
					<span style="color: #ff0000;">5.65</span>, <span style="color: #ff0000;">5.60</span>, <span style="color: #ff0000;">5.40</span>,
					<span style="color: #ff0000;">5.70</span>, <span style="color: #ff0000;">5.65</span>, <span style="color: #ff0000;">5.55</span>,
					<span style="color: #ff0000;">6.30</span>, <span style="color: #ff0000;">6.30</span>, <span style="color: #ff0000;">6.25</span><span style="color: #080;">&#41;</span>,
					Wine <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">factor</span><span style="color: #080;">&#40;</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;">&quot;Wine A&quot;</span>, <span style="color: #ff0000;">&quot;Wine B&quot;</span>, <span style="color: #ff0000;">&quot;Wine C&quot;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">22</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,
					Taster <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">factor</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">22</span>, <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">22</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>
&nbsp;
	<span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>WineTasting , <span style="color: #0000FF; font-weight: bold;">boxplot</span><span style="color: #080;">&#40;</span> Taste  ~ Wine <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># boxploting </span>
	friedman.<span style="">test</span>.<span style="">with</span>.<span style="">post</span>.<span style="">hoc</span><span style="color: #080;">&#40;</span>Taste ~ Wine <span style="color: #080;">|</span> Taster ,WineTasting<span style="color: #080;">&#41;</span>	<span style="color: #228B22;"># the same with our function. With post hoc, and cool plots</span></pre></td></tr></table></div>

<p>If you find this code useful, please let me know (in the comments) so I will know there is a point in publishing more such code snippets&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/02/post-hoc-analysis-for-friedmans-test-r-code/feed/</wfw:commentRss>
		<slash:comments>46</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="p7529"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p75code29"><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="p7530"><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="p75code30"><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>
	</channel>
</rss>

