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 |
3454
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{pval}, @var{f}, @var{df_num}, @var{df_den}] =} f_test_regression (@var{y}, @var{X}, @var{R}, @var{r}) |
|
19 ## Perform an F test for the null hypothesis R * b = r in a classical |
3200
|
20 ## normal regression model y = X * b + e. |
|
21 ## |
3454
|
22 ## Under the null, the test statistic @var{f} follows an F distribution |
|
23 ## with @var{df_num} and @var{df_den} degrees of freedom. |
3200
|
24 ## |
3454
|
25 ## The p-value (1 minus the CDF of this distribution at @var{f}) is |
|
26 ## returned in @var{pval}. |
|
27 ## |
|
28 ## If not given explicitly, @var{r} = 0. |
3200
|
29 ## |
|
30 ## If no output argument is given, the p-value is displayed. |
3454
|
31 ## @end deftypefn |
3200
|
32 |
|
33 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
34 ## Description: Test linear hypotheses in linear regression model |
|
35 |
|
36 function [pval, f, df_num, df_den] = f_test_regression (y, X, R, r) |
3426
|
37 |
3200
|
38 if (nargin < 3 || nargin > 4) |
|
39 usage (["[pval, f, df_num, df_den] ", ... |
3426
|
40 "= f_test_regression (y, X, R [, r])"]); |
3200
|
41 endif |
|
42 |
|
43 [T, k] = size (X); |
|
44 if !( is_vector (y) && (length (y) == T) ) |
|
45 error (["f_test_regression: ", ... |
3426
|
46 "y must be a vector of length rows (X)."]); |
3200
|
47 endif |
|
48 y = reshape (y, T, 1); |
3426
|
49 |
3200
|
50 [q, c_R ] = size (R); |
|
51 if (c_R != k) |
|
52 error (["f_test_regression: ", ... |
3426
|
53 "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)) |
|
59 error (["f_test_regression: ", ... |
3426
|
60 "r must be a vector of length rows (R)."]); |
3200
|
61 endif |
|
62 r = reshape (r, q, 1); |
|
63 else |
|
64 r = zeros (q, 1); |
|
65 endif |
|
66 |
|
67 df_num = q; |
|
68 df_den = T - k; |
3426
|
69 |
3200
|
70 [b, v] = ols (y, X); |
|
71 diff = R * b - r; |
|
72 f = diff' * inv (R * inv (X' * X) * R') * diff / ( q * v ); |
|
73 pval = 1 - f_cdf (f, df_num, df_den); |
3426
|
74 |
3200
|
75 if (nargout == 0) |
|
76 printf (" pval: %g\n", pval); |
|
77 endif |
|
78 |
|
79 endfunction |