1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
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} =
22 ## @var{r}} in a classical normal regression model @code{@var{y} =
23 ## @var{x} * @var{b} + @var{e}}. Under the null, the test statistic @var{t}
24 ## follows a @var{t} distribution with @var{df} degrees of freedom.
26 ## If @var{r} is omitted, a value of 0 is assumed.
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
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} *
34 ## @var{b} < @var{r}} is used. The default is the two-sided case.
36 ## The p-value of the test is returned in @var{pval}.
38 ## If no output argument is given, the p-value of the test is displayed.
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
42 ## Description: Test one linear hypothesis in linear regression model
44 function [pval, t, df] = t_test_regression (y, x, rr, r, alt)
56 elseif (! (nargin == 5))
61 error ("t_test_regression: R must be a scalar");
62 elseif (! ischar (alt))
63 error ("t_test_regression: ALT must be a string");
67 if (! (isvector (y) && (length (y) == T)))
68 error ("t_test_regression: Y must be a vector of length rows (X)");
71 if (! ((max (s) == k) && (min (s) == 1)))
72 error ("t_test_regression: RR must be a vector of length columns (X)");
75 rr = reshape (rr, 1, k);
76 y = reshape (y, T, 1);
79 t = (rr * b - r) / sqrt (v * rr * inv (x' * x) * rr');
82 if (strcmp (alt, "!=") || strcmp (alt, "<>"))
83 pval = 2 * min (cdf, 1 - cdf);
84 elseif strcmp (alt, ">")
86 elseif strcmp (alt, "<")
89 error ("t_test_regression: the value `%s' for alt is not possible", alt);
93 printf ("pval: %g\n", pval);