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