Implemented the /RUNS subcommand for NPAR TESTS.
[pspp] / src / language / stats / npar.c
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009, 2010 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/npar.h>
20 #include "npar-summary.h"
21
22 #include <stdlib.h>
23 #include <math.h>
24
25 #include "xalloc.h"
26
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/settings.h>
33 #include <data/variable.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/cast.h>
36 #include <libpspp/hmapx.h>
37 #include <libpspp/hash-functions.h>
38 #include <libpspp/message.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
41 #include <libpspp/taint.h>
42 #include <language/command.h>
43 #include <language/lexer/lexer.h>
44 #include <language/lexer/variable-parser.h>
45 #include <language/lexer/value-parser.h>
46 #include <language/stats/binomial.h>
47 #include <language/stats/chisquare.h>
48 #include <language/stats/runs.h>
49 #include <language/stats/kruskal-wallis.h>
50 #include <language/stats/wilcoxon.h>
51 #include <language/stats/sign.h>
52 #include <math/moments.h>
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56
57 /* Settings for subcommand specifiers. */
58 enum missing_type
59   {
60     MISS_ANALYSIS,
61     MISS_LISTWISE,
62   };
63
64 /* Array indices for STATISTICS subcommand. */
65 enum
66   {
67     NPAR_ST_DESCRIPTIVES = 0,
68     NPAR_ST_QUARTILES = 1,
69     NPAR_ST_ALL = 2,
70     NPAR_ST_count
71   };
72
73 /* NPAR TESTS structure. */
74 struct cmd_npar_tests
75   {
76     /* Count variables indicating how many
77        of the subcommands have been given. */
78     int chisquare;
79     int binomial;
80     int wilcoxon;
81     int sign;
82     int runs;
83     int kruskal_wallis;
84     int missing;
85     int method;
86     int statistics;
87
88     /* How missing values should be treated */
89     long miss;
90
91     /* Which statistics have been requested */
92     int a_statistics[NPAR_ST_count];
93   };
94
95
96 struct npar_specs
97 {
98   struct pool *pool;
99   struct npar_test **test;
100   size_t n_tests;
101
102   const struct variable **vv; /* Compendium of all variables
103                                   (those mentioned on ANY subcommand */
104   int n_vars; /* Number of variables in vv */
105
106   enum mv_class filter;    /* Missing values to filter. */
107
108   bool descriptives;       /* Descriptive statistics should be calculated */
109   bool quartiles;          /* Quartiles should be calculated */
110
111   bool exact;  /* Whether exact calculations have been requested */
112   double timer;   /* Maximum time (in minutes) to wait for exact calculations */
113 };
114
115
116 /* Prototype for custom subcommands of NPAR TESTS. */
117 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
118 static int npar_binomial (struct lexer *, struct dataset *,  struct npar_specs *);
119 static int npar_runs (struct lexer *lexer, struct dataset *, struct npar_specs *);
120
121 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
122 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
123 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
124 static int npar_method (struct lexer *, struct npar_specs *);
125
126 /* Command parsing functions. */
127 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
128                              struct npar_specs *npar_specs );
129
130 static int
131 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
132                   struct npar_specs *nps)
133 {
134   npt->chisquare = 0;
135   npt->binomial = 0;
136   npt->wilcoxon = 0;
137   npt->runs = 0;
138   npt->sign = 0;
139   npt->missing = 0;
140   npt->miss = MISS_ANALYSIS;
141   npt->method = 0;
142   npt->statistics = 0;
143   memset (npt->a_statistics, 0, sizeof npt->a_statistics);
144   for (;;)
145     {
146       if (lex_match_hyphenated_word (lexer, "RUNS"))
147         {
148           npt->runs++;
149           switch (npar_runs (lexer, ds, nps))
150             {
151             case 0:
152               goto lossage;
153             case 1:
154               break;
155             case 2:
156               lex_error (lexer, NULL);
157               goto lossage;
158             default:
159               NOT_REACHED ();
160             }
161
162         }
163       else if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
164         {
165           lex_match (lexer, '=');
166           npt->chisquare++;
167           switch (npar_chisquare (lexer, ds, nps))
168             {
169             case 0:
170               goto lossage;
171             case 1:
172               break;
173             case 2:
174               lex_error (lexer, NULL);
175               goto lossage;
176             default:
177               NOT_REACHED ();
178             }
179         }
180       else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
181         {
182           lex_match (lexer, '=');
183           npt->binomial++;
184           switch (npar_binomial (lexer, ds, nps))
185             {
186             case 0:
187               goto lossage;
188             case 1:
189               break;
190             case 2:
191               lex_error (lexer, NULL);
192               goto lossage;
193             default:
194               NOT_REACHED ();
195             }
196         }
197       else if (lex_match_hyphenated_word (lexer, "K-W") ||
198                lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
199         {
200           lex_match (lexer, '=');
201           npt->kruskal_wallis++;
202           switch (npar_kruskal_wallis (lexer, ds, nps))
203             {
204             case 0:
205               goto lossage;
206             case 1:
207               break;
208             case 2:
209               lex_error (lexer, NULL);
210               goto lossage;
211             default:
212               NOT_REACHED ();
213             }
214         }
215       else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
216         {
217           lex_match (lexer, '=');
218           npt->wilcoxon++;
219           switch (npar_wilcoxon (lexer, ds, nps))
220             {
221             case 0:
222               goto lossage;
223             case 1:
224               break;
225             case 2:
226               lex_error (lexer, NULL);
227               goto lossage;
228             default:
229               NOT_REACHED ();
230             }
231         }
232       else if (lex_match_hyphenated_word (lexer, "SIGN"))
233         {
234           lex_match (lexer, '=');
235           npt->sign++;
236           switch (npar_sign (lexer, ds, nps))
237             {
238             case 0:
239               goto lossage;
240             case 1:
241               break;
242             case 2:
243               lex_error (lexer, NULL);
244               goto lossage;
245             default:
246               NOT_REACHED ();
247             }
248         }
249       else if (lex_match_hyphenated_word (lexer, "MISSING"))
250         {
251           lex_match (lexer, '=');
252           npt->missing++;
253           if (npt->missing > 1)
254             {
255               msg (SE, _("The %s subcommand may be given only once."), "MISSING");
256               goto lossage;
257             }
258           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
259             {
260               if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
261                 npt->miss = MISS_ANALYSIS;
262               else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
263                 npt->miss = MISS_LISTWISE;
264               else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
265                 nps->filter = MV_SYSTEM;
266               else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
267                 nps->filter = MV_ANY;
268               else
269                 {
270                   lex_error (lexer, NULL);
271                   goto lossage;
272                 }
273               lex_match (lexer, ',');
274             }
275         }
276       else if (lex_match_hyphenated_word (lexer, "METHOD"))
277         {
278           lex_match (lexer, '=');
279           npt->method++;
280           if (npt->method > 1)
281             {
282               msg (SE, _("The %s subcommand may be given only once."), "METHOD");
283               goto lossage;
284             }
285           switch (npar_method (lexer, nps))
286             {
287             case 0:
288               goto lossage;
289             case 1:
290               break;
291             case 2:
292               lex_error (lexer, NULL);
293               goto lossage;
294             default:
295               NOT_REACHED ();
296             }
297         }
298       else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
299         {
300           lex_match (lexer, '=');
301           npt->statistics++;
302           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
303             {
304               if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
305                 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
306               else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
307                 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
308               else if (lex_match (lexer, T_ALL))
309                 npt->a_statistics[NPAR_ST_ALL] = 1;
310               else
311                 {
312                   lex_error (lexer, NULL);
313                   goto lossage;
314                 }
315               lex_match (lexer, ',');
316             }
317         }
318       else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
319         {
320           lex_match (lexer, '=');
321           if (lex_match_id (lexer, "COMPATIBLE"))
322             settings_set_cmd_algorithm (COMPATIBLE);
323           else if (lex_match_id (lexer, "ENHANCED"))
324             settings_set_cmd_algorithm (ENHANCED);
325           }
326         if (!lex_match (lexer, '/'))
327           break;
328       }
329
330     if (lex_token (lexer) != '.')
331       {
332         lex_error (lexer, _("expecting end of command"));
333         goto lossage;
334       }
335
336   return true;
337
338 lossage:
339   return false;
340 }
341
342
343 static void one_sample_insert_variables (const struct npar_test *test,
344                                          struct hmapx *);
345
346 static void two_sample_insert_variables (const struct npar_test *test,
347                                          struct hmapx *);
348
349 static void n_sample_insert_variables (const struct npar_test *test,
350                                        struct hmapx *);
351
352 static void
353 npar_execute (struct casereader *input,
354              const struct npar_specs *specs,
355              const struct dataset *ds)
356 {
357   int t;
358   struct descriptives *summary_descriptives = NULL;
359
360   for ( t = 0 ; t < specs->n_tests; ++t )
361     {
362       const struct npar_test *test = specs->test[t];
363       if ( NULL == test->execute )
364         {
365           msg (SW, _("NPAR subcommand not currently implemented."));
366           continue;
367         }
368       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
369     }
370
371   if ( specs->descriptives )
372     {
373       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
374                                        specs->n_vars);
375
376       npar_summary_calc_descriptives (summary_descriptives,
377                                       casereader_clone (input),
378                                       dataset_dict (ds),
379                                       specs->vv, specs->n_vars,
380                                       specs->filter);
381     }
382
383   if ( (specs->descriptives || specs->quartiles)
384        && !taint_has_tainted_successor (casereader_get_taint (input)) )
385     do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
386
387   free (summary_descriptives);
388   casereader_destroy (input);
389 }
390
391 int
392 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
393 {
394   struct cmd_npar_tests cmd;
395   bool ok;
396   int i;
397   struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
398   struct casegrouper *grouper;
399   struct casereader *input, *group;
400   struct hmapx var_map = HMAPX_INITIALIZER (var_map);
401
402
403   npar_specs.pool = pool_create ();
404   npar_specs.filter = MV_ANY;
405   npar_specs.n_vars = -1;
406   npar_specs.vv = NULL;
407
408   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
409     {
410       pool_destroy (npar_specs.pool);
411       return CMD_FAILURE;
412     }
413
414   for (i = 0; i < npar_specs.n_tests; ++i )
415     {
416       const struct npar_test *test = npar_specs.test[i];
417       test->insert_variables (test, &var_map);
418     }
419
420   {
421     struct hmapx_node *node;
422     struct variable *var;
423     npar_specs.n_vars = 0;
424
425     HMAPX_FOR_EACH (var, node, &var_map)
426       {
427         npar_specs.n_vars ++;
428         npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
429         npar_specs.vv[npar_specs.n_vars - 1] = var;
430       }
431   }
432
433   qsort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv), 
434          compare_var_ptrs_by_name);
435
436   if ( cmd.statistics )
437     {
438       int i;
439
440       for ( i = 0 ; i < NPAR_ST_count; ++i )
441         {
442           if ( cmd.a_statistics[i] )
443             {
444               switch ( i )
445                 {
446                 case NPAR_ST_DESCRIPTIVES:
447                   npar_specs.descriptives = true;
448                   break;
449                 case NPAR_ST_QUARTILES:
450                   npar_specs.quartiles = true;
451                   break;
452                 case NPAR_ST_ALL:
453                   npar_specs.quartiles = true;
454                   npar_specs.descriptives = true;
455                   break;
456                 default:
457                   NOT_REACHED ();
458                 };
459             }
460         }
461     }
462
463   input = proc_open (ds);
464   if ( cmd.miss == MISS_LISTWISE )
465     {
466       input = casereader_create_filter_missing (input,
467                                                 npar_specs.vv,
468                                                 npar_specs.n_vars,
469                                                 npar_specs.filter,
470                                                 NULL, NULL);
471     }
472
473
474   grouper = casegrouper_create_splits (input, dataset_dict (ds));
475   while (casegrouper_get_next_group (grouper, &group))
476     npar_execute (group, &npar_specs, ds);
477   ok = casegrouper_destroy (grouper);
478   ok = proc_commit (ds) && ok;
479
480   pool_destroy (npar_specs.pool);
481   hmapx_destroy (&var_map);
482
483   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
484 }
485
486 static int
487 npar_runs (struct lexer *lexer, struct dataset *ds,
488            struct npar_specs *specs)
489 {
490   struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
491   struct one_sample_test *tp = &rt->parent;
492   struct npar_test *nt = &tp->parent;
493
494   nt->execute = runs_execute;
495   nt->insert_variables = one_sample_insert_variables;
496
497   if ( lex_force_match (lexer, '(') )
498     {
499       if ( lex_match_id (lexer, "MEAN"))
500         {
501           rt->cp_mode = CP_MEAN;
502         }
503       else if (lex_match_id (lexer, "MEDIAN"))
504         {
505           rt->cp_mode = CP_MEDIAN;
506         }
507       else if (lex_match_id (lexer, "MODE"))
508         {
509           rt->cp_mode = CP_MODE;
510         }
511       else if (lex_is_number (lexer))
512         {
513           rt->cutpoint = lex_number (lexer);
514           rt->cp_mode = CP_CUSTOM;
515           lex_get (lexer);
516         }
517       else
518         {
519           lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
520           return 0;
521         }
522                   
523       lex_force_match (lexer, ')');
524       lex_force_match (lexer, '=');
525       if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
526                                   &tp->vars, &tp->n_vars,
527                                   PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
528         {
529           return 2;
530         }
531     }
532
533   specs->n_tests++;
534   specs->test = pool_realloc (specs->pool,
535                               specs->test,
536                               sizeof (*specs->test) * specs->n_tests);
537
538   specs->test[specs->n_tests - 1] = nt;
539
540   return 1;
541 }
542
543
544 static int
545 npar_chisquare (struct lexer *lexer, struct dataset *ds,
546                 struct npar_specs *specs)
547 {
548   struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
549   struct one_sample_test *tp = &cstp->parent;
550   struct npar_test *nt = &tp->parent;
551
552
553   nt->execute = chisquare_execute;
554   nt->insert_variables = one_sample_insert_variables;
555
556   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
557                                    &tp->vars, &tp->n_vars,
558                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
559     {
560       return 2;
561     }
562
563   cstp->ranged = false;
564
565   if ( lex_match (lexer, '('))
566     {
567       cstp->ranged = true;
568       if ( ! lex_force_num (lexer)) return 0;
569       cstp->lo = lex_integer (lexer);
570       lex_get (lexer);
571       lex_force_match (lexer, ',');
572       if (! lex_force_num (lexer) ) return 0;
573       cstp->hi = lex_integer (lexer);
574       if ( cstp->lo >= cstp->hi )
575         {
576           msg (ME,
577               _("The specified value of HI (%d) is "
578                 "lower than the specified value of LO (%d)"),
579               cstp->hi, cstp->lo);
580           return 0;
581         }
582       lex_get (lexer);
583       if (! lex_force_match (lexer, ')')) return 0;
584     }
585
586   cstp->n_expected = 0;
587   cstp->expected = NULL;
588   if ( lex_match (lexer, '/') )
589     {
590       if ( lex_match_id (lexer, "EXPECTED") )
591         {
592           lex_force_match (lexer, '=');
593           if ( ! lex_match_id (lexer, "EQUAL") )
594             {
595               double f;
596               int n;
597               while ( lex_is_number (lexer) )
598                 {
599                   int i;
600                   n = 1;
601                   f = lex_number (lexer);
602                   lex_get (lexer);
603                   if ( lex_match (lexer, '*'))
604                     {
605                       n = f;
606                       f = lex_number (lexer);
607                       lex_get (lexer);
608                     }
609                   lex_match (lexer, ',');
610
611                   cstp->n_expected += n;
612                   cstp->expected = pool_realloc (specs->pool,
613                                                  cstp->expected,
614                                                  sizeof (double) *
615                                                  cstp->n_expected);
616                   for ( i = cstp->n_expected - n ;
617                         i < cstp->n_expected;
618                         ++i )
619                     cstp->expected[i] = f;
620
621                 }
622             }
623         }
624       else
625         lex_put_back (lexer, '/');
626     }
627
628   if ( cstp->ranged && cstp->n_expected > 0 &&
629        cstp->n_expected != cstp->hi - cstp->lo + 1 )
630     {
631       msg (ME,
632           _("%d expected values were given, but the specified "
633             "range (%d-%d) requires exactly %d values."),
634           cstp->n_expected, cstp->lo, cstp->hi,
635           cstp->hi - cstp->lo +1);
636       return 0;
637     }
638
639   specs->n_tests++;
640   specs->test = pool_realloc (specs->pool,
641                               specs->test,
642                               sizeof (*specs->test) * specs->n_tests);
643
644   specs->test[specs->n_tests - 1] = nt;
645
646   return 1;
647 }
648
649
650 static int
651 npar_binomial (struct lexer *lexer, struct dataset *ds,
652                struct npar_specs *specs)
653 {
654   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
655   struct one_sample_test *tp = &btp->parent;
656   struct npar_test *nt = &tp->parent;
657
658   nt->execute = binomial_execute;
659   nt->insert_variables = one_sample_insert_variables;
660
661   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
662
663   btp->p = 0.5;
664
665   if ( lex_match (lexer, '(') )
666     {
667       if ( lex_force_num (lexer) )
668         {
669           btp->p = lex_number (lexer);
670           lex_get (lexer);
671           lex_force_match (lexer, ')');
672         }
673       else
674         return 0;
675     }
676   else
677     /* Kludge: q2c swallows the '=' so put it back here  */
678      lex_put_back (lexer, '=');
679
680   if (lex_match (lexer, '=') )
681     {
682       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
683                                       &tp->vars, &tp->n_vars,
684                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
685         {
686           if (lex_match (lexer, '('))
687             {
688               lex_force_num (lexer);
689               btp->category1 = lex_number (lexer);
690               lex_get (lexer);
691               if ( lex_match (lexer, ','))
692                 {
693                   if ( ! lex_force_num (lexer) ) return 2;
694                   btp->category2 = lex_number (lexer);
695                   lex_get (lexer);
696                 }
697               else
698                 {
699                   btp->cutpoint = btp->category1;
700                 }
701
702               lex_force_match (lexer, ')');
703             }
704         }
705       else
706         return 2;
707
708     }
709
710   specs->n_tests++;
711   specs->test = pool_realloc (specs->pool,
712                               specs->test,
713                               sizeof (*specs->test) * specs->n_tests);
714
715   specs->test[specs->n_tests - 1] = nt;
716
717   return 1;
718 }
719
720
721 static bool
722 parse_two_sample_related_test (struct lexer *lexer,
723                                     const struct dictionary *dict,
724                                     struct two_sample_test *test_parameters,
725                                     struct pool *pool
726                                     );
727
728
729 static bool
730 parse_two_sample_related_test (struct lexer *lexer,
731                                const struct dictionary *dict,
732                                struct two_sample_test *test_parameters,
733                                struct pool *pool
734                                )
735 {
736   int n = 0;
737   bool paired = false;
738   bool with = false;
739   const struct variable **vlist1;
740   size_t n_vlist1;
741
742   const struct variable **vlist2;
743   size_t n_vlist2;
744
745   test_parameters->parent.insert_variables = two_sample_insert_variables;
746
747   if (!parse_variables_const_pool (lexer, pool,
748                                    dict,
749                                    &vlist1, &n_vlist1,
750                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
751     return false;
752
753   if ( lex_match (lexer, T_WITH))
754     {
755       with = true;
756       if ( !parse_variables_const_pool (lexer, pool, dict,
757                                         &vlist2, &n_vlist2,
758                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
759         return false;
760
761       paired = (lex_match (lexer, '(') &&
762                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
763     }
764
765
766   if ( with )
767     {
768       if (paired)
769         {
770           if ( n_vlist1 != n_vlist2)
771             msg (SE, _("PAIRED was specified but the number of variables "
772                        "preceding WITH (%zu) did not match the number "
773                        "following (%zu)."), n_vlist1, n_vlist2);
774
775           test_parameters->n_pairs = n_vlist1 ;
776         }
777       else
778         {
779           test_parameters->n_pairs = n_vlist1 * n_vlist2;
780         }
781     }
782   else
783     {
784       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
785     }
786
787   test_parameters->pairs =
788     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
789
790   if ( with )
791     {
792       if (paired)
793         {
794           int i;
795           assert (n_vlist1 == n_vlist2);
796           for ( i = 0 ; i < n_vlist1; ++i )
797             {
798               test_parameters->pairs[n][1] = vlist1[i];
799               test_parameters->pairs[n][0] = vlist2[i];
800               n++;
801             }
802         }
803       else
804         {
805           int i,j;
806           for ( i = 0 ; i < n_vlist1; ++i )
807             {
808               for ( j = 0 ; j < n_vlist2; ++j )
809                 {
810                   test_parameters->pairs[n][1] = vlist1[i];
811                   test_parameters->pairs[n][0] = vlist2[j];
812                   n++;
813                 }
814             }
815         }
816     }
817   else
818     {
819       int i,j;
820       for ( i = 0 ; i < n_vlist1 - 1; ++i )
821         {
822           for ( j = i + 1 ; j < n_vlist1; ++j )
823             {
824               assert ( n < test_parameters->n_pairs);
825               test_parameters->pairs[n][1] = vlist1[i];
826               test_parameters->pairs[n][0] = vlist1[j];
827               n++;
828             }
829         }
830     }
831
832   assert ( n == test_parameters->n_pairs);
833
834   return true;
835 }
836
837
838 static bool
839 parse_n_sample_related_test (struct lexer *lexer,
840                              const struct dictionary *dict,
841                              struct n_sample_test *nst,
842                              struct pool *pool
843                              )
844 {
845   if (!parse_variables_const_pool (lexer, pool,
846                                    dict,
847                                    &nst->vars, &nst->n_vars,
848                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
849     return false;
850
851   if ( ! lex_force_match (lexer, T_BY))
852     return false;
853
854   nst->indep_var = parse_variable_const (lexer, dict);
855
856   if ( ! lex_force_match (lexer, '('))
857     return false;
858
859   value_init (&nst->val1, var_get_width (nst->indep_var));
860   if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
861     {
862       value_destroy (&nst->val1, var_get_width (nst->indep_var));
863       return false;
864     }
865
866   if ( ! lex_force_match (lexer, ','))
867     return false;
868
869   value_init (&nst->val2, var_get_width (nst->indep_var));
870   if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
871     {
872       value_destroy (&nst->val2, var_get_width (nst->indep_var));
873       return false;
874     }
875
876   if ( ! lex_force_match (lexer, ')'))
877     return false;
878
879   return true;
880 }
881
882 static int
883 npar_wilcoxon (struct lexer *lexer,
884                struct dataset *ds,
885                struct npar_specs *specs )
886 {
887
888
889   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
890   struct npar_test *nt = &tp->parent;
891   nt->execute = wilcoxon_execute;
892
893   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
894                                       tp, specs->pool) )
895     return 0;
896
897   specs->n_tests++;
898   specs->test = pool_realloc (specs->pool,
899                               specs->test,
900                               sizeof (*specs->test) * specs->n_tests);
901   specs->test[specs->n_tests - 1] = nt;
902
903   return 1;
904 }
905
906 static int
907 npar_sign (struct lexer *lexer, struct dataset *ds,
908            struct npar_specs *specs)
909 {
910   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
911   struct npar_test *nt = &tp->parent;
912
913   nt->execute = sign_execute;
914
915   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
916                                       tp, specs->pool) )
917     return 0;
918
919   specs->n_tests++;
920   specs->test = pool_realloc (specs->pool,
921                               specs->test,
922                               sizeof (*specs->test) * specs->n_tests);
923   specs->test[specs->n_tests - 1] = nt;
924
925   return 1;
926 }
927
928 static int
929 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
930                       struct npar_specs *specs)
931 {
932   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
933   struct npar_test *nt = &tp->parent;
934
935   nt->insert_variables = n_sample_insert_variables;
936
937   nt->execute = kruskal_wallis_execute;
938
939   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
940                                       tp, specs->pool) )
941     return 0;
942
943   specs->n_tests++;
944   specs->test = pool_realloc (specs->pool,
945                               specs->test,
946                               sizeof (*specs->test) * specs->n_tests);
947   specs->test[specs->n_tests - 1] = nt;
948
949   return 1;
950 }
951
952 static void
953 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
954 {
955   size_t hash = hash_pointer (var, 0);
956   struct hmapx_node *node;
957   const struct variable *v = NULL;
958       
959   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
960     {
961       if ( v == var)
962         return ;
963     }
964
965   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
966 }
967
968 /* Insert the variables for TEST into VAR_MAP */
969 static void
970 one_sample_insert_variables (const struct npar_test *test,
971                              struct hmapx *var_map)
972 {
973   int i;
974   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
975
976   for ( i = 0 ; i < ost->n_vars ; ++i )
977     insert_variable_into_map (var_map, ost->vars[i]);
978 }
979
980
981 static void
982 two_sample_insert_variables (const struct npar_test *test,
983                              struct hmapx *var_map)
984 {
985   int i;
986   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
987
988   for ( i = 0 ; i < tst->n_pairs ; ++i )
989     {
990       variable_pair *pair = &tst->pairs[i];
991
992       insert_variable_into_map (var_map, (*pair)[0]);
993       insert_variable_into_map (var_map, (*pair)[1]);
994     }
995 }
996
997 static void 
998 n_sample_insert_variables (const struct npar_test *test,
999                            struct hmapx *var_map)
1000 {
1001   int i;
1002   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1003
1004   for ( i = 0 ; i < tst->n_vars ; ++i )
1005     insert_variable_into_map (var_map, tst->vars[i]);
1006
1007   insert_variable_into_map (var_map, tst->indep_var);
1008 }
1009
1010
1011 static int
1012 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1013 {
1014   if ( lex_match_id (lexer, "EXACT") )
1015     {
1016       specs->exact = true;
1017       specs->timer = 0.0;
1018       if (lex_match_id (lexer, "TIMER"))
1019         {
1020           specs->timer = 5.0;
1021
1022           if ( lex_match (lexer, '('))
1023             {
1024               if ( lex_force_num (lexer) )
1025                 {
1026                   specs->timer = lex_number (lexer);
1027                   lex_get (lexer);
1028                 }
1029               lex_force_match (lexer, ')');
1030             }
1031         }
1032     }
1033
1034   return 1;
1035 }