d7b2cff85311015ae739644163a126a577d76c8c
[pspp] / src / language / stats / npar.c
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009, 2010, 2011, 2016 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 (lexer, "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 (lexer, "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     if (lex_token (lexer) != T_ENDCMD)
502       {
503         lex_error (lexer, _("Syntax error expecting end of command."));
504         goto lossage;
505       }
506
507   return true;
508
509 lossage:
510   return false;
511 }
512
513
514 static void one_sample_insert_variables (const struct npar_test *test,
515                                          struct hmapx *);
516
517 static void two_sample_insert_variables (const struct npar_test *test,
518                                          struct hmapx *);
519
520 static void n_sample_insert_variables (const struct npar_test *test,
521                                        struct hmapx *);
522
523 static void
524 npar_execute (struct casereader *input,
525              const struct npar_specs *specs,
526              const struct dataset *ds)
527 {
528   int t;
529   struct descriptives *summary_descriptives = NULL;
530
531   for (t = 0 ; t < specs->n_tests; ++t)
532     {
533       const struct npar_test *test = specs->test[t];
534       if (NULL == test->execute)
535         {
536           msg (SW, _("%s subcommand not currently implemented."), "NPAR");
537           continue;
538         }
539       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
540     }
541
542   if (specs->descriptives && specs->n_vars > 0)
543     {
544       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
545                                        specs->n_vars);
546
547       npar_summary_calc_descriptives (summary_descriptives,
548                                       casereader_clone (input),
549                                       dataset_dict (ds),
550                                       specs->vv, specs->n_vars,
551                                       specs->filter);
552     }
553
554   if ((specs->descriptives || specs->quartiles)
555        && !taint_has_tainted_successor (casereader_get_taint (input)))
556     do_summary_box (summary_descriptives, specs->vv, specs->n_vars,
557                     dict_get_weight_format (dataset_dict (ds)));
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, _("Syntax error expecting %s, %s, %s or a number."),
692                      "MEAN", "MEDIAN", "MODE");
693           return 0;
694         }
695
696       if (! lex_force_match (lexer, T_RPAREN))
697         return 2;
698
699       if (! lex_force_match (lexer, T_EQUALS))
700         return 2;
701
702       if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
703                                   &tp->vars, &tp->n_vars,
704                                   PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
705         {
706           return 2;
707         }
708     }
709
710   specs->n_tests++;
711   specs->test = pool_realloc (specs->pool,
712                               specs->test,
713                               sizeof (*specs->test) * specs->n_tests);
714
715   specs->test[specs->n_tests - 1] = nt;
716
717   return 1;
718 }
719
720 static int
721 npar_friedman (struct lexer *lexer, struct dataset *ds,
722                struct npar_specs *specs)
723 {
724   struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
725   struct one_sample_test *ost = &ft->parent;
726   struct npar_test *nt = &ost->parent;
727
728   ft->kendalls_w = false;
729   nt->execute = friedman_execute;
730   nt->insert_variables = one_sample_insert_variables;
731
732   lex_match (lexer, T_EQUALS);
733
734   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
735                                    &ost->vars, &ost->n_vars,
736                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
737     {
738       return 2;
739     }
740
741   specs->n_tests++;
742   specs->test = pool_realloc (specs->pool,
743                               specs->test,
744                               sizeof (*specs->test) * specs->n_tests);
745
746   specs->test[specs->n_tests - 1] = nt;
747
748   return 1;
749 }
750
751 static int
752 npar_kendall (struct lexer *lexer, struct dataset *ds,
753                struct npar_specs *specs)
754 {
755   struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
756   struct one_sample_test *ost = &kt->parent;
757   struct npar_test *nt = &ost->parent;
758
759   kt->kendalls_w = true;
760   nt->execute = friedman_execute;
761   nt->insert_variables = one_sample_insert_variables;
762
763   lex_match (lexer, T_EQUALS);
764
765   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
766                                    &ost->vars, &ost->n_vars,
767                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
768     {
769       return 2;
770     }
771
772   specs->n_tests++;
773   specs->test = pool_realloc (specs->pool,
774                               specs->test,
775                               sizeof (*specs->test) * specs->n_tests);
776
777   specs->test[specs->n_tests - 1] = nt;
778
779   return 1;
780 }
781
782
783 static int
784 npar_cochran (struct lexer *lexer, struct dataset *ds,
785                struct npar_specs *specs)
786 {
787   struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
788   struct npar_test *nt = &ft->parent;
789
790   nt->execute = cochran_execute;
791   nt->insert_variables = one_sample_insert_variables;
792
793   lex_match (lexer, T_EQUALS);
794
795   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
796                                    &ft->vars, &ft->n_vars,
797                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
798     {
799       return 2;
800     }
801
802   specs->n_tests++;
803   specs->test = pool_realloc (specs->pool,
804                               specs->test,
805                               sizeof (*specs->test) * specs->n_tests);
806
807   specs->test[specs->n_tests - 1] = nt;
808
809   return 1;
810 }
811
812
813 static int
814 npar_chisquare (struct lexer *lexer, struct dataset *ds,
815                 struct npar_specs *specs)
816 {
817   struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
818   struct one_sample_test *tp = &cstp->parent;
819   struct npar_test *nt = &tp->parent;
820   int retval = 1;
821
822   nt->execute = chisquare_execute;
823   nt->insert_variables = one_sample_insert_variables;
824
825   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
826                                    &tp->vars, &tp->n_vars,
827                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
828     {
829       return 2;
830     }
831
832   cstp->ranged = false;
833
834   if (lex_match (lexer, T_LPAREN))
835     {
836       cstp->ranged = true;
837       if (! lex_force_num (lexer)) return 0;
838       cstp->lo = lex_number (lexer);
839       lex_get (lexer);
840       if (! lex_force_match (lexer, T_COMMA)) return 0;
841       if (! lex_force_num (lexer)) return 0;
842       cstp->hi = lex_number (lexer);
843       if (cstp->lo >= cstp->hi)
844         {
845           msg (ME,
846               _("The specified value of HI (%d) is "
847                 "lower than the specified value of LO (%d)"),
848               cstp->hi, cstp->lo);
849           return 0;
850         }
851       lex_get (lexer);
852       if (! lex_force_match (lexer, T_RPAREN)) return 0;
853     }
854
855   cstp->n_expected = 0;
856   cstp->expected = NULL;
857   if (lex_match_phrase (lexer, "/EXPECTED"))
858     {
859       if (! lex_force_match (lexer, T_EQUALS)) return 0;
860       if (! lex_match_id (lexer, "EQUAL"))
861         {
862           double f;
863           int n;
864           while (lex_is_number (lexer))
865             {
866               int i;
867               n = 1;
868               f = lex_number (lexer);
869               lex_get (lexer);
870               if (lex_match (lexer, T_ASTERISK))
871                 {
872                   n = f;
873                   if (!lex_force_num (lexer))
874                     return 0;
875                   f = lex_number (lexer);
876                   lex_get (lexer);
877                 }
878               lex_match (lexer, T_COMMA);
879
880               cstp->n_expected += n;
881               cstp->expected = pool_realloc (specs->pool,
882                                              cstp->expected,
883                                              sizeof (double) *
884                                              cstp->n_expected);
885               for (i = cstp->n_expected - n ;
886                     i < cstp->n_expected;
887                     ++i)
888                 cstp->expected[i] = f;
889
890             }
891         }
892     }
893
894   if (cstp->ranged && cstp->n_expected > 0 &&
895        cstp->n_expected != cstp->hi - cstp->lo + 1)
896     {
897       msg (ME,
898           _("%d expected values were given, but the specified "
899             "range (%d-%d) requires exactly %d values."),
900           cstp->n_expected, cstp->lo, cstp->hi,
901           cstp->hi - cstp->lo +1);
902       return 0;
903     }
904
905   specs->n_tests++;
906   specs->test = pool_realloc (specs->pool,
907                               specs->test,
908                               sizeof (*specs->test) * specs->n_tests);
909
910   specs->test[specs->n_tests - 1] = nt;
911
912   return retval;
913 }
914
915
916 static int
917 npar_binomial (struct lexer *lexer, struct dataset *ds,
918                struct npar_specs *specs)
919 {
920   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
921   struct one_sample_test *tp = &btp->parent;
922   struct npar_test *nt = &tp->parent;
923   bool equals = false;
924
925   nt->execute = binomial_execute;
926   nt->insert_variables = one_sample_insert_variables;
927
928   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
929
930   btp->p = 0.5;
931
932   if (lex_match (lexer, T_LPAREN))
933     {
934       equals = false;
935       if (lex_force_num (lexer))
936         {
937           btp->p = lex_number (lexer);
938           lex_get (lexer);
939           if (!lex_force_match (lexer, T_RPAREN))
940             return 0;
941         }
942       else
943         return 0;
944     }
945   else
946     equals = true;
947
948   if (!equals)
949     if (!lex_force_match (lexer, T_EQUALS))
950       return 0;
951
952   {
953     if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
954                                     &tp->vars, &tp->n_vars,
955                                     PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
956       {
957         if (lex_match (lexer, T_LPAREN))
958           {
959             if (! lex_force_num (lexer))
960               return 2;
961             btp->category1 = lex_number (lexer);
962             lex_get (lexer);
963             if (lex_match (lexer, T_COMMA))
964               {
965                 if (! lex_force_num (lexer)) return 2;
966                 btp->category2 = lex_number (lexer);
967                 lex_get (lexer);
968               }
969             else
970               {
971                 btp->cutpoint = btp->category1;
972               }
973
974             if (! lex_force_match (lexer, T_RPAREN))
975               return 0;
976           }
977       }
978     else
979       {
980         return 2;
981       }
982   }
983
984   specs->n_tests++;
985   specs->test = pool_realloc (specs->pool,
986                               specs->test,
987                               sizeof (*specs->test) * specs->n_tests);
988
989   specs->test[specs->n_tests - 1] = nt;
990
991   return 1;
992 }
993
994
995
996 static void
997 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
998 {
999   assert (params == 1 || params == 2);
1000
1001   if (lex_is_number (lexer))
1002     {
1003       kst->p[0] = lex_number (lexer);
1004
1005       lex_get (lexer);
1006       if (params == 2)
1007         {
1008           lex_match (lexer, T_COMMA);
1009           if (lex_force_num (lexer))
1010             {
1011               kst->p[1] = lex_number (lexer);
1012               lex_get (lexer);
1013             }
1014         }
1015     }
1016 }
1017
1018 static int
1019 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
1020 {
1021   struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
1022   struct one_sample_test *tp = &kst->parent;
1023   struct npar_test *nt = &tp->parent;
1024
1025   nt->execute = ks_one_sample_execute;
1026   nt->insert_variables = one_sample_insert_variables;
1027
1028   kst->p[0] = kst->p[1] = SYSMIS;
1029
1030   if (! lex_force_match (lexer, T_LPAREN))
1031     return 2;
1032
1033   if (lex_match_id (lexer, "NORMAL"))
1034     {
1035       kst->dist = KS_NORMAL;
1036       ks_one_sample_parse_params (lexer, kst, 2);
1037     }
1038   else if (lex_match_id (lexer, "POISSON"))
1039     {
1040       kst->dist = KS_POISSON;
1041       ks_one_sample_parse_params (lexer, kst, 1);
1042     }
1043   else if (lex_match_id (lexer, "UNIFORM"))
1044     {
1045       kst->dist = KS_UNIFORM;
1046       ks_one_sample_parse_params (lexer, kst, 2);
1047     }
1048   else if (lex_match_id (lexer, "EXPONENTIAL"))
1049     {
1050       kst->dist = KS_EXPONENTIAL;
1051       ks_one_sample_parse_params (lexer, kst, 1);
1052     }
1053   else
1054     return 2;
1055
1056   if (! lex_force_match (lexer, T_RPAREN))
1057     return 2;
1058
1059   lex_match (lexer, T_EQUALS);
1060
1061   if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1062                                   &tp->vars, &tp->n_vars,
1063                                   PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
1064     return 2;
1065
1066   specs->n_tests++;
1067   specs->test = pool_realloc (specs->pool,
1068                               specs->test,
1069                               sizeof (*specs->test) * specs->n_tests);
1070
1071   specs->test[specs->n_tests - 1] = nt;
1072
1073   return 1;
1074 }
1075
1076
1077 static bool
1078 parse_two_sample_related_test (struct lexer *lexer,
1079                                const struct dictionary *dict,
1080                                struct two_sample_test *test_parameters,
1081                                struct pool *pool)
1082 {
1083   int n = 0;
1084   bool paired = false;
1085   bool with = false;
1086   const struct variable **vlist1;
1087   size_t n_vlist1;
1088
1089   const struct variable **vlist2;
1090   size_t n_vlist2;
1091
1092   test_parameters->parent.insert_variables = two_sample_insert_variables;
1093
1094   if (!parse_variables_const_pool (lexer, pool,
1095                                    dict,
1096                                    &vlist1, &n_vlist1,
1097                                    PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
1098     return false;
1099
1100   if (lex_match (lexer, T_WITH))
1101     {
1102       with = true;
1103       if (!parse_variables_const_pool (lexer, pool, dict,
1104                                         &vlist2, &n_vlist2,
1105                                         PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
1106         return false;
1107
1108       paired = (lex_match (lexer, T_LPAREN) &&
1109                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1110     }
1111
1112
1113   if (with)
1114     {
1115       if (paired)
1116         {
1117           if (n_vlist1 != n_vlist2)
1118             {
1119               msg (SE, _("PAIRED was specified but the number of variables "
1120                        "preceding WITH (%zu) did not match the number "
1121                        "following (%zu)."), n_vlist1, n_vlist2);
1122               return false;
1123             }
1124
1125           test_parameters->n_pairs = n_vlist1 ;
1126         }
1127       else
1128         {
1129           test_parameters->n_pairs = n_vlist1 * n_vlist2;
1130         }
1131     }
1132   else
1133     {
1134       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1135     }
1136
1137   test_parameters->pairs =
1138     pool_alloc (pool, sizeof (variable_pair) * test_parameters->n_pairs);
1139
1140   if (with)
1141     {
1142       if (paired)
1143         {
1144           int i;
1145           assert (n_vlist1 == n_vlist2);
1146           for (i = 0 ; i < n_vlist1; ++i)
1147             {
1148               test_parameters->pairs[n][0] = vlist1[i];
1149               test_parameters->pairs[n][1] = vlist2[i];
1150               n++;
1151             }
1152         }
1153       else
1154         {
1155           int i,j;
1156           for (i = 0 ; i < n_vlist1; ++i)
1157             {
1158               for (j = 0 ; j < n_vlist2; ++j)
1159                 {
1160                   test_parameters->pairs[n][0] = vlist1[i];
1161                   test_parameters->pairs[n][1] = vlist2[j];
1162                   n++;
1163                 }
1164             }
1165         }
1166     }
1167   else
1168     {
1169       int i,j;
1170       for (i = 0 ; i < n_vlist1 - 1; ++i)
1171         {
1172           for (j = i + 1 ; j < n_vlist1; ++j)
1173             {
1174               assert (n < test_parameters->n_pairs);
1175               test_parameters->pairs[n][0] = vlist1[i];
1176               test_parameters->pairs[n][1] = vlist1[j];
1177               n++;
1178             }
1179         }
1180     }
1181
1182   assert (n == test_parameters->n_pairs);
1183
1184   return true;
1185 }
1186
1187
1188 static bool
1189 parse_n_sample_related_test (struct lexer *lexer,
1190                              const struct dictionary *dict,
1191                              struct n_sample_test *nst,
1192                              struct pool *pool
1193                         )
1194 {
1195   if (!parse_variables_const_pool (lexer, pool,
1196                                    dict,
1197                                    &nst->vars, &nst->n_vars,
1198                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
1199     return false;
1200
1201   if (! lex_force_match (lexer, T_BY))
1202     return false;
1203
1204   nst->indep_var = parse_variable_const (lexer, dict);
1205   if (!nst->indep_var)
1206     return false;
1207
1208   if (! lex_force_match (lexer, T_LPAREN))
1209     return false;
1210
1211   value_init (&nst->val1, var_get_width (nst->indep_var));
1212   if (! parse_value (lexer, &nst->val1, nst->indep_var))
1213     {
1214       value_destroy (&nst->val1, var_get_width (nst->indep_var));
1215       return false;
1216     }
1217
1218   lex_match (lexer, T_COMMA);
1219
1220   value_init (&nst->val2, var_get_width (nst->indep_var));
1221   if (! parse_value (lexer, &nst->val2, nst->indep_var))
1222     {
1223       value_destroy (&nst->val2, var_get_width (nst->indep_var));
1224       return false;
1225     }
1226
1227   if (! lex_force_match (lexer, T_RPAREN))
1228     return false;
1229
1230   return true;
1231 }
1232
1233 static int
1234 npar_wilcoxon (struct lexer *lexer,
1235                struct dataset *ds,
1236                struct npar_specs *specs)
1237 {
1238   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1239   struct npar_test *nt = &tp->parent;
1240   nt->execute = wilcoxon_execute;
1241
1242   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1243                                       tp, specs->pool))
1244     return 0;
1245
1246   specs->n_tests++;
1247   specs->test = pool_realloc (specs->pool,
1248                               specs->test,
1249                               sizeof (*specs->test) * specs->n_tests);
1250   specs->test[specs->n_tests - 1] = nt;
1251
1252   return 1;
1253 }
1254
1255
1256 static int
1257 npar_mann_whitney (struct lexer *lexer,
1258                struct dataset *ds,
1259                struct npar_specs *specs)
1260 {
1261   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1262   struct npar_test *nt = &tp->parent;
1263
1264   nt->insert_variables = n_sample_insert_variables;
1265   nt->execute = mann_whitney_execute;
1266
1267   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1268                                     tp, specs->pool))
1269     return 0;
1270
1271   specs->n_tests++;
1272   specs->test = pool_realloc (specs->pool,
1273                               specs->test,
1274                               sizeof (*specs->test) * specs->n_tests);
1275   specs->test[specs->n_tests - 1] = nt;
1276
1277   return 1;
1278 }
1279
1280
1281 static int
1282 npar_median (struct lexer *lexer,
1283              struct dataset *ds,
1284              struct npar_specs *specs)
1285 {
1286   struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
1287   struct n_sample_test *tp = &mt->parent;
1288   struct npar_test *nt = &tp->parent;
1289
1290   mt->median = SYSMIS;
1291
1292   if (lex_match (lexer, T_LPAREN) && lex_force_num (lexer))
1293     {
1294       mt->median = lex_number (lexer);
1295       lex_get (lexer);
1296       if (! lex_force_match (lexer, T_RPAREN))
1297         return 0;
1298     }
1299
1300   lex_match (lexer, T_EQUALS);
1301
1302   nt->insert_variables = n_sample_insert_variables;
1303   nt->execute = median_execute;
1304
1305   if (!parse_n_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_sign (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 = sign_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_mcnemar (struct lexer *lexer, struct dataset *ds,
1344            struct npar_specs *specs)
1345 {
1346   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1347   struct npar_test *nt = &tp->parent;
1348
1349   nt->execute = mcnemar_execute;
1350
1351   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1352                                       tp, specs->pool))
1353     return 0;
1354
1355   specs->n_tests++;
1356   specs->test = pool_realloc (specs->pool,
1357                               specs->test,
1358                               sizeof (*specs->test) * specs->n_tests);
1359   specs->test[specs->n_tests - 1] = nt;
1360
1361   return 1;
1362 }
1363
1364
1365 static int
1366 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
1367                       struct npar_specs *specs)
1368 {
1369   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1370   struct npar_test *nt = &tp->parent;
1371
1372   nt->insert_variables = n_sample_insert_variables;
1373
1374   nt->execute = jonckheere_terpstra_execute;
1375
1376   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1377                                       tp, specs->pool))
1378     return 0;
1379
1380   specs->n_tests++;
1381   specs->test = pool_realloc (specs->pool,
1382                               specs->test,
1383                               sizeof (*specs->test) * specs->n_tests);
1384   specs->test[specs->n_tests - 1] = nt;
1385
1386   return 1;
1387 }
1388
1389 static int
1390 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1391                       struct npar_specs *specs)
1392 {
1393   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1394   struct npar_test *nt = &tp->parent;
1395
1396   nt->insert_variables = n_sample_insert_variables;
1397
1398   nt->execute = kruskal_wallis_execute;
1399
1400   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1401                                       tp, specs->pool))
1402     return 0;
1403
1404   specs->n_tests++;
1405   specs->test = pool_realloc (specs->pool,
1406                               specs->test,
1407                               sizeof (*specs->test) * specs->n_tests);
1408   specs->test[specs->n_tests - 1] = nt;
1409
1410   return 1;
1411 }
1412
1413 static void
1414 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1415 {
1416   size_t hash = hash_pointer (var, 0);
1417   struct hmapx_node *node;
1418   const struct variable *v = NULL;
1419
1420   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1421     {
1422       if (v == var)
1423         return ;
1424     }
1425
1426   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1427 }
1428
1429 /* Insert the variables for TEST into VAR_MAP */
1430 static void
1431 one_sample_insert_variables (const struct npar_test *test,
1432                              struct hmapx *var_map)
1433 {
1434   int i;
1435   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1436
1437   for (i = 0 ; i < ost->n_vars ; ++i)
1438     insert_variable_into_map (var_map, ost->vars[i]);
1439 }
1440
1441
1442 static void
1443 two_sample_insert_variables (const struct npar_test *test,
1444                              struct hmapx *var_map)
1445 {
1446   int i;
1447   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1448
1449   for (i = 0 ; i < tst->n_pairs ; ++i)
1450     {
1451       variable_pair *pair = &tst->pairs[i];
1452
1453       insert_variable_into_map (var_map, (*pair)[0]);
1454       insert_variable_into_map (var_map, (*pair)[1]);
1455     }
1456 }
1457
1458 static void
1459 n_sample_insert_variables (const struct npar_test *test,
1460                            struct hmapx *var_map)
1461 {
1462   int i;
1463   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1464
1465   for (i = 0 ; i < tst->n_vars ; ++i)
1466     insert_variable_into_map (var_map, tst->vars[i]);
1467
1468   insert_variable_into_map (var_map, tst->indep_var);
1469 }
1470
1471
1472 static int
1473 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1474 {
1475   if (lex_match_id (lexer, "EXACT"))
1476     {
1477       specs->exact = true;
1478       specs->timer = 0.0;
1479       if (lex_match_id (lexer, "TIMER"))
1480         {
1481           specs->timer = 5.0;
1482
1483           if (lex_match (lexer, T_LPAREN))
1484             {
1485               if (lex_force_num (lexer))
1486                 {
1487                   specs->timer = lex_number (lexer);
1488                   lex_get (lexer);
1489                 }
1490               if (lex_force_match (lexer, T_RPAREN))
1491                 return 0;
1492             }
1493         }
1494     }
1495
1496   return 1;
1497 }