changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/tests/wilcoxon_test.m

changeset 10289: 4b124317dc38
parent:93c65f2a5668
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (62 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
2## Kurt Hornik
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {[@var{pval}, @var{z}] =} wilcoxon_test (@var{x}, @var{y}, @var{alt})
22## For two matched-pair sample vectors @var{x} and @var{y}, perform a
23## Wilcoxon signed-rank test of the null hypothesis PROB (@var{x} >
24## @var{y}) == 1/2. Under the null, the test statistic @var{z}
25## approximately follows a standard normal distribution when @var{n} > 25.
26##
27## @strong{Warning}: This function assumes a normal distribution for @var{z}
28## and thus is invalid for @var{n} <= 25.
29##
30## With the optional argument string @var{alt}, the alternative of
31## interest can be selected. If @var{alt} is @code{"!="} or
32## @code{"<>"}, the null is tested against the two-sided alternative
33## PROB (@var{x} > @var{y}) != 1/2. If alt is @code{">"}, the one-sided
34## alternative PROB (@var{x} > @var{y}) > 1/2 is considered. Similarly
35## for @code{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) <
36## 1/2 is considered. The default is the two-sided case.
37##
38## The p-value of the test is returned in @var{pval}.
39##
40## If no output argument is given, the p-value of the test is displayed.
41## @end deftypefn
42
43## Author: KH <Kurt.Hornik@wu-wien.ac.at>
44## Description: Wilcoxon signed-rank test
45
46function [pval, z] = wilcoxon_test (x, y, alt)
47
48 if (nargin < 2 || nargin > 3)
49 print_usage ();
50 endif
51
52 if (! (isvector (x) && isvector (y) && (length (x) == length (y))))
53 error ("wilcoxon_test: x and y must be vectors of the same length");
54 endif
55
56 n = length (x);
57 x = reshape (x, 1, n);
58 y = reshape (y, 1, n);
59 d = x - y;
60 d = d (find (d != 0));
61 n = length (d);
62 if (n > 25)
63 r = ranks (abs (d));
64 z = sum (r (find (d > 0)));
65 z = ((z - n * (n + 1) / 4) / sqrt (n * (n + 1) * (2 * n + 1) / 24));
66 else
67 error ("wilcoxon_test: implementation requires more than 25 different pairs");
68 endif
69
70 cdf = stdnormal_cdf (z);
71
72 if (nargin == 2)
73 alt = "!=";
74 endif
75
76 if (! ischar (alt))
77 error("wilcoxon_test: alt must be a string");
78 elseif (strcmp (alt, "!=") || strcmp (alt, "<>"))
79 pval = 2 * min (cdf, 1 - cdf);
80 elseif (strcmp (alt, ">"))
81 pval = 1 - cdf;
82 elseif (strcmp (alt, "<"))
83 pval = cdf;
84 else
85 error ("wilcoxon_test: option %s not recognized", alt);
86 endif
87
88 if (nargout == 0)
89 printf (" pval: %g\n", pval);
90 endif
91
92endfunction