7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, |
|
2 ## 2007 Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
3499
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{t}, @var{df}] =} t_test_regression (@var{y}, @var{x}, @var{rr}, @var{r}, @var{alt}) |
|
22 ## Perform an t test for the null hypothesis @code{@var{rr} * @var{b} = |
3454
|
23 ## @var{r}} in a classical normal regression model @code{@var{y} = |
3499
|
24 ## @var{x} * @var{b} + @var{e}}. Under the null, the test statistic @var{t} |
3454
|
25 ## follows a @var{t} distribution with @var{df} degrees of freedom. |
3200
|
26 ## |
3454
|
27 ## If @var{r} is omitted, a value of 0 is assumed. |
3200
|
28 ## |
3454
|
29 ## With the optional argument string @var{alt}, the alternative of |
|
30 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
31 ## @code{"<>"}, the null is tested against the two-sided alternative |
3499
|
32 ## @code{@var{rr} * @var{b} != @var{r}}. If @var{alt} is @code{">"}, the |
|
33 ## one-sided alternative @code{@var{rr} * @var{b} > @var{r}} is used. |
|
34 ## Similarly for @var{"<"}, the one-sided alternative @code{@var{rr} * |
3457
|
35 ## @var{b} < @var{r}} is used. The default is the two-sided case. |
3200
|
36 ## |
3454
|
37 ## The p-value of the test is returned in @var{pval}. |
3426
|
38 ## |
3200
|
39 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
40 ## @end deftypefn |
3200
|
41 |
5428
|
42 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
43 ## Description: Test one linear hypothesis in linear regression model |
3200
|
44 |
|
45 function [pval, t, df] = t_test_regression (y, X, R, r, alt) |
3426
|
46 |
3200
|
47 if (nargin == 3) |
|
48 r = 0; |
|
49 alt = "!="; |
|
50 elseif (nargin == 4) |
5443
|
51 if (ischar (r)) |
3200
|
52 alt = r; |
|
53 r = 0; |
|
54 else |
|
55 alt = "!="; |
|
56 endif |
3457
|
57 elseif (! (nargin == 5)) |
6046
|
58 print_usage (); |
3200
|
59 endif |
3426
|
60 |
4030
|
61 if (! isscalar (r)) |
3456
|
62 error ("t_test_regression: r must be a scalar"); |
5443
|
63 elseif (! ischar (alt)) |
3456
|
64 error ("t_test_regression: alt must be a string"); |
3200
|
65 endif |
3426
|
66 |
3200
|
67 [T, k] = size (X); |
4030
|
68 if (! (isvector (y) && (length (y) == T))) |
3456
|
69 error ("t_test_regression: y must be a vector of length rows (X)"); |
3200
|
70 endif |
|
71 s = size (R); |
3457
|
72 if (! ((max (s) == k) && (min (s) == 1))) |
3456
|
73 error ("t_test_regression: R must be a vector of length columns (X)"); |
3200
|
74 endif |
3426
|
75 |
3200
|
76 R = reshape (R, 1, k); |
|
77 y = reshape (y, T, 1); |
|
78 [b, v] = ols (y, X); |
|
79 df = T - k; |
|
80 t = (R * b - r) / sqrt (v * R * inv (X' * X) * R'); |
|
81 cdf = t_cdf (t, df); |
3426
|
82 |
3200
|
83 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
84 pval = 2 * min (cdf, 1 - cdf); |
|
85 elseif strcmp (alt, ">") |
|
86 pval = 1 - cdf; |
|
87 elseif strcmp (alt, "<") |
|
88 pval = cdf; |
|
89 else |
3273
|
90 error ("t_test_regression: the value `%s' for alt is not possible", alt); |
3200
|
91 endif |
3426
|
92 |
3200
|
93 if (nargout == 0) |
3456
|
94 printf ("pval: %g\n", pval); |
3200
|
95 endif |
|
96 |
|
97 endfunction |