home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / tests / f_test_regression.m < prev    next >
Text File  |  1997-02-26  |  2KB  |  77 lines

  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, f, df_num, df_den] = f_test_regression (y, X, R [, r])
  18. ##
  19. ## Performs an F test for the null hypothesis R * b = r in a classical
  20. ## normal regression model y = X * b + e.
  21. ##
  22. ## Under the null, the test statistic f follows an F distribution with
  23. ## df_num and df_den degrees of freedom;  pval is the p-value (1 minus
  24. ## the CDF of this distribution at f) of the test.
  25. ##
  26. ## If not given explicitly, r = 0.
  27. ##
  28. ## If no output argument is given, the p-value is displayed.
  29.  
  30. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  31. ## Description:  Test linear hypotheses in linear regression model
  32.  
  33. function [pval, f, df_num, df_den] = f_test_regression (y, X, R, r)
  34.   
  35.   if (nargin < 3 || nargin > 4)
  36.     usage (["[pval, f, df_num, df_den] ", ...
  37.         "= f_test_regression (y, X, R [, r])"]);
  38.   endif
  39.  
  40.   [T, k] = size (X);
  41.   if !( is_vector (y) && (length (y) == T) )
  42.     error (["f_test_regression:  ", ...
  43.         "y must be a vector of length rows (X)."]);
  44.   endif
  45.   y = reshape (y, T, 1);
  46.   
  47.   [q, c_R ] = size (R);
  48.   if (c_R != k)
  49.     error (["f_test_regression:  ", ...
  50.         "R must have as many columns as X."]);
  51.   endif
  52.   
  53.   if (nargin == 4)
  54.     s_r = size (r);
  55.     if ((min (s_r) != 1) || (max (s_r) != q))
  56.       error (["f_test_regression:  ", ...
  57.           "r must be a vector of length rows (R)."]); 
  58.     endif
  59.     r = reshape (r, q, 1);
  60.   else
  61.     r = zeros (q, 1);
  62.   endif
  63.  
  64.   df_num = q;
  65.   df_den = T - k;
  66.   
  67.   [b, v] = ols (y, X);
  68.   diff   = R * b - r;
  69.   f      = diff' * inv (R * inv (X' * X) * R') * diff / ( q * v );
  70.   pval  = 1 - f_cdf (f, df_num, df_den);
  71.   
  72.   if (nargout == 0)
  73.     printf ("  pval:  %g\n", pval);
  74.   endif
  75.  
  76. endfunction
  77.