ad1724da151fbdd1313570b1a59d11bead7f4d3a
[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 #include <data/case.h>
27 #include <data/casegrouper.h>
28 #include <data/casereader.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <language/lexer/value-parser.h>
37 #include <language/stats/binomial.h>
38 #include <language/stats/chisquare.h>
39 #include <language/stats/wilcoxon.h>
40 #include <language/stats/sign.h>
41 #include <libpspp/assertion.h>
42 #include <libpspp/cast.h>
43 #include <libpspp/hash.h>
44 #include <libpspp/message.h>
45 #include <libpspp/pool.h>
46 #include <libpspp/str.h>
47 #include <libpspp/taint.h>
48 #include <math/moments.h>
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52
53 /* Settings for subcommand specifiers. */
54 enum missing_type
55   {
56     MISS_ANALYSIS,
57     MISS_LISTWISE,
58   };
59
60 /* Array indices for STATISTICS subcommand. */
61 enum
62   {
63     NPAR_ST_DESCRIPTIVES = 0,
64     NPAR_ST_QUARTILES = 1,
65     NPAR_ST_ALL = 2,
66     NPAR_ST_count
67   };
68
69 /* NPAR TESTS structure. */
70 struct cmd_npar_tests
71   {
72     /* Count variables indicating how many
73        of the subcommands have been given. */
74     int chisquare;
75     int binomial;
76     int wilcoxon;
77     int sign;
78     int missing;
79     int method;
80     int statistics;
81
82     /* How missing values should be treated */
83     long miss;
84
85     /* Which statistics have been requested */
86     int a_statistics[NPAR_ST_count];
87   };
88
89
90 struct npar_specs
91 {
92   struct pool *pool;
93   struct npar_test **test;
94   size_t n_tests;
95
96   const struct variable **vv; /* Compendium of all variables
97                                   (those mentioned on ANY subcommand */
98   int n_vars; /* Number of variables in vv */
99
100   enum mv_class filter;    /* Missing values to filter. */
101
102   bool descriptives;       /* Descriptive statistics should be calculated */
103   bool quartiles;          /* Quartiles should be calculated */
104
105   bool exact;  /* Whether exact calculations have been requested */
106   double timer;   /* Maximum time (in minutes) to wait for exact calculations */
107 };
108
109
110 /* Prototype for custom subcommands of NPAR TESTS. */
111 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
112 static int npar_binomial (struct lexer *, struct dataset *,  struct npar_specs *);
113 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
114 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
115 static int npar_method (struct lexer *, struct npar_specs *);
116
117 /* Command parsing functions. */
118 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
119                              struct npar_specs *npar_specs );
120
121 static int
122 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
123                   struct npar_specs *nps)
124 {
125   npt->chisquare = 0;
126   npt->binomial = 0;
127   npt->wilcoxon = 0;
128   npt->sign = 0;
129   npt->missing = 0;
130   npt->miss = MISS_ANALYSIS;
131   npt->method = 0;
132   npt->statistics = 0;
133   memset (npt->a_statistics, 0, sizeof npt->a_statistics);
134   for (;;)
135     {
136       if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
137         {
138           lex_match (lexer, '=');
139           npt->chisquare++;
140           switch (npar_chisquare (lexer, ds, nps))
141             {
142             case 0:
143               goto lossage;
144             case 1:
145               break;
146             case 2:
147               lex_error (lexer, NULL);
148               goto lossage;
149             default:
150               NOT_REACHED ();
151             }
152         }
153       else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
154         {
155           lex_match (lexer, '=');
156           npt->binomial++;
157           switch (npar_binomial (lexer, ds, nps))
158             {
159             case 0:
160               goto lossage;
161             case 1:
162               break;
163             case 2:
164               lex_error (lexer, NULL);
165               goto lossage;
166             default:
167               NOT_REACHED ();
168             }
169         }
170       else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
171         {
172           lex_match (lexer, '=');
173           npt->wilcoxon++;
174           switch (npar_wilcoxon (lexer, ds, nps))
175             {
176             case 0:
177               goto lossage;
178             case 1:
179               break;
180             case 2:
181               lex_error (lexer, NULL);
182               goto lossage;
183             default:
184               NOT_REACHED ();
185             }
186         }
187       else if (lex_match_hyphenated_word (lexer, "SIGN"))
188         {
189           lex_match (lexer, '=');
190           npt->sign++;
191           switch (npar_sign (lexer, ds, nps))
192             {
193             case 0:
194               goto lossage;
195             case 1:
196               break;
197             case 2:
198               lex_error (lexer, NULL);
199               goto lossage;
200             default:
201               NOT_REACHED ();
202             }
203         }
204       else if (lex_match_hyphenated_word (lexer, "MISSING"))
205         {
206           lex_match (lexer, '=');
207           npt->missing++;
208           if (npt->missing > 1)
209             {
210               msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
211               goto lossage;
212             }
213           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
214             {
215               if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
216                 npt->miss = MISS_ANALYSIS;
217               else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
218                 npt->miss = MISS_LISTWISE;
219               else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
220                 nps->filter = MV_SYSTEM;
221               else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
222                 nps->filter = MV_ANY;
223               else
224                 {
225                   lex_error (lexer, NULL);
226                   goto lossage;
227                 }
228               lex_match (lexer, ',');
229             }
230         }
231       else if (lex_match_hyphenated_word (lexer, "METHOD"))
232         {
233           lex_match (lexer, '=');
234           npt->method++;
235           if (npt->method > 1)
236             {
237               msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
238               goto lossage;
239             }
240           switch (npar_method (lexer, nps))
241             {
242             case 0:
243               goto lossage;
244             case 1:
245               break;
246             case 2:
247               lex_error (lexer, NULL);
248               goto lossage;
249             default:
250               NOT_REACHED ();
251             }
252         }
253       else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
254         {
255           lex_match (lexer, '=');
256           npt->statistics++;
257           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
258             {
259               if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
260                 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
261               else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
262                 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
263               else if (lex_match (lexer, T_ALL))
264                 npt->a_statistics[NPAR_ST_ALL] = 1;
265               else
266                 {
267                   lex_error (lexer, NULL);
268                   goto lossage;
269                 }
270               lex_match (lexer, ',');
271             }
272         }
273       else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
274         {
275           lex_match (lexer, '=');
276           if (lex_match_id (lexer, "COMPATIBLE"))
277             settings_set_cmd_algorithm (COMPATIBLE);
278           else if (lex_match_id (lexer, "ENHANCED"))
279             settings_set_cmd_algorithm (ENHANCED);
280           }
281         if (!lex_match (lexer, '/'))
282           break;
283       }
284
285     if (lex_token (lexer) != '.')
286       {
287         lex_error (lexer, _ ("expecting end of command"));
288         goto lossage;
289       }
290
291   return true;
292
293 lossage:
294   return false;
295 }
296
297
298
299
300 static void one_sample_insert_variables (const struct npar_test *test,
301                                          struct const_hsh_table *variables);
302
303 static void two_sample_insert_variables (const struct npar_test *test,
304                                          struct const_hsh_table *variables);
305
306
307 static void n_sample_insert_variables (const struct npar_test *test,
308                                          struct const_hsh_table *variables);
309
310
311
312 static void
313 npar_execute (struct casereader *input,
314              const struct npar_specs *specs,
315              const struct dataset *ds)
316 {
317   int t;
318   struct descriptives *summary_descriptives = NULL;
319
320   for ( t = 0 ; t < specs->n_tests; ++t )
321     {
322       const struct npar_test *test = specs->test[t];
323       if ( NULL == test->execute )
324         {
325           msg (SW, _ ("NPAR subcommand not currently implemented."));
326           continue;
327         }
328       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
329     }
330
331   if ( specs->descriptives )
332     {
333       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
334                                        specs->n_vars);
335
336       npar_summary_calc_descriptives (summary_descriptives,
337                                       casereader_clone (input),
338                                       dataset_dict (ds),
339                                       specs->vv, specs->n_vars,
340                                       specs->filter);
341     }
342
343   if ( (specs->descriptives || specs->quartiles)
344        && !taint_has_tainted_successor (casereader_get_taint (input)) )
345     do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
346
347   free (summary_descriptives);
348   casereader_destroy (input);
349 }
350
351
352 int
353 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
354 {
355   struct cmd_npar_tests cmd;
356   bool ok;
357   int i;
358   struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
359   struct const_hsh_table *var_hash;
360   struct casegrouper *grouper;
361   struct casereader *input, *group;
362
363   npar_specs.pool = pool_create ();
364   npar_specs.filter = MV_ANY;
365
366   var_hash = const_hsh_create_pool (npar_specs.pool, 0,
367                                     compare_vars_by_name, hash_var_by_name,
368                                     NULL, NULL);
369
370   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
371     {
372       pool_destroy (npar_specs.pool);
373       return CMD_FAILURE;
374     }
375
376   for (i = 0; i < npar_specs.n_tests; ++i )
377     {
378       const struct npar_test *test = npar_specs.test[i];
379       test->insert_variables (test, var_hash);
380     }
381
382   npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
383   npar_specs.n_vars = const_hsh_count (var_hash);
384
385   if ( cmd.statistics )
386     {
387       int i;
388
389       for ( i = 0 ; i < NPAR_ST_count; ++i )
390         {
391           if ( cmd.a_statistics[i] )
392             {
393               switch ( i )
394                 {
395                 case NPAR_ST_DESCRIPTIVES:
396                   npar_specs.descriptives = true;
397                   break;
398                 case NPAR_ST_QUARTILES:
399                   npar_specs.quartiles = true;
400                   break;
401                 case NPAR_ST_ALL:
402                   npar_specs.quartiles = true;
403                   npar_specs.descriptives = true;
404                   break;
405                 default:
406                   NOT_REACHED ();
407                 };
408             }
409         }
410     }
411
412   input = proc_open (ds);
413   if ( cmd.miss == MISS_LISTWISE )
414     {
415       input = casereader_create_filter_missing (input,
416                                                 npar_specs.vv,
417                                                 npar_specs.n_vars,
418                                                 npar_specs.filter,
419                                                 NULL, NULL);
420     }
421
422
423   grouper = casegrouper_create_splits (input, dataset_dict (ds));
424   while (casegrouper_get_next_group (grouper, &group))
425     npar_execute (group, &npar_specs, ds);
426   ok = casegrouper_destroy (grouper);
427   ok = proc_commit (ds) && ok;
428
429   const_hsh_destroy (var_hash);
430
431   pool_destroy (npar_specs.pool);
432
433   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
434 }
435
436 static int
437 npar_chisquare (struct lexer *lexer, struct dataset *ds,
438                 struct npar_specs *specs)
439 {
440   struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
441   struct one_sample_test *tp = &cstp->parent;
442   struct npar_test *nt = &tp->parent;
443
444   nt->execute = chisquare_execute;
445   nt->insert_variables = one_sample_insert_variables;
446
447   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
448                                    &tp->vars, &tp->n_vars,
449                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
450     {
451       return 2;
452     }
453
454   cstp->ranged = false;
455
456   if ( lex_match (lexer, '('))
457     {
458       cstp->ranged = true;
459       if ( ! lex_force_num (lexer)) return 0;
460       cstp->lo = lex_integer (lexer);
461       lex_get (lexer);
462       lex_force_match (lexer, ',');
463       if (! lex_force_num (lexer) ) return 0;
464       cstp->hi = lex_integer (lexer);
465       if ( cstp->lo >= cstp->hi )
466         {
467           msg (ME,
468               _ ("The specified value of HI (%d) is "
469                 "lower than the specified value of LO (%d)"),
470               cstp->hi, cstp->lo);
471           return 0;
472         }
473       lex_get (lexer);
474       if (! lex_force_match (lexer, ')')) return 0;
475     }
476
477   cstp->n_expected = 0;
478   cstp->expected = NULL;
479   if ( lex_match (lexer, '/') )
480     {
481       if ( lex_match_id (lexer, "EXPECTED") )
482         {
483           lex_force_match (lexer, '=');
484           if ( ! lex_match_id (lexer, "EQUAL") )
485             {
486               double f;
487               int n;
488               while ( lex_is_number (lexer) )
489                 {
490                   int i;
491                   n = 1;
492                   f = lex_number (lexer);
493                   lex_get (lexer);
494                   if ( lex_match (lexer, '*'))
495                     {
496                       n = f;
497                       f = lex_number (lexer);
498                       lex_get (lexer);
499                     }
500                   lex_match (lexer, ',');
501
502                   cstp->n_expected += n;
503                   cstp->expected = pool_realloc (specs->pool,
504                                                  cstp->expected,
505                                                  sizeof (double) *
506                                                  cstp->n_expected);
507                   for ( i = cstp->n_expected - n ;
508                         i < cstp->n_expected;
509                         ++i )
510                     cstp->expected[i] = f;
511
512                 }
513             }
514         }
515       else
516         lex_put_back (lexer, '/');
517     }
518
519   if ( cstp->ranged && cstp->n_expected > 0 &&
520        cstp->n_expected != cstp->hi - cstp->lo + 1 )
521     {
522       msg (ME,
523           _ ("%d expected values were given, but the specified "
524             "range (%d-%d) requires exactly %d values."),
525           cstp->n_expected, cstp->lo, cstp->hi,
526           cstp->hi - cstp->lo +1);
527       return 0;
528     }
529
530   specs->n_tests++;
531   specs->test = pool_realloc (specs->pool,
532                               specs->test,
533                               sizeof (*specs->test) * specs->n_tests);
534
535   specs->test[specs->n_tests - 1] = nt;
536
537   return 1;
538 }
539
540
541 static int
542 npar_binomial (struct lexer *lexer, struct dataset *ds,
543                struct npar_specs *specs)
544 {
545   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
546   struct one_sample_test *tp = &btp->parent;
547   struct npar_test *nt = &tp->parent;
548
549   nt->execute = binomial_execute;
550   nt->insert_variables = one_sample_insert_variables;
551
552   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
553
554   btp->p = 0.5;
555
556   if ( lex_match (lexer, '(') )
557     {
558       if ( lex_force_num (lexer) )
559         {
560           btp->p = lex_number (lexer);
561           lex_get (lexer);
562           lex_force_match (lexer, ')');
563         }
564       else
565         return 0;
566     }
567   else
568     /* Kludge: q2c swallows the '=' so put it back here  */
569      lex_put_back (lexer, '=');
570
571   if (lex_match (lexer, '=') )
572     {
573       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
574                                       &tp->vars, &tp->n_vars,
575                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
576         {
577           if (lex_match (lexer, '('))
578             {
579               lex_force_num (lexer);
580               btp->category1 = lex_number (lexer);
581               lex_get (lexer);
582               if ( lex_match (lexer, ','))
583                 {
584                   if ( ! lex_force_num (lexer) ) return 2;
585                   btp->category2 = lex_number (lexer);
586                   lex_get (lexer);
587                 }
588               else
589                 {
590                   btp->cutpoint = btp->category1;
591                 }
592
593               lex_force_match (lexer, ')');
594             }
595         }
596       else
597         return 2;
598
599     }
600
601   specs->n_tests++;
602   specs->test = pool_realloc (specs->pool,
603                               specs->test,
604                               sizeof (*specs->test) * specs->n_tests);
605
606   specs->test[specs->n_tests - 1] = nt;
607
608   return 1;
609 }
610
611
612 static bool
613 parse_two_sample_related_test (struct lexer *lexer,
614                                     const struct dictionary *dict,
615                                     struct two_sample_test *test_parameters,
616                                     struct pool *pool
617                                     );
618
619
620 static bool
621 parse_two_sample_related_test (struct lexer *lexer,
622                                const struct dictionary *dict,
623                                struct two_sample_test *test_parameters,
624                                struct pool *pool
625                                )
626 {
627   int n = 0;
628   bool paired = false;
629   bool with = false;
630   const struct variable **vlist1;
631   size_t n_vlist1;
632
633   const struct variable **vlist2;
634   size_t n_vlist2;
635
636   test_parameters->parent.insert_variables = two_sample_insert_variables;
637
638   if (!parse_variables_const_pool (lexer, pool,
639                                    dict,
640                                    &vlist1, &n_vlist1,
641                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
642     return false;
643
644   if ( lex_match (lexer, T_WITH))
645     {
646       with = true;
647       if ( !parse_variables_const_pool (lexer, pool, dict,
648                                         &vlist2, &n_vlist2,
649                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
650         return false;
651
652       paired = (lex_match (lexer, '(') &&
653                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
654     }
655
656
657   if ( with )
658     {
659       if (paired)
660         {
661           if ( n_vlist1 != n_vlist2)
662             msg (SE, _ ("PAIRED was specified but the number of variables "
663                        "preceding WITH (%zu) did not match the number "
664                        "following (%zu)."), n_vlist1, n_vlist2);
665
666           test_parameters->n_pairs = n_vlist1 ;
667         }
668       else
669         {
670           test_parameters->n_pairs = n_vlist1 * n_vlist2;
671         }
672     }
673   else
674     {
675       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
676     }
677
678   test_parameters->pairs =
679     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
680
681   if ( with )
682     {
683       if (paired)
684         {
685           int i;
686           assert (n_vlist1 == n_vlist2);
687           for ( i = 0 ; i < n_vlist1; ++i )
688             {
689               test_parameters->pairs[n][1] = vlist1[i];
690               test_parameters->pairs[n][0] = vlist2[i];
691               n++;
692             }
693         }
694       else
695         {
696           int i,j;
697           for ( i = 0 ; i < n_vlist1; ++i )
698             {
699               for ( j = 0 ; j < n_vlist2; ++j )
700                 {
701                   test_parameters->pairs[n][1] = vlist1[i];
702                   test_parameters->pairs[n][0] = vlist2[j];
703                   n++;
704                 }
705             }
706         }
707     }
708   else
709     {
710       int i,j;
711       for ( i = 0 ; i < n_vlist1 - 1; ++i )
712         {
713           for ( j = i + 1 ; j < n_vlist1; ++j )
714             {
715               assert ( n < test_parameters->n_pairs);
716               test_parameters->pairs[n][1] = vlist1[i];
717               test_parameters->pairs[n][0] = vlist1[j];
718               n++;
719             }
720         }
721     }
722
723   assert ( n == test_parameters->n_pairs);
724
725   return true;
726 }
727
728
729 static bool
730 parse_n_sample_related_test (struct lexer *lexer,
731                              const struct dictionary *dict,
732                              struct n_sample_test *nst,
733                              struct pool *pool
734                              )
735 {
736   union value val1, val2;
737
738   if (!parse_variables_const_pool (lexer, pool,
739                                    dict,
740                                    &nst->vars, &nst->n_vars,
741                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
742     return false;
743
744   if ( ! lex_force_match (lexer, T_BY))
745     return false;
746
747   nst->indep_var = parse_variable_const (lexer, dict);
748
749   if ( ! lex_force_match (lexer, '('))
750     return false;
751
752   value_init (&val1, var_get_width (nst->indep_var));
753   if ( ! parse_value (lexer, &val1, var_get_width (nst->indep_var)))
754     {
755       value_destroy (&val1, var_get_width (nst->indep_var));
756       return false;
757     }
758
759   if ( ! lex_force_match (lexer, ','))
760     return false;
761
762   value_init (&val2, var_get_width (nst->indep_var));
763   if ( ! parse_value (lexer, &val2, var_get_width (nst->indep_var)))
764     {
765       value_destroy (&val2, var_get_width (nst->indep_var));
766       return false;
767     }
768
769   if ( ! lex_force_match (lexer, ')'))
770     return false;
771
772   return true;
773 }
774
775 static int
776 npar_wilcoxon (struct lexer *lexer,
777                struct dataset *ds,
778                struct npar_specs *specs )
779 {
780
781
782   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
783   struct npar_test *nt = &tp->parent;
784   nt->execute = wilcoxon_execute;
785
786   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
787                                       tp, specs->pool) )
788     return 0;
789
790   specs->n_tests++;
791   specs->test = pool_realloc (specs->pool,
792                               specs->test,
793                               sizeof (*specs->test) * specs->n_tests);
794   specs->test[specs->n_tests - 1] = nt;
795
796   return 1;
797 }
798
799 static int
800 npar_sign (struct lexer *lexer, struct dataset *ds,
801            struct npar_specs *specs)
802 {
803   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
804   struct npar_test *nt = &tp->parent;
805
806   nt->execute = sign_execute;
807
808   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
809                                       tp, specs->pool) )
810     return 0;
811
812   specs->n_tests++;
813   specs->test = pool_realloc (specs->pool,
814                               specs->test,
815                               sizeof (*specs->test) * specs->n_tests);
816   specs->test[specs->n_tests - 1] = nt;
817
818   return 1;
819 }
820
821 /* Insert the variables for TEST into VAR_HASH */
822 static void
823 one_sample_insert_variables (const struct npar_test *test,
824                              struct const_hsh_table *var_hash)
825 {
826   int i;
827   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
828
829   for ( i = 0 ; i < ost->n_vars ; ++i )
830     const_hsh_insert (var_hash, ost->vars[i]);
831 }
832
833
834 static void
835 two_sample_insert_variables (const struct npar_test *test,
836                              struct const_hsh_table *var_hash)
837 {
838   int i;
839
840   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
841
842   for ( i = 0 ; i < tst->n_pairs ; ++i )
843     {
844       variable_pair *pair = &tst->pairs[i];
845
846       const_hsh_insert (var_hash, (*pair)[0]);
847       const_hsh_insert (var_hash, (*pair)[1]);
848     }
849 }
850
851 static void 
852 n_sample_insert_variables (const struct npar_test *test,
853                            struct const_hsh_table *var_hash)
854 {
855   int i;
856   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
857
858   for ( i = 0 ; i < tst->n_vars ; ++i )
859     const_hsh_insert (var_hash, tst->vars[i]);
860
861   const_hsh_insert (var_hash, tst->indep_var);
862 }
863
864 static int
865 npar_method (struct lexer *lexer,  struct npar_specs *specs)
866 {
867   if ( lex_match_id (lexer, "EXACT") )
868     {
869       specs->exact = true;
870       specs->timer = 0.0;
871       if (lex_match_id (lexer, "TIMER"))
872         {
873           specs->timer = 5.0;
874
875           if ( lex_match (lexer, '('))
876             {
877               if ( lex_force_num (lexer) )
878                 {
879                   specs->timer = lex_number (lexer);
880                   lex_get (lexer);
881                 }
882               lex_force_match (lexer, ')');
883             }
884         }
885     }
886
887   return 1;
888 }