b44c5a6f165ea38160430bfedb3012630169afc2
[pspp-builds.git] / src / language / stats / binomial.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <libpspp/compiler.h>
19 #include <output/table.h>
20
21 #include <data/format.h>
22 #include <data/case.h>
23 #include <data/casereader.h>
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <data/value.h>
28 #include <data/value-labels.h>
29
30 #include <libpspp/message.h>
31 #include <libpspp/assertion.h>
32
33 #include "binomial.h"
34 #include "freq.h"
35
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 #include <libpspp/misc.h>
42
43 #include <gsl/gsl_cdf.h>
44 #include <gsl/gsl_randist.h>
45
46 #include <minmax.h>
47
48 #include <libpspp/hash.h>
49
50 static double calculate_binomial_internal (double n1, double n2,
51                                            double p);
52
53
54 static void
55 swap (double *i1, double *i2)
56 {
57   double temp = *i1;
58   *i1 = *i2;
59   *i2 = temp;
60 }
61
62 static double
63 calculate_binomial (double n1, double n2, double p)
64 {
65   const double n = n1 + n2;
66   const bool test_reversed = (n1 / n > p ) ;
67   if ( test_reversed )
68     {
69       p = 1 - p ;
70       swap (&n1, &n2);
71     }
72
73   return calculate_binomial_internal (n1, n2, p);
74 }
75
76 static double
77 calculate_binomial_internal (double n1, double n2, double p)
78 {
79   /* SPSS Statistical Algorithms has completely different and WRONG
80      advice here. */
81
82   double sig1tailed = gsl_cdf_binomial_P (n1, p, n1 + n2);
83
84   if ( p == 0.5 )
85     return sig1tailed > 0.5 ? 1.0 :sig1tailed * 2.0;
86
87   return sig1tailed ;
88 }
89
90 static bool
91 do_binomial (const struct dictionary *dict,
92              struct casereader *input,
93              const struct binomial_test *bst,
94              struct freq_mutable *cat1,
95              struct freq_mutable *cat2,
96              enum mv_class exclude
97              )
98 {
99   bool warn = true;
100
101   const struct one_sample_test *ost = (const struct one_sample_test *) bst;
102   struct ccase *c;
103
104   for (; (c = casereader_read (input)) != NULL; case_unref (c))
105     {
106       int v;
107       double w = dict_get_case_weight (dict, c, &warn);
108
109       for (v = 0 ; v < ost->n_vars ; ++v )
110         {
111           const struct variable *var = ost->vars[v];
112           double value = case_num (c, var);
113
114           if (var_is_num_missing (var, value, exclude))
115             continue;
116
117           if (bst->cutpoint != SYSMIS)
118             {
119               if ( cat1[v].value.f >= value )
120                   cat1[v].count  += w;
121               else
122                   cat2[v].count += w;
123             }
124           else
125             {
126               if ( SYSMIS == cat1[v].value.f )
127                 {
128                   cat1[v].value.f = value;
129                   cat1[v].count = w;
130                 }
131               else if ( cat1[v].value.f == value )
132                 cat1[v].count += w;
133               else if ( SYSMIS == cat2[v].value.f )
134                 {
135                   cat2[v].value.f = value;
136                   cat2[v].count = w;
137                 }
138               else if ( cat2[v].value.f == value )
139                 cat2[v].count += w;
140               else if ( bst->category1 == SYSMIS)
141                 msg (ME, _("Variable %s is not dichotomous"), var_get_name (var));
142             }
143         }
144     }
145   return casereader_destroy (input);
146 }
147
148
149
150 void
151 binomial_execute (const struct dataset *ds,
152                   struct casereader *input,
153                   enum mv_class exclude,
154                   const struct npar_test *test,
155                   bool exact UNUSED,
156                   double timer UNUSED)
157 {
158   int v;
159   const struct dictionary *dict = dataset_dict (ds);
160   const struct binomial_test *bst = (const struct binomial_test *) test;
161   const struct one_sample_test *ost = (const struct one_sample_test*) test;
162
163   struct freq_mutable *cat[2];
164   int i;
165
166   assert ((bst->category1 == SYSMIS) == (bst->category2 == SYSMIS) || bst->cutpoint != SYSMIS);
167
168   for (i = 0; i < 2; i++)
169     {
170       double value;
171       if (i == 0)
172         value = bst->cutpoint != SYSMIS ? bst->cutpoint : bst->category1;
173       else
174         value = bst->category2;
175
176       cat[i] = xnmalloc (ost->n_vars, sizeof *cat[i]);
177       for (v = 0; v < ost->n_vars; v++)
178         {
179           cat[i][v].value.f = value;
180           cat[i][v].count = 0;
181         }
182     }
183
184   if (do_binomial (dataset_dict (ds), input, bst, cat[0], cat[1], exclude))
185     {
186       const struct variable *wvar = dict_get_weight (dict);
187       const struct fmt_spec *wfmt = wvar ?
188         var_get_print_format (wvar) : & F_8_0;
189
190       struct tab_table *table = tab_create (7, ost->n_vars * 3 + 1, 0);
191
192       tab_dim (table, tab_natural_dimensions, NULL);
193
194       tab_title (table, _("Binomial Test"));
195
196       tab_headers (table, 2, 0, 1, 0);
197
198       tab_box (table, TAL_1, TAL_1, -1, TAL_1,
199                0, 0, table->nc - 1, tab_nr(table) - 1 );
200
201       for (v = 0 ; v < ost->n_vars; ++v)
202         {
203           double n_total, sig;
204           struct string catstr[2];
205           const struct variable *var = ost->vars[v];
206
207           ds_init_empty (&catstr[0]);
208           ds_init_empty (&catstr[1]);
209
210           if ( bst->cutpoint != SYSMIS)
211             {
212               ds_put_format (&catstr[0], "<= %g", bst->cutpoint);
213             }
214           else
215             {
216               var_append_value_name (var, &cat[0][v].value, &catstr[0]);
217               var_append_value_name (var, &cat[1][v].value, &catstr[1]);
218             }
219
220           tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
221
222           /* Titles */
223           tab_text (table, 0, 1 + v * 3, TAB_LEFT, var_to_string (var));
224           tab_text (table, 1, 1 + v * 3, TAB_LEFT, _("Group1"));
225           tab_text (table, 1, 2 + v * 3, TAB_LEFT, _("Group2"));
226           tab_text (table, 1, 3 + v * 3, TAB_LEFT, _("Total"));
227
228           /* Test Prop */
229           tab_double (table, 5, 1 + v * 3, TAB_NONE, bst->p, NULL);
230
231           /* Category labels */
232           tab_text (table, 2, 1 + v * 3, TAB_NONE, ds_cstr (&catstr[0]));
233           tab_text (table, 2, 2 + v * 3, TAB_NONE, ds_cstr (&catstr[1]));
234
235           /* Observed N */
236           tab_double (table, 3, 1 + v * 3, TAB_NONE, cat[0][v].count, wfmt);
237           tab_double (table, 3, 2 + v * 3, TAB_NONE, cat[1][v].count, wfmt);
238
239           n_total = cat[0][v].count + cat[1][v].count;
240           tab_double (table, 3, 3 + v * 3, TAB_NONE, n_total, wfmt);
241
242           /* Observed Proportions */
243           tab_double (table, 4, 1 + v * 3, TAB_NONE,
244                      cat[0][v].count / n_total, NULL);
245           tab_double (table, 4, 2 + v * 3, TAB_NONE,
246                      cat[1][v].count / n_total, NULL);
247
248           tab_double (table, 4, 3 + v * 3, TAB_NONE,
249                      (cat[0][v].count + cat[1][v].count) / n_total, NULL);
250
251           /* Significance */
252           sig = calculate_binomial (cat[0][v].count, cat[1][v].count, bst->p);
253           tab_double (table, 6, 1 + v * 3, TAB_NONE, sig, NULL);
254
255           ds_destroy (&catstr[0]);
256           ds_destroy (&catstr[1]);
257         }
258
259       tab_text (table,  2, 0,  TAB_CENTER, _("Category"));
260       tab_text (table,  3, 0,  TAB_CENTER, _("N"));
261       tab_text (table,  4, 0,  TAB_CENTER, _("Observed Prop."));
262       tab_text (table,  5, 0,  TAB_CENTER, _("Test Prop."));
263
264       tab_text (table,  6, 0,  TAB_CENTER | TAT_PRINTF,
265                 _("Exact Sig. (%d-tailed)"),
266                 bst->p == 0.5 ? 2: 1);
267
268       tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);
269       tab_submit (table);
270     }
271
272   for (i = 0; i < 2; i++)
273     free (cat[i]);
274 }