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 -*- |
3499
|
20 ## @deftypefn {Function File} {[@var{pval}, @var{t}, @var{df}] =} t_test_regression (@var{y}, @var{x}, @var{rr}, @var{r}, @var{alt}) |
|
21 ## Perform an t test for the null hypothesis @code{@var{rr} * @var{b} = |
3454
|
22 ## @var{r}} in a classical normal regression model @code{@var{y} = |
3499
|
23 ## @var{x} * @var{b} + @var{e}}. Under the null, the test statistic @var{t} |
3454
|
24 ## follows a @var{t} distribution with @var{df} degrees of freedom. |
3200
|
25 ## |
3454
|
26 ## If @var{r} is omitted, a value of 0 is assumed. |
3200
|
27 ## |
3454
|
28 ## With the optional argument string @var{alt}, the alternative of |
|
29 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
30 ## @code{"<>"}, the null is tested against the two-sided alternative |
3499
|
31 ## @code{@var{rr} * @var{b} != @var{r}}. If @var{alt} is @code{">"}, the |
|
32 ## one-sided alternative @code{@var{rr} * @var{b} > @var{r}} is used. |
|
33 ## Similarly for @var{"<"}, the one-sided alternative @code{@var{rr} * |
3457
|
34 ## @var{b} < @var{r}} is used. The default is the two-sided case. |
3200
|
35 ## |
3454
|
36 ## The p-value of the test is returned in @var{pval}. |
3426
|
37 ## |
3200
|
38 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
39 ## @end deftypefn |
3200
|
40 |
5428
|
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
42 ## Description: Test one linear hypothesis in linear regression model |
3200
|
43 |
|
44 function [pval, t, df] = t_test_regression (y, X, R, r, alt) |
3426
|
45 |
3200
|
46 if (nargin == 3) |
|
47 r = 0; |
|
48 alt = "!="; |
|
49 elseif (nargin == 4) |
5443
|
50 if (ischar (r)) |
3200
|
51 alt = r; |
|
52 r = 0; |
|
53 else |
|
54 alt = "!="; |
|
55 endif |
3457
|
56 elseif (! (nargin == 5)) |
6046
|
57 print_usage (); |
3200
|
58 endif |
3426
|
59 |
4030
|
60 if (! isscalar (r)) |
3456
|
61 error ("t_test_regression: r must be a scalar"); |
5443
|
62 elseif (! ischar (alt)) |
3456
|
63 error ("t_test_regression: alt must be a string"); |
3200
|
64 endif |
3426
|
65 |
3200
|
66 [T, k] = size (X); |
4030
|
67 if (! (isvector (y) && (length (y) == T))) |
3456
|
68 error ("t_test_regression: y must be a vector of length rows (X)"); |
3200
|
69 endif |
|
70 s = size (R); |
3457
|
71 if (! ((max (s) == k) && (min (s) == 1))) |
3456
|
72 error ("t_test_regression: R must be a vector of length columns (X)"); |
3200
|
73 endif |
3426
|
74 |
3200
|
75 R = reshape (R, 1, k); |
|
76 y = reshape (y, T, 1); |
|
77 [b, v] = ols (y, X); |
|
78 df = T - k; |
|
79 t = (R * b - r) / sqrt (v * R * inv (X' * X) * R'); |
|
80 cdf = t_cdf (t, df); |
3426
|
81 |
3200
|
82 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
83 pval = 2 * min (cdf, 1 - cdf); |
|
84 elseif strcmp (alt, ">") |
|
85 pval = 1 - cdf; |
|
86 elseif strcmp (alt, "<") |
|
87 pval = cdf; |
|
88 else |
3273
|
89 error ("t_test_regression: the value `%s' for alt is not possible", alt); |
3200
|
90 endif |
3426
|
91 |
3200
|
92 if (nargout == 0) |
3456
|
93 printf ("pval: %g\n", pval); |
3200
|
94 endif |
|
95 |
|
96 endfunction |