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