forked from byuistats/Statistics-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WilcoxonTests.Rmd
executable file
·1087 lines (732 loc) · 44.9 KB
/
WilcoxonTests.Rmd
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
---
title: "Wilcoxon Tests"
output:
html_document:
theme: cerulean
css: styles.css
---
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
</script>
```{r, include=FALSE}
library(mosaic)
```
----
Wilcoxon tests allow for the testing of hypotheses about the value of the the *median* 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.
----
### Wilcoxon Signed-Rank Test {.tabset .tabset-fade .tabset-pills}
<div style="float:left;width:125px;" align=center>
<img src="./Images/QuantY.png" width=35px;>
</div>
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.
#### Overview
<div style="padding-left:125px;">
The nonparametric equivalent of the paired-samples t test as well as the one-sample t test.
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.
While the test will work in most scenarios it suffers slightly when ties (repeated values) are present in the data. If *many* ties are present in the data, the test is not appropriate. If only a few ties are present, the test is still appropriate.
**Hypotheses**
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.
<div style="padding-left:15px;">
**One Sample of Data**
<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;">
```{}
$$
H_0: \text{Median} = \text{(Some Number)}
$$
$$
H_a: \text{Median} \neq \text{(Same Number)}
$$
```
</div>
$H_0: \text{Median} = \text{(Some Number)}$
$H_a: \text{Median} \ \left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ \text{(Some Number)}$
<br/>
**Paired Samples of Data**
<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;">
```{}
$$
H_0: \text{median of differences} = 0
$$
$$
H_a: \text{median of differences} \neq 0
$$
```
</div>
$H_0: \text{median of differences} = 0$
$H_a: \text{median of differences} \ \left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ 0$
</div>
**Examples**: [sleep](./Analyses/Wilcoxon Tests/Examples/SleepPairedWilcoxon.html), [CornHeights](./Analyses/Wilcoxon Tests/Examples/CornHeightsPairedWilcoxon.html)
</div>
----
#### R Instructions
<div style="padding-left:125px;">
**Console** Help Command: `?wilcox.test()`
##### Paired Data
`wilcox.test(Y1, Y2, mu = YourNull, alternative = YourAlternative, paired = TRUE, conf.level = 0.95)`
* `Y1` must be a "numeric" vector. One set of measurements from the pair.
* `Y2` also a "numeric" vector. Other set of measurements from the pair.
* `YourNull` is the numeric value from your null hypothesis for the median of differences from the paired data. Usually zero.
* `YourAlternative` is one of the three options: `"two.sided"`, `"greater"`, `"less"` and should correspond to your alternative hypothesis.
* The value for `conf.level = 0.95` can be changed to any desired confidence level, like 0.90 or 0.99. It should correspond to $1-\alpha$.
**Example Code**
Hover your mouse over the example codes to learn more.
<a href="javascript:showhide('wilcoxonSignedRank')">
<div class="hoverchunk">
<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 $\text{median of differences} = 0$.</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 $\text{median of differences} \neq0$.</span>
</span><span class="tooltipr">
conf.level = 0.95)
<span class="tooltiprtext">This test has a 0.95 confidence level which corresponds to 1 - $\alpha$.</span>
</span><span class="tooltipr">
<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>
</div>
</a>
<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>
<br/>
<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>
<br>
##### One Sample
`wilcox.test(object, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)`
* `object` must be a "numeric" vector.
* `YourNull` is the numeric value from your null hypothesis for the median (even though it says "mu").
* `YourAlternative` is one of the three options: `"two.sided"`, `"greater"`, `"less"` and should correspond to your alternative hypothesis.
* The value for `conf.level = 0.95` can be changed to any desired confidence level, like 0.90 or 0.99. It should correspond to $1-\alpha$.
**Example Code**
Hover your mouse over the example codes to learn more.
<a href="javascript:showhide('wilcoxOneSample')">
<div class="hoverchunk">
<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 $\mu = 20$. </span>
</span><span class="tooltipr">
alternative = "two.sided",
<span class="tooltiprtext"> The alternative is "two.sided" meaning the alternative hypothesis is $\mu\neq20$.</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">
<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>
</div>
</a>
<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>
<br/>
<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>
----
#### Explanation
<div style="padding-left:125px;">
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.
The Wilcoxon Signed-Rank Test covers two important scenarios.
1. **One sample** of data from a population. (Not very common.)
2. The differences obtained from **paired data**. (Very common.)
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 [One Sample Example](#one) if that is what you are really interested in. However, it is still recommended that you read the paired data example first.
##### Paired Data Example
<div style="padding-left:15px;">
<div style="color:#a8a8a8;">
Note: the data for this example comes from the original 1945 paper [Individual Comparison by Ranking Methods](http://sci2s.ugr.es/keel/pdf/algorithm/articulo/wilcoxon1945.pdf) by Frank Wilcoxon.
</div>
###### Background
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:
<div style="padding-left:15px;">
**Differences**: 14, 56, 60, 16, 6, 8, -48, 49, 24, 28, 29, 41, -67, 23, 75
</div>
###### Step 1
The first step of the Wilcoxon Signed Rank Test is to order the differences from smallest *magnitude* to largest *magnitude*. Negative signs are essentially ignored at this point and only magnitudes of the numbers matter.
<div style="padding-left:15px;">
**Sorted Differences**: 6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75
</div>
###### Step 2
The next step is to rank the ordered values. Negative signs are attached to the ranks corresponding to negative numbers.
<div style="padding-left:15px;">
| | | | | | | | | | | | | | | | |
|-----------------|--|--|--|----|---|---|---|---|---|----|---|---|---|----|---|
|**Differences**: | 6| 8| 14| 16| 23| 24| 28| 29| 41| -48| 49| 56| 60| -67| 75 |
|**Ranks**: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9| -10| 11 | 12 | 13 | -14 | 15 |
</div>
Note that the ranks will always be of the form $1, 2, \ldots, n$. In this case, $n=15$.
###### Step 3
The ranks are then put into two groups.
| Negative Ranks | Positive Ranks |
|----------------|----------------|
| -10, -14 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 15 |
###### Step 4
One of the groups is summed, usually the group with the fewest observations. Only the absolute values of the ranks are summed.
<div style="padding-left:15px;">
**Sum of Negative Ranks**: $\left|-10\right| + \left|-14\right| = 24$
The sum of the ranks becomes the *test statistic* of the Wilcoxon Test. The test statistic is sometimes called $W$ or $V$ or $U$.
</div>
###### Step 5
The $p$-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.
<div style="padding-left:30px; padding-right:15px;">
<a href="javascript:showhide('uniquename')">**Details**</a>
<div id="uniquename" style="display:none;">
When there are $n=15$ ranks, the possible sums of ranks range from 0 to 120 and hit every integer in between, i.e., $1, 2, 3, \ldots, 120$. (Note, if summing the negative ranks these sums would technically all be negative.)
To verify that $120$ is the largest sum possible for $n=15$ ranks, note that:
* $1+15 = 16$,
* $2 + 14 = 16$,
* $3+13 = 16$,
* $4+12=16$,
* $5+11=16$,
* $6+10=16$,
* $7+9=16$,
* and finally that $8 = \frac{16}{2}$.
Thus, there are 7 sums of 16 and one sum of $\frac{16}{2}$. This could be said in a mathematically equivalent way by stating there are $\frac{14}{2}$ sums of 16 and one sum of $\frac{16}{2}$. By multiplication this gives
$$
\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
$$
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 $n=15$ ranks.)
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 $24$ (or its opposite of $120-24=96$).
</div>
</div>
```{r, echo=FALSE}
tmp <- cbind(rep(c(0,1), each=32768/2),
rep(c(0,1), each=32768/4, times=2),
rep(c(0,1), each=32768/8, times=4),
rep(c(0,1), each=32768/16, times=8),
rep(c(0,1), each=32768/32, times=16),
rep(c(0,1), each=32768/64, times=32),
rep(c(0,1), each=32768/128, times=64),
rep(c(0,1), each=32768/256, times=128),
rep(c(0,1), each=32768/512, times=256),
rep(c(0,1), each=32768/1024, times=512),
rep(c(0,1), each=32768/2048, times=1024),
rep(c(0,1), each=32768/4096, times=2048),
rep(c(0,1), each=32768/8192, times=4096),
rep(c(0,1), each=32768/16384, times=8192),
rep(c(0,1), each=1, times=16384))
tmp2 <- tmp%*%1:15
tmp3 <- sapply(0:120, function(s) sum(tmp2 == s))
plot(0:120, tmp3/sum(tmp3), col='skyblue2', type='h', ylab="Probability", xlab="Sum of Ranks", main="Probability Distribution of Possible Sums")
points(c(0:24,96:120), tmp3[c(1:25,97:121)]/sum(tmp3), col='firebrick', type='h')
text(11,.01, "Observed Sum")
lines(c(24,24),c(0.009,0.003))
```
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 $p$-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.
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 $24$ is $p=0.04126$ (the sum of the probabilities of the red bars in the plot above). Thus, at the $\alpha=0.05$ 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).
```{r, echo=FALSE, fig.height = 4}
stripchart(c(14,56, 60, 16, 6, 8, -48, 49, 24, 28, 29, 41, -67, 23, 75), method="stack", pch=16, xlab="Differences", main="Cross- vs. Self-Fertilized Corn Plant Height Differences")
```
###### Comment
If the distribution of differences is symmetric, then the hypotheses can be written as
$$
H_0: \mu = 0
$$
$$
H_a: \mu \neq 0
$$
If the distribution is skewed, then the hypotheses technically refer to the median instead of the mean and should be written as
$$
H_0: \text{median} = 0
$$
$$
H_a: \text{median} \neq 0
$$
</div>
##### One Sample Example {#one}
<div style="padding-left:15px;">
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 *differences*. Note that the mean is equal to the median when data is symmetric.
###### Background
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.
<div style="padding-left:15px;">
**Wages**: $6.00, $9.00, $8.10, $18.00, $10.45
</div>
```{r, include=FALSE}
wages <- c(6.00, 9.00, 8.10, 18.00, 10.45)
```
The differences are then obtained by subtracting the hypothesized value for the median (or mean if the data is symmetric) from all observations.
<div style="padding-left:15px;">
```{r, include=FALSE}
diff <- wages - 7.25
```
**Differences**: -1.25, 1.75, 0.85, 10.75, 3.20
<div style="color:#a8a8a8;">
Note: from this point down, the wording of this example is identical to the paired data example (above) with the numbers changed to match $n=5$. It is useful to continue reading to reinforce the idea of the Wilcoxon Signed Rank Test, but no new knowledge will be presented.
</div>
</div>
###### Step 1
The first step of the Wilcoxon Signed Rank Test is to order the differences from smallest *magnitude* to largest *magnitude*. Negative signs are essentially ignored at this point and only magnitudes of the numbers matter.
<div style="padding-left:15px;">
**Sorted Differences**: 0.85, -1.25, 1.75, 3.20, 10.75
</div>
###### Step 2
The next step is to rank the ordered values. Negative signs are attached to the ranks corresponding to negative numbers.
<div style="padding-left:15px;">
**Ranks**: 1, -2, 3, 4, 5
</div>
Note that the ranks will always be of the form $1, 2, \ldots, n$. In this case, $n=5$.
###### Step 3
The ranks are then put into two groups.
| Negative Ranks | Positive Ranks |
|----------------|----------------|
| -2 | 1, 3, 4, 5 |
###### Step 4
One of the groups is summed, usually the group with the fewest observations.
<div style="padding-left:15px;">
**Sum of Negative Ranks**: $\left|-2\right| = 2$
</div>
###### Step 5
The $p$-value of the test is then obtained by computing the probabilities of all possible sums 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.
<div style="padding-left:30px; padding-right:15px;">
**Details**
When there are $n=5$ ranks, the possible sums of ranks range from 0 to 15 and hit every integer in between, i.e., $1, 2, 3, \ldots, 15$. (Note, if summing the negative ranks these sums would technically all be negative.)
To verify that $15$ is the largest sum possible for $n=5$ ranks, note that:
* $1+5 = 6$,
* $2 + 4 = 6$,
* and finally $3 = \frac{6}{2}$.
Thus, there are 2 sums of 6 and one sum of $\frac{6}{2}$. This could be said in a mathematically equivalent way by stating there are $\frac{4}{2}$ sums of 6 and one sum of $\frac{6}{2}$. By multiplication this gives
$$
\frac{4}{2}\cdot\frac{6}{1} + \frac{1}{1}\cdot\frac{6}{2} = \frac{4\cdot6 + 1\cdot6}{2} = \frac{5\cdot6}{2} = \frac{n(n+1)}{2} = 15
$$
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 total different groups of ranks possible when there are $n=5$ ranks.)
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 $2$ (or its opposite of $15-2=13$).
```{r, echo=FALSE}
tmp <- cbind(rep(c(0,1), each=32/2),
rep(c(0,1), each=32/4, times=2),
rep(c(0,1), each=32/8, times=4),
rep(c(0,1), each=32/16, times=8),
rep(c(0,1), each=32/32, times=16))
tmp2 <- tmp%*%1:5
tmp3 <- sapply(0:15, function(s) sum(tmp2 == s))
plot(0:15, tmp3/sum(tmp3), col='skyblue2', type='h', ylab="Probability", xlab="Sum of Ranks", main="Probability Distribution of Possible Sums", ylim=c(0,.1), xlim=c(-1,16))
points(c(0:2,13:15), tmp3[c(1:3,14:16)]/sum(tmp3), col='firebrick', type='h')
text(1,.08, "Observed Sum = 2")
lines(c(2,2),c(0.035,0.075))
arrows(2,0.075,2,0.035, angle=30, length=.05)
```
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 $p$-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.
</div>
For this example, it turns out that the probability of getting a sum of negative ranks as extreme or more extreme than $-2$ is $p=0.1875$ (the sum of the probabilities of the red bars in the plot above). Thus, at the $\alpha=0.05$ level we would fail to reject the null hypothesis that the center of the distribution of differences is zero. We will continue to assume the null hypothesis was true, that the median off-track hourly wage of BYU-Idaho students is the same as the Idaho minimum wage.
###### Final Comment
Notice that when the sample size is large the distribution of the test statistic can be approximated by a normal distribution. Most software applications use this approximation when the sample size is over $50$, i.e., for $n\geq50$ because computing all the possible sums of ranks becomes incredibly time consuming. Thus, for large sample sizes the results will be almost identical whether the Wilcoxon Tests or a t Test is used.
</div>
</div>
----
### Wilcoxon Rank Sum (Mann-Whitney) Test {.tabset .tabset-fade .tabset-pills}
<div style="float:left;width:125px;" align=center>
<img src="./Images/QuantYQualXg2.png" width=58px;>
</div>
For testing the equality of the medians of two (possibly different) distributions of a quantitative variable.
#### Overview
<div style="padding-left:125px;">
The nonparametric equivalent of the Independent Samples t Test. Can also be used when data is ordered (ordinal) but does not have an exact measurement. For example, first place, second place, and so on.
The Independent Samples t Test is more appropriate when the distributions are normal, or when the sample size for each sample is large.
The test is negatively affected when there are ties (repeated values) present in the data, but the results are still useful if there are relatively few ties.
**Hypotheses**
Originally designed to test for the equality of medians from two identically shaped distributions.
<div style="padding-left:15px;">
<div style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;"><a style="color:darkgray;" href="javascript:showhide('wilcoxonranksumlatex')">Math Code</a></div>
<div id="wilcoxonranksumlatex" style="display:none;">
```{}
$$
H_0: \text{difference in medians} = 0
$$
$$
H_a: \text{difference in medians} \neq 0
$$
```
</div>
$H_0: \text{difference in medians} = 0$
$H_a: \text{difference in medians} \neq 0$
</div>
However, the test also allows for the more general hypotheses that one distribution is *stochastically greater* than the other.
<div style="padding-left:15px;">
$H_0: \text{the distributions are stochastically equal}$
$H_a: \text{one distribution is stochastically greater than the other}$
</div>
If these hypotheses are used, then the distributions do not have to be identically distributed.
(Note: Men's heights are *stochastically greater* than women's heights because men are generally taller than women, but not all men are taller than all women.)
**Examples**: [BugSpray](./Analyses/Wilcoxon Tests/Examples/BugSprayWilcoxonRankSum.html), [MoralIntegration](./Analyses/Wilcoxon Tests/Examples/MoralIntegration.html)
</div>
----
#### R Instructions
<div style="padding-left:125px;">
**Console** Help Command: `?wilcox.test()`
There are two ways to perform the test.
**Option 1:**
`wilcox.test(Y ~ X, data = YourData, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)`
* `Y` must be a "numeric" vector from `YourData` that represents the data for both samples.
* `X` must be a "factor" or "character" vector from `YourData` that represents the group assignment for each observation. There can only be two groups specified in this column of data.
* `YourNull` is the numeric value from your null hypothesis for the difference in medians from the two groups.
* `YourAlternative` is one of the three options: `"two.sided"`, `"greater"`, `"less"` and should correspond to your alternative hypothesis.
* The value for `conf.level = 0.95` can be changed to any desired confidence level, like 0.90 or 0.99. It should correspond to $1-\alpha$.
**Example Code**
Hover your mouse over the example codes to learn more.
<a href="javascript:showhide('wilcoxonRankSum')">
<div class="hoverchunk">
<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">
length
<span class="tooltiprtext">'length' is a quantitative variable (numeric vector).</span>
</span><span class="tooltipr">
~
<span class="tooltiprtext">'~' is the tilde symbol.</span>
</span><span class="tooltipr">
sex,
<span class="tooltiprtext">'sex' is a 'factor' or 'character' vector that represents the group assignment for each observation. There are two groups.</span>
</span><span class="tooltipr">
data=KidsFeet,
<span class="tooltiprtext">'KidsFeet' is a dataset in library(mosaic). Type View(KidsFeet) to view it.</span>
</span><span class="tooltipr">
mu = 0,
<span class="tooltiprtext">The numeric value from the null hypothesis for the difference in medians from the two groups is 0 meaning the null hypothesis is $\text{difference in medians} = 0$</span>
</span><span class="tooltipr">
alternative = "two.sided",
<span class="tooltiprtext"> The alternative is "two-sided" meaning the alternative hypothesis is $\text{difference in medians} \neq 0$.</span>
</span><span class="tooltipr">
conf.level = 0.95)
<span class="tooltiprtext">This test has a 0.95 confidence level which corresponds to $1-\alpha$</span>
</span><span class="tooltipr">
<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>
</div>
</a>
<div id="wilcoxonRankSum" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout">
Wilcoxon rank sum test with continuity correction
<span class="tooltiprouttext">This states the test that was performed and that a normal approximation to the test statistic was used instead of the exact distribution.</span>
</span>
</td>
</tr>
</table>
<br/>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout">
data: length by sex
<span class="tooltiprouttext">This states that the length column was split into the two groups found in the "sex" column. Unfortunately, it forgets to remind us that the test used the KidsFeet data set.</span>
</td><td>
<span class="tooltiprout">
V = 252,
<span class="tooltiprouttext">The test statistic of the test. In this case, the sum of the ranks from the alphabetically first group minus the minimum sum of ranks possible.</span>
</td><td>
<span class="tooltiprout">
p-value = 0.0836
<span class="tooltiprouttext">The p-value of the test.</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 test used an alternative hypothesis of "not equal" (a two-sided test). Further, the phrase "true location shift" emphasizes that the Wilcoxon Rank Sum Test is testing to see if one distribution is shifted higher or lower than the other.</span>
</td>
</tr>
</table>
</div>
<br>
**Option 2:**
`wilcox.test(object1, object2, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)`
* `object1` must be a "numeric" vector that represents the first sample of data.
* `obejct2` must be a "numeric" vector that represents the second sample of data.
* `YourNull` is the numeric value from your null hypothesis for the difference in medians from the two groups.
* `YourAlternative` is one of the three options: `"two.sided"`, `"greater"`, `"less"` and should correspond to your alternative hypothesis.
* The value for `conf.level = 0.95` can be changed to any desired confidence level, like 0.90 or 0.99. It should correspond to $1-\alpha$.
**Example Code**
<a href="javascript:showhide('wilcoxonRankSum2')">
<div class="hoverchunk">
<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">
KidsFeet\$length[KidsFeet\$sex == "B"],
<span class="tooltiprtext">A numeric vector of foot length for the first sample of data or for the group of boys.</span>
</span><span class="tooltipr">
KidsFeet\$length[KidsFeet$sex == "G"],
<span class="tooltiprtext">A numeric vector of foot length for the second sample of data or for the group of girls.</span>
</span><span class="tooltipr">
mu = 0,
<span class="tooltiprtext">The numeric value from the null hypothesis for the difference in medians from the two groups is 0 meaning the null hypothesis is $\text{difference in medians} = 0$</span>
</span><span class="tooltipr">
alternative = "two.sided",
<span class="tooltiprtext"> The alternative is "two-sided" meaning the alternative hypothesis is $\text{difference in medians} \neq 0$.</span>
</span><span class="tooltipr">
conf.level = 0.95)
<span class="tooltiprtext">This test has a 0.95 confidence level which corresponds to $1-\alpha$</span>
</span><span class="tooltipr">
<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>
</div>
</a>
<div id="wilcoxonRankSum2" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout">
Wilcoxon rank sum test with continuity correction
<span class="tooltiprouttext">This states the type of test performed.</span>
</span>
</td>
</tr>
</table>
<br/>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout">
data: KidsFeet\$length[KidsFeet\$sex == "B"] and KidsFeet\$length[KidsFeet\$sex == "G"]
<span class="tooltiprouttext">Reminds you what data was used for the test and points out that the first set of data listed is "Group 1" and the second set of data listed is "Group 2."</span>
</td><td>
<span class="tooltiprout">
V = 252,
<span class="tooltiprouttext">The test statistic of the test.</span>
</td><td>
<span class="tooltiprout">
p-value = 0.0836
<span class="tooltiprouttext">The p-value of the test.</span>
</td>
</tr><tr>
<td>
<span class="tooltiprout">
alternative hypothesis: true location shift is not equal to 0
<span class="tooltiprouttext">The alternative hypothesis of the test. The phrase "not equal" implies a "two-sided" alternative was used.</span>
</td>
</tr>
</table>
</div>
</div>
----
#### Explanation
<div style="padding-left:125px;">
In many cases it is of interest to perform a hypothesis test concerning the equality of the centers of two (possibly different) distributions. In other words, an independent samples test. The Wilcoxon Rank Sum Test allows a nonparametric approach to doing this. It is often considered the nonparametric equivalent of the independent samples t test.
The method is most easily explained through an example. The theory behind it is very similar to the theory behind the Wilcoxon Signed-Rank Test.
<br />
##### Independent Samples Data Example
<div style="padding-left:15px;">
<div style="color:#a8a8a8;">
Note: the data for this example comes from the original 1945 paper [Individual Comparison by Ranking Methods](http://sci2s.ugr.es/keel/pdf/algorithm/articulo/wilcoxon1945.pdf) by Frank Wilcoxon.
</div>
###### Background
The percent of flies (bugs) killed from two different concentrations of a certain spray were recorded from 16 different trials, 8 trials per treatment concentration. The experiment hypothesized that the center of the distributions of the percent killed by either concentration were the same. In other words, that both treatments were equally effective. The alternative hypothesis was that the treatments differed in their effectiveness.
<div style="padding-left:15px;">
| Spray Concentration | Percent Killed |
|---------------------|----------------|
| A | 68, 68, 59, 72, 64, 67, 70, 74 |
| B | 60, 67, 61, 62, 67, 63, 56, 58 |
```{r, echo=FALSE, render='asis'}
kill <- cbind(PercentKillA = c(68,68,59,72,64,67,70,74),
PercentKillB = c(60,67,61,62,67,63,56,58))
```
</div>
###### Step 1
The first step of the Wilcoxon Rank Sum Test is to order all the data from smallest *magnitude* to largest *magnitude*, while keeping track of the group.
<div style="padding-left:15px;">
| Sorted Data | | | | | | | | | | | | | | | | |
|-------------|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
| Percent Killed | 56| 58| 59| 60| 61| 62| 63| 64| 67| 67| 67| 68| 68| 70| 72| 74|
| Concentration | B| B| A| B| B| B| B| A| A| B| B| A| A| A| A| A|
</div>
###### Step 2
The next step is to rank the ordered values.
<div style="padding-left:15px;">
| Sorted Data | | | | | | | | | | | | | | | | |
|-------------|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
| Percent Killed | 56| 58| 59| 60| 61| 62| 63| 64| 67| 67| 67| 68| 68| 70| 72| 74|
| Concentration | B| B| A| B| B| B| B| A| A| B| B| A| A| A| A| A|
| Rank | 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|
</div>
Note that the ranks will always be of the form $1, 2, \ldots, n$. In this case, $n=16$.
Any ranks that are tied need to have the average rank assigned to each of those that are tied.
| Sorted Data | | | | | | | | | | | | | | | | |
|-------------|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
| Percent Killed | 56| 58| 59| 60| 61| 62| 63| 64| <span style="color:#B22222;">67</span>| <span style="color:#B22222;">67</span>| <span style="color:#B22222;">67</span>| <span style="color:#7EC0EE;">68</span>| <span style="color:#7EC0EE;">68</span>| 70| 72| 74|
| Concentration | B| B| A| B| B| B| B| A| A| B| B| A| A| A| A| A|
| Rank | 1| 2| 3| 4| 5| 6| 7| 8| <span style="color:#B22222;">10</span>|<span style="color:#B22222;">10</span>|<span style="color:#B22222;">10</span>|<span style="color:#7EC0EE;">12.5</span>|<span style="color:#7EC0EE;">12.5</span>|14|15|16|
###### Step 3
The ranks are then returned to their original groups.
| Ranks of Spray A | Ranks of Spray B |
|----------------|----------------|
| 3, 8, 10, 12.5, 12.5, 14, 15, 16 | 1, 2, 4, 5, 6, 7, 10, 10 |
###### Step 4
The ranks are summed for one of the groups. (It does not matter which group.)
<div style="padding-left:15px;">
**Sum of Ranks for Spray A**:
$$
3+8+10+12.5+12.5+14+15+16 = 91
$$
</div>
###### Step 5
The $p$-value of the test is then obtained by computing the probabilities of all possible sums that one group can achieve 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.