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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
3499
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_num}, @var{df_den}] =} f_test_regression (@var{y}, @var{x}, @var{rr}, @var{r}) |
|
22 ## Perform an F test for the null hypothesis rr * b = r in a classical |
3200
|
23 ## normal regression model y = X * b + e. |
|
24 ## |
3454
|
25 ## Under the null, the test statistic @var{f} follows an F distribution |
|
26 ## with @var{df_num} and @var{df_den} degrees of freedom. |
3200
|
27 ## |
3454
|
28 ## The p-value (1 minus the CDF of this distribution at @var{f}) is |
|
29 ## returned in @var{pval}. |
|
30 ## |
|
31 ## If not given explicitly, @var{r} = 0. |
3200
|
32 ## |
|
33 ## If no output argument is given, the p-value is displayed. |
3454
|
34 ## @end deftypefn |
3200
|
35 |
3456
|
36 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
37 ## Description: Test linear hypotheses in linear regression model |
3200
|
38 |
|
39 function [pval, f, df_num, df_den] = f_test_regression (y, X, R, r) |
3426
|
40 |
3200
|
41 if (nargin < 3 || nargin > 4) |
3456
|
42 usage ("[pval, f, df_num, df_den] = f_test_regression (y, X, R, r)"); |
3200
|
43 endif |
|
44 |
|
45 [T, k] = size (X); |
4030
|
46 if (! (isvector (y) && (length (y) == T))) |
3456
|
47 error ("f_test_regression: y must be a vector of length rows (X)"); |
3200
|
48 endif |
|
49 y = reshape (y, T, 1); |
3426
|
50 |
3200
|
51 [q, c_R ] = size (R); |
|
52 if (c_R != k) |
3457
|
53 error ("f_test_regression: R must have as many columns as X"); |
3200
|
54 endif |
3426
|
55 |
3200
|
56 if (nargin == 4) |
|
57 s_r = size (r); |
|
58 if ((min (s_r) != 1) || (max (s_r) != q)) |
3457
|
59 error ("f_test_regression: r must be a vector of length rows (R)"); |
3200
|
60 endif |
|
61 r = reshape (r, q, 1); |
|
62 else |
|
63 r = zeros (q, 1); |
|
64 endif |
|
65 |
|
66 df_num = q; |
|
67 df_den = T - k; |
3426
|
68 |
3200
|
69 [b, v] = ols (y, X); |
|
70 diff = R * b - r; |
3457
|
71 f = diff' * inv (R * inv (X' * X) * R') * diff / (q * v); |
3200
|
72 pval = 1 - f_cdf (f, df_num, df_den); |
3426
|
73 |
3200
|
74 if (nargout == 0) |
3456
|
75 printf (" pval: %g\n", pval); |
3200
|
76 endif |
|
77 |
|
78 endfunction |