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