7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, |
|
2 ## 2007 Kurt Hornik |
3426
|
3 ## |
3922
|
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 |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{chisq}, @var{df}] =} chisquare_test_homogeneity (@var{x}, @var{y}, @var{c}) |
|
22 ## Given two samples @var{x} and @var{y}, perform a chisquare test for |
|
23 ## homogeneity of the null hypothesis that @var{x} and @var{y} come from |
|
24 ## the same distribution, based on the partition induced by the |
|
25 ## (strictly increasing) entries of @var{c}. |
3200
|
26 ## |
3454
|
27 ## For large samples, the test statistic @var{chisq} approximately follows a |
|
28 ## chisquare distribution with @var{df} = @code{length (@var{c})} |
|
29 ## degrees of freedom. |
3200
|
30 ## |
3454
|
31 ## The p-value (1 minus the CDF of this distribution at @var{chisq}) is |
|
32 ## returned in @var{pval}. |
3200
|
33 ## |
|
34 ## If no output argument is given, the p-value is displayed. |
3454
|
35 ## @end deftypefn |
3200
|
36 |
5428
|
37 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
38 ## Description: Chi-square test for homogeneity |
3200
|
39 |
|
40 function [pval, chisq, df] = chisquare_test_homogeneity (x, y, c) |
3426
|
41 |
3200
|
42 if (nargin != 3) |
6046
|
43 print_usage (); |
3200
|
44 endif |
3426
|
45 |
4030
|
46 if (! (isvector(x) && isvector(y) && isvector(c))) |
3200
|
47 error ("chisquare_test_homogeneity: x, y and c must be vectors"); |
|
48 endif |
|
49 ## Now test c for strictly increasing entries |
|
50 df = length (c); |
3457
|
51 if (any ((c(2 : df) - c(1 : (df - 1))) <= 0)) |
3456
|
52 error ("chisquare_test_homogeneity: c must be increasing"); |
3200
|
53 endif |
3426
|
54 |
3273
|
55 c = [(reshape (c, 1, df)), Inf]; |
3200
|
56 l_x = length (x); |
|
57 x = reshape (x, l_x, 1); |
|
58 n_x = sum (x * ones (1, df+1) < ones (l_x, 1) * c); |
|
59 l_y = length (y); |
|
60 y = reshape (y, l_y, 1); |
|
61 n_y = sum(y * ones (1, df+1) < ones (l_y, 1) * c); |
|
62 chisq = l_x * l_y * sum ((n_x/l_x - n_y/l_y).^2 ./ (n_x + n_y)); |
|
63 pval = 1 - chisquare_cdf (chisq, df); |
3426
|
64 |
3200
|
65 if (nargout == 0) |
3456
|
66 printf(" pval: %g\n", pval); |
3200
|
67 endif |
|
68 |
|
69 endfunction |