Mercurial > octave
annotate scripts/statistics/tests/wilcoxon_test.m @ 23219:3ac9f9ecfae5 stable
maint: Update copyright dates.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 Feb 2017 12:39:29 -0500 |
parents | e9a0469dedd9 |
children | 092078913d54 |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 1995-2017 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3454 | 19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
20 ## @deftypefn {} {[@var{pval}, @var{z}] =} wilcoxon_test (@var{x}, @var{y}, @var{alt}) |
3454 | 21 ## For two matched-pair sample vectors @var{x} and @var{y}, perform a |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
22 ## Wilcoxon signed-rank test of the null hypothesis |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
23 ## PROB (@var{x} > @var{y}) == 1/2. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
24 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
25 ## Under the null, the test statistic @var{z} approximately follows a |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
26 ## standard normal distribution when @var{n} > 25. |
6701 | 27 ## |
12973
b80b18f537ca
doc: Correct various Texinfo warnings about incorrect use of macros.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 ## @strong{Caution:} This function assumes a normal distribution for @var{z} |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
7017
diff
changeset
|
29 ## and thus is invalid for @var{n} @leq{} 25. |
3200 | 30 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
31 ## With the optional argument string @var{alt}, the alternative of interest |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
32 ## can be selected. If @var{alt} is @qcode{"!="} or @qcode{"<>"}, the null |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
33 ## is tested against the two-sided alternative |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
34 ## PROB (@var{x} > @var{y}) != 1/2. If alt is @qcode{">"}, the one-sided |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
35 ## alternative PROB (@var{x} > @var{y}) > 1/2 is considered. Similarly for |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
36 ## @qcode{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) < 1/2 is |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
37 ## considered. The default is the two-sided case. |
3200 | 38 ## |
3454 | 39 ## The p-value of the test is returned in @var{pval}. |
3426 | 40 ## |
3200 | 41 ## If no output argument is given, the p-value of the test is displayed. |
3454 | 42 ## @end deftypefn |
3200 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 45 ## Description: Wilcoxon signed-rank test |
3426 | 46 |
3200 | 47 function [pval, z] = wilcoxon_test (x, y, alt) |
3426 | 48 |
6701 | 49 if (nargin < 2 || nargin > 3) |
6046 | 50 print_usage (); |
3200 | 51 endif |
3426 | 52 |
4030 | 53 if (! (isvector (x) && isvector (y) && (length (x) == length (y)))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
54 error ("wilcoxon_test: X and Y must be vectors of the same length"); |
3200 | 55 endif |
56 | |
57 n = length (x); | |
58 x = reshape (x, 1, n); | |
59 y = reshape (y, 1, n); | |
60 d = x - y; | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
61 d = d(find (d != 0)); |
3200 | 62 n = length (d); |
6701 | 63 if (n > 25) |
3200 | 64 r = ranks (abs (d)); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
65 z = sum (r(find (d > 0))); |
3200 | 66 z = ((z - n * (n + 1) / 4) / sqrt (n * (n + 1) * (2 * n + 1) / 24)); |
67 else | |
6701 | 68 error ("wilcoxon_test: implementation requires more than 25 different pairs"); |
3200 | 69 endif |
70 | |
71 cdf = stdnormal_cdf (z); | |
3426 | 72 |
3200 | 73 if (nargin == 2) |
74 alt = "!="; | |
75 endif | |
76 | |
5443 | 77 if (! ischar (alt)) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 error ("wilcoxon_test: ALT must be a string"); |
3200 | 79 elseif (strcmp (alt, "!=") || strcmp (alt, "<>")) |
80 pval = 2 * min (cdf, 1 - cdf); | |
81 elseif (strcmp (alt, ">")) | |
82 pval = 1 - cdf; | |
83 elseif (strcmp (alt, "<")) | |
84 pval = cdf; | |
85 else | |
3456 | 86 error ("wilcoxon_test: option %s not recognized", alt); |
3200 | 87 endif |
88 | |
89 if (nargout == 0) | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
90 printf (" pval: %g\n", pval); |
3200 | 91 endif |
3426 | 92 |
3200 | 93 endfunction |