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