Change many %g format specifiers to %.*g with precision DBL_DIG + 1.
[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
188       tab_title (table, _("Binomial Test"));
189
190       tab_headers (table, 2, 0, 1, 0);
191
192       tab_box (table, TAL_1, TAL_1, -1, TAL_1,
193                0, 0, tab_nc (table) - 1, tab_nr(table) - 1 );
194
195       for (v = 0 ; v < ost->n_vars; ++v)
196         {
197           double n_total, sig;
198           struct string catstr[2];
199           const struct variable *var = ost->vars[v];
200
201           ds_init_empty (&catstr[0]);
202           ds_init_empty (&catstr[1]);
203
204           if ( bst->cutpoint != SYSMIS)
205             {
206               ds_put_format (&catstr[0], "<= %.*g",
207                              DBL_DIG + 1, bst->cutpoint);
208             }
209           else
210             {
211               var_append_value_name (var, &cat[0][v].value, &catstr[0]);
212               var_append_value_name (var, &cat[1][v].value, &catstr[1]);
213             }
214
215           tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
216
217           /* Titles */
218           tab_text (table, 0, 1 + v * 3, TAB_LEFT, var_to_string (var));
219           tab_text (table, 1, 1 + v * 3, TAB_LEFT, _("Group1"));
220           tab_text (table, 1, 2 + v * 3, TAB_LEFT, _("Group2"));
221           tab_text (table, 1, 3 + v * 3, TAB_LEFT, _("Total"));
222
223           /* Test Prop */
224           tab_double (table, 5, 1 + v * 3, TAB_NONE, bst->p, NULL);
225
226           /* Category labels */
227           tab_text (table, 2, 1 + v * 3, TAB_NONE, ds_cstr (&catstr[0]));
228           tab_text (table, 2, 2 + v * 3, TAB_NONE, ds_cstr (&catstr[1]));
229
230           /* Observed N */
231           tab_double (table, 3, 1 + v * 3, TAB_NONE, cat[0][v].count, wfmt);
232           tab_double (table, 3, 2 + v * 3, TAB_NONE, cat[1][v].count, wfmt);
233
234           n_total = cat[0][v].count + cat[1][v].count;
235           tab_double (table, 3, 3 + v * 3, TAB_NONE, n_total, wfmt);
236
237           /* Observed Proportions */
238           tab_double (table, 4, 1 + v * 3, TAB_NONE,
239                      cat[0][v].count / n_total, NULL);
240           tab_double (table, 4, 2 + v * 3, TAB_NONE,
241                      cat[1][v].count / n_total, NULL);
242
243           tab_double (table, 4, 3 + v * 3, TAB_NONE,
244                      (cat[0][v].count + cat[1][v].count) / n_total, NULL);
245
246           /* Significance */
247           sig = calculate_binomial (cat[0][v].count, cat[1][v].count, bst->p);
248           tab_double (table, 6, 1 + v * 3, TAB_NONE, sig, NULL);
249
250           ds_destroy (&catstr[0]);
251           ds_destroy (&catstr[1]);
252         }
253
254       tab_text (table,  2, 0,  TAB_CENTER, _("Category"));
255       tab_text (table,  3, 0,  TAB_CENTER, _("N"));
256       tab_text (table,  4, 0,  TAB_CENTER, _("Observed Prop."));
257       tab_text (table,  5, 0,  TAB_CENTER, _("Test Prop."));
258
259       tab_text_format (table,  6, 0,  TAB_CENTER,
260                        _("Exact Sig. (%d-tailed)"),
261                        bst->p == 0.5 ? 2 : 1);
262
263       tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);
264       tab_submit (table);
265     }
266
267   for (i = 0; i < 2; i++)
268     free (cat[i]);
269 }