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