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