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{t}, @var{df}] =} t_test_regression (@var{y}, @var{x}, @var{R}, @var{r}, @var{alt}) |
|
19 ## Perform an t test for the null hypothesis @code{@var{R} * @var{b} = |
|
20 ## @var{r}} in a classical normal regression model @code{@var{y} = |
|
21 ## @var{X} * @var{b} + @var{e}}. Under the null, the test statistic @var{t} |
|
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 |
|
29 ## @code{@var{R} * @var{b} != @var{r}}. If @var{alt} is @code{">"}, the |
|
30 ## one-sided alternative @code{@var{R} * @var{b} > @var{r}} is used. |
|
31 ## Similarly for @var{"<"}. The default is the two-sided case. |
3200
|
32 ## |
3454
|
33 ## The p-value of the test is returned in @var{pval}. |
3426
|
34 ## |
3200
|
35 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
36 ## @end deftypefn |
3200
|
37 |
|
38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
39 ## Description: Test one linear hypothesis in linear regression model |
|
40 |
|
41 function [pval, t, df] = t_test_regression (y, X, R, r, alt) |
3426
|
42 |
3200
|
43 if (nargin == 3) |
|
44 r = 0; |
|
45 alt = "!="; |
|
46 elseif (nargin == 4) |
|
47 if (isstr (r)) |
|
48 alt = r; |
|
49 r = 0; |
|
50 else |
|
51 alt = "!="; |
|
52 endif |
|
53 elseif !(nargin == 5) |
|
54 usage (["[pval, t, df] ", ... |
3426
|
55 "= t_test_regression (y, X, R [, r] [, alt]"]); |
3200
|
56 endif |
3426
|
57 |
3200
|
58 if (! is_scalar (r)) |
|
59 error ("t_test_regression: r must be a scalar"); |
|
60 elseif (! isstr (alt)) |
|
61 error ("t_test_regression: alt must be a string"); |
|
62 endif |
3426
|
63 |
3200
|
64 [T, k] = size (X); |
|
65 if !(is_vector (y) && (length (y) == T)) |
|
66 error (["t_test_regression: ", ... |
3426
|
67 "y must be a vector of length rows (X)"]); |
3200
|
68 endif |
|
69 s = size (R); |
|
70 if !((max (s) == k) && (min (s) == 1)) |
|
71 error (["t_test_regression: ", ... |
3426
|
72 "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) |
|
93 printf ("pval: %g\n", pval); |
|
94 endif |
|
95 |
|
96 endfunction |