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