output: Add auxiliary data parameter to tab_dim.
[pspp-builds.git] / src / language / stats / wilcoxon.c
1 /* Pspp - a program for statistical analysis.
2    Copyright (C) 2008, 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
18
19 #include <config.h>
20 #include "wilcoxon.h"
21 #include <data/variable.h>
22 #include <data/casereader.h>
23 #include <data/casewriter.h>
24 #include <data/subcase.h>
25 #include <math/sort.h>
26 #include <libpspp/message.h>
27 #include <xalloc.h>
28 #include <output/table.h>
29 #include <data/procedure.h>
30 #include <data/dictionary.h>
31 #include <math/wilcoxon-sig.h>
32 #include <gsl/gsl_cdf.h>
33 #include <unistd.h>
34 #include <signal.h>
35 #include <libpspp/assertion.h>
36 #include <data/format.h>
37
38 static double
39 append_difference (const struct ccase *c, casenumber n UNUSED, void *aux)
40 {
41   const variable_pair *vp = aux;
42
43   return case_data (c, (*vp)[0])->f - case_data (c, (*vp)[1])->f;
44 }
45
46 static void show_ranks_box (const struct wilcoxon_state *,
47                             const struct two_sample_test *,
48                             const struct dictionary *);
49
50 static void show_tests_box (const struct wilcoxon_state *,
51                             const struct two_sample_test *,
52                             bool exact, double timer);
53
54
55
56 static void
57 distinct_callback (double v UNUSED, casenumber n, double w UNUSED, void *aux)
58 {
59   struct wilcoxon_state *ws = aux;
60
61   ws->tiebreaker += pow3 (n) - n;
62 }
63
64 #define WEIGHT_IDX 2
65
66 void
67 wilcoxon_execute (const struct dataset *ds,
68                   struct casereader *input,
69                   enum mv_class exclude,
70                   const struct npar_test *test,
71                   bool exact,
72                   double timer)
73 {
74   int i;
75   bool warn = true;
76   const struct dictionary *dict = dataset_dict (ds);
77   const struct two_sample_test *t2s = (struct two_sample_test *) test;
78
79   struct wilcoxon_state *ws = xcalloc (sizeof (*ws), t2s->n_pairs);
80   const struct variable *weight = dict_get_weight (dict);
81   struct variable *weightx = var_create_internal (WEIGHT_IDX);
82
83   input =
84     casereader_create_filter_weight (input, dict, &warn, NULL);
85
86   for (i = 0 ; i < t2s->n_pairs; ++i )
87     {
88       struct casereader *r = casereader_clone (input);
89       struct casewriter *writer;
90       struct ccase *c;
91       struct subcase ordering;
92       variable_pair *vp = &t2s->pairs[i];
93
94       const int reader_width = weight ? 3 : 2;
95
96       ws[i].sign = var_create_internal (0);
97       ws[i].absdiff = var_create_internal (1);
98
99       r = casereader_create_filter_missing (r, *vp, 2,
100                                             exclude,
101                                             NULL, NULL);
102
103       subcase_init_var (&ordering, ws[i].absdiff, SC_ASCEND);
104       writer = sort_create_writer (&ordering, reader_width);
105       subcase_destroy (&ordering);
106
107       for (; (c = casereader_read (r)) != NULL; case_unref (c))
108         {
109           struct ccase *output = case_create (reader_width);
110           double d = append_difference (c, 0, vp);
111
112           if (d > 0)
113             {
114               case_data_rw (output, ws[i].sign)->f = 1.0;
115
116             }
117           else if (d < 0)
118             {
119               case_data_rw (output, ws[i].sign)->f = -1.0;
120             }
121           else
122             {
123               double w = 1.0;
124               if (weight)
125                 w = case_data (c, weight)->f;
126
127               /* Central point values should be dropped */
128               ws[i].n_zeros += w;
129               case_unref (output);
130               continue;
131             }
132
133           case_data_rw (output, ws[i].absdiff)->f = fabs (d);
134
135           if (weight)
136            case_data_rw (output, weightx)->f = case_data (c, weight)->f;
137
138           casewriter_write (writer, output);
139         }
140       casereader_destroy (r);
141       ws[i].reader = casewriter_make_reader (writer);
142     }
143
144   for (i = 0 ; i < t2s->n_pairs; ++i )
145     {
146       struct casereader *rr ;
147       struct ccase *c;
148       enum rank_error err = 0;
149
150       rr = casereader_create_append_rank (ws[i].reader, ws[i].absdiff,
151                                           weight ? weightx : NULL, &err,
152                                           distinct_callback, &ws[i]
153                                           );
154
155       for (; (c = casereader_read (rr)) != NULL; case_unref (c))
156         {
157           double sign = case_data (c, ws[i].sign)->f;
158           double rank = case_data_idx (c, weight ? 3 : 2)->f;
159           double w = 1.0;
160           if (weight)
161             w = case_data (c, weightx)->f;
162
163           if ( sign > 0 )
164             {
165               ws[i].positives.sum += rank * w;
166               ws[i].positives.n += w;
167             }
168           else if (sign < 0)
169             {
170               ws[i].negatives.sum += rank * w;
171               ws[i].negatives.n += w;
172             }
173           else
174             NOT_REACHED ();
175         }
176
177       casereader_destroy (rr);
178     }
179
180   casereader_destroy (input);
181
182   var_destroy (weightx);
183
184   show_ranks_box (ws, t2s, dict);
185   show_tests_box (ws, t2s, exact, timer);
186
187   for (i = 0 ; i < t2s->n_pairs; ++i )
188     {
189       var_destroy (ws[i].sign);
190       var_destroy (ws[i].absdiff);
191     }
192
193   free (ws);
194 }
195
196
197 \f
198
199 #include "gettext.h"
200 #define _(msgid) gettext (msgid)
201
202 static void
203 show_ranks_box (const struct wilcoxon_state *ws,
204                 const struct two_sample_test *t2s,
205                 const struct dictionary *dict)
206 {
207   size_t i;
208
209   const struct variable *wv = dict_get_weight (dict);
210   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
211
212   struct tab_table *table = tab_create (5, 1 + 4 * t2s->n_pairs, 0);
213
214   tab_dim (table, tab_natural_dimensions, NULL);
215
216   tab_title (table, _("Ranks"));
217
218   tab_headers (table, 2, 0, 1, 0);
219
220   /* Vertical lines inside the box */
221   tab_box (table, 0, 0, -1, TAL_1,
222            1, 0, table->nc - 1, tab_nr (table) - 1 );
223
224   /* Box around entire table */
225   tab_box (table, TAL_2, TAL_2, -1, -1,
226            0, 0, table->nc - 1, tab_nr (table) - 1 );
227
228
229   tab_text (table,  2, 0,  TAB_CENTER, _("N"));
230   tab_text (table,  3, 0,  TAB_CENTER, _("Mean Rank"));
231   tab_text (table,  4, 0,  TAB_CENTER, _("Sum of Ranks"));
232
233
234   for (i = 0 ; i < t2s->n_pairs; ++i)
235     {
236       variable_pair *vp = &t2s->pairs[i];
237
238       struct string pair_name;
239       ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
240       ds_put_cstr (&pair_name, " - ");
241       ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
242
243       tab_text (table, 1, 1 + i * 4, TAB_LEFT, _("Negative Ranks"));
244       tab_text (table, 1, 2 + i * 4, TAB_LEFT, _("Positive Ranks"));
245       tab_text (table, 1, 3 + i * 4, TAB_LEFT, _("Ties"));
246       tab_text (table, 1, 4 + i * 4, TAB_LEFT, _("Total"));
247
248       tab_hline (table, TAL_1, 0, table->nc - 1, 1 + i * 4);
249
250
251       tab_text (table, 0, 1 + i * 4, TAB_LEFT, ds_cstr (&pair_name));
252       ds_destroy (&pair_name);
253
254
255       /* N */
256       tab_double (table, 2, 1 + i * 4, TAB_RIGHT, ws[i].negatives.n, wfmt);
257       tab_double (table, 2, 2 + i * 4, TAB_RIGHT, ws[i].positives.n, wfmt);
258       tab_double (table, 2, 3 + i * 4, TAB_RIGHT, ws[i].n_zeros, wfmt);
259
260       tab_double (table, 2, 4 + i * 4, TAB_RIGHT,
261                  ws[i].n_zeros + ws[i].positives.n + ws[i].negatives.n, wfmt);
262
263       /* Sums */
264       tab_double (table, 4, 1 + i * 4, TAB_RIGHT, ws[i].negatives.sum, NULL);
265       tab_double (table, 4, 2 + i * 4, TAB_RIGHT, ws[i].positives.sum, NULL);
266
267
268       /* Means */
269       tab_double (table, 3, 1 + i * 4, TAB_RIGHT,
270                  ws[i].negatives.sum / (double) ws[i].negatives.n, NULL);
271
272       tab_double (table, 3, 2 + i * 4, TAB_RIGHT,
273                  ws[i].positives.sum / (double) ws[i].positives.n, NULL);
274
275     }
276
277   tab_hline (table, TAL_2, 0, table->nc - 1, 1);
278   tab_vline (table, TAL_2, 2, 0, table->nr - 1);
279
280
281   tab_submit (table);
282 }
283
284
285 static void
286 show_tests_box (const struct wilcoxon_state *ws,
287                 const struct two_sample_test *t2s,
288                 bool exact,
289                 double timer UNUSED
290                 )
291 {
292   size_t i;
293   struct tab_table *table = tab_create (1 + t2s->n_pairs, exact ? 5 : 3, 0);
294
295   tab_dim (table, tab_natural_dimensions, NULL);
296
297   tab_title (table, _("Test Statistics"));
298
299   tab_headers (table, 1, 0, 1, 0);
300
301   /* Vertical lines inside the box */
302   tab_box (table, 0, 0, -1, TAL_1,
303            0, 0, table->nc - 1, tab_nr (table) - 1 );
304
305   /* Box around entire table */
306   tab_box (table, TAL_2, TAL_2, -1, -1,
307            0, 0, table->nc - 1, tab_nr (table) - 1 );
308
309
310   tab_text (table,  0, 1,  TAB_LEFT, _("Z"));
311   tab_text (table,  0, 2,  TAB_LEFT, _("Asymp. Sig (2-tailed)"));
312
313   if ( exact )
314     {
315       tab_text (table,  0, 3,  TAB_LEFT, _("Exact Sig (2-tailed)"));
316       tab_text (table,  0, 4,  TAB_LEFT, _("Exact Sig (1-tailed)"));
317
318 #if 0
319       tab_text (table,  0, 5,  TAB_LEFT, _("Point Probability"));
320 #endif
321     }
322
323   for (i = 0 ; i < t2s->n_pairs; ++i)
324     {
325       double z;
326       double n = ws[i].positives.n + ws[i].negatives.n;
327       variable_pair *vp = &t2s->pairs[i];
328
329       struct string pair_name;
330       ds_init_cstr (&pair_name, var_to_string ((*vp)[0]));
331       ds_put_cstr (&pair_name, " - ");
332       ds_put_cstr (&pair_name, var_to_string ((*vp)[1]));
333
334
335       tab_text (table, 1 + i, 0, TAB_CENTER, ds_cstr (&pair_name));
336       ds_destroy (&pair_name);
337
338       z = MIN (ws[i].positives.sum, ws[i].negatives.sum);
339       z -= n * (n + 1)/ 4.0;
340
341       z /= sqrt (n * (n + 1) * (2*n + 1)/24.0 - ws[i].tiebreaker / 48.0);
342
343       tab_double (table, 1 + i, 1, TAB_RIGHT, z, NULL);
344
345       tab_double (table, 1 + i, 2, TAB_RIGHT,
346                  2.0 * gsl_cdf_ugaussian_P (z),
347                  NULL);
348
349       if (exact)
350         {
351           double p = LevelOfSignificanceWXMPSR (ws[i].positives.sum, n);
352           if (p < 0)
353             {
354               msg (MW, ("Too many pairs to calculate exact significance."));
355             }
356           else
357             {
358               tab_double (table, 1 + i, 3, TAB_RIGHT, p, NULL);
359               tab_double (table, 1 + i, 4, TAB_RIGHT, p / 2.0, NULL);
360             }
361         }
362     }
363
364   tab_hline (table, TAL_2, 0, table->nc - 1, 1);
365   tab_vline (table, TAL_2, 1, 0, table->nr - 1);
366
367
368   tab_submit (table);
369 }