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 |
|
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 |
3426
|
26 ## can be selected. |
3200
|
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. |
3426
|
34 ## |
3200
|
35 ## If no output argument is given, the p-value of the test is displayed. |
3426
|
36 |
3200
|
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) |
3426
|
41 |
3200
|
42 if ((nargin < 4) || (nargin > 5)) |
3426
|
43 usage ("[pval, z] = prop_test_2 (x1, n1, x2, n2 [, alt])"); |
3200
|
44 endif |
3426
|
45 |
3200
|
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); |
3426
|
51 |
3200
|
52 z = (p1 - p2) / sqrt (pc * (1 - pc) * (1/n1 + 1/n2)); |
3426
|
53 |
3200
|
54 cdf = stdnormal_cdf (z); |
3426
|
55 |
3200
|
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 |