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