3f2bae298ef45669cd6331f896c160d0988ff465
[pspp-builds.git] / src / language / stats / npar.c
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009, 2010 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 #include "npar-summary.h"
21
22 #include <stdlib.h>
23 #include <math.h>
24
25 #include "xalloc.h"
26
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/settings.h>
33 #include <data/variable.h>
34 #include <libpspp/array.h>
35 #include <libpspp/assertion.h>
36 #include <libpspp/cast.h>
37 #include <libpspp/hmapx.h>
38 #include <libpspp/hash-functions.h>
39 #include <libpspp/message.h>
40 #include <libpspp/pool.h>
41 #include <libpspp/str.h>
42 #include <libpspp/taint.h>
43 #include <language/command.h>
44 #include <language/lexer/lexer.h>
45 #include <language/lexer/variable-parser.h>
46 #include <language/lexer/value-parser.h>
47 #include <language/stats/binomial.h>
48 #include <language/stats/chisquare.h>
49 #include <language/stats/cochran.h>
50 #include <language/stats/runs.h>
51 #include <language/stats/friedman.h>
52 #include <language/stats/kruskal-wallis.h>
53 #include <language/stats/mann-whitney.h>
54 #include <language/stats/wilcoxon.h>
55 #include <language/stats/sign.h>
56 #include <math/moments.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_hyphenated_word (lexer, "K-W") ||
262                lex_match_hyphenated_word (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_hyphenated_word (lexer, "M-W") ||
280                lex_match_hyphenated_word (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 (lexer, T_SLASH) )
763     {
764       if ( lex_match_id (lexer, "EXPECTED") )
765         {
766           lex_force_match (lexer, T_EQUALS);
767           if ( ! lex_match_id (lexer, "EQUAL") )
768             {
769               double f;
770               int n;
771               while ( lex_is_number (lexer) )
772                 {
773                   int i;
774                   n = 1;
775                   f = lex_number (lexer);
776                   lex_get (lexer);
777                   if ( lex_match (lexer, T_ASTERISK))
778                     {
779                       n = f;
780                       f = lex_number (lexer);
781                       lex_get (lexer);
782                     }
783                   lex_match (lexer, T_COMMA);
784
785                   cstp->n_expected += n;
786                   cstp->expected = pool_realloc (specs->pool,
787                                                  cstp->expected,
788                                                  sizeof (double) *
789                                                  cstp->n_expected);
790                   for ( i = cstp->n_expected - n ;
791                         i < cstp->n_expected;
792                         ++i )
793                     cstp->expected[i] = f;
794
795                 }
796             }
797         }
798       else
799         retval = 3;
800     }
801
802   if ( cstp->ranged && cstp->n_expected > 0 &&
803        cstp->n_expected != cstp->hi - cstp->lo + 1 )
804     {
805       msg (ME,
806           _("%d expected values were given, but the specified "
807             "range (%d-%d) requires exactly %d values."),
808           cstp->n_expected, cstp->lo, cstp->hi,
809           cstp->hi - cstp->lo +1);
810       return 0;
811     }
812
813   specs->n_tests++;
814   specs->test = pool_realloc (specs->pool,
815                               specs->test,
816                               sizeof (*specs->test) * specs->n_tests);
817
818   specs->test[specs->n_tests - 1] = nt;
819
820   return retval;
821 }
822
823
824 static int
825 npar_binomial (struct lexer *lexer, struct dataset *ds,
826                struct npar_specs *specs)
827 {
828   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
829   struct one_sample_test *tp = &btp->parent;
830   struct npar_test *nt = &tp->parent;
831   bool equals;
832
833   nt->execute = binomial_execute;
834   nt->insert_variables = one_sample_insert_variables;
835
836   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
837
838   btp->p = 0.5;
839
840   if ( lex_match (lexer, T_LPAREN) )
841     {
842       equals = false;
843       if ( lex_force_num (lexer) )
844         {
845           btp->p = lex_number (lexer);
846           lex_get (lexer);
847           lex_force_match (lexer, T_RPAREN);
848         }
849       else
850         return 0;
851     }
852   else
853     equals = true;
854
855   if (equals || lex_match (lexer, T_EQUALS) )
856     {
857       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
858                                       &tp->vars, &tp->n_vars,
859                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
860         {
861           if (lex_match (lexer, T_LPAREN))
862             {
863               lex_force_num (lexer);
864               btp->category1 = lex_number (lexer);
865               lex_get (lexer);
866               if ( lex_match (lexer, T_COMMA))
867                 {
868                   if ( ! lex_force_num (lexer) ) return 2;
869                   btp->category2 = lex_number (lexer);
870                   lex_get (lexer);
871                 }
872               else
873                 {
874                   btp->cutpoint = btp->category1;
875                 }
876
877               lex_force_match (lexer, T_RPAREN);
878             }
879         }
880       else
881         return 2;
882
883     }
884
885   specs->n_tests++;
886   specs->test = pool_realloc (specs->pool,
887                               specs->test,
888                               sizeof (*specs->test) * specs->n_tests);
889
890   specs->test[specs->n_tests - 1] = nt;
891
892   return 1;
893 }
894
895
896 static bool
897 parse_two_sample_related_test (struct lexer *lexer,
898                                     const struct dictionary *dict,
899                                     struct two_sample_test *test_parameters,
900                                     struct pool *pool
901                                     );
902
903
904 static bool
905 parse_two_sample_related_test (struct lexer *lexer,
906                                const struct dictionary *dict,
907                                struct two_sample_test *test_parameters,
908                                struct pool *pool
909                                )
910 {
911   int n = 0;
912   bool paired = false;
913   bool with = false;
914   const struct variable **vlist1;
915   size_t n_vlist1;
916
917   const struct variable **vlist2;
918   size_t n_vlist2;
919
920   test_parameters->parent.insert_variables = two_sample_insert_variables;
921
922   if (!parse_variables_const_pool (lexer, pool,
923                                    dict,
924                                    &vlist1, &n_vlist1,
925                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
926     return false;
927
928   if ( lex_match (lexer, T_WITH))
929     {
930       with = true;
931       if ( !parse_variables_const_pool (lexer, pool, dict,
932                                         &vlist2, &n_vlist2,
933                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
934         return false;
935
936       paired = (lex_match (lexer, T_LPAREN) &&
937                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
938     }
939
940
941   if ( with )
942     {
943       if (paired)
944         {
945           if ( n_vlist1 != n_vlist2)
946             msg (SE, _("PAIRED was specified but the number of variables "
947                        "preceding WITH (%zu) did not match the number "
948                        "following (%zu)."), n_vlist1, n_vlist2);
949
950           test_parameters->n_pairs = n_vlist1 ;
951         }
952       else
953         {
954           test_parameters->n_pairs = n_vlist1 * n_vlist2;
955         }
956     }
957   else
958     {
959       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
960     }
961
962   test_parameters->pairs =
963     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
964
965   if ( with )
966     {
967       if (paired)
968         {
969           int i;
970           assert (n_vlist1 == n_vlist2);
971           for ( i = 0 ; i < n_vlist1; ++i )
972             {
973               test_parameters->pairs[n][1] = vlist1[i];
974               test_parameters->pairs[n][0] = vlist2[i];
975               n++;
976             }
977         }
978       else
979         {
980           int i,j;
981           for ( i = 0 ; i < n_vlist1; ++i )
982             {
983               for ( j = 0 ; j < n_vlist2; ++j )
984                 {
985                   test_parameters->pairs[n][1] = vlist1[i];
986                   test_parameters->pairs[n][0] = vlist2[j];
987                   n++;
988                 }
989             }
990         }
991     }
992   else
993     {
994       int i,j;
995       for ( i = 0 ; i < n_vlist1 - 1; ++i )
996         {
997           for ( j = i + 1 ; j < n_vlist1; ++j )
998             {
999               assert ( n < test_parameters->n_pairs);
1000               test_parameters->pairs[n][1] = vlist1[i];
1001               test_parameters->pairs[n][0] = vlist1[j];
1002               n++;
1003             }
1004         }
1005     }
1006
1007   assert ( n == test_parameters->n_pairs);
1008
1009   return true;
1010 }
1011
1012
1013 static bool
1014 parse_n_sample_related_test (struct lexer *lexer,
1015                              const struct dictionary *dict,
1016                              struct n_sample_test *nst,
1017                              struct pool *pool
1018                              )
1019 {
1020   if (!parse_variables_const_pool (lexer, pool,
1021                                    dict,
1022                                    &nst->vars, &nst->n_vars,
1023                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1024     return false;
1025
1026   if ( ! lex_force_match (lexer, T_BY))
1027     return false;
1028
1029   nst->indep_var = parse_variable_const (lexer, dict);
1030
1031   if ( ! lex_force_match (lexer, T_LPAREN))
1032     return false;
1033
1034   value_init (&nst->val1, var_get_width (nst->indep_var));
1035   if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
1036     {
1037       value_destroy (&nst->val1, var_get_width (nst->indep_var));
1038       return false;
1039     }
1040
1041   lex_match (lexer, T_COMMA);
1042
1043   value_init (&nst->val2, var_get_width (nst->indep_var));
1044   if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
1045     {
1046       value_destroy (&nst->val2, var_get_width (nst->indep_var));
1047       return false;
1048     }
1049
1050   if ( ! lex_force_match (lexer, T_RPAREN))
1051     return false;
1052
1053   return true;
1054 }
1055
1056 static int
1057 npar_wilcoxon (struct lexer *lexer,
1058                struct dataset *ds,
1059                struct npar_specs *specs )
1060 {
1061
1062
1063   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1064   struct npar_test *nt = &tp->parent;
1065   nt->execute = wilcoxon_execute;
1066
1067   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1068                                       tp, specs->pool) )
1069     return 0;
1070
1071   specs->n_tests++;
1072   specs->test = pool_realloc (specs->pool,
1073                               specs->test,
1074                               sizeof (*specs->test) * specs->n_tests);
1075   specs->test[specs->n_tests - 1] = nt;
1076
1077   return 1;
1078 }
1079
1080
1081 static int
1082 npar_mann_whitney (struct lexer *lexer,
1083                struct dataset *ds,
1084                struct npar_specs *specs )
1085 {
1086   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1087   struct npar_test *nt = &tp->parent;
1088
1089   nt->insert_variables = n_sample_insert_variables;
1090   nt->execute = mann_whitney_execute;
1091
1092   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1093                                     tp, specs->pool) )
1094     return 0;
1095
1096   specs->n_tests++;
1097   specs->test = pool_realloc (specs->pool,
1098                               specs->test,
1099                               sizeof (*specs->test) * specs->n_tests);
1100   specs->test[specs->n_tests - 1] = nt;
1101
1102   return 1;
1103 }
1104
1105
1106 static int
1107 npar_sign (struct lexer *lexer, struct dataset *ds,
1108            struct npar_specs *specs)
1109 {
1110   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1111   struct npar_test *nt = &tp->parent;
1112
1113   nt->execute = sign_execute;
1114
1115   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1116                                       tp, specs->pool) )
1117     return 0;
1118
1119   specs->n_tests++;
1120   specs->test = pool_realloc (specs->pool,
1121                               specs->test,
1122                               sizeof (*specs->test) * specs->n_tests);
1123   specs->test[specs->n_tests - 1] = nt;
1124
1125   return 1;
1126 }
1127
1128 static int
1129 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1130                       struct npar_specs *specs)
1131 {
1132   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1133   struct npar_test *nt = &tp->parent;
1134
1135   nt->insert_variables = n_sample_insert_variables;
1136
1137   nt->execute = kruskal_wallis_execute;
1138
1139   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1140                                       tp, specs->pool) )
1141     return 0;
1142
1143   specs->n_tests++;
1144   specs->test = pool_realloc (specs->pool,
1145                               specs->test,
1146                               sizeof (*specs->test) * specs->n_tests);
1147   specs->test[specs->n_tests - 1] = nt;
1148
1149   return 1;
1150 }
1151
1152 static void
1153 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1154 {
1155   size_t hash = hash_pointer (var, 0);
1156   struct hmapx_node *node;
1157   const struct variable *v = NULL;
1158       
1159   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1160     {
1161       if ( v == var)
1162         return ;
1163     }
1164
1165   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1166 }
1167
1168 /* Insert the variables for TEST into VAR_MAP */
1169 static void
1170 one_sample_insert_variables (const struct npar_test *test,
1171                              struct hmapx *var_map)
1172 {
1173   int i;
1174   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1175
1176   for ( i = 0 ; i < ost->n_vars ; ++i )
1177     insert_variable_into_map (var_map, ost->vars[i]);
1178 }
1179
1180
1181 static void
1182 two_sample_insert_variables (const struct npar_test *test,
1183                              struct hmapx *var_map)
1184 {
1185   int i;
1186   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1187
1188   for ( i = 0 ; i < tst->n_pairs ; ++i )
1189     {
1190       variable_pair *pair = &tst->pairs[i];
1191
1192       insert_variable_into_map (var_map, (*pair)[0]);
1193       insert_variable_into_map (var_map, (*pair)[1]);
1194     }
1195 }
1196
1197 static void 
1198 n_sample_insert_variables (const struct npar_test *test,
1199                            struct hmapx *var_map)
1200 {
1201   int i;
1202   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1203
1204   for ( i = 0 ; i < tst->n_vars ; ++i )
1205     insert_variable_into_map (var_map, tst->vars[i]);
1206
1207   insert_variable_into_map (var_map, tst->indep_var);
1208 }
1209
1210
1211 static int
1212 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1213 {
1214   if ( lex_match_id (lexer, "EXACT") )
1215     {
1216       specs->exact = true;
1217       specs->timer = 0.0;
1218       if (lex_match_id (lexer, "TIMER"))
1219         {
1220           specs->timer = 5.0;
1221
1222           if ( lex_match (lexer, T_LPAREN))
1223             {
1224               if ( lex_force_num (lexer) )
1225                 {
1226                   specs->timer = lex_number (lexer);
1227                   lex_get (lexer);
1228                 }
1229               lex_force_match (lexer, T_RPAREN);
1230             }
1231         }
1232     }
1233
1234   return 1;
1235 }