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, chisq, df] = chisquare_test_homogeneity (x, y, c) |
|
18 ## |
|
19 ## Given two samples x and y, perform a chisquare test for homogeneity |
|
20 ## of the null hypothesis that x and y come from the same distribution, |
|
21 ## based on the partition induced by the (strictly increasing) entries |
|
22 ## of c. |
|
23 ## |
|
24 ## For large samples, the test statistic chisq approximately follows a |
|
25 ## chisquare distribution with df = length(c) degrees pf freedom. pval |
|
26 ## is the p-value (1 minus the CDF of this distribution at chisq) of the |
|
27 ## test. |
|
28 ## |
|
29 ## If no output argument is given, the p-value is displayed. |
|
30 |
|
31 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
32 ## Description: Chi-square test for homogeneity |
|
33 |
|
34 function [pval, chisq, df] = chisquare_test_homogeneity (x, y, c) |
3426
|
35 |
3200
|
36 if (nargin != 3) |
|
37 usage ("[pval, chisq, df] = chisquare_test_homogeneity (x, y, c)"); |
|
38 endif |
3426
|
39 |
3200
|
40 if (! (is_vector(x) && is_vector(y) && is_vector(c))) |
|
41 error ("chisquare_test_homogeneity: x, y and c must be vectors"); |
|
42 endif |
|
43 ## Now test c for strictly increasing entries |
|
44 df = length (c); |
|
45 if (any ( (c(2 : df) - c(1 : (df - 1))) <= 0)) |
|
46 error ("chisquare_test_homogeneity: c must be increasing"); |
|
47 endif |
3426
|
48 |
3273
|
49 c = [(reshape (c, 1, df)), Inf]; |
3200
|
50 l_x = length (x); |
|
51 x = reshape (x, l_x, 1); |
|
52 n_x = sum (x * ones (1, df+1) < ones (l_x, 1) * c); |
|
53 l_y = length (y); |
|
54 y = reshape (y, l_y, 1); |
|
55 n_y = sum(y * ones (1, df+1) < ones (l_y, 1) * c); |
|
56 chisq = l_x * l_y * sum ((n_x/l_x - n_y/l_y).^2 ./ (n_x + n_y)); |
|
57 pval = 1 - chisquare_cdf (chisq, df); |
3426
|
58 |
3200
|
59 if (nargout == 0) |
|
60 printf(" pval: %g\n", pval); |
|
61 endif |
|
62 |
|
63 endfunction |