7017
|
1 ## Copyright (C) 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007 |
|
2 ## Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3454
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{z}] =} prop_test_2 (@var{x1}, @var{n1}, @var{x2}, @var{n2}, @var{alt}) |
|
22 ## If @var{x1} and @var{n1} are the counts of successes and trials in |
|
23 ## one sample, and @var{x2} and @var{n2} those in a second one, test the |
|
24 ## null hypothesis that the success probabilities @var{p1} and @var{p2} |
|
25 ## are the same. Under the null, the test statistic @var{z} |
|
26 ## approximately follows a standard normal distribution. |
3200
|
27 ## |
3454
|
28 ## With the optional argument string @var{alt}, the alternative of |
|
29 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
30 ## @code{"<>"}, the null is tested against the two-sided alternative |
|
31 ## @var{p1} != @var{p2}. If @var{alt} is @code{">"}, the one-sided |
3457
|
32 ## alternative @var{p1} > @var{p2} is used. Similarly for @code{"<"}, |
|
33 ## the one-sided alternative @var{p1} < @var{p2} is used. |
3200
|
34 ## The default is the two-sided case. |
|
35 ## |
3454
|
36 ## The p-value of the test is returned in @var{pval}. |
3426
|
37 ## |
3200
|
38 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
39 ## @end deftypefn |
3426
|
40 |
5428
|
41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
42 ## Description: Compare two proportions |
3200
|
43 |
|
44 function [pval, z] = prop_test_2 (x1, n1, x2, n2, alt) |
3426
|
45 |
3200
|
46 if ((nargin < 4) || (nargin > 5)) |
6046
|
47 print_usage (); |
3200
|
48 endif |
3426
|
49 |
3200
|
50 ## Could do sanity checking on x1, n1, x2, n2 here |
|
51 |
|
52 p1 = x1 / n1; |
|
53 p2 = x2 / n2; |
|
54 pc = (x1 + x2) / (n1 + n2); |
3426
|
55 |
3200
|
56 z = (p1 - p2) / sqrt (pc * (1 - pc) * (1/n1 + 1/n2)); |
3426
|
57 |
3200
|
58 cdf = stdnormal_cdf (z); |
3426
|
59 |
3200
|
60 if (nargin == 4) |
|
61 alt = "!="; |
|
62 endif |
|
63 |
5443
|
64 if (! ischar (alt)) |
3456
|
65 error ("prop_test_2: alt must be a string"); |
3200
|
66 endif |
|
67 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
68 pval = 2 * min (cdf, 1 - cdf); |
|
69 elseif strcmp (alt, ">") |
|
70 pval = 1 - cdf; |
|
71 elseif strcmp (alt, "<") |
|
72 pval = cdf; |
|
73 else |
3456
|
74 error ("prop_test_2: option %s not recognized", alt); |
3200
|
75 endif |
|
76 |
|
77 if (nargout == 0) |
3456
|
78 printf (" pval: %g\n", pval); |
3200
|
79 endif |
|
80 |
|
81 endfunction |