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

  1. ## Copyright (C) 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, z] = prop_test_2 (x1, n1, x2, n2 [, alt])
  18. ##
  19. ## If x1 and n1 are the counts of successes and trials in one sample,
  20. ## and x2 and n2 those in a second one, test the null hypothesis that
  21. ## the success probabilities p1 and p2 are the same.
  22. ## Under the null, the test statistic z approximately follows a
  23. ## standard normal distribution.
  24. ##
  25. ## With the optional argument string alt, the alternative of interest
  26. ## can be selected.  
  27. ## If alt is "!=" or "<>", the null is tested against the two-sided
  28. ## alternative p1 != p2.
  29. ## If alt is ">", the one-sided alternative p1 > p2 is used, similarly
  30. ## for "<".
  31. ## The default is the two-sided case.
  32. ##
  33. ## pval is the p-value of the test.
  34. ##  
  35. ## If no output argument is given, the p-value of the test is displayed.
  36.   
  37. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  38. ## Description:  Compare two proportions
  39.  
  40. function [pval, z] = prop_test_2 (x1, n1, x2, n2, alt)
  41.   
  42.   if ((nargin < 4) || (nargin > 5))
  43.     usage ("[pval, z] = prop_test_2 (x1, n1, x2, n2 [, alt])");
  44.   endif
  45.     
  46.   ## Could do sanity checking on x1, n1, x2, n2 here
  47.  
  48.   p1  = x1 / n1;
  49.   p2  = x2 / n2;
  50.   pc  = (x1 + x2) / (n1 + n2);
  51.   
  52.   z   = (p1 - p2) / sqrt (pc * (1 - pc) * (1/n1 + 1/n2));
  53.   
  54.   cdf = stdnormal_cdf (z);
  55.   
  56.   if (nargin == 4)
  57.     alt  = "!=";
  58.   endif
  59.  
  60.   if !isstr (alt)
  61.     error ("prop_test_2:  alt must be a string");
  62.   endif
  63.   if (strcmp (alt, "!=") || strcmp (alt, "<>"))
  64.     pval = 2 * min (cdf, 1 - cdf);
  65.   elseif strcmp (alt, ">")
  66.     pval = 1 - cdf;
  67.   elseif strcmp (alt, "<")
  68.     pval = cdf;
  69.   else
  70.     error (sprintf ("prop_test_2:  option %s not recognized", alt));
  71.   endif
  72.  
  73.   if (nargout == 0)
  74.     printf ("  pval:  %g\n", pval);
  75.   endif
  76.  
  77. endfunction
  78.