<?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; friedman test</title>
	<atom:link href="http://www.r-statistics.com/tag/friedman-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r-statistics.com</link>
	<description>Writing about statistics with R, and open source stuff (software, data, community)</description>
	<lastBuildDate>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>Repeated measures ANOVA with R (functions and tutorials)</title>
		<link>http://www.r-statistics.com/2010/04/repeated-measures-anova-with-r-tutorials/</link>
		<comments>http://www.r-statistics.com/2010/04/repeated-measures-anova-with-r-tutorials/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 08:10:51 +0000</pubDate>
		<dc:creator>Tal Galili</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[R links]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[ANOVA]]></category>
		<category><![CDATA[aov]]></category>
		<category><![CDATA[car]]></category>
		<category><![CDATA[ez]]></category>
		<category><![CDATA[ezANOVA]]></category>
		<category><![CDATA[friedman]]></category>
		<category><![CDATA[friedman test]]></category>
		<category><![CDATA[friedman's test]]></category>
		<category><![CDATA[repeated measures]]></category>
		<category><![CDATA[repeated measures anova]]></category>
		<category><![CDATA[Repeated measuresANOVA]]></category>
		<category><![CDATA[Repeatedmeasures]]></category>
		<category><![CDATA[SS]]></category>
		<category><![CDATA[SS type I]]></category>
		<category><![CDATA[SS type I error]]></category>
		<category><![CDATA[SS type III]]></category>
		<category><![CDATA[SS type III error]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Unbalanced design]]></category>

		<guid isPermaLink="false">http://www.r-statistics.com/?p=236</guid>
		<description><![CDATA[Repeated measures ANOVA is a common task for the data analyst. There are (at least) two ways of performing &#8220;repeated measures ANOVA&#8221; using R but none is really trivial, and each way has it&#8217;s own complication/pitfalls (explanation/solution to which I was usually able to find through searching in the R-help mailing list). So for future [...]]]></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/repeated-measures-anova-with-r-tutorials/&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/repeated-measures-anova-with-r-tutorials/"></g:plusone></div></div><p>Repeated measures ANOVA is a common task for the data analyst.</p>
<p>There are (at least) two ways of performing &#8220;repeated measures ANOVA&#8221; using R but none is really trivial, and each way has it&#8217;s own complication/pitfalls (explanation/solution to which I was usually able to find through searching in the R-help mailing list).</p>
<p>So for future reference, I am starting this page to document links I find to tutorials, explanations (and troubleshooting) of &#8220;repeated measure ANOVA&#8221; done with R</p>
<h3>Functions and packages</h3>
<p>(I suggest using the tutorials supplied bellow for how to use these functions)</p>
<ul>
<li>aov {stats} &#8211; offers SS type I repeated measures anova,  by a call to lm for each stratum.  A short example is given in the ?aov help file</li>
<li>Anova {<a href="http://cran.r-project.org/web/packages/car/index.html">car</a>} &#8211; Calculates type-II or type-III analysis-of-variance tables for model objects produced by lm, and for various other object.  The ?Anova help file offers an example for how to use this for repeated measures</li>
<li>ezANOVA {<a href="http://cran.r-project.org/web/packages/ez/index.html">ez</a>} &#8211; This function provides easy analysis of data from factorial experiments, including purely within-Ss designs (a.k.a. &#8220;repeated measures&#8221;), purely between-Ss designs, and mixed within-and-between-Ss designs, yielding ANOVA results and assumption checks.  It is a wrapper of the Anova {car} function, and is easier to use.  The ez package also offers the functions ezPlot and ezStats to give plot and statistics of the ANOVA analysis.  The ?ezANOVA help file gives a good demonstration for the functions use (My thanks goes to Matthew Finkbe for letting me know about this cool package)</li>
<li>friedman.test {stats} &#8211; Performs a Friedman rank sum test with unreplicated blocked data.  That is, a non-parametric one-way repeated measures anova.  I also wrote a wrapper function to perform and plot <a href="http://www.r-statistics.com/2010/02/post-hoc-analysis-for-friedmans-test-r-code/">a post-hoc analysis on the friedman test results</a></li>
<li>Non parametric multi way repeated measures anova &#8211; I believe such a function could be developed based on the Proportional Odds Model, maybe using the {repolr} or the {ordinal} packages.  But I still didn&#8217;t come across any function that implements these models (if you do &#8211; please let me know in the comments).</li>
<li>Repeated measures, non-parametric, multivariate analysis of variance &#8211; as far as I know, such a method is not currently available in R.  There is, however, the Analysis of similarities (ANOSIM) analysis which provides a way to test statistically whether there is a signiﬁcantdifference between two or more groups of sampling units.  Is is available in the {<a href="http://cran.r-project.org/web/packages/vegan/vegan.pdf">vegan</a>} package through the &#8220;anosim&#8221; function.  There is also a <a href="http://cc.oulu.fi/~jarioksa/opetus/metodi/vegantutor.pdf">tutorial </a>and <a href="http://onlinelibrary.wiley.com/doi/10.1111/j.1442-9993.2001.01070.pp.x/full">a relevant published paper</a>.</li>
</ul>
<h3>Good Tutorials</h3>
<ul>
<li>A basic tutorial about ANOVA with R (only the last bit holds some example of repeated measures) on <a href="http://www.personality-project.org/R/r.anova.html">personality-project</a></li>
<li><a href="http://www.personality-project.org/R/r.anova.html"></a>A thorough tutorial on <a href="http://gribblelab.org/2009/03/09/repeated-measures-anova-using-r/">motor control lab</a></li>
<li><a href="http://blog.gribblelab.org/2009/03/09/repeated-measures-anova-using-r/"></a>A thorough tutorial on <a href="http://www.ats.ucla.edu/stat/R/seminars/Repeated_Measures/repeated_measures.htm">UCLA seminar page</a></li>
<li><a href="http://www.psych.upenn.edu/~baron/rpsych/rpsych.html#htoc60">Another good tutorial</a> by<br />
Jonathan Baron and Yuelin Li on <a href="http://www.psych.upenn.edu/~baron/rpsych/rpsych.html">&#8220;Notes on the use of R for psychology experiments and questionnaires&#8221;</a></li>
</ul>
<h3>Troubelshooting</h3>
<p><strong>Unbalanced design</strong><br />
Unbalanced design doesn&#8217;t work when doing  repeated measures ANOVA with aov, it just doesn&#8217;t.  This situation occurs if there are missing values in the data or that the data is not from a fully balanced design.  The way this will show up in your output is that you will see the between subject section showing withing subject variables.</p>
<p>A solution for this might be to use the<a href="http://finzi.psych.upenn.edu/R/library/car/html/Anova.html"> Anova</a> function from library car with parameter type=&#8221;III&#8221;.  But before doing that, first make sure you understand the difference between SS type I, II and III. <a href="http://prometheus.scp.rochester.edu/zlab/sites/default/files/InteractionsAndTypesOfSS.pdf">Here is a good tutorial</a> for helping you out with that.<br />
By the way, these links are also useful in case you want to do a simple two way ANOVA for unbalanced design</p>
<p>I will &#8220;later&#8221; add R-help mailing list discussions that I found helpful on the subject.</p>
<p>If you come across good resources, please let me know about them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r-statistics.com/2010/04/repeated-measures-anova-with-r-tutorials/feed/</wfw:commentRss>
		<slash:comments>10</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="p1503"><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="p150code3"><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="p1504"><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="p150code4"><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>
	</channel>
</rss>

