a572e09f56cbbcccc2b0bba90dc150db5f239cf7
[pspp-builds.git] / 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/dictionary.h"
28 #include "data/procedure.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
899 static bool
900 parse_two_sample_related_test (struct lexer *lexer,
901                                const struct dictionary *dict,
902                                struct two_sample_test *test_parameters,
903                                struct pool *pool
904                                )
905 {
906   int n = 0;
907   bool paired = false;
908   bool with = false;
909   const struct variable **vlist1;
910   size_t n_vlist1;
911
912   const struct variable **vlist2;
913   size_t n_vlist2;
914
915   test_parameters->parent.insert_variables = two_sample_insert_variables;
916
917   if (!parse_variables_const_pool (lexer, pool,
918                                    dict,
919                                    &vlist1, &n_vlist1,
920                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
921     return false;
922
923   if ( lex_match (lexer, T_WITH))
924     {
925       with = true;
926       if ( !parse_variables_const_pool (lexer, pool, dict,
927                                         &vlist2, &n_vlist2,
928                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
929         return false;
930
931       paired = (lex_match (lexer, T_LPAREN) &&
932                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
933     }
934
935
936   if ( with )
937     {
938       if (paired)
939         {
940           if ( n_vlist1 != n_vlist2)
941             msg (SE, _("PAIRED was specified but the number of variables "
942                        "preceding WITH (%zu) did not match the number "
943                        "following (%zu)."), n_vlist1, n_vlist2);
944
945           test_parameters->n_pairs = n_vlist1 ;
946         }
947       else
948         {
949           test_parameters->n_pairs = n_vlist1 * n_vlist2;
950         }
951     }
952   else
953     {
954       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
955     }
956
957   test_parameters->pairs =
958     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
959
960   if ( with )
961     {
962       if (paired)
963         {
964           int i;
965           assert (n_vlist1 == n_vlist2);
966           for ( i = 0 ; i < n_vlist1; ++i )
967             {
968               test_parameters->pairs[n][1] = vlist1[i];
969               test_parameters->pairs[n][0] = vlist2[i];
970               n++;
971             }
972         }
973       else
974         {
975           int i,j;
976           for ( i = 0 ; i < n_vlist1; ++i )
977             {
978               for ( j = 0 ; j < n_vlist2; ++j )
979                 {
980                   test_parameters->pairs[n][1] = vlist1[i];
981                   test_parameters->pairs[n][0] = vlist2[j];
982                   n++;
983                 }
984             }
985         }
986     }
987   else
988     {
989       int i,j;
990       for ( i = 0 ; i < n_vlist1 - 1; ++i )
991         {
992           for ( j = i + 1 ; j < n_vlist1; ++j )
993             {
994               assert ( n < test_parameters->n_pairs);
995               test_parameters->pairs[n][1] = vlist1[i];
996               test_parameters->pairs[n][0] = vlist1[j];
997               n++;
998             }
999         }
1000     }
1001
1002   assert ( n == test_parameters->n_pairs);
1003
1004   return true;
1005 }
1006
1007
1008 static bool
1009 parse_n_sample_related_test (struct lexer *lexer,
1010                              const struct dictionary *dict,
1011                              struct n_sample_test *nst,
1012                              struct pool *pool
1013                              )
1014 {
1015   if (!parse_variables_const_pool (lexer, pool,
1016                                    dict,
1017                                    &nst->vars, &nst->n_vars,
1018                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1019     return false;
1020
1021   if ( ! lex_force_match (lexer, T_BY))
1022     return false;
1023
1024   nst->indep_var = parse_variable_const (lexer, dict);
1025
1026   if ( ! lex_force_match (lexer, T_LPAREN))
1027     return false;
1028
1029   value_init (&nst->val1, var_get_width (nst->indep_var));
1030   if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
1031     {
1032       value_destroy (&nst->val1, var_get_width (nst->indep_var));
1033       return false;
1034     }
1035
1036   lex_match (lexer, T_COMMA);
1037
1038   value_init (&nst->val2, var_get_width (nst->indep_var));
1039   if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
1040     {
1041       value_destroy (&nst->val2, var_get_width (nst->indep_var));
1042       return false;
1043     }
1044
1045   if ( ! lex_force_match (lexer, T_RPAREN))
1046     return false;
1047
1048   return true;
1049 }
1050
1051 static int
1052 npar_wilcoxon (struct lexer *lexer,
1053                struct dataset *ds,
1054                struct npar_specs *specs )
1055 {
1056
1057
1058   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1059   struct npar_test *nt = &tp->parent;
1060   nt->execute = wilcoxon_execute;
1061
1062   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1063                                       tp, specs->pool) )
1064     return 0;
1065
1066   specs->n_tests++;
1067   specs->test = pool_realloc (specs->pool,
1068                               specs->test,
1069                               sizeof (*specs->test) * specs->n_tests);
1070   specs->test[specs->n_tests - 1] = nt;
1071
1072   return 1;
1073 }
1074
1075
1076 static int
1077 npar_mann_whitney (struct lexer *lexer,
1078                struct dataset *ds,
1079                struct npar_specs *specs )
1080 {
1081   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1082   struct npar_test *nt = &tp->parent;
1083
1084   nt->insert_variables = n_sample_insert_variables;
1085   nt->execute = mann_whitney_execute;
1086
1087   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1088                                     tp, specs->pool) )
1089     return 0;
1090
1091   specs->n_tests++;
1092   specs->test = pool_realloc (specs->pool,
1093                               specs->test,
1094                               sizeof (*specs->test) * specs->n_tests);
1095   specs->test[specs->n_tests - 1] = nt;
1096
1097   return 1;
1098 }
1099
1100
1101 static int
1102 npar_sign (struct lexer *lexer, struct dataset *ds,
1103            struct npar_specs *specs)
1104 {
1105   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1106   struct npar_test *nt = &tp->parent;
1107
1108   nt->execute = sign_execute;
1109
1110   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1111                                       tp, specs->pool) )
1112     return 0;
1113
1114   specs->n_tests++;
1115   specs->test = pool_realloc (specs->pool,
1116                               specs->test,
1117                               sizeof (*specs->test) * specs->n_tests);
1118   specs->test[specs->n_tests - 1] = nt;
1119
1120   return 1;
1121 }
1122
1123 static int
1124 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1125                       struct npar_specs *specs)
1126 {
1127   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1128   struct npar_test *nt = &tp->parent;
1129
1130   nt->insert_variables = n_sample_insert_variables;
1131
1132   nt->execute = kruskal_wallis_execute;
1133
1134   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1135                                       tp, specs->pool) )
1136     return 0;
1137
1138   specs->n_tests++;
1139   specs->test = pool_realloc (specs->pool,
1140                               specs->test,
1141                               sizeof (*specs->test) * specs->n_tests);
1142   specs->test[specs->n_tests - 1] = nt;
1143
1144   return 1;
1145 }
1146
1147 static void
1148 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1149 {
1150   size_t hash = hash_pointer (var, 0);
1151   struct hmapx_node *node;
1152   const struct variable *v = NULL;
1153       
1154   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1155     {
1156       if ( v == var)
1157         return ;
1158     }
1159
1160   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1161 }
1162
1163 /* Insert the variables for TEST into VAR_MAP */
1164 static void
1165 one_sample_insert_variables (const struct npar_test *test,
1166                              struct hmapx *var_map)
1167 {
1168   int i;
1169   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1170
1171   for ( i = 0 ; i < ost->n_vars ; ++i )
1172     insert_variable_into_map (var_map, ost->vars[i]);
1173 }
1174
1175
1176 static void
1177 two_sample_insert_variables (const struct npar_test *test,
1178                              struct hmapx *var_map)
1179 {
1180   int i;
1181   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1182
1183   for ( i = 0 ; i < tst->n_pairs ; ++i )
1184     {
1185       variable_pair *pair = &tst->pairs[i];
1186
1187       insert_variable_into_map (var_map, (*pair)[0]);
1188       insert_variable_into_map (var_map, (*pair)[1]);
1189     }
1190 }
1191
1192 static void 
1193 n_sample_insert_variables (const struct npar_test *test,
1194                            struct hmapx *var_map)
1195 {
1196   int i;
1197   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1198
1199   for ( i = 0 ; i < tst->n_vars ; ++i )
1200     insert_variable_into_map (var_map, tst->vars[i]);
1201
1202   insert_variable_into_map (var_map, tst->indep_var);
1203 }
1204
1205
1206 static int
1207 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1208 {
1209   if ( lex_match_id (lexer, "EXACT") )
1210     {
1211       specs->exact = true;
1212       specs->timer = 0.0;
1213       if (lex_match_id (lexer, "TIMER"))
1214         {
1215           specs->timer = 5.0;
1216
1217           if ( lex_match (lexer, T_LPAREN))
1218             {
1219               if ( lex_force_num (lexer) )
1220                 {
1221                   specs->timer = lex_number (lexer);
1222                   lex_get (lexer);
1223                 }
1224               lex_force_match (lexer, T_RPAREN);
1225             }
1226         }
1227     }
1228
1229   return 1;
1230 }