3200
|
1 ## Copyright (C) 1995, 1996, 1997 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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{b}, @var{n}] =} sign_test (@var{x}, @var{y}, @var{alt}) |
|
22 ## For two matched-pair samples @var{x} and @var{y}, perform a sign test |
|
23 ## of the null hypothesis PROB (@var{x} > @var{y}) == PROB (@var{x} < |
|
24 ## @var{y}) == 1/2. Under the null, the test statistic @var{b} roughly |
|
25 ## follows a binomial distribution with parameters @code{@var{n} = sum |
|
26 ## (@var{x} != @var{y})} and @var{p} = 1/2. |
3200
|
27 ## |
3454
|
28 ## With the optional argument @code{alt}, the alternative of interest |
|
29 ## can be selected. If @var{alt} is @code{"!="} or @code{"<>"}, the |
|
30 ## null hypothesis is tested against the two-sided alternative PROB |
|
31 ## (@var{x} < @var{y}) != 1/2. If @var{alt} is @code{">"}, the |
|
32 ## one-sided alternative PROB (@var{x} > @var{y}) > 1/2 ("x is |
3457
|
33 ## stochastically greater than y") is considered. Similarly for |
|
34 ## @code{"<"}, the one-sided alternative PROB (@var{x} > @var{y}) < 1/2 |
|
35 ## ("x is stochastically less than y") is considered. The default is |
|
36 ## the two-sided case. |
3200
|
37 ## |
3454
|
38 ## The p-value of the test is returned in @var{pval}. |
3426
|
39 ## |
3200
|
40 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
41 ## @end deftypefn |
3200
|
42 |
5428
|
43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
44 ## Description: Sign test |
3200
|
45 |
|
46 function [pval, b, n] = sign_test (x, y, alt) |
|
47 |
|
48 if ((nargin < 2) || (nargin > 3)) |
3456
|
49 usage ("[pval, b, n] = sign_test (x, y, alt)"); |
3200
|
50 endif |
3426
|
51 |
4030
|
52 if (! (isvector (x) && isvector (y) && (length (x) == length (y)))) |
3456
|
53 error ("sign_test: x and y must be vectors of the same length"); |
3200
|
54 endif |
|
55 |
|
56 n = length (x); |
|
57 x = reshape (x, 1, n); |
|
58 y = reshape (y, 1, n); |
|
59 n = sum (x != y); |
|
60 b = sum (x > y); |
|
61 cdf = binomial_cdf (b, n, 1/2); |
3426
|
62 |
3200
|
63 if (nargin == 2) |
|
64 alt = "!="; |
|
65 endif |
|
66 |
|
67 if (! isstr (alt)) |
3456
|
68 error ("sign_test: alt must be a string"); |
3200
|
69 endif |
|
70 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
71 pval = 2 * min (cdf, 1 - cdf); |
|
72 elseif strcmp (alt, ">") |
|
73 pval = 1 - cdf; |
|
74 elseif strcmp (alt, "<") |
|
75 pval = cdf; |
|
76 else |
3456
|
77 error ("sign_test: option %s not recognized", alt); |
3200
|
78 endif |
3426
|
79 |
3200
|
80 if (nargout == 0) |
3456
|
81 printf (" pval: %g\n", pval); |
3200
|
82 endif |
3426
|
83 |
3200
|
84 endfunction |