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