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