forked from saundersg/Statistics-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WilcoxonTests.html
1880 lines (1804 loc) · 62.2 KB
/
WilcoxonTests.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Wilcoxon Tests</title>
<script src="site_libs/header-attrs-2.14/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cerulean.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<link rel="stylesheet" href="styles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Statistics Notebook</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
R Help
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="RCommands.html">R Commands</a>
</li>
<li>
<a href="RMarkdownHints.html">R Markdown Hints</a>
</li>
<li>
<a href="RCheatSheetsAndNotes.html">R Cheatsheets & Notes</a>
</li>
<li>
<a href="DataSources.html">Data Sources</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Describing Data
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="GraphicalSummaries.html">Graphical Summaries</a>
</li>
<li>
<a href="NumericalSummaries.html">Numerical Summaries</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Making Inference
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="MakingInference.html">Making Inference</a>
</li>
<li>
<a href="tTests.html">t Tests</a>
</li>
<li>
<a href="WilcoxonTests.html">Wilcoxon Tests</a>
</li>
<li>
<a href="Kruskal.html">Kruskal-Wallis Test</a>
</li>
<li>
<a href="ANOVA.html">ANOVA</a>
</li>
<li>
<a href="LinearRegression.html">Linear Regression</a>
</li>
<li>
<a href="LogisticRegression.html">Logistic Regression</a>
</li>
<li>
<a href="ChiSquaredTests.html">Chi Squared Tests</a>
</li>
<li>
<a href="PermutationTests.html">Randomization</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Analyses
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="./Analyses/StudentHousing.html">Good Example Analysis</a>
</li>
<li>
<a href="./Analyses/StudentHousingPOOR.html">Poor Example Analysis</a>
</li>
<li>
<a href="./Analyses/Rent.html">Rent</a>
</li>
<li>
<a href="./Analyses/Stephanie.html">Stephanie</a>
</li>
<li>
<a href="./Analyses/t Tests/HighSchoolSeniors.html">High School Seniors</a>
</li>
<li>
<a href="./Analyses/Wilcoxon Tests/RecallingWords.html">Recalling Words</a>
</li>
<li>
<a href="./Analyses/ANOVA/MyTwoWayANOVA.html">My Two-way ANOVA</a>
</li>
<li>
<a href="./Analyses/Kruskal-Wallis Test/Food.html">Food</a>
</li>
<li>
<a href="./Analyses/Linear Regression/MySimpleLinearRegression.html">My Simple Linear Regression</a>
</li>
<li>
<a href="./Analyses/Linear Regression/CarPrices.html">Car Prices</a>
</li>
<li>
<a href="./Analyses/Logistic Regression/MyLogisticRegression.html">My Logistic Regression</a>
</li>
<li>
<a href="./Analyses/Chi Squared Tests/MyChiSquaredTest.html">My Chi-sqaured Test</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Wilcoxon Tests</h1>
</div>
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
</script>
<hr />
<p>Wilcoxon tests allow for the testing of hypotheses about the value of
the the <em>median</em> without assuming the test statistic follows any
parametric distribution. They are often seen as nonparametric
alternatives to the various t tests. However, they can also be used on
ordinal data (data that is not quite quantitative, but is ordered)
unlike t tests which require quantitative data.</p>
<hr />
<div id="wilcoxon-signed-rank-test"
class="section level3 tabset tabset-fade tabset-pills">
<h3 class="tabset tabset-fade tabset-pills">Wilcoxon Signed-Rank
Test</h3>
<div style="float:left;width:125px;" align="center">
<p><img src="Images/QuantY.png" width=35px;></p>
</div>
<p>For testing hypotheses about the value of the median of (1) one
sample of quantitative data or (2) one set of differences from paired
data.</p>
<div id="overview" class="section level4">
<h4>Overview</h4>
<div style="padding-left:125px;">
<p>The nonparametric equivalent of the paired-samples t test as well as
the one-sample t test.</p>
<p>Best for smaller sample sizes where the distribution of the data is
not normal. The t test is more appropriate when the data is normal or
when the sample size is large.</p>
<p>While the test will work in most scenarios it suffers slightly when
ties (repeated values) are present in the data. If <em>many</em> ties
are present in the data, the test is not appropriate. If only a few ties
are present, the test is still appropriate.</p>
<p><strong>Hypotheses</strong></p>
<p>Originally created to test hypotheses about the value of the median,
but works as well for the mean when the distribution of the data is
symmetrical.</p>
<div style="padding-left:15px;">
<p><strong>One Sample of Data</strong></p>
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('wilcoxonsignedranklatex')">Math
Code</a>
</div>
<div id="wilcoxonsignedranklatex" style="display:none;">
<pre><code>$$
H_0: \text{Median} = \text{(Some Number)}
$$
$$
H_a: \text{Median} \neq \text{(Same Number)}
$$</code></pre>
</div>
<p><span class="math inline">\(H_0: \text{Median} = \text{(Some
Number)}\)</span></p>
<p><span class="math inline">\(H_a: \text{Median} \
\left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ \text{(Some
Number)}\)</span></p>
<p><br/></p>
<p><strong>Paired Samples of Data</strong></p>
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('wilcoxonsignedranklatexpaired')">Math
Code</a>
</div>
<div id="wilcoxonsignedranklatexpaired" style="display:none;">
<pre><code>$$
H_0: \text{median of differences} = 0
$$
$$
H_a: \text{median of differences} \neq 0
$$</code></pre>
</div>
<p><span class="math inline">\(H_0: \text{median of differences} =
0\)</span></p>
<p><span class="math inline">\(H_a: \text{median of differences} \
\left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ 0\)</span></p>
</div>
<p><strong>Examples</strong>: <a
href="./Analyses/Wilcoxon%20Tests/Examples/SleepPairedWilcoxon.html">sleep</a>,
<a
href="./Analyses/Wilcoxon%20Tests/Examples/CornHeightsPairedWilcoxon.html">CornHeights</a></p>
</div>
<hr />
</div>
<div id="r-instructions" class="section level4">
<h4>R Instructions</h4>
<div style="padding-left:125px;">
<p><strong>Console</strong> Help Command:
<code>?wilcox.test()</code></p>
<div id="paired-data" class="section level5">
<h5>Paired Data</h5>
<p><code>wilcox.test(Y1, Y2, mu = YourNull, alternative = YourAlternative, paired = TRUE, conf.level = 0.95)</code></p>
<ul>
<li><code>Y1</code> must be a “numeric” vector. One set of measurements
from the pair.</li>
<li><code>Y2</code> also a “numeric” vector. Other set of measurements
from the pair.</li>
<li><code>YourNull</code> is the numeric value from your null hypothesis
for the median of differences from the paired data. Usually zero.</li>
<li><code>YourAlternative</code> is one of the three options:
<code>"two.sided"</code>, <code>"greater"</code>, <code>"less"</code>
and should correspond to your alternative hypothesis.</li>
<li>The value for <code>conf.level = 0.95</code> can be changed to any
desired confidence level, like 0.90 or 0.99. It should correspond to
<span class="math inline">\(1-\alpha\)</span>.</li>
</ul>
<p><strong>Example Code</strong></p>
<p>Hover your mouse over the example codes to learn more.</p>
<a href="javascript:showhide('wilcoxonSignedRank')">
<div class="hoverchunk">
<p><span class="tooltipr"> wilcox.test( <span
class="tooltiprtext">‘wilcox.test’ is a function for non-parametric one
and two sample tests.</span> </span><span class="tooltipr">
sleep$extra[sleep$group==1], <span class="tooltiprtext">The hours of
extra sleep that the group had with drug 2.</span> </span><span
class="tooltipr"> sleep$extra[sleep$group==2], <span
class="tooltiprtext">The hours of extra sleep that the same group had
with drug 1.</span> </span><span class="tooltipr"> mu = 0, <span
class="tooltiprtext">The numeric value from the null hypothesis for the
median of differences from the paired data is 0 meaning the null
hypothesis is <span class="math inline">\(\text{median of differences} =
0\)</span>.</span> </span><span class="tooltipr"> paired=TRUE, <span
class="tooltiprtext">This command forces a “paired” samples test to be
performed.</span> </span><span class="tooltipr"> alternative =
“two.sided”, <span class="tooltiprtext">The alternative hypothesis is
“two.sided” meaning the alternative hypothesis is <span
class="math inline">\(\text{median of differences}
\neq0\)</span>.</span> </span><span class="tooltipr"> conf.level = 0.95)
<span class="tooltiprtext">This test has a 0.95 confidence level which
corresponds to 1 - <span class="math inline">\(\alpha\)</span>.</span>
</span><span class="tooltipr"> <br />
<span class="tooltiprtext">Press Enter to run the code if you have typed
it in yourself. You can also click here to view the output.</span>
</span><span class="tooltipr" style="float:right;"> … <span
class="tooltiprtext">Click to View Output.</span> </span></p>
</div>
<p></a></p>
<div id="wilcoxonSignedRank" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Wilcoxon signed rank test with continuity
correction <span class="tooltiprouttext">The phrase “with continuity
correction” implies that instead of using the “exact” distribution of
the test statistic a “normal approximation” was used instead to compute
the p-value. Further, a small correction was made to allow for the
change from the “discrete” exact distribution to the “continuous normal
distribution” when calculating the p-value.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> data: sleep$extra[sleep$group == 1] and
sleep$extra[sleep$group == 2] <span class="tooltiprouttext">This
statement of the output just reminds you of the code you used to perform
the test. The important thing is recognizing that the first group listed
is “Group 1” and the second group listed is “Group 2.” This is
especially important when using alternative hypotheses of “less” or
“greater” as the order is always “Group 1” is “less” than “Group 2” or
“Group 1” is “greater” than “Group 2.”</span>
</td>
<td>
<span class="tooltiprout"> V = 0, <span class="tooltiprouttext">This is
the test statistic of the test, i.e., the sum of the ranks from the
positive group minus the minimum sum of ranks possible.</span>
</td>
<td>
<span class="tooltiprout"> p-value = 0.009091 <span
class="tooltiprouttext">This is the p-value of the test. If no warning
is displayed when the test is run, then this is the “exact” p-value from
the non-parametric Wilcoxon Test Statistic distribution. Sometimes a
message will appear stating “Cannot compute exact p-value with ties” or
other similar messages. In those cases, the p-value is still considered
valid even though it is obtained through a normal approximation to the
exact distribution.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> alternative hypothesis: true location shift
is not equal to 0 <span class="tooltiprouttext">This reports that the
alternative hypothesis was “two-sided.” If the alternative had been
“less” or “greater” the wording would change accordingly.</span>
</td>
</tr>
</table>
</div>
<p><br></p>
</div>
<div id="one-sample" class="section level5">
<h5>One Sample</h5>
<p><code>wilcox.test(object, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)</code></p>
<ul>
<li><code>object</code> must be a “numeric” vector.</li>
<li><code>YourNull</code> is the numeric value from your null hypothesis
for the median (even though it says “mu”).</li>
<li><code>YourAlternative</code> is one of the three options:
<code>"two.sided"</code>, <code>"greater"</code>, <code>"less"</code>
and should correspond to your alternative hypothesis.</li>
<li>The value for <code>conf.level = 0.95</code> can be changed to any
desired confidence level, like 0.90 or 0.99. It should correspond to
<span class="math inline">\(1-\alpha\)</span>.</li>
</ul>
<p><strong>Example Code</strong></p>
<p>Hover your mouse over the example codes to learn more.</p>
<a href="javascript:showhide('wilcoxOneSample')">
<div class="hoverchunk">
<p><span class="tooltipr"> wilcox.test( <span
class="tooltiprtext">‘wilcox.test’ is a function for non-parametric one
and two sample tests.</span> </span><span class="tooltipr"> mtcars <span
class="tooltiprtext">‘mtcars’ is a dataset. Type ‘View(mtcars)’ in R to
view the dataset.</span> </span><span class="tooltipr"> $ <span
class="tooltiprtext">The $ allows us to access any variable from the
mtcars dataset.</span> </span><span class="tooltipr"> mpg, <span
class="tooltiprtext">‘mpg’ is a quantitative variable (numeric vector)
from the mtcars dataset.</span> </span><span class="tooltipr"> mu = 20,
<span class="tooltiprtext"> The numeric value from the null hypothesis
is 20 meaning <span class="math inline">\(\mu = 20\)</span>. </span>
</span><span class="tooltipr"> alternative = “two.sided”, <span
class="tooltiprtext"> The alternative is “two.sided” meaning the
alternative hypothesis is <span
class="math inline">\(\mu\neq20\)</span>.</span> </span><span
class="tooltipr"> conf.level = 0.95) <span class="tooltiprtext">This
test has a 0.95 confidence level which corresponds to 1−α. </span>
</span><span class="tooltipr"> <br />
<span class="tooltiprtext">Press Enter to run the code if you have typed
it in yourself. You can also click here to view the output.</span>
</span><span class="tooltipr" style="float:right;"> … <span
class="tooltiprtext">Click to View Output.</span> </span></p>
</div>
<p></a></p>
<div id="wilcoxOneSample" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Wilcoxon signed rank test with continuity
correction <span class="tooltiprouttext">This reports on the type of
test performed. The phrase “with continuity correction” implies the
normal approximation was used when calculating the p-value of the
test.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> data: mtcars$mpg <span
class="tooltiprouttext">This print-out reminds us that the mpg column of
the mtcars data was used as “Y” in the test.</span>
</td>
<td>
<span class="tooltiprout"> V = 249, <span class="tooltiprouttext">The
test statistic of the test.</span>
</td>
<td>
<span class="tooltiprout"> p-value = 0.7863 <span
class="tooltiprouttext">The p-value of the test.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> alternative hypothesis: true location is not
equal to 20 <span class="tooltiprouttext">The words “not equal” tell us
this was a two-sided test. Had it been a one-sided test, either the word
“less” or the word “greater” would have appeared instead of “not
equal.”</span>
</td>
</tr>
</table>
</div>
</div>
</div>
<hr />
</div>
<div id="explanation" class="section level4">
<h4>Explanation</h4>
<div style="padding-left:125px;">
<p>In many cases it is of interest to perform a hypothesis test about
the location of the center of a distribution of data. The Wilcoxon
Signed Rank Test allows a nonparametric approach to doing this.</p>
<p>The Wilcoxon Signed-Rank Test covers two important scenarios.</p>
<ol style="list-style-type: decimal">
<li><strong>One sample</strong> of data from a population. (Not very
common.)</li>
<li>The differences obtained from <strong>paired data</strong>. (Very
common.)</li>
</ol>
<p>The Wilcoxon methods are most easily explained through examples,
beginning with the paired data for which the method was originally
created. Scroll down for the <a href="#one">One Sample Example</a> if
that is what you are really interested in. However, it is still
recommended that you read the paired data example first.</p>
<div id="paired-data-example" class="section level5">
<h5>Paired Data Example</h5>
<div style="padding-left:15px;">
<div style="color:#a8a8a8;">
<p>Note: the data for this example comes from the original 1945 paper <a
href="http://sci2s.ugr.es/keel/pdf/algorithm/articulo/wilcoxon1945.pdf">Individual
Comparison by Ranking Methods</a> by Frank Wilcoxon.</p>
</div>
<div id="background" class="section level6">
<h6>Background</h6>
<p>Height differences “between cross- and self- fertilized corn plants
of the same pair” were collected. The experiment hypothesized that the
center of the distribution of the height differences would be zero, with
the alternative being that the center was not zero. The result of the
data collection was 15 height differences:</p>
<div style="padding-left:15px;">
<p><strong>Differences</strong>: 14, 56, 60, 16, 6, 8, -48, 49, 24, 28,
29, 41, -67, 23, 75</p>
</div>
</div>
<div id="step-1" class="section level6">
<h6>Step 1</h6>
<p>The first step of the Wilcoxon Signed Rank Test is to order the
differences from smallest <em>magnitude</em> to largest
<em>magnitude</em>. Negative signs are essentially ignored at this point
and only magnitudes of the numbers matter.</p>
<div style="padding-left:15px;">
<p><strong>Sorted Differences</strong>: 6, 8, 14, 16, 23, 24, 28, 29,
41, -48, 49, 56, 60, -67, 75</p>
</div>
</div>
<div id="step-2" class="section level6">
<h6>Step 2</h6>
<p>The next step is to rank the ordered values. Negative signs are
attached to the ranks corresponding to negative numbers.</p>
<div style="padding-left:15px;">
<table style="width:100%;">
<colgroup>
<col width="27%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
<col width="6%" />
<col width="4%" />
<col width="4%" />
<col width="4%" />
<col width="4%" />
<col width="4%" />
<col width="6%" />
<col width="4%" />
<col width="4%" />
<col width="4%" />
<col width="6%" />
<col width="4%" />
</colgroup>
<thead>
<tr class="header">
<th> </th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Differences</strong>:</td>
<td>6</td>
<td>8</td>
<td>14</td>
<td>16</td>
<td>23</td>
<td>24</td>
<td>28</td>
<td>29</td>
<td>41</td>
<td>-48</td>
<td>49</td>
<td>56</td>
<td>60</td>
<td>-67</td>
<td>75</td>
</tr>
<tr class="even">
<td><strong>Ranks</strong>:</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>-10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>-14</td>
<td>15</td>
</tr>
</tbody>
</table>
</div>
<p>Note that the ranks will always be of the form <span
class="math inline">\(1, 2, \ldots, n\)</span>. In this case, <span
class="math inline">\(n=15\)</span>.</p>
</div>
<div id="step-3" class="section level6">
<h6>Step 3</h6>
<p>The ranks are then put into two groups.</p>
<table>
<thead>
<tr class="header">
<th>Negative Ranks</th>
<th>Positive Ranks</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>-10, -14</td>
<td>1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 15</td>
</tr>
</tbody>
</table>
</div>
<div id="step-4" class="section level6">
<h6>Step 4</h6>
<p>One of the groups is summed, usually the group with the fewest
observations. Only the absolute values of the ranks are summed.</p>
<div style="padding-left:15px;">
<p><strong>Sum of Negative Ranks</strong>: <span
class="math inline">\(\left|-10\right| + \left|-14\right| =
24\)</span></p>
<p>The sum of the ranks becomes the <em>test statistic</em> of the
Wilcoxon Test. The test statistic is sometimes called <span
class="math inline">\(W\)</span> or <span
class="math inline">\(V\)</span> or <span
class="math inline">\(U\)</span>.</p>
</div>
</div>
<div id="step-5" class="section level6">
<h6>Step 5</h6>
<p>The <span class="math inline">\(p\)</span>-value of the test is then
obtained by computing the probability of the test statistic being as
extreme or more extreme than the one obtained. This is done by first
computing the probability of all possible values the test statistic
could have obtained using mathematical counting techniques. This is a
very tedious process that only a mathematician would enjoy pursuing.
However, the end result is fairly easily understood. If you are
interested, read the details.</p>
<div style="padding-left:30px; padding-right:15px;">
<p><a href="javascript:showhide('uniquename')"><strong>Details</strong></a></p>
<div id="uniquename" style="display:none;">
<p>When there are <span class="math inline">\(n=15\)</span> ranks, the
possible sums of ranks range from 0 to 120 and hit every integer in
between, i.e., <span class="math inline">\(1, 2, 3, \ldots,
120\)</span>. (Note, if summing the negative ranks these sums would
technically all be negative.)</p>
<p>To verify that <span class="math inline">\(120\)</span> is the
largest sum possible for <span class="math inline">\(n=15\)</span>
ranks, note that:</p>
<ul>
<li><p><span class="math inline">\(1+15 = 16\)</span>,</p></li>
<li><p><span class="math inline">\(2 + 14 = 16\)</span>,</p></li>
<li><p><span class="math inline">\(3+13 = 16\)</span>,</p></li>
<li><p><span class="math inline">\(4+12=16\)</span>,</p></li>
<li><p><span class="math inline">\(5+11=16\)</span>,</p></li>
<li><p><span class="math inline">\(6+10=16\)</span>,</p></li>
<li><p><span class="math inline">\(7+9=16\)</span>,</p></li>
<li><p>and finally that <span class="math inline">\(8 =
\frac{16}{2}\)</span>.</p></li>
</ul>
<p>Thus, there are 7 sums of 16 and one sum of <span
class="math inline">\(\frac{16}{2}\)</span>. This could be said in a
mathematically equivalent way by stating there are <span
class="math inline">\(\frac{14}{2}\)</span> sums of 16 and one sum of
<span class="math inline">\(\frac{16}{2}\)</span>. By multiplication
this gives <span class="math display">\[
\frac{14}{2}\cdot\frac{16}{1} + \frac{1}{1}\cdot\frac{16}{2} =
\frac{14\cdot16 + 1\cdot16}{2} = \frac{15\cdot16}{2} = \frac{n(n+1)}{2}
= 120
\]</span></p>
<p>The probability of each sum occurring is computed by counting all of
the ways a certain sum can occur (combinations) and dividing by the
total number of sums possible. (There are 32,768 total different groups
of ranks possible when there are <span
class="math inline">\(n=15\)</span> ranks.)</p>
<p>For example, a sum of 1 can happen only one way, only the rank of 1
is in the group. A sum of 2 can also only happen 1 way. The sum of 3
however, can happen two ways: we could have the ranks of 1 and 2 in the
group, or just the rank of 3 in the group. A similar counting technique
is then implemented for each possible sum. After all the calculations
are performed, the distribution of possible sums looks like what is
shown in the following plot, where the red bars show those sums that are
as extreme or more extreme than a sum of <span
class="math inline">\(24\)</span> (or its opposite of <span
class="math inline">\(120-24=96\)</span>).</p>
</div>
</div>
<p><img src="WilcoxonTests_files/figure-html/unnamed-chunk-2-1.png" width="672" /></p>
<p>Computing the probabilities of all possible sums creates a
distribution of the test statistic (shown in the plot above). Note that
the test statistic is obtained in Step 4 (above) by taking the sum of
the ranks. Once the distribution of the test statistic is established,
the <span class="math inline">\(p\)</span>-value of the test can be
calculated as the combined probability of possible sums that are as
extreme or more extreme than the one observed.</p>
<p>For this example, it turns out that the probability of getting a sum
of (the absolute value of) negative ranks as extreme or more extreme
than <span class="math inline">\(24\)</span> is <span
class="math inline">\(p=0.04126\)</span> (the sum of the probabilities
of the red bars in the plot above). Thus, at the <span
class="math inline">\(\alpha=0.05\)</span> level we would reject the
null hypothesis that the center of the distribution of differences is
zero. We conclude that the center of the distribution is greater than
zero because the sum of negative ranks is much smaller than we expected
under the zero center hypothesis (the null). Thus, there is sufficient
evidence to conclude that the centers of the distributions of “cross-
and self-fertilized corn plants” heights are not equal. One is greater
than the other. Notice how the following dot plot shows that the
differences are in favor of the cross-fertilized plants (the first group
in the subtraction) being taller. This is true even though two
self-fertilized plants were much taller than their cross-fertilized
counterpart (the two negative differences).</p>
<p><img src="WilcoxonTests_files/figure-html/unnamed-chunk-3-1.png" width="672" /></p>
</div>
<div id="comment" class="section level6">
<h6>Comment</h6>
<p>If the distribution of differences is symmetric, then the hypotheses
can be written as <span class="math display">\[
H_0: \mu = 0
\]</span> <span class="math display">\[
H_a: \mu \neq 0
\]</span></p>
<p>If the distribution is skewed, then the hypotheses technically refer
to the median instead of the mean and should be written as</p>
<p><span class="math display">\[
H_0: \text{median} = 0
\]</span> <span class="math display">\[
H_a: \text{median} \neq 0
\]</span></p>
</div>
</div>
</div>
<div id="one" class="section level5">
<h5>One Sample Example</h5>
<div style="padding-left:15px;">
<p>The idea behind the one sample Wilcoxon Signed Rank test is nearly
identical to the paired data. The only change is that the median must be
subtracted from all observed values to obtain the <em>differences</em>.
Note that the mean is equal to the median when data is symmetric.</p>
<div id="background-1" class="section level6">
<h6>Background</h6>
<p>Suppose we are interested in testing to see if the median hourly wage
of BYU-Idaho students during their off-track employment is equal to the
minimum wage in Idaho, $7.25 an hour as of January 1st, 2015. Five
randomly sampled hourly wages from BYU-Idaho Math 221B students provides
the following data.</p>
<div style="padding-left:15px;">
<p><strong>Wages</strong>: $6.00, $9.00, $8.10, $18.00, $10.45</p>
</div>
<p>The differences are then obtained by subtracting the hypothesized
value for the median (or mean if the data is symmetric) from all
observations.</p>
<div style="padding-left:15px;">
<p><strong>Differences</strong>: -1.25, 1.75, 0.85, 10.75, 3.20</p>
<div style="color:#a8a8a8;">
<p>Note: from this point down, the wording of this example is identical
to the paired data example (above) with the numbers changed to match
<span class="math inline">\(n=5\)</span>. It is useful to continue
reading to reinforce the idea of the Wilcoxon Signed Rank Test, but no
new knowledge will be presented.</p>
</div>
</div>
</div>
<div id="step-1-1" class="section level6">
<h6>Step 1</h6>
<p>The first step of the Wilcoxon Signed Rank Test is to order the
differences from smallest <em>magnitude</em> to largest
<em>magnitude</em>. Negative signs are essentially ignored at this point
and only magnitudes of the numbers matter.</p>
<div style="padding-left:15px;">
<p><strong>Sorted Differences</strong>: 0.85, -1.25, 1.75, 3.20,
10.75</p>
</div>
</div>
<div id="step-2-1" class="section level6">
<h6>Step 2</h6>
<p>The next step is to rank the ordered values. Negative signs are
attached to the ranks corresponding to negative numbers.</p>
<div style="padding-left:15px;">
<p><strong>Ranks</strong>: 1, -2, 3, 4, 5</p>
</div>
<p>Note that the ranks will always be of the form <span
class="math inline">\(1, 2, \ldots, n\)</span>. In this case, <span
class="math inline">\(n=5\)</span>.</p>
</div>
<div id="step-3-1" class="section level6">
<h6>Step 3</h6>
<p>The ranks are then put into two groups.</p>
<table>
<thead>
<tr class="header">
<th>Negative Ranks</th>
<th>Positive Ranks</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>-2</td>
<td>1, 3, 4, 5</td>
</tr>
</tbody>
</table>
</div>
<div id="step-4-1" class="section level6">
<h6>Step 4</h6>