Added an implementation of the Jonckheere-Terpstra test
[pspp] / src / language / stats / npar.c
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009, 2010, 2011 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
21 #include <stdlib.h>
22 #include <math.h>
23
24 #include "data/case.h"
25 #include "data/casegrouper.h"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/settings.h"
30 #include "data/variable.h"
31 #include "language/command.h"
32 #include "language/lexer/lexer.h"
33 #include "language/lexer/value-parser.h"
34 #include "language/lexer/variable-parser.h"
35 #include "language/stats/binomial.h"
36 #include "language/stats/chisquare.h"
37 #include "language/stats/ks-one-sample.h"
38 #include "language/stats/cochran.h"
39 #include "language/stats/friedman.h"
40 #include "language/stats/jonckheere-terpstra.h"
41 #include "language/stats/kruskal-wallis.h"
42 #include "language/stats/mann-whitney.h"
43 #include "language/stats/mcnemar.h"
44 #include "language/stats/median.h"
45 #include "language/stats/npar-summary.h"
46 #include "language/stats/runs.h"
47 #include "language/stats/sign.h"
48 #include "language/stats/wilcoxon.h"
49 #include "libpspp/array.h"
50 #include "libpspp/assertion.h"
51 #include "libpspp/cast.h"
52 #include "libpspp/hash-functions.h"
53 #include "libpspp/hmapx.h"
54 #include "libpspp/message.h"
55 #include "libpspp/pool.h"
56 #include "libpspp/str.h"
57 #include "libpspp/taint.h"
58 #include "math/moments.h"
59
60 #include "gl/xalloc.h"
61
62 #include "gettext.h"
63 #define _(msgid) gettext (msgid)
64
65 /* Settings for subcommand specifiers. */
66 enum missing_type
67   {
68     MISS_ANALYSIS,
69     MISS_LISTWISE,
70   };
71
72 /* Array indices for STATISTICS subcommand. */
73 enum
74   {
75     NPAR_ST_DESCRIPTIVES = 0,
76     NPAR_ST_QUARTILES = 1,
77     NPAR_ST_ALL = 2,
78     NPAR_ST_count
79   };
80
81 /* NPAR TESTS structure. */
82 struct cmd_npar_tests
83   {
84     /* Count variables indicating how many
85        of the subcommands have been given. */
86     int chisquare;
87     int cochran;
88     int binomial;
89     int ks_one_sample;
90     int wilcoxon;
91     int sign;
92     int runs;
93     int friedman;
94     int kendall;
95     int kruskal_wallis;
96     int mann_whitney;
97     int mcnemar;
98     int median;
99     int jonckheere_terpstra;
100     int missing;
101     int method;
102     int statistics;
103
104     /* How missing values should be treated */
105     long miss;
106
107     /* Which statistics have been requested */
108     int a_statistics[NPAR_ST_count];
109   };
110
111
112 struct npar_specs
113 {
114   struct pool *pool;
115   struct npar_test **test;
116   size_t n_tests;
117
118   const struct variable **vv; /* Compendium of all variables
119                                   (those mentioned on ANY subcommand */
120   int n_vars; /* Number of variables in vv */
121
122   enum mv_class filter;    /* Missing values to filter. */
123
124   bool descriptives;       /* Descriptive statistics should be calculated */
125   bool quartiles;          /* Quartiles should be calculated */
126
127   bool exact;  /* Whether exact calculations have been requested */
128   double timer;   /* Maximum time (in minutes) to wait for exact calculations */
129 };
130
131
132 /* Prototype for custom subcommands of NPAR TESTS. */
133 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
134 static int npar_binomial (struct lexer *, struct dataset *,  struct npar_specs *);
135 static int npar_ks_one_sample (struct lexer *, struct dataset *, struct npar_specs *);
136 static int npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
137 static int npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
138 static int npar_kendall (struct lexer *, struct dataset *, struct npar_specs *);
139 static int npar_cochran (struct lexer *, struct dataset *, struct npar_specs *);
140 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
141 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
142 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
143 static int npar_jonckheere_terpstra (struct lexer *, struct dataset *, struct npar_specs *);
144 static int npar_mann_whitney (struct lexer *, struct dataset *, struct npar_specs *);
145 static int npar_mcnemar (struct lexer *, struct dataset *, struct npar_specs *);
146 static int npar_median (struct lexer *, struct dataset *, struct npar_specs *);
147
148 static int npar_method (struct lexer *, struct npar_specs *);
149
150 /* Command parsing functions. */
151 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
152                              struct npar_specs *npar_specs );
153
154 static int
155 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
156                   struct npar_specs *nps)
157 {
158   npt->binomial = 0;
159   npt->chisquare = 0;
160   npt->ks_one_sample = 0;
161   npt->cochran = 0;
162   npt->friedman = 0;
163   npt->kruskal_wallis = 0;
164   npt->mann_whitney = 0;
165   npt->jonckheere_terpstra = 0;
166   npt->mcnemar = 0;
167   npt->runs = 0;
168   npt->sign = 0;
169   npt->wilcoxon = 0;
170   npt->missing = 0;
171   npt->miss = MISS_ANALYSIS;
172   npt->method = 0;
173   npt->statistics = 0;
174   memset (npt->a_statistics, 0, sizeof npt->a_statistics);
175   for (;;)
176     {
177       if (lex_match_id (lexer, "COCHRAN"))
178         {
179           npt->cochran++;
180           switch (npar_cochran (lexer, ds, nps))
181             {
182             case 0:
183               goto lossage;
184             case 1:
185               break;
186             case 2:
187               lex_error (lexer, NULL);
188               goto lossage;
189             default:
190               NOT_REACHED ();
191             }
192         }
193       else if (lex_match_id (lexer, "FRIEDMAN"))
194         {
195           npt->friedman++;
196           switch (npar_friedman (lexer, ds, nps))
197             {
198             case 0:
199               goto lossage;
200             case 1:
201               break;
202             case 2:
203               lex_error (lexer, NULL);
204               goto lossage;
205             default:
206               NOT_REACHED ();
207             }
208         }
209       else if (lex_match_id (lexer, "KENDALL"))
210         {
211           npt->kendall++;
212           switch (npar_kendall (lexer, ds, nps))
213             {
214             case 0:
215               goto lossage;
216             case 1:
217               break;
218             case 2:
219               lex_error (lexer, NULL);
220               goto lossage;
221             default:
222               NOT_REACHED ();
223             }
224         }
225       else if (lex_match_id (lexer, "RUNS"))
226         {
227           npt->runs++;
228           switch (npar_runs (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_id (lexer, "CHISQUARE"))
242         {
243           lex_match (lexer, T_EQUALS);
244           npt->chisquare++;
245           switch (npar_chisquare (lexer, ds, nps))
246             {
247             case 0:
248               goto lossage;
249             case 1:
250               break;
251             case 2:
252               lex_error (lexer, NULL);
253               goto lossage;
254             case 3:
255               continue;
256             default:
257               NOT_REACHED ();
258             }
259         }
260       else if (lex_match_id (lexer, "BINOMIAL"))
261         {
262           lex_match (lexer, T_EQUALS);
263           npt->binomial++;
264           switch (npar_binomial (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_phrase (lexer, "K-S") ||
278                lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
279         {
280           lex_match (lexer, T_EQUALS);
281           npt->ks_one_sample++;
282           switch (npar_ks_one_sample (lexer, ds, nps))
283             {
284             case 0:
285               goto lossage;
286             case 1:
287               break;
288             case 2:
289               lex_error (lexer, NULL);
290               goto lossage;
291             default:
292               NOT_REACHED ();
293             }
294         }
295       else if (lex_match_phrase (lexer, "J-T") ||
296                lex_match_phrase (lexer, "JONCKHEERE-TERPSTRA"))
297         {
298           lex_match (lexer, T_EQUALS);
299           npt->jonckheere_terpstra++;
300           switch (npar_jonckheere_terpstra (lexer, ds, nps))
301             {
302             case 0:
303               goto lossage;
304             case 1:
305               break;
306             case 2:
307               lex_error (lexer, NULL);
308               goto lossage;
309             default:
310               NOT_REACHED ();
311             }
312         }
313       else if (lex_match_phrase (lexer, "K-W") ||
314                lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
315         {
316           lex_match (lexer, T_EQUALS);
317           npt->kruskal_wallis++;
318           switch (npar_kruskal_wallis (lexer, ds, nps))
319             {
320             case 0:
321               goto lossage;
322             case 1:
323               break;
324             case 2:
325               lex_error (lexer, NULL);
326               goto lossage;
327             default:
328               NOT_REACHED ();
329             }
330         }
331       else if (lex_match_phrase (lexer, "MCNEMAR"))
332         {
333           lex_match (lexer, T_EQUALS);
334           npt->mcnemar++;
335           switch (npar_mcnemar (lexer, ds, nps))
336             {
337             case 0:
338               goto lossage;
339             case 1:
340               break;
341             case 2:
342               lex_error (lexer, NULL);
343               goto lossage;
344             default:
345               NOT_REACHED ();
346             }
347         }
348       else if (lex_match_phrase (lexer, "M-W") ||
349                lex_match_phrase (lexer, "MANN-WHITNEY"))
350         {
351           lex_match (lexer, T_EQUALS);
352           npt->mann_whitney++;
353           switch (npar_mann_whitney (lexer, ds, nps))
354             {
355             case 0:
356               goto lossage;
357             case 1:
358               break;
359             case 2:
360               lex_error (lexer, NULL);
361               goto lossage;
362             default:
363               NOT_REACHED ();
364             }
365         }
366       else if (lex_match_phrase (lexer, "MEDIAN"))
367         {
368           npt->median++;
369
370           switch (npar_median (lexer, ds, nps))
371             {
372             case 0:
373               goto lossage;
374             case 1:
375               break;
376             case 2:
377               lex_error (lexer, NULL);
378               goto lossage;
379             default:
380               NOT_REACHED ();
381             }
382         }
383       else if (lex_match_id (lexer, "WILCOXON"))
384         {
385           lex_match (lexer, T_EQUALS);
386           npt->wilcoxon++;
387           switch (npar_wilcoxon (lexer, ds, nps))
388             {
389             case 0:
390               goto lossage;
391             case 1:
392               break;
393             case 2:
394               lex_error (lexer, NULL);
395               goto lossage;
396             default:
397               NOT_REACHED ();
398             }
399         }
400       else if (lex_match_id (lexer, "SIGN"))
401         {
402           lex_match (lexer, T_EQUALS);
403           npt->sign++;
404           switch (npar_sign (lexer, ds, nps))
405             {
406             case 0:
407               goto lossage;
408             case 1:
409               break;
410             case 2:
411               lex_error (lexer, NULL);
412               goto lossage;
413             default:
414               NOT_REACHED ();
415             }
416         }
417       else if (lex_match_id (lexer, "MISSING"))
418         {
419           lex_match (lexer, T_EQUALS);
420           npt->missing++;
421           if (npt->missing > 1)
422             {
423               lex_sbc_only_once ("MISSING");
424               goto lossage;
425             }
426           while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
427             {
428               if (lex_match_id (lexer, "ANALYSIS"))
429                 npt->miss = MISS_ANALYSIS;
430               else if (lex_match_id (lexer, "LISTWISE"))
431                 npt->miss = MISS_LISTWISE;
432               else if (lex_match_id (lexer, "INCLUDE"))
433                 nps->filter = MV_SYSTEM;
434               else if (lex_match_id (lexer, "EXCLUDE"))
435                 nps->filter = MV_ANY;
436               else
437                 {
438                   lex_error (lexer, NULL);
439                   goto lossage;
440                 }
441               lex_match (lexer, T_COMMA);
442             }
443         }
444       else if (lex_match_id (lexer, "METHOD"))
445         {
446           lex_match (lexer, T_EQUALS);
447           npt->method++;
448           if (npt->method > 1)
449             {
450               lex_sbc_only_once ("METHOD");
451               goto lossage;
452             }
453           switch (npar_method (lexer, nps))
454             {
455             case 0:
456               goto lossage;
457             case 1:
458               break;
459             case 2:
460               lex_error (lexer, NULL);
461               goto lossage;
462             default:
463               NOT_REACHED ();
464             }
465         }
466       else if (lex_match_id (lexer, "STATISTICS"))
467         {
468           lex_match (lexer, T_EQUALS);
469           npt->statistics++;
470           while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
471             {
472               if (lex_match_id (lexer, "DESCRIPTIVES"))
473                 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
474               else if (lex_match_id (lexer, "QUARTILES"))
475                 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
476               else if (lex_match (lexer, T_ALL))
477                 npt->a_statistics[NPAR_ST_ALL] = 1;
478               else
479                 {
480                   lex_error (lexer, NULL);
481                   goto lossage;
482                 }
483               lex_match (lexer, T_COMMA);
484             }
485         }
486       else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
487         {
488           lex_match (lexer, T_EQUALS);
489           if (lex_match_id (lexer, "COMPATIBLE"))
490             settings_set_cmd_algorithm (COMPATIBLE);
491           else if (lex_match_id (lexer, "ENHANCED"))
492             settings_set_cmd_algorithm (ENHANCED);
493           }
494         if (!lex_match (lexer, T_SLASH))
495           break;
496       }
497
498     if (lex_token (lexer) != T_ENDCMD)
499       {
500         lex_error (lexer, _("expecting end of command"));
501         goto lossage;
502       }
503
504   return true;
505
506 lossage:
507   return false;
508 }
509
510
511 static void one_sample_insert_variables (const struct npar_test *test,
512                                          struct hmapx *);
513
514 static void two_sample_insert_variables (const struct npar_test *test,
515                                          struct hmapx *);
516
517 static void n_sample_insert_variables (const struct npar_test *test,
518                                        struct hmapx *);
519
520 static void
521 npar_execute (struct casereader *input,
522              const struct npar_specs *specs,
523              const struct dataset *ds)
524 {
525   int t;
526   struct descriptives *summary_descriptives = NULL;
527
528   for ( t = 0 ; t < specs->n_tests; ++t )
529     {
530       const struct npar_test *test = specs->test[t];
531       if ( NULL == test->execute )
532         {
533           msg (SW, _("NPAR subcommand not currently implemented."));
534           continue;
535         }
536       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
537     }
538
539   if ( specs->descriptives )
540     {
541       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
542                                        specs->n_vars);
543
544       npar_summary_calc_descriptives (summary_descriptives,
545                                       casereader_clone (input),
546                                       dataset_dict (ds),
547                                       specs->vv, specs->n_vars,
548                                       specs->filter);
549     }
550
551   if ( (specs->descriptives || specs->quartiles)
552        && !taint_has_tainted_successor (casereader_get_taint (input)) )
553     do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
554
555   free (summary_descriptives);
556   casereader_destroy (input);
557 }
558
559 int
560 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
561 {
562   struct cmd_npar_tests cmd;
563   bool ok;
564   int i;
565   struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
566   struct casegrouper *grouper;
567   struct casereader *input, *group;
568   struct hmapx var_map = HMAPX_INITIALIZER (var_map);
569
570
571   npar_specs.pool = pool_create ();
572   npar_specs.filter = MV_ANY;
573   npar_specs.n_vars = -1;
574   npar_specs.vv = NULL;
575
576   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
577     {
578       pool_destroy (npar_specs.pool);
579       return CMD_FAILURE;
580     }
581
582   for (i = 0; i < npar_specs.n_tests; ++i )
583     {
584       const struct npar_test *test = npar_specs.test[i];
585       test->insert_variables (test, &var_map);
586     }
587
588   {
589     struct hmapx_node *node;
590     struct variable *var;
591     npar_specs.n_vars = 0;
592
593     HMAPX_FOR_EACH (var, node, &var_map)
594       {
595         npar_specs.n_vars ++;
596         npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
597         npar_specs.vv[npar_specs.n_vars - 1] = var;
598       }
599   }
600
601   sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv), 
602          compare_var_ptrs_by_name, NULL);
603
604   if ( cmd.statistics )
605     {
606       int i;
607
608       for ( i = 0 ; i < NPAR_ST_count; ++i )
609         {
610           if ( cmd.a_statistics[i] )
611             {
612               switch ( i )
613                 {
614                 case NPAR_ST_DESCRIPTIVES:
615                   npar_specs.descriptives = true;
616                   break;
617                 case NPAR_ST_QUARTILES:
618                   npar_specs.quartiles = true;
619                   break;
620                 case NPAR_ST_ALL:
621                   npar_specs.quartiles = true;
622                   npar_specs.descriptives = true;
623                   break;
624                 default:
625                   NOT_REACHED ();
626                 };
627             }
628         }
629     }
630
631   input = proc_open (ds);
632   if ( cmd.miss == MISS_LISTWISE )
633     {
634       input = casereader_create_filter_missing (input,
635                                                 npar_specs.vv,
636                                                 npar_specs.n_vars,
637                                                 npar_specs.filter,
638                                                 NULL, NULL);
639     }
640
641
642   grouper = casegrouper_create_splits (input, dataset_dict (ds));
643   while (casegrouper_get_next_group (grouper, &group))
644     npar_execute (group, &npar_specs, ds);
645   ok = casegrouper_destroy (grouper);
646   ok = proc_commit (ds) && ok;
647
648   pool_destroy (npar_specs.pool);
649   hmapx_destroy (&var_map);
650
651   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
652 }
653
654 static int
655 npar_runs (struct lexer *lexer, struct dataset *ds,
656            struct npar_specs *specs)
657 {
658   struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
659   struct one_sample_test *tp = &rt->parent;
660   struct npar_test *nt = &tp->parent;
661
662   nt->execute = runs_execute;
663   nt->insert_variables = one_sample_insert_variables;
664
665   if ( lex_force_match (lexer, T_LPAREN) )
666     {
667       if ( lex_match_id (lexer, "MEAN"))
668         {
669           rt->cp_mode = CP_MEAN;
670         }
671       else if (lex_match_id (lexer, "MEDIAN"))
672         {
673           rt->cp_mode = CP_MEDIAN;
674         }
675       else if (lex_match_id (lexer, "MODE"))
676         {
677           rt->cp_mode = CP_MODE;
678         }
679       else if (lex_is_number (lexer))
680         {
681           rt->cutpoint = lex_number (lexer);
682           rt->cp_mode = CP_CUSTOM;
683           lex_get (lexer);
684         }
685       else
686         {
687           lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
688           return 0;
689         }
690                   
691       lex_force_match (lexer, T_RPAREN);
692       lex_force_match (lexer, T_EQUALS);
693       if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
694                                   &tp->vars, &tp->n_vars,
695                                   PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
696         {
697           return 2;
698         }
699     }
700
701   specs->n_tests++;
702   specs->test = pool_realloc (specs->pool,
703                               specs->test,
704                               sizeof (*specs->test) * specs->n_tests);
705
706   specs->test[specs->n_tests - 1] = nt;
707
708   return 1;
709 }
710
711 static int
712 npar_friedman (struct lexer *lexer, struct dataset *ds,
713                struct npar_specs *specs)
714 {
715   struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft)); 
716   struct one_sample_test *ost = &ft->parent;
717   struct npar_test *nt = &ost->parent;
718
719   ft->kendalls_w = false;
720   nt->execute = friedman_execute;
721   nt->insert_variables = one_sample_insert_variables;
722
723   lex_match (lexer, T_EQUALS);
724
725   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
726                                    &ost->vars, &ost->n_vars,
727                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
728     {
729       return 2;
730     }
731
732   specs->n_tests++;
733   specs->test = pool_realloc (specs->pool,
734                               specs->test,
735                               sizeof (*specs->test) * specs->n_tests);
736
737   specs->test[specs->n_tests - 1] = nt;
738
739   return 1;
740 }
741
742 static int
743 npar_kendall (struct lexer *lexer, struct dataset *ds,
744                struct npar_specs *specs)
745 {
746   struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt)); 
747   struct one_sample_test *ost = &kt->parent;
748   struct npar_test *nt = &ost->parent;
749
750   kt->kendalls_w = true;
751   nt->execute = friedman_execute;
752   nt->insert_variables = one_sample_insert_variables;
753
754   lex_match (lexer, T_EQUALS);
755
756   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
757                                    &ost->vars, &ost->n_vars,
758                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
759     {
760       return 2;
761     }
762
763   specs->n_tests++;
764   specs->test = pool_realloc (specs->pool,
765                               specs->test,
766                               sizeof (*specs->test) * specs->n_tests);
767
768   specs->test[specs->n_tests - 1] = nt;
769
770   return 1;
771 }
772
773
774 static int
775 npar_cochran (struct lexer *lexer, struct dataset *ds,
776                struct npar_specs *specs)
777 {
778   struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft)); 
779   struct npar_test *nt = &ft->parent;
780
781   nt->execute = cochran_execute;
782   nt->insert_variables = one_sample_insert_variables;
783
784   lex_match (lexer, T_EQUALS);
785
786   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
787                                    &ft->vars, &ft->n_vars,
788                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
789     {
790       return 2;
791     }
792
793   specs->n_tests++;
794   specs->test = pool_realloc (specs->pool,
795                               specs->test,
796                               sizeof (*specs->test) * specs->n_tests);
797
798   specs->test[specs->n_tests - 1] = nt;
799
800   return 1;
801 }
802
803
804 static int
805 npar_chisquare (struct lexer *lexer, struct dataset *ds,
806                 struct npar_specs *specs)
807 {
808   struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
809   struct one_sample_test *tp = &cstp->parent;
810   struct npar_test *nt = &tp->parent;
811   int retval = 1;
812
813   nt->execute = chisquare_execute;
814   nt->insert_variables = one_sample_insert_variables;
815
816   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
817                                    &tp->vars, &tp->n_vars,
818                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
819     {
820       return 2;
821     }
822
823   cstp->ranged = false;
824
825   if ( lex_match (lexer, T_LPAREN))
826     {
827       cstp->ranged = true;
828       if ( ! lex_force_num (lexer)) return 0;
829       cstp->lo = lex_integer (lexer);
830       lex_get (lexer);
831       lex_force_match (lexer, T_COMMA);
832       if (! lex_force_num (lexer) ) return 0;
833       cstp->hi = lex_integer (lexer);
834       if ( cstp->lo >= cstp->hi )
835         {
836           msg (ME,
837               _("The specified value of HI (%d) is "
838                 "lower than the specified value of LO (%d)"),
839               cstp->hi, cstp->lo);
840           return 0;
841         }
842       lex_get (lexer);
843       if (! lex_force_match (lexer, T_RPAREN)) return 0;
844     }
845
846   cstp->n_expected = 0;
847   cstp->expected = NULL;
848   if (lex_match_phrase (lexer, "/EXPECTED"))
849     {
850       lex_force_match (lexer, T_EQUALS);
851       if ( ! lex_match_id (lexer, "EQUAL") )
852         {
853           double f;
854           int n;
855           while ( lex_is_number (lexer) )
856             {
857               int i;
858               n = 1;
859               f = lex_number (lexer);
860               lex_get (lexer);
861               if ( lex_match (lexer, T_ASTERISK))
862                 {
863                   n = f;
864                   f = lex_number (lexer);
865                   lex_get (lexer);
866                 }
867               lex_match (lexer, T_COMMA);
868
869               cstp->n_expected += n;
870               cstp->expected = pool_realloc (specs->pool,
871                                              cstp->expected,
872                                              sizeof (double) *
873                                              cstp->n_expected);
874               for ( i = cstp->n_expected - n ;
875                     i < cstp->n_expected;
876                     ++i )
877                 cstp->expected[i] = f;
878
879             }
880         }
881     }
882
883   if ( cstp->ranged && cstp->n_expected > 0 &&
884        cstp->n_expected != cstp->hi - cstp->lo + 1 )
885     {
886       msg (ME,
887           _("%d expected values were given, but the specified "
888             "range (%d-%d) requires exactly %d values."),
889           cstp->n_expected, cstp->lo, cstp->hi,
890           cstp->hi - cstp->lo +1);
891       return 0;
892     }
893
894   specs->n_tests++;
895   specs->test = pool_realloc (specs->pool,
896                               specs->test,
897                               sizeof (*specs->test) * specs->n_tests);
898
899   specs->test[specs->n_tests - 1] = nt;
900
901   return retval;
902 }
903
904
905 static int
906 npar_binomial (struct lexer *lexer, struct dataset *ds,
907                struct npar_specs *specs)
908 {
909   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
910   struct one_sample_test *tp = &btp->parent;
911   struct npar_test *nt = &tp->parent;
912   bool equals = false;
913
914   nt->execute = binomial_execute;
915   nt->insert_variables = one_sample_insert_variables;
916
917   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
918
919   btp->p = 0.5;
920
921   if ( lex_match (lexer, T_LPAREN) )
922     {
923       equals = false;
924       if ( lex_force_num (lexer) )
925         {
926           btp->p = lex_number (lexer);
927           lex_get (lexer);
928           lex_force_match (lexer, T_RPAREN);
929         }
930       else
931         return 0;
932     }
933   else
934     equals = true;
935
936   if (equals || lex_match (lexer, T_EQUALS) )
937     {
938       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
939                                       &tp->vars, &tp->n_vars,
940                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
941         {
942           if (lex_match (lexer, T_LPAREN))
943             {
944               lex_force_num (lexer);
945               btp->category1 = lex_number (lexer);
946               lex_get (lexer);
947               if ( lex_match (lexer, T_COMMA))
948                 {
949                   if ( ! lex_force_num (lexer) ) return 2;
950                   btp->category2 = lex_number (lexer);
951                   lex_get (lexer);
952                 }
953               else
954                 {
955                   btp->cutpoint = btp->category1;
956                 }
957
958               lex_force_match (lexer, T_RPAREN);
959             }
960         }
961       else
962         return 2;
963
964     }
965
966   specs->n_tests++;
967   specs->test = pool_realloc (specs->pool,
968                               specs->test,
969                               sizeof (*specs->test) * specs->n_tests);
970
971   specs->test[specs->n_tests - 1] = nt;
972
973   return 1;
974 }
975
976
977
978 static void
979 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
980 {
981   assert (params == 1 || params == 2);
982
983   if (lex_is_number (lexer))
984     {
985       kst->p[0] = lex_number (lexer);
986
987       lex_get (lexer);
988       if ( params == 2)
989         {
990           lex_match (lexer, T_COMMA);
991           if (lex_force_num (lexer))
992             {
993               kst->p[1] = lex_number (lexer);
994               lex_get (lexer);
995             }
996         }
997     }
998 }
999
1000 static int
1001 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
1002 {
1003   struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
1004   struct one_sample_test *tp = &kst->parent;
1005   struct npar_test *nt = &tp->parent;
1006
1007   nt->execute = ks_one_sample_execute;
1008   nt->insert_variables = one_sample_insert_variables;
1009
1010   kst->p[0] = kst->p[1] = SYSMIS;
1011
1012   if (! lex_force_match (lexer, T_LPAREN))
1013     return 2;
1014
1015   if (lex_match_id (lexer, "NORMAL"))
1016     {
1017       kst->dist = KS_NORMAL;
1018       ks_one_sample_parse_params (lexer, kst, 2);
1019     }
1020   else if (lex_match_id (lexer, "POISSON"))
1021     {
1022       kst->dist = KS_POISSON;
1023       ks_one_sample_parse_params (lexer, kst, 1);
1024     }
1025   else if (lex_match_id (lexer, "UNIFORM"))
1026     {
1027       kst->dist = KS_UNIFORM;
1028       ks_one_sample_parse_params (lexer, kst, 2);
1029     }
1030   else if (lex_match_id (lexer, "EXPONENTIAL"))
1031     {
1032       kst->dist = KS_EXPONENTIAL;
1033       ks_one_sample_parse_params (lexer, kst, 1);
1034     }
1035   else
1036     return 2;
1037
1038   if (! lex_force_match (lexer, T_RPAREN))
1039     return 2;
1040
1041   lex_match (lexer, T_EQUALS);
1042
1043   if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1044                                   &tp->vars, &tp->n_vars,
1045                                   PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1046     return 2;
1047
1048   specs->n_tests++;
1049   specs->test = pool_realloc (specs->pool,
1050                               specs->test,
1051                               sizeof (*specs->test) * specs->n_tests);
1052
1053   specs->test[specs->n_tests - 1] = nt;
1054
1055   return 1;
1056 }
1057
1058
1059 static bool
1060 parse_two_sample_related_test (struct lexer *lexer,
1061                                const struct dictionary *dict,
1062                                struct two_sample_test *test_parameters,
1063                                struct pool *pool)
1064 {
1065   int n = 0;
1066   bool paired = false;
1067   bool with = false;
1068   const struct variable **vlist1;
1069   size_t n_vlist1;
1070
1071   const struct variable **vlist2;
1072   size_t n_vlist2;
1073
1074   test_parameters->parent.insert_variables = two_sample_insert_variables;
1075
1076   if (!parse_variables_const_pool (lexer, pool,
1077                                    dict,
1078                                    &vlist1, &n_vlist1,
1079                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1080     return false;
1081
1082   if ( lex_match (lexer, T_WITH))
1083     {
1084       with = true;
1085       if ( !parse_variables_const_pool (lexer, pool, dict,
1086                                         &vlist2, &n_vlist2,
1087                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1088         return false;
1089
1090       paired = (lex_match (lexer, T_LPAREN) &&
1091                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1092     }
1093
1094
1095   if ( with )
1096     {
1097       if (paired)
1098         {
1099           if ( n_vlist1 != n_vlist2)
1100             msg (SE, _("PAIRED was specified but the number of variables "
1101                        "preceding WITH (%zu) did not match the number "
1102                        "following (%zu)."), n_vlist1, n_vlist2);
1103
1104           test_parameters->n_pairs = n_vlist1 ;
1105         }
1106       else
1107         {
1108           test_parameters->n_pairs = n_vlist1 * n_vlist2;
1109         }
1110     }
1111   else
1112     {
1113       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1114     }
1115
1116   test_parameters->pairs =
1117     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
1118
1119   if ( with )
1120     {
1121       if (paired)
1122         {
1123           int i;
1124           assert (n_vlist1 == n_vlist2);
1125           for ( i = 0 ; i < n_vlist1; ++i )
1126             {
1127               test_parameters->pairs[n][0] = vlist1[i];
1128               test_parameters->pairs[n][1] = vlist2[i];
1129               n++;
1130             }
1131         }
1132       else
1133         {
1134           int i,j;
1135           for ( i = 0 ; i < n_vlist1; ++i )
1136             {
1137               for ( j = 0 ; j < n_vlist2; ++j )
1138                 {
1139                   test_parameters->pairs[n][0] = vlist1[i];
1140                   test_parameters->pairs[n][1] = vlist2[j];
1141                   n++;
1142                 }
1143             }
1144         }
1145     }
1146   else
1147     {
1148       int i,j;
1149       for ( i = 0 ; i < n_vlist1 - 1; ++i )
1150         {
1151           for ( j = i + 1 ; j < n_vlist1; ++j )
1152             {
1153               assert ( n < test_parameters->n_pairs);
1154               test_parameters->pairs[n][0] = vlist1[i];
1155               test_parameters->pairs[n][1] = vlist1[j];
1156               n++;
1157             }
1158         }
1159     }
1160
1161   assert ( n == test_parameters->n_pairs);
1162
1163   return true;
1164 }
1165
1166
1167 static bool
1168 parse_n_sample_related_test (struct lexer *lexer,
1169                              const struct dictionary *dict,
1170                              struct n_sample_test *nst,
1171                              struct pool *pool
1172                              )
1173 {
1174   if (!parse_variables_const_pool (lexer, pool,
1175                                    dict,
1176                                    &nst->vars, &nst->n_vars,
1177                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1178     return false;
1179
1180   if ( ! lex_force_match (lexer, T_BY))
1181     return false;
1182
1183   nst->indep_var = parse_variable_const (lexer, dict);
1184
1185   if ( ! lex_force_match (lexer, T_LPAREN))
1186     return false;
1187
1188   value_init (&nst->val1, var_get_width (nst->indep_var));
1189   if ( ! parse_value (lexer, &nst->val1, nst->indep_var))
1190     {
1191       value_destroy (&nst->val1, var_get_width (nst->indep_var));
1192       return false;
1193     }
1194
1195   lex_match (lexer, T_COMMA);
1196
1197   value_init (&nst->val2, var_get_width (nst->indep_var));
1198   if ( ! parse_value (lexer, &nst->val2, nst->indep_var))
1199     {
1200       value_destroy (&nst->val2, var_get_width (nst->indep_var));
1201       return false;
1202     }
1203
1204   if ( ! lex_force_match (lexer, T_RPAREN))
1205     return false;
1206
1207   return true;
1208 }
1209
1210 static int
1211 npar_wilcoxon (struct lexer *lexer,
1212                struct dataset *ds,
1213                struct npar_specs *specs )
1214 {
1215   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1216   struct npar_test *nt = &tp->parent;
1217   nt->execute = wilcoxon_execute;
1218
1219   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1220                                       tp, specs->pool) )
1221     return 0;
1222
1223   specs->n_tests++;
1224   specs->test = pool_realloc (specs->pool,
1225                               specs->test,
1226                               sizeof (*specs->test) * specs->n_tests);
1227   specs->test[specs->n_tests - 1] = nt;
1228
1229   return 1;
1230 }
1231
1232
1233 static int
1234 npar_mann_whitney (struct lexer *lexer,
1235                struct dataset *ds,
1236                struct npar_specs *specs )
1237 {
1238   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1239   struct npar_test *nt = &tp->parent;
1240
1241   nt->insert_variables = n_sample_insert_variables;
1242   nt->execute = mann_whitney_execute;
1243
1244   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1245                                     tp, specs->pool) )
1246     return 0;
1247
1248   specs->n_tests++;
1249   specs->test = pool_realloc (specs->pool,
1250                               specs->test,
1251                               sizeof (*specs->test) * specs->n_tests);
1252   specs->test[specs->n_tests - 1] = nt;
1253
1254   return 1;
1255 }
1256
1257
1258 static int
1259 npar_median (struct lexer *lexer,
1260              struct dataset *ds,
1261              struct npar_specs *specs)
1262 {
1263   struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
1264   struct n_sample_test *tp = &mt->parent;
1265   struct npar_test *nt = &tp->parent;
1266
1267   mt->median = SYSMIS;
1268
1269   if ( lex_match (lexer, T_LPAREN))
1270     {
1271       lex_force_num (lexer);
1272       mt->median = lex_number (lexer);
1273       lex_get (lexer);
1274       lex_force_match (lexer, T_RPAREN);
1275     }
1276
1277   lex_match (lexer, T_EQUALS);
1278
1279   nt->insert_variables = n_sample_insert_variables;
1280   nt->execute = median_execute;
1281
1282   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1283                                     tp, specs->pool) )
1284     return 0;
1285
1286   specs->n_tests++;
1287   specs->test = pool_realloc (specs->pool,
1288                               specs->test,
1289                               sizeof (*specs->test) * specs->n_tests);
1290   specs->test[specs->n_tests - 1] = nt;
1291
1292   return 1;
1293 }
1294
1295
1296 static int
1297 npar_sign (struct lexer *lexer, struct dataset *ds,
1298            struct npar_specs *specs)
1299 {
1300   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1301   struct npar_test *nt = &tp->parent;
1302
1303   nt->execute = sign_execute;
1304
1305   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1306                                       tp, specs->pool) )
1307     return 0;
1308
1309   specs->n_tests++;
1310   specs->test = pool_realloc (specs->pool,
1311                               specs->test,
1312                               sizeof (*specs->test) * specs->n_tests);
1313   specs->test[specs->n_tests - 1] = nt;
1314
1315   return 1;
1316 }
1317
1318
1319 static int
1320 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
1321            struct npar_specs *specs)
1322 {
1323   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1324   struct npar_test *nt = &tp->parent;
1325
1326   nt->execute = mcnemar_execute;
1327
1328   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1329                                       tp, specs->pool) )
1330     return 0;
1331
1332   specs->n_tests++;
1333   specs->test = pool_realloc (specs->pool,
1334                               specs->test,
1335                               sizeof (*specs->test) * specs->n_tests);
1336   specs->test[specs->n_tests - 1] = nt;
1337
1338   return 1;
1339 }
1340
1341
1342 static int
1343 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
1344                       struct npar_specs *specs)
1345 {
1346   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1347   struct npar_test *nt = &tp->parent;
1348
1349   nt->insert_variables = n_sample_insert_variables;
1350
1351   nt->execute = jonckheere_terpstra_execute;
1352
1353   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1354                                       tp, specs->pool) )
1355     return 0;
1356
1357   specs->n_tests++;
1358   specs->test = pool_realloc (specs->pool,
1359                               specs->test,
1360                               sizeof (*specs->test) * specs->n_tests);
1361   specs->test[specs->n_tests - 1] = nt;
1362
1363   return 1;
1364 }
1365
1366 static int
1367 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1368                       struct npar_specs *specs)
1369 {
1370   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1371   struct npar_test *nt = &tp->parent;
1372
1373   nt->insert_variables = n_sample_insert_variables;
1374
1375   nt->execute = kruskal_wallis_execute;
1376
1377   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1378                                       tp, specs->pool) )
1379     return 0;
1380
1381   specs->n_tests++;
1382   specs->test = pool_realloc (specs->pool,
1383                               specs->test,
1384                               sizeof (*specs->test) * specs->n_tests);
1385   specs->test[specs->n_tests - 1] = nt;
1386
1387   return 1;
1388 }
1389
1390 static void
1391 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1392 {
1393   size_t hash = hash_pointer (var, 0);
1394   struct hmapx_node *node;
1395   const struct variable *v = NULL;
1396       
1397   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1398     {
1399       if ( v == var)
1400         return ;
1401     }
1402
1403   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1404 }
1405
1406 /* Insert the variables for TEST into VAR_MAP */
1407 static void
1408 one_sample_insert_variables (const struct npar_test *test,
1409                              struct hmapx *var_map)
1410 {
1411   int i;
1412   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1413
1414   for ( i = 0 ; i < ost->n_vars ; ++i )
1415     insert_variable_into_map (var_map, ost->vars[i]);
1416 }
1417
1418
1419 static void
1420 two_sample_insert_variables (const struct npar_test *test,
1421                              struct hmapx *var_map)
1422 {
1423   int i;
1424   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1425
1426   for ( i = 0 ; i < tst->n_pairs ; ++i )
1427     {
1428       variable_pair *pair = &tst->pairs[i];
1429
1430       insert_variable_into_map (var_map, (*pair)[0]);
1431       insert_variable_into_map (var_map, (*pair)[1]);
1432     }
1433 }
1434
1435 static void 
1436 n_sample_insert_variables (const struct npar_test *test,
1437                            struct hmapx *var_map)
1438 {
1439   int i;
1440   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1441
1442   for ( i = 0 ; i < tst->n_vars ; ++i )
1443     insert_variable_into_map (var_map, tst->vars[i]);
1444
1445   insert_variable_into_map (var_map, tst->indep_var);
1446 }
1447
1448
1449 static int
1450 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1451 {
1452   if ( lex_match_id (lexer, "EXACT") )
1453     {
1454       specs->exact = true;
1455       specs->timer = 0.0;
1456       if (lex_match_id (lexer, "TIMER"))
1457         {
1458           specs->timer = 5.0;
1459
1460           if ( lex_match (lexer, T_LPAREN))
1461             {
1462               if ( lex_force_num (lexer) )
1463                 {
1464                   specs->timer = lex_number (lexer);
1465                   lex_get (lexer);
1466                 }
1467               lex_force_match (lexer, T_RPAREN);
1468             }
1469         }
1470     }
1471
1472   return 1;
1473 }