1## Copyright (C) 1995-2017 Kurt Hornik
2##
3## This program is free software: you can redistribute it and/or
4## modify it under the terms of the GNU General Public License as
5## published by the Free Software Foundation, either version 3 of the
6## License, or (at your option) 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 program; see the file COPYING.  If not, see
15## <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn {} {[@var{pval}, @var{t}, @var{df}] =} t_test_regression (@var{y}, @var{x}, @var{rr}, @var{r}, @var{alt})
19## Perform a t test for the null hypothesis
20## @nospell{@code{@var{rr} * @var{b} = @var{r}}} in a classical normal
21## regression model @code{@var{y} = @var{x} * @var{b} + @var{e}}.
22##
23## Under the null, the test statistic @var{t} follows a @var{t} distribution
24## with @var{df} degrees of freedom.
25##
26## If @var{r} is omitted, a value of 0 is assumed.
27##
28## With the optional argument string @var{alt}, the alternative of interest
29## can be selected.  If @var{alt} is @qcode{"!="} or @qcode{"<>"}, the null
30## is tested against the two-sided alternative @nospell{@code{@var{rr} *
31## @var{b} != @var{r}}}.  If @var{alt} is @qcode{">"}, the one-sided
32## alternative @nospell{@code{@var{rr} * @var{b} > @var{r}}} is used.
33## Similarly for @var{"<"}, the one-sided alternative @nospell{@code{@var{rr}
34## * @var{b} < @var{r}}} is used.  The default is the two-sided case.
35##
36## The p-value of the test is returned in @var{pval}.
37##
38## If no output argument is given, the p-value of the test is displayed.
39## @end deftypefn
40
41## Author: KH <Kurt.Hornik@wu-wien.ac.at>
42## Description: Test one linear hypothesis in linear regression model
43
44function [pval, t, df] = t_test_regression (y, x, rr, r, alt)
45
46  if (nargin == 3)
47    r   = 0;
48    alt = "!=";
49  elseif (nargin == 4)
50    if (ischar (r))
51      alt = r;
52      r   = 0;
53    else
54      alt = "!=";
55    endif
56  elseif (! (nargin == 5))
57    print_usage ();
58  endif
59
60  if (! isscalar (r))
61    error ("t_test_regression: R must be a scalar");
62  elseif (! ischar (alt))
63    error ("t_test_regression: ALT must be a string");
64  endif
65
66  [T, k] = size (x);
67  if (! (isvector (y) && (length (y) == T)))
68    error ("t_test_regression: Y must be a vector of length rows (X)");
69  endif
70  s = size (rr);
71  if (! ((max (s) == k) && (min (s) == 1)))
72    error ("t_test_regression: RR must be a vector of length columns (X)");
73  endif
74
75  rr     = reshape (rr, 1, k);
76  y      = reshape (y, T, 1);
77  [b, v] = ols (y, x);
78  df     = T - k;
79  t      = (rr * b - r) / sqrt (v * rr * inv (x' * x) * rr');
80  cdf    = tcdf (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: the value '%s' for ALT is not possible", alt);
90  endif
91
92  if (nargout == 0)
93    printf ("pval: %g\n", pval);
94  endif
95
96endfunction
97