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