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