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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) 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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3454
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{pval}, @var{chisq}, @var{df}] =} chisquare_test_independence (@var{x}) |
7001
|
21 ## Perform a chi-square test for independence based on the contingency |
3454
|
22 ## table @var{x}. Under the null hypothesis of independence, |
|
23 ## @var{chisq} approximately has a chi-square distribution with |
|
24 ## @var{df} degrees of freedom. |
3200
|
25 ## |
3454
|
26 ## The p-value (1 minus the CDF of this distribution at chisq) of the |
|
27 ## test is returned in @var{pval}. |
3200
|
28 ## |
|
29 ## If no output argument is given, the p-value is displayed. |
3454
|
30 ## @end deftypefn |
3200
|
31 |
5428
|
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
33 ## Description: Chi-square test for independence |
3200
|
34 |
|
35 function [pval, chisq, df] = chisquare_test_independence (X) |
|
36 |
|
37 if (nargin != 1) |
6046
|
38 print_usage (); |
3200
|
39 endif |
3426
|
40 |
3238
|
41 [r, s] = size (X); |
|
42 df = (r - 1) * (s - 1); |
|
43 n = sum (sum (X)); |
|
44 Y = sum (X')' * sum (X) / n; |
|
45 X = (X - Y) .^2 ./ Y; |
3200
|
46 chisq = sum (sum (X)); |
|
47 pval = 1 - chisquare_cdf (chisq, df); |
3426
|
48 |
3200
|
49 if (nargout == 0) |
3456
|
50 printf(" pval: %g\n", pval); |
3200
|
51 endif |
|
52 |
7001
|
53 endfunction |