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