]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/tests/f_test_regression.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / tests / f_test_regression.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_num}, @var{df_den}] =} f_test_regression (@var{y}, @var{x}, @var{rr}, @var{r})
21 ## Perform an F test for the null hypothesis rr * b = r in a classical
22 ## normal regression model y = X * b + e.
23 ##
24 ## Under the null, the test statistic @var{f} follows an F distribution
25 ## with @var{df_num} and @var{df_den} degrees of freedom.
26 ##
27 ## The p-value (1 minus the CDF of this distribution at @var{f}) is
28 ## returned in @var{pval}.
29 ##
30 ## If not given explicitly, @var{r} = 0.
31 ##
32 ## If no output argument is given, the p-value is displayed.
33 ## @end deftypefn
34
35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
36 ## Description: Test linear hypotheses in linear regression model
37
38 function [pval, f, df_num, df_den] = f_test_regression (y, x, rr, r)
39
40   if (nargin < 3 || nargin > 4)
41     print_usage ();
42   endif
43
44   [T, k] = size (x);
45   if (! (isvector (y) && (length (y) == T)))
46     error ("f_test_regression: Y must be a vector of length rows (X)");
47   endif
48   y = reshape (y, T, 1);
49
50   [q, c_R ] = size (rr);
51   if (c_R != k)
52     error ("f_test_regression: RR must have as many columns as X");
53   endif
54
55   if (nargin == 4)
56     s_r = size (r);
57     if ((min (s_r) != 1) || (max (s_r) != q))
58       error ("f_test_regression: R must be a vector of length rows (RR)");
59     endif
60     r = reshape (r, q, 1);
61   else
62     r = zeros (q, 1);
63   endif
64
65   df_num = q;
66   df_den = T - k;
67
68   [b, v] = ols (y, x);
69   diff   = rr * b - r;
70   f      = diff' * inv (rr * inv (x' * x) * rr') * diff / (q * v);
71   pval  = 1 - fcdf (f, df_num, df_den);
72
73   if (nargout == 0)
74     printf ("  pval: %g\n", pval);
75   endif
76
77 endfunction