3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
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. |
|
7 ## |
|
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 |
|
11 ## General Public License for more details. |
|
12 ## |
|
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 |
|
17 ## usage: [pval, t, df] = t_test_regression (y, X, R [, r] [, alt]) |
|
18 ## |
|
19 ## Performs an t test for the null hypothesis R * b = r in a classical |
|
20 ## normal regression model y = X * b + e. |
|
21 ## Under the null, the test statistic t follows a t distribution with |
|
22 ## df degrees of freedom. |
|
23 ## |
|
24 ## r is taken as 0 if not given explicitly. |
|
25 ## |
|
26 ## With the optional argument string alt, the alternative of interest |
|
27 ## can be selected. |
|
28 ## If alt is "!=" or "<>", the null is tested against the two-sided |
|
29 ## alternative R * b != r. |
|
30 ## If alt is ">", the one-sided alternative R * b > r is used, |
|
31 ## similarly for "<". |
|
32 ## The default is the two-sided case. |
|
33 ## |
|
34 ## pval is the p-value of the test. |
|
35 ## |
|
36 ## If no output argument is given, the p-value of the test is displayed. |
|
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) |
|
42 |
|
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] ", ... |
|
55 "= t_test_regression (y, X, R [, r] [, alt]"]); |
|
56 endif |
|
57 |
|
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 |
|
63 |
|
64 [T, k] = size (X); |
|
65 if !(is_vector (y) && (length (y) == T)) |
|
66 error (["t_test_regression: ", ... |
|
67 "y must be a vector of length rows (X)"]); |
|
68 endif |
|
69 s = size (R); |
|
70 if !((max (s) == k) && (min (s) == 1)) |
|
71 error (["t_test_regression: ", ... |
|
72 "R must be a vector of length columns (X)"]); |
|
73 endif |
|
74 |
|
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); |
|
81 |
|
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 |
|
89 error (["t_test_regression: ", ... |
|
90 sprintf ("the value %s for alt is not possible", alt)]); |
|
91 endif |
|
92 |
|
93 if (nargout == 0) |
|
94 printf ("pval: %g\n", pval); |
|
95 endif |
|
96 |
|
97 endfunction |