xalloc.h-instead-of-alloc.h.patch from patch #6230.
[pspp-builds.git] / src / language / stats / binomial.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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 #include <gsl-extras/gsl-extras.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 = gslextras_cdf_binomial_P (n1, n1 + n2, p);
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 (casereader_read(input, &c))
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             break;
117
118           if ( NULL == cat1[v].value )
119             {
120               cat1[v].value = value_dup (value, width);
121               cat1[v].count = w;
122             }
123           else if ( 0 == compare_values (cat1[v].value, value, width))
124             cat1[v].count += w;
125           else if ( NULL == cat2[v].value )
126             {
127               cat2[v].value = value_dup (value, width);
128               cat2[v].count = w;
129             }
130           else if ( 0 == compare_values (cat2[v].value, value, width))
131             cat2[v].count += w;
132           else if ( bst->category1 == SYSMIS)
133             msg (ME, _("Variable %s is not dichotomous"), var_get_name (var));
134         }
135
136       case_destroy (&c);
137     }
138   return casereader_destroy (input);
139 }
140
141
142
143 void
144 binomial_execute (const struct dataset *ds,
145                   struct casereader *input,
146                   enum mv_class exclude,
147                   const struct npar_test *test)
148 {
149   int v;
150   const struct binomial_test *bst = (const struct binomial_test *) test;
151   const struct one_sample_test *ost = (const struct one_sample_test*) test;
152
153   struct freq_mutable *cat1 = xzalloc (sizeof (*cat1) * ost->n_vars);
154   struct freq_mutable *cat2 = xzalloc (sizeof (*cat1) * ost->n_vars);
155
156   assert ((bst->category1 == SYSMIS) == (bst->category2 == SYSMIS) );
157
158   if ( bst->category1 != SYSMIS )
159     {
160       union value v;
161       v.f = bst->category1;
162       cat1->value = value_dup (&v, 0);
163     }
164
165   if ( bst->category2 != SYSMIS )
166     {
167       union value v;
168       v.f = bst->category2;
169       cat2->value = value_dup (&v, 0);
170     }
171
172   if (do_binomial (dataset_dict(ds), input, bst, cat1, cat2, exclude))
173     {
174       struct tab_table *table = tab_create (7, ost->n_vars * 3 + 1, 0);
175
176       tab_dim (table, tab_natural_dimensions);
177
178       tab_title (table, _("Binomial Test"));
179
180       tab_headers (table, 2, 0, 1, 0);
181
182       tab_box (table, TAL_1, TAL_1, -1, TAL_1,
183                0, 0, table->nc - 1, tab_nr(table) - 1 );
184
185       for (v = 0 ; v < ost->n_vars; ++v)
186         {
187           double n_total, sig;
188           const struct variable *var = ost->vars[v];
189           tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
190
191           /* Titles */
192           tab_text (table, 0, 1 + v * 3, TAB_LEFT, var_to_string (var));
193           tab_text (table, 1, 1 + v * 3, TAB_LEFT, _("Group1"));
194           tab_text (table, 1, 2 + v * 3, TAB_LEFT, _("Group2"));
195           tab_text (table, 1, 3 + v * 3, TAB_LEFT, _("Total"));
196
197           /* Test Prop */
198           tab_float (table, 5, 1 + v * 3, TAB_NONE, bst->p, 8, 3);
199
200           /* Category labels */
201           tab_text (table, 2, 1 + v * 3, TAB_NONE,
202                     var_get_value_name (var, cat1[v].value));
203           tab_text (table, 2, 2 + v * 3, TAB_NONE,
204                     var_get_value_name (var, cat2[v].value));
205
206           /* Observed N */
207           tab_float (table, 3, 1 + v * 3, TAB_NONE, cat1[v].count, 8, 0);
208           tab_float (table, 3, 2 + v * 3, TAB_NONE, cat2[v].count, 8, 0);
209
210           n_total = cat1[v].count + cat2[v].count;
211           tab_float (table, 3, 3 + v * 3, TAB_NONE, n_total, 8, 0);
212
213           /* Observed Proportions */
214           tab_float (table, 4, 1 + v * 3, TAB_NONE,
215                      cat1[v].count / n_total, 8, 3);
216           tab_float (table, 4, 2 + v * 3, TAB_NONE,
217                      cat2[v].count / n_total, 8, 3);
218           tab_float (table, 4, 3 + v * 3, TAB_NONE,
219                      (cat1[v].count + cat2[v].count) / n_total, 8, 2);
220
221           /* Significance */
222           sig = calculate_binomial (cat1[v].count, cat2[v].count, bst->p);
223           tab_float (table, 6, 1 + v * 3, TAB_NONE, sig, 8, 3);
224         }
225
226       tab_text (table,  2, 0,  TAB_CENTER, _("Category"));
227       tab_text (table,  3, 0,  TAB_CENTER, _("N"));
228       tab_text (table,  4, 0,  TAB_CENTER, _("Observed Prop."));
229       tab_text (table,  5, 0,  TAB_CENTER, _("Test Prop."));
230
231       tab_text (table,  6, 0,  TAB_CENTER | TAT_PRINTF,
232                 _("Exact Sig. (%d-tailed)"),
233                 bst->p == 0.5 ? 2: 1);
234
235       tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);
236       tab_submit (table);
237     }
238
239   for (v = 0; v < ost->n_vars; v++)
240     {
241       free (cat1[v].value);
242       free (cat2[v].value);
243     }
244   free (cat1);
245   free (cat2);
246 }