<?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; nonparametric</title> <atom:link href="http://www.r-statistics.com/tag/nonparametric/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>Wed, 08 Sep 2010 20:39:52 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Correlation scatter-plot matrix for ordered-categorical data</title><link>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/</link> <comments>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/#comments</comments> <pubDate>Wed, 07 Apr 2010 21:37:26 +0000</pubDate> <dc:creator>Tal Galili</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[statistics]]></category> <category><![CDATA[visualization]]></category> <category><![CDATA[code]]></category> <category><![CDATA[correlation]]></category> <category><![CDATA[correlation matrix]]></category> <category><![CDATA[correlation scatter plot]]></category> <category><![CDATA[non-parametric]]></category> <category><![CDATA[non-parametric test]]></category> <category><![CDATA[nonparametric]]></category> <category><![CDATA[nonparametric test]]></category> <category><![CDATA[R code]]></category> <category><![CDATA[scatter plot]]></category> <category><![CDATA[scatter plot matrix]]></category> <category><![CDATA[spearman correlation]]></category> <category><![CDATA[spearman test]]></category> <category><![CDATA[stackoverflow]]></category> <category><![CDATA[survey]]></category> <category><![CDATA[tutorial]]></category><guid
isPermaLink="false">http://www.r-statistics.com/?p=256</guid> <description><![CDATA[When analyzing a questionnaire, one often wants to view the correlation between two or more Likert questionnaire item&#8217;s (for example: two ordered categorical vectors ranging from 1 to 5). When dealing with several such Likert variable&#8217;s, a clear presentation of all the pairwise relation&#8217;s between our variable can be achieved by inspecting the (Spearman) correlation matrix (easily achieved in R by using the &#8220;cor.test&#8221; command on a matrix of variables). Yet, a challenge appears once we wish to plot this [...]]]></description> <content:encoded><![CDATA[<p>When analyzing a questionnaire, one often wants to view the correlation between two or more <a
href="http://en.wikipedia.org/wiki/Likert_scale">Likert questionnaire</a> item&#8217;s (for example: two ordered categorical vectors ranging from 1 to 5).</p><p>When dealing with several such Likert variable&#8217;s, a clear presentation of all the pairwise relation&#8217;s between our variable can be achieved by inspecting the (Spearman) correlation matrix (easily achieved in R by using the &#8220;cor.test&#8221; command on a matrix of variables).<br
/> Yet, a challenge appears once we wish to plot this correlation matrix.  The challenge stems from the fact that the classic presentation for a correlation matrix is a <strong>scatter plot matrix</strong> &#8211; but scatter plots don&#8217;t (usually) work well for ordered categorical vectors since the dots on the scatter plot often overlap each other.</p><p>There are four solution for the point-overlap problem that I know of:</p><ol><li>Jitter the data a bit to give a sense of the &#8220;density&#8221; of the points</li><li>Use a color spectrum to represent when a point actually represent &#8220;many points&#8221;</li><li>Use different points sizes to represent when there are &#8220;many points&#8221; in the location of that point</li><li>Add a LOWESS (or LOESS) line to the scatter plot &#8211; to show the trend of the data</li></ol><p>In this post I will offer the code for the  a solution that uses solution 3-4 (and possibly 2, please read this post comments). Here is the output (click to see a larger image):</p><p><a
href="http://www.r-statistics.com/wp-content/uploads/2010/04/scatter-plot-correlation-matrix.png"><img
class="alignnone size-full wp-image-257" title="scatter plot correlation matrix" src="http://www.r-statistics.com/wp-content/uploads/2010/04/scatter-plot-correlation-matrix.png" alt="" width="550"/></a></p><p>And here is the code to produce this plot:</p><p><span
id="more-256"></span></p><h3>R code for producing a Correlation scatter-plot matrix &#8211; for ordered-categorical data</h3><p><strong>Note</strong> that this code will work fine for continues data points (although I might suggest to enlarge the &#8220;point.size.rescale&#8221; parameter to something bigger then 1.5 in the &#8220;panel.smooth.ordered.categorical&#8221; function)</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
</pre></td><td
class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;"># -----------------</span>
<span style="color: #228B22;"># Functions</span>
<span style="color: #228B22;"># -----------------</span>
&nbsp;
panel.<span style="">cor</span>.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x, y, digits<span style="color: #080;">=</span><span style="color: #ff0000;">2</span>, prefix<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span>, cex.<span style="">cor</span><span style="color: #080;">&#41;</span> 
<span style="color: #080;">&#123;</span>
&nbsp;
    usr <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;usr&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">;</span> <span style="color: #0000FF; font-weight: bold;">on.<span style="">exit</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
&nbsp;
    r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cor</span><span style="color: #080;">&#40;</span>x, y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notive we use spearman, non parametric correlation here</span>
    r.<span style="">no</span>.<span style="">abs</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">cor</span><span style="color: #080;">&#40;</span>x, y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
    txt <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">format</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>r.<span style="">no</span>.<span style="">abs</span> , <span style="color: #ff0000;">0.123456789</span><span style="color: #080;">&#41;</span>, digits<span style="color: #080;">=</span>digits<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> 
    txt <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span>prefix, txt, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">missing</span><span style="color: #080;">&#40;</span>cex.<span style="">cor</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> cex <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.8</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">strwidth</span><span style="color: #080;">&#40;</span>txt<span style="color: #080;">&#41;</span> 
&nbsp;
    test <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">cor.<span style="">test</span></span><span style="color: #080;">&#40;</span>x,y, method <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;spearman&quot;</span><span style="color: #080;">&#41;</span> 
    <span style="color: #228B22;"># borrowed from printCoefmat</span>
    Signif <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">symnum</span><span style="color: #080;">&#40;</span>test$p.<span style="">value</span>, corr <span style="color: #080;">=</span> FALSE, na <span style="color: #080;">=</span> FALSE, 
                  cutpoints <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0.001</span>, <span style="color: #ff0000;">0.01</span>, <span style="color: #ff0000;">0.05</span>, <span style="color: #ff0000;">0.1</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>,
                  <span style="color: #0000FF; font-weight: bold;">symbols</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;***&quot;</span>, <span style="color: #ff0000;">&quot;**&quot;</span>, <span style="color: #ff0000;">&quot;*&quot;</span>, <span style="color: #ff0000;">&quot;.&quot;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">text</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0.5</span>, <span style="color: #ff0000;">0.5</span>, txt, cex <span style="color: #080;">=</span> cex <span style="color: #080;">*</span> r<span style="color: #080;">&#41;</span> 
    <span style="color: #0000FF; font-weight: bold;">text</span><span style="color: #080;">&#40;</span>.8, .8, Signif, cex<span style="color: #080;">=</span>cex, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> 
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
panel.<span style="">smooth</span>.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span> <span style="color: #080;">&#40;</span>x, y, <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;col&quot;</span><span style="color: #080;">&#41;</span>, bg <span style="color: #080;">=</span> NA, pch <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;pch&quot;</span><span style="color: #080;">&#41;</span>, 
												cex <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span>, col.<span style="">smooth</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;red&quot;</span>, span <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span><span style="color: #080;">/</span><span style="color: #ff0000;">3</span>, iter <span style="color: #080;">=</span> <span style="color: #ff0000;">3</span>, 
												point.<span style="">size</span>.<span style="">rescale</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1.5</span>, ...<span style="color: #080;">&#41;</span> 
<span style="color: #080;">&#123;</span>
	<span style="color: #228B22;">#require(colorspace)</span>
    <span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#41;</span>
    z <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">merge</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>, melt<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">table</span><span style="color: #080;">&#40;</span>x ,y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">sort</span> <span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>$value
    <span style="color: #228B22;">#the.col &lt;- heat_hcl(length(x))[z]</span>
    z <span style="color: #080;">&lt;-</span> point.<span style="">size</span>.<span style="">rescale</span><span style="color: #080;">*</span>z<span style="color: #080;">/</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># notice how we rescale the dots accourding to the maximum z could have gotten</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">symbols</span><span style="color: #080;">&#40;</span> x, y,  circles <span style="color: #080;">=</span> z,<span style="color: #228B22;">#rep(0.1, length(x)), #sample(1:2, length(x), replace = T) ,</span>
			inches<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span>, bg<span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;grey&quot;</span>,<span style="color: #228B22;">#the.col ,</span>
			fg <span style="color: #080;">=</span> bg, add <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
&nbsp;
    <span style="color: #228B22;"># points(x, y, pch = pch, col = col, bg = bg, cex = cex)</span>
    ok <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">finite</span></span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> <span style="color: #0000FF; font-weight: bold;">is.<span style="">finite</span></span><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">any</span><span style="color: #080;">&#40;</span>ok<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
        <span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span>stats<span style="color: #080;">::</span><span style="color: #0000FF; font-weight: bold;">lowess</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>ok<span style="color: #080;">&#93;</span>, y<span style="color: #080;">&#91;</span>ok<span style="color: #080;">&#93;</span>, f <span style="color: #080;">=</span> span, iter <span style="color: #080;">=</span> iter<span style="color: #080;">&#41;</span>, 
            <span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">=</span> col.<span style="">smooth</span>, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
panel.<span style="">hist</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#123;</span>
    usr <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;usr&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">;</span> <span style="color: #0000FF; font-weight: bold;">on.<span style="">exit</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>usr <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>usr<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">1.5</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
    h <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">hist</span><span style="color: #080;">&#40;</span>x, <span style="color: #0000FF; font-weight: bold;">plot</span> <span style="color: #080;">=</span> FALSE, br <span style="color: #080;">=</span> <span style="color: #ff0000;">20</span><span style="color: #080;">&#41;</span>
    breaks <span style="color: #080;">&lt;-</span> h$breaks<span style="color: #080;">;</span> nB <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>breaks<span style="color: #080;">&#41;</span>
    y <span style="color: #080;">&lt;-</span> h$counts<span style="color: #080;">;</span> y <span style="color: #080;">&lt;-</span> y<span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">max</span><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">rect</span><span style="color: #080;">&#40;</span>breaks<span style="color: #080;">&#91;</span><span style="color: #080;">-</span>nB<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">0</span>, breaks<span style="color: #080;">&#91;</span><span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>, y, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;orange&quot;</span>, ...<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
pairs.<span style="">ordered</span>.<span style="">categorical</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>xx,...<span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#123;</span>
			<span style="color: #0000FF; font-weight: bold;">pairs</span><span style="color: #080;">&#40;</span>xx , 
					diag.<span style="">panel</span> <span style="color: #080;">=</span> panel.<span style="">hist</span> ,
					lower.<span style="">panel</span><span style="color: #080;">=</span>panel.<span style="">smooth</span>.<span style="">ordered</span>.<span style="">categorical</span>,
					upper.<span style="">panel</span><span style="color: #080;">=</span>panel.<span style="">cor</span>.<span style="">ordered</span>.<span style="">categorical</span>,
					cex.<span style="">labels</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1.5</span>, ...<span style="color: #080;">&#41;</span> 
		<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #228B22;"># -----------------</span>
<span style="color: #228B22;"># Example</span>
<span style="color: #228B22;"># -----------------</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">set.<span style="">seed</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">666</span><span style="color: #080;">&#41;</span>
a1 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sample</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">5</span>, <span style="color: #ff0000;">100</span>, <span style="color: #0000FF; font-weight: bold;">replace</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
a2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sample</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">5</span>, <span style="color: #ff0000;">100</span>, <span style="color: #0000FF; font-weight: bold;">replace</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
a3 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">jitter</span><span style="color: #080;">&#40;</span>a2, <span style="color: #ff0000;">7</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
	a3<span style="color: #080;">&#91;</span>a3 <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span> <span style="color: #080;">|</span> a3 <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span>
a4 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">6</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">jitter</span><span style="color: #080;">&#40;</span>a1, <span style="color: #ff0000;">7</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>
	a4<span style="color: #080;">&#91;</span>a4 <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span> <span style="color: #080;">|</span> a4 <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span>
&nbsp;
aa <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>a1,a2,a3, a4<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">reshape</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># plotting :)		</span>
pairs.<span style="">ordered</span>.<span style="">categorical</span><span style="color: #080;">&#40;</span>aa<span style="color: #080;">&#41;</span></pre></td></tr></table></div><h3> Credits:</h3><ul><li>The original R code for the correlation matrix plot was taken from <a
href="http://addictedtor.free.fr/graphiques/graphcode.php?graph=137">R Graph Gallery</a> (The differences are: 1) The use of spearman correlation;  2) The adding of hist panel and;  3) The changing of points sizes</li><li>The idea to use symbols for changing the point sizes was <a
href="http://stackoverflow.com/questions/2593643/correlation-scatter-matrix-plot-with-different-point-size-in-r">offered</a> by <a
href="http://www.linkedin.com/pub/doug-y-barbo/2/356/416">Doug Y&#8217;barbo</a>.<br
/> And also to<a
href="http://dirk.eddelbuettel.com/"> Dirk Eddelbuettel </a>for offering to use cex (although I ended up not using that)</li></ul><p>If you got ideas on how to improve this code (or reproducing it with ggplot2 or lattice), please do so in the comments (or on your own blog, but be sure to let me know <img
src='http://www.r-statistics.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</p> ]]></content:encoded> <wfw:commentRss>http://www.r-statistics.com/2010/04/correlation-scatter-plot-matrix-for-ordered-categorical-data/feed/</wfw:commentRss> <slash:comments>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[Siegel]]></category> <category><![CDATA[Siegel-Tukey]]></category> <category><![CDATA[Tukey]]></category><guid
isPermaLink="false">http://www.r-statistics.com/?p=161</guid> <description><![CDATA[Daniel Malter just shared on the R mailing list (link to the thread) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability. Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied &#8220;yes&#8221;. From here on I copy his note at full. p.s: (The R function can be downloaded from here) * * * * Hi, I recently ran into the problem that I needed a Siegel-Tukey test [...]]]></description> <content:encoded><![CDATA[<p>Daniel Malter just shared on the R mailing list (<a
href="http://n4.nabble.com/Siegel-Tukey-test-for-equal-variability-code-td1565053.html">link to the thread</a>) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability.<br
/> Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied &#8220;yes&#8221;.<br
/> From here on I copy his note at full.</p><p>p.s: (The R function can be <a
href="http://www.r-statistics.com/wp-content/uploads/2010/02/siegel-tukey-non-parametric-test-for-equal-variance.r.txt">downloaded from here</a>)</p><p>*  *  *  *<br
/> <span
id="more-161"></span></p><p>Hi, I recently ran into the problem that I needed a Siegel-Tukey test for equal variability based on ranks. Maybe there is a package that has it implemented, but I could not find it. So I programmed an R function to do it. The Siegel-Tukey test requires to recode the ranks so that they express variability rather than ascending order. This is essentially what the code further below does. After the rank  transformation, a regular Mann-Whitney U test is applied. The &#8220;manual&#8221; and code are pasted below.</p><p><strong><span
style="text-decoration: underline;">Description</span></strong>:  Non-parametric Siegel-Tukey test for equality in variability. The null hypothesis is that the variability of x is equal between two groups. A rejection of the null indicates that variability differs between<br
/> the two groups.</p><p><strong><span
style="text-decoration: underline;">Usage:</span></strong></p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
</pre></td><td
class="code"><pre class="rsplus" style="font-family:monospace;">siegel.<span style="">tukey</span><span style="color: #080;">&#40;</span>x,y,id.<span style="">col</span><span style="color: #080;">=</span>FALSE,adjust.<span style="">median</span><span style="color: #080;">=</span>FALSE,rnd<span style="color: #080;">=</span><span style="color: #ff0000;">8</span>, ...<span style="color: #080;">&#41;</span></pre></td></tr></table></div><p><strong><span
style="text-decoration: underline;">Arguments:</span></strong></p><p>x: a vector of data</p><p>y: Data of the second group (if id.col=FALSE) or group indicator (if id.col=TRUE). In the latter case, y MUST take 1 or 2 to indicate observations of group 1 and 2, respectively, and x must contain the data for both groups.</p><p>id.col: If FALSE (default), then x and y are the data columns for group 1 and 2, respectively. If TRUE, the y is the group indicator.</p><p>adjust.median: Should between-group differences in medians be leveled before performing the test? In certain cases, the Siegel-Tukey test is susceptible to median differences and may indicate significant differences in variability that, in reality, stem from differences in medians.</p><p>rnd: Should the data be rounded and, if so, to which decimal? The default (-1) uses the data as is. Otherwise, rnd must be a non-negative integer. Typically, this option is not needed. However, occasionally, differences in<br
/> the precision with which certain functions return values cause the merging of two data frames to fail within the siegel.tukey function. Only then  rounding is necessary. This operation should not be performed if it affects<br
/> the ranks of observations.</p><p>&#8230; arguments passed on to the Wilcoxon test. See ?wilcox.test</p><p><strong><span
style="text-decoration: underline;">Value</span></strong>: Among other output, the function returns rank sums for the two groups, the associated Wilcoxon&#8217;s W, and the p-value for a Wilcoxon test on tie-adjusted Siegel-Tukey ranks (i.e., it performs and returns a<br
/> Siegel-Tukey test). If significant, the group with the smaller rank sum has greater variability.</p><p><strong><span
style="text-decoration: underline;">References</span></strong>: Sidney Siegel and John Wilder Tukey (1960) &#8220;A nonparametric sum of ranks procedure for relative spread in unpaired samples.&#8221; Journal of the<br
/> American Statistical Association. See also, David J. Sheskin (2004) &#8221;Handbook of parametric and nonparametric statistical procedures.&#8221; 3rd<br
/> edition. Chapman and Hall/CRC. Boca Raton, FL.</p><p><strong><span
style="text-decoration: underline;">Notes</span></strong>: The Siegel-Tukey test has relatively low power and may, under certain conditions, indicate significance due to differences in medians rather than<br
/> differences in variabilities (consider using the argument adjust.median).</p><p><strong><span
style="text-decoration: underline;">Output</span></strong> (in this order)</p><p
style="padding-left: 30px;">1. Group medians<br
/> 2. Wilcoxon-test for between-group differences in median (after the median<br
/> adjustment if specified)<br
/> 3. Unique values of x and their tie-adjusted Siegel-Tukey ranks<br
/> 4. Xs of group 1 and their tie-adjusted Siegel-Tukey ranks<br
/> 5. Xs of group 2 and their tie-adjusted Siegel-Tukey ranks<br
/> 6. Siegel-Tukey test (Wilcoxon test on tie-adjusted Siegel-Tukey ranks)</p><p><strong>And here is the code:</strong></p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td
class="code"><pre class="rsplus" style="font-family:monospace;">siegel.<span style="">tukey</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x,y,id.<span style="">col</span><span style="color: #080;">=</span>FALSE,adjust.<span style="">median</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span>,rnd<span style="color: #080;">=-</span><span style="color: #ff0000;">1</span>,alternative<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;two.sided&quot;</span>,mu<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>,paired<span style="color: #080;">=</span>FALSE,exact<span style="color: #080;">=</span>FALSE,correct<span style="color: #080;">=</span>TRUE,conf.<span style="">int</span><span style="color: #080;">=</span>FALSE,conf.<span style="">level</span><span style="color: #080;">=</span><span style="color: #ff0000;">0.95</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>id.<span style="">col</span><span style="color: #080;">==</span>FALSE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
   <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
	<span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>
   <span style="color: #080;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#41;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;y&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">order</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>rnd<span style="color: #080;">&gt;-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x,rnd<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>adjust.<span style="">median</span><span style="color: #080;">==</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	<span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">-</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
	<span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">-</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
 <span style="color: #080;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Median of group 1 = &quot;</span>,<span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Median of group 2 = &quot;</span>,<span style="color: #0000FF; font-weight: bold;">median</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Test of median differences&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">wilcox.<span style="">test</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,<span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
 a<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,each<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
 b<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
 rk.<span style="">up</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #080;">&#40;</span>a<span style="color: #080;">*</span><span style="color: #ff0000;">4</span><span style="color: #080;">+</span>b<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">ceiling</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
 rk.<span style="">down</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">rev</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>a<span style="color: #080;">*</span><span style="color: #ff0000;">4</span><span style="color: #080;">+</span>b<span style="color: #080;">-</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">floor</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Performing Siegel-Tukey rank transformation...&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 rks<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>rk.<span style="">up</span>,rk.<span style="">down</span><span style="color: #080;">&#41;</span>
 unqs<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">unique</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">sort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
 corr.<span style="">rks</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">tapply</span><span style="color: #080;">&#40;</span>rks,<span style="color: #0000FF; font-weight: bold;">data</span>$x,<span style="color: #0000FF; font-weight: bold;">mean</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>unqs,corr.<span style="">rks</span><span style="color: #080;">&#41;</span>
 rks.<span style="">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>unqs,corr.<span style="">rks</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span><span style="color: #080;">&#41;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;unique values of x&quot;</span>,<span style="color: #ff0000;">&quot;tie-adjusted Siegel-Tukey rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span>,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>rks.<span style="">data</span><span style="color: #080;">&#41;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;unqs&quot;</span>,<span style="color: #ff0000;">&quot;corr.rks&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">merge</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>,rks.<span style="">data</span>,by.<span style="">x</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;x&quot;</span>,by.<span style="">y</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;unqs&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 rk1<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$corr.<span style="">rks</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
 rk2<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data</span>$corr.<span style="">rks</span><span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;Tie-adjusted Siegel-Tukey ranks of group 1&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 group1<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,rk1<span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>group1<span style="color: #080;">&#41;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>group1,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #ff0000;">&quot;Tie-adjusted Siegel-Tukey ranks of group 2&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 group2<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$x<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">data</span>$y<span style="color: #080;">==</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>,rk2<span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>group2<span style="color: #080;">&#41;</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;x&quot;</span>,<span style="color: #ff0000;">&quot;rank&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>group2,<span style="color: #0000FF; font-weight: bold;">row.<span style="">names</span></span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">F</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Siegel-Tukey test&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Siegel-Tukey rank transformation performed.&quot;</span>,<span style="color: #ff0000;">&quot;Tie adjusted ranks computed.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
 <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>adjust.<span style="">median</span><span style="color: #080;">==</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Medians adjusted to equality.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span><span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Medians not adjusted.&quot;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
 <span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Rank sum of group 1 =&quot;</span>, <span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>rk1<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;    Rank sum of group 2 =&quot;</span>,<span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>rk2<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">wilcox.<span style="">test</span></span><span style="color: #080;">&#40;</span>rk1,rk2,alternative<span style="color: #080;">=</span>alternative,mu<span style="color: #080;">=</span>mu,paired<span style="color: #080;">=</span>paired,exact<span style="color: #080;">=</span>exact,correct<span style="color: #080;">=</span>correct,conf.<span style="">int</span><span style="color: #080;">=</span>conf.<span style="">int</span>,conf.<span style="">level</span><span style="color: #080;">=</span>conf.<span style="">level</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">#Example:</span>
&nbsp;
x<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">4</span>,<span style="color: #ff0000;">4</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">6</span>,<span style="color: #ff0000;">6</span><span style="color: #080;">&#41;</span>
y<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">9</span>,<span style="color: #ff0000;">10</span>,<span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>
&nbsp;
siegel.<span style="">tukey</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span></pre></td></tr></table></div><pre>
<strong>Here is the code output:</strong>
<div id="_mcePaste" style="padding-left: 30px;">Median of group 1 =  5</div>
<div id="_mcePaste" style="padding-left: 30px;">Median of group 2 =  5</div>
<div id="_mcePaste" style="padding-left: 30px;">Test of median differences</div>
<div id="_mcePaste" style="padding-left: 30px;">Wilcoxon rank sum test with continuity correction</div>
<div id="_mcePaste" style="padding-left: 30px;">data:  data$x[data$y == 1] and data$x[data$y == y]</div>
<div id="_mcePaste" style="padding-left: 30px;">W = 1, p-value = 0.4274</div>
<div id="_mcePaste" style="padding-left: 30px;">alternative hypothesis: true location shift is not equal to 0</div>
<div id="_mcePaste" style="padding-left: 30px;">Performing Siegel-Tukey rank transformation...</div>
<div id="_mcePaste" style="padding-left: 30px;">unique values of x tie-adjusted Siegel-Tukey rank</div>
<div id="_mcePaste" style="padding-left: 30px;">0                            2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">1                            5.0</div>
<div id="_mcePaste" style="padding-left: 30px;">4                            8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5                           11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6                            8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">9                            6.0</div>
<div id="_mcePaste" style="padding-left: 30px;">10                            2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Tie-adjusted Siegel-Tukey ranks of group 1</div>
<div id="_mcePaste" style="padding-left: 30px;">x rank</div>
<div id="_mcePaste" style="padding-left: 30px;">4  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">4  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5 11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">5 11.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">6  8.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Tie-adjusted Siegel-Tukey ranks of group 2</div>
<div id="_mcePaste" style="padding-left: 30px;">x rank</div>
<div id="_mcePaste" style="padding-left: 30px;">0  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">0  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">1  5.0</div>
<div id="_mcePaste" style="padding-left: 30px;">9  6.0</div>
<div id="_mcePaste" style="padding-left: 30px;">10  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">10  2.5</div>
<div id="_mcePaste" style="padding-left: 30px;">Siegel-Tukey test</div>
<div id="_mcePaste" style="padding-left: 30px;">Siegel-Tukey rank transformation performed. Tie adjusted ranks computed.</div>
<div id="_mcePaste" style="padding-left: 30px;">Medians not adjusted.</div>
<div id="_mcePaste" style="padding-left: 30px;">Rank sum of group 1 = 57     Rank sum of group 2 = 21</div>
<div id="_mcePaste" style="padding-left: 30px;">Wilcoxon rank sum test with continuity correction</div>
<div id="_mcePaste" style="padding-left: 30px;">data:  rk1 and rk2</div>
<div id="_mcePaste" style="padding-left: 30px;">W = 36, p-value = 0.003601</div>
<div id="_mcePaste" style="padding-left: 30px;">alternative hypothesis: true location shift is not equal to 0</div>
<div id="_mcePaste" style="padding-left: 30px;">Warning message:</div>
<div id="_mcePaste" style="padding-left: 30px;">In wilcox.test.default(data$x[data$y == 1], data$x[data$y == y]) :</div>
<div id="_mcePaste" style="padding-left: 30px;">cannot compute exact p-value with ties</div>
<p style="padding-left: 30px;">Median of group 1 =  5 Median of group 2 =  5  Test of median differences
Wilcoxon rank sum test with continuity correction
data:  data$x[data$y == 1] and data$x[data$y == y] W = 1, p-value = 0.4274alternative hypothesis: true location shift is not equal to 0
Performing Siegel-Tukey rank transformation...   unique values of x tie-adjusted Siegel-Tukey rank                  0                            2.5                  1                            5.0                  4                            8.5                  5                           11.5                  6                            8.5                  9                            6.0                 10                            2.5
Tie-adjusted Siegel-Tukey ranks of group 1  x rank 4  8.5 4  8.5 5 11.5 5 11.5 6  8.5 6  8.5
Tie-adjusted Siegel-Tukey ranks of group 2   x rank  0  2.5  0  2.5  1  5.0  9  6.0 10  2.5 10  2.5
Siegel-Tukey test Siegel-Tukey rank transformation performed. Tie adjusted ranks computed. Medians not adjusted. Rank sum of group 1 = 57     Rank sum of group 2 = 21
Wilcoxon rank sum test with continuity correction
data:  rk1 and rk2 W = 36, p-value = 0.003601alternative hypothesis: true location shift is not equal to 0
Warning message:In wilcox.test.default(data$x[data$y == 1], data$x[data$y == y]) :  cannot compute exact p-value with ties
</pre><p>(The R function can be <a
href="http://www.r-statistics.com/wp-content/uploads/2010/02/siegel-tukey-non-parametric-test-for-equal-variance.r.txt">downloaded from here</a>)</p> ]]></content:encoded> <wfw:commentRss>http://www.r-statistics.com/2010/02/siegel-tukey-a-non-parametric-test-for-equality-in-variability-r-code/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>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 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 [...]]]></description> <content:encoded><![CDATA[<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_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
</pre></td><td
class="code"><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_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td
class="code"><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>29</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching using disk
Object Caching 422/467 objects using disk

Served from: www.r-statistics.com @ 2010-09-10 13:31:12 -->