3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3200
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3200
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3200
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
|
17 ## usage: [pval, b, n] = sign_test (x, y [, alt]) |
|
18 ## |
|
19 ## For two matched-pair samples x and y, perform a sign test of the |
|
20 ## null hypothesis PROB(x > y) == PROB(x < y) == 1/2. |
|
21 ## Under the null, the test statistic b roughly follows a binomial |
|
22 ## distribution with parameters n = sum (x != y) and p = 1/2. |
|
23 ## |
|
24 ## With the optional argument alt, the alternative of interest can be |
|
25 ## selected. |
|
26 ## If alt is "!=" or "<>", the null hypothesis is tested against the |
3426
|
27 ## two-sided alternative PROB(x < y) != 1/2. |
3200
|
28 ## If alt is ">", the one-sided alternative PROB(x > y) > 1/2 ("x is |
|
29 ## stochastically greater than y") is considered, similarly for "<". |
|
30 ## The default is the two-sided case. |
|
31 ## |
|
32 ## pval is the p-value of the test. |
3426
|
33 ## |
3200
|
34 ## If no output argument is given, the p-value of the test is displayed. |
|
35 |
|
36 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
37 ## Description: Sign test |
|
38 |
|
39 function [pval, b, n] = sign_test (x, y, alt) |
|
40 |
|
41 if ((nargin < 2) || (nargin > 3)) |
|
42 usage ("[pval, b, n] = sign_test (x, y [, alt])"); |
|
43 endif |
3426
|
44 |
3200
|
45 if (! (is_vector (x) && is_vector (y) && (length (x) == length (y)))) |
|
46 error ("sign_test: x and y must be vectors of the same length"); |
|
47 endif |
|
48 |
|
49 n = length (x); |
|
50 x = reshape (x, 1, n); |
|
51 y = reshape (y, 1, n); |
|
52 n = sum (x != y); |
|
53 b = sum (x > y); |
|
54 cdf = binomial_cdf (b, n, 1/2); |
3426
|
55 |
3200
|
56 if (nargin == 2) |
|
57 alt = "!="; |
|
58 endif |
|
59 |
|
60 if (! isstr (alt)) |
|
61 error ("sign_test: alt must be a string"); |
|
62 endif |
|
63 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
64 pval = 2 * min (cdf, 1 - cdf); |
|
65 elseif strcmp (alt, ">") |
|
66 pval = 1 - cdf; |
|
67 elseif strcmp (alt, "<") |
|
68 pval = cdf; |
|
69 else |
|
70 error (sprintf ("sign_test: option %s not recognized", alt)); |
|
71 endif |
3426
|
72 |
3200
|
73 if (nargout == 0) |
|
74 printf (" pval: %g\n", pval); |
|
75 endif |
3426
|
76 |
3200
|
77 endfunction |