Use sort instead of qsort thus avoiding compiler warning
[pspp-builds.git] / src / language / stats / npar.c
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <language/stats/npar.h>
20 #include "npar-summary.h"
21
22 #include <stdlib.h>
23 #include <math.h>
24
25 #include "xalloc.h"
26
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/settings.h>
33 #include <data/variable.h>
34 #include <libpspp/array.h>
35 #include <libpspp/assertion.h>
36 #include <libpspp/cast.h>
37 #include <libpspp/hmapx.h>
38 #include <libpspp/hash-functions.h>
39 #include <libpspp/message.h>
40 #include <libpspp/pool.h>
41 #include <libpspp/str.h>
42 #include <libpspp/taint.h>
43 #include <language/command.h>
44 #include <language/lexer/lexer.h>
45 #include <language/lexer/variable-parser.h>
46 #include <language/lexer/value-parser.h>
47 #include <language/stats/binomial.h>
48 #include <language/stats/chisquare.h>
49 #include <language/stats/runs.h>
50 #include <language/stats/friedman.h>
51 #include <language/stats/kruskal-wallis.h>
52 #include <language/stats/wilcoxon.h>
53 #include <language/stats/sign.h>
54 #include <math/moments.h>
55
56 #include "gettext.h"
57 #define _(msgid) gettext (msgid)
58
59 /* Settings for subcommand specifiers. */
60 enum missing_type
61   {
62     MISS_ANALYSIS,
63     MISS_LISTWISE,
64   };
65
66 /* Array indices for STATISTICS subcommand. */
67 enum
68   {
69     NPAR_ST_DESCRIPTIVES = 0,
70     NPAR_ST_QUARTILES = 1,
71     NPAR_ST_ALL = 2,
72     NPAR_ST_count
73   };
74
75 /* NPAR TESTS structure. */
76 struct cmd_npar_tests
77   {
78     /* Count variables indicating how many
79        of the subcommands have been given. */
80     int chisquare;
81     int binomial;
82     int wilcoxon;
83     int sign;
84     int runs;
85     int friedman;
86     int kruskal_wallis;
87     int missing;
88     int method;
89     int statistics;
90
91     /* How missing values should be treated */
92     long miss;
93
94     /* Which statistics have been requested */
95     int a_statistics[NPAR_ST_count];
96   };
97
98
99 struct npar_specs
100 {
101   struct pool *pool;
102   struct npar_test **test;
103   size_t n_tests;
104
105   const struct variable **vv; /* Compendium of all variables
106                                   (those mentioned on ANY subcommand */
107   int n_vars; /* Number of variables in vv */
108
109   enum mv_class filter;    /* Missing values to filter. */
110
111   bool descriptives;       /* Descriptive statistics should be calculated */
112   bool quartiles;          /* Quartiles should be calculated */
113
114   bool exact;  /* Whether exact calculations have been requested */
115   double timer;   /* Maximum time (in minutes) to wait for exact calculations */
116 };
117
118
119 /* Prototype for custom subcommands of NPAR TESTS. */
120 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
121 static int npar_binomial (struct lexer *, struct dataset *,  struct npar_specs *);
122 static int npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
123 static int npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
124 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
125 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
126 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
127 static int npar_method (struct lexer *, struct npar_specs *);
128
129 /* Command parsing functions. */
130 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
131                              struct npar_specs *npar_specs );
132
133 static int
134 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
135                   struct npar_specs *nps)
136 {
137   npt->chisquare = 0;
138   npt->binomial = 0;
139   npt->wilcoxon = 0;
140   npt->runs = 0;
141   npt->friedman = 0;
142   npt->sign = 0;
143   npt->missing = 0;
144   npt->miss = MISS_ANALYSIS;
145   npt->method = 0;
146   npt->statistics = 0;
147   memset (npt->a_statistics, 0, sizeof npt->a_statistics);
148   for (;;)
149     {
150       if (lex_match_hyphenated_word (lexer, "FRIEDMAN"))
151         {
152           npt->friedman++;
153           switch (npar_friedman (lexer, ds, nps))
154             {
155             case 0:
156               goto lossage;
157             case 1:
158               break;
159             case 2:
160               lex_error (lexer, NULL);
161               goto lossage;
162             default:
163               NOT_REACHED ();
164             }
165         }
166       else if (lex_match_hyphenated_word (lexer, "RUNS"))
167         {
168           npt->runs++;
169           switch (npar_runs (lexer, ds, nps))
170             {
171             case 0:
172               goto lossage;
173             case 1:
174               break;
175             case 2:
176               lex_error (lexer, NULL);
177               goto lossage;
178             default:
179               NOT_REACHED ();
180             }
181         }
182       else if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
183         {
184           lex_match (lexer, '=');
185           npt->chisquare++;
186           switch (npar_chisquare (lexer, ds, nps))
187             {
188             case 0:
189               goto lossage;
190             case 1:
191               break;
192             case 2:
193               lex_error (lexer, NULL);
194               goto lossage;
195             default:
196               NOT_REACHED ();
197             }
198         }
199       else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
200         {
201           lex_match (lexer, '=');
202           npt->binomial++;
203           switch (npar_binomial (lexer, ds, nps))
204             {
205             case 0:
206               goto lossage;
207             case 1:
208               break;
209             case 2:
210               lex_error (lexer, NULL);
211               goto lossage;
212             default:
213               NOT_REACHED ();
214             }
215         }
216       else if (lex_match_hyphenated_word (lexer, "K-W") ||
217                lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
218         {
219           lex_match (lexer, '=');
220           npt->kruskal_wallis++;
221           switch (npar_kruskal_wallis (lexer, ds, nps))
222             {
223             case 0:
224               goto lossage;
225             case 1:
226               break;
227             case 2:
228               lex_error (lexer, NULL);
229               goto lossage;
230             default:
231               NOT_REACHED ();
232             }
233         }
234       else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
235         {
236           lex_match (lexer, '=');
237           npt->wilcoxon++;
238           switch (npar_wilcoxon (lexer, ds, nps))
239             {
240             case 0:
241               goto lossage;
242             case 1:
243               break;
244             case 2:
245               lex_error (lexer, NULL);
246               goto lossage;
247             default:
248               NOT_REACHED ();
249             }
250         }
251       else if (lex_match_hyphenated_word (lexer, "SIGN"))
252         {
253           lex_match (lexer, '=');
254           npt->sign++;
255           switch (npar_sign (lexer, ds, nps))
256             {
257             case 0:
258               goto lossage;
259             case 1:
260               break;
261             case 2:
262               lex_error (lexer, NULL);
263               goto lossage;
264             default:
265               NOT_REACHED ();
266             }
267         }
268       else if (lex_match_hyphenated_word (lexer, "MISSING"))
269         {
270           lex_match (lexer, '=');
271           npt->missing++;
272           if (npt->missing > 1)
273             {
274               msg (SE, _("The %s subcommand may be given only once."), "MISSING");
275               goto lossage;
276             }
277           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
278             {
279               if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
280                 npt->miss = MISS_ANALYSIS;
281               else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
282                 npt->miss = MISS_LISTWISE;
283               else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
284                 nps->filter = MV_SYSTEM;
285               else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
286                 nps->filter = MV_ANY;
287               else
288                 {
289                   lex_error (lexer, NULL);
290                   goto lossage;
291                 }
292               lex_match (lexer, ',');
293             }
294         }
295       else if (lex_match_hyphenated_word (lexer, "METHOD"))
296         {
297           lex_match (lexer, '=');
298           npt->method++;
299           if (npt->method > 1)
300             {
301               msg (SE, _("The %s subcommand may be given only once."), "METHOD");
302               goto lossage;
303             }
304           switch (npar_method (lexer, 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_hyphenated_word (lexer, "STATISTICS"))
318         {
319           lex_match (lexer, '=');
320           npt->statistics++;
321           while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
322             {
323               if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
324                 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
325               else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
326                 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
327               else if (lex_match (lexer, T_ALL))
328                 npt->a_statistics[NPAR_ST_ALL] = 1;
329               else
330                 {
331                   lex_error (lexer, NULL);
332                   goto lossage;
333                 }
334               lex_match (lexer, ',');
335             }
336         }
337       else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
338         {
339           lex_match (lexer, '=');
340           if (lex_match_id (lexer, "COMPATIBLE"))
341             settings_set_cmd_algorithm (COMPATIBLE);
342           else if (lex_match_id (lexer, "ENHANCED"))
343             settings_set_cmd_algorithm (ENHANCED);
344           }
345         if (!lex_match (lexer, '/'))
346           break;
347       }
348
349     if (lex_token (lexer) != '.')
350       {
351         lex_error (lexer, _("expecting end of command"));
352         goto lossage;
353       }
354
355   return true;
356
357 lossage:
358   return false;
359 }
360
361
362 static void one_sample_insert_variables (const struct npar_test *test,
363                                          struct hmapx *);
364
365 static void two_sample_insert_variables (const struct npar_test *test,
366                                          struct hmapx *);
367
368 static void n_sample_insert_variables (const struct npar_test *test,
369                                        struct hmapx *);
370
371 static void
372 npar_execute (struct casereader *input,
373              const struct npar_specs *specs,
374              const struct dataset *ds)
375 {
376   int t;
377   struct descriptives *summary_descriptives = NULL;
378
379   for ( t = 0 ; t < specs->n_tests; ++t )
380     {
381       const struct npar_test *test = specs->test[t];
382       if ( NULL == test->execute )
383         {
384           msg (SW, _("NPAR subcommand not currently implemented."));
385           continue;
386         }
387       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
388     }
389
390   if ( specs->descriptives )
391     {
392       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
393                                        specs->n_vars);
394
395       npar_summary_calc_descriptives (summary_descriptives,
396                                       casereader_clone (input),
397                                       dataset_dict (ds),
398                                       specs->vv, specs->n_vars,
399                                       specs->filter);
400     }
401
402   if ( (specs->descriptives || specs->quartiles)
403        && !taint_has_tainted_successor (casereader_get_taint (input)) )
404     do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
405
406   free (summary_descriptives);
407   casereader_destroy (input);
408 }
409
410 int
411 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
412 {
413   struct cmd_npar_tests cmd;
414   bool ok;
415   int i;
416   struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
417   struct casegrouper *grouper;
418   struct casereader *input, *group;
419   struct hmapx var_map = HMAPX_INITIALIZER (var_map);
420
421
422   npar_specs.pool = pool_create ();
423   npar_specs.filter = MV_ANY;
424   npar_specs.n_vars = -1;
425   npar_specs.vv = NULL;
426
427   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
428     {
429       pool_destroy (npar_specs.pool);
430       return CMD_FAILURE;
431     }
432
433   for (i = 0; i < npar_specs.n_tests; ++i )
434     {
435       const struct npar_test *test = npar_specs.test[i];
436       test->insert_variables (test, &var_map);
437     }
438
439   {
440     struct hmapx_node *node;
441     struct variable *var;
442     npar_specs.n_vars = 0;
443
444     HMAPX_FOR_EACH (var, node, &var_map)
445       {
446         npar_specs.n_vars ++;
447         npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
448         npar_specs.vv[npar_specs.n_vars - 1] = var;
449       }
450   }
451
452   sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv), 
453          compare_var_ptrs_by_name, NULL);
454
455   if ( cmd.statistics )
456     {
457       int i;
458
459       for ( i = 0 ; i < NPAR_ST_count; ++i )
460         {
461           if ( cmd.a_statistics[i] )
462             {
463               switch ( i )
464                 {
465                 case NPAR_ST_DESCRIPTIVES:
466                   npar_specs.descriptives = true;
467                   break;
468                 case NPAR_ST_QUARTILES:
469                   npar_specs.quartiles = true;
470                   break;
471                 case NPAR_ST_ALL:
472                   npar_specs.quartiles = true;
473                   npar_specs.descriptives = true;
474                   break;
475                 default:
476                   NOT_REACHED ();
477                 };
478             }
479         }
480     }
481
482   input = proc_open (ds);
483   if ( cmd.miss == MISS_LISTWISE )
484     {
485       input = casereader_create_filter_missing (input,
486                                                 npar_specs.vv,
487                                                 npar_specs.n_vars,
488                                                 npar_specs.filter,
489                                                 NULL, NULL);
490     }
491
492
493   grouper = casegrouper_create_splits (input, dataset_dict (ds));
494   while (casegrouper_get_next_group (grouper, &group))
495     npar_execute (group, &npar_specs, ds);
496   ok = casegrouper_destroy (grouper);
497   ok = proc_commit (ds) && ok;
498
499   pool_destroy (npar_specs.pool);
500   hmapx_destroy (&var_map);
501
502   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
503 }
504
505 static int
506 npar_runs (struct lexer *lexer, struct dataset *ds,
507            struct npar_specs *specs)
508 {
509   struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
510   struct one_sample_test *tp = &rt->parent;
511   struct npar_test *nt = &tp->parent;
512
513   nt->execute = runs_execute;
514   nt->insert_variables = one_sample_insert_variables;
515
516   if ( lex_force_match (lexer, '(') )
517     {
518       if ( lex_match_id (lexer, "MEAN"))
519         {
520           rt->cp_mode = CP_MEAN;
521         }
522       else if (lex_match_id (lexer, "MEDIAN"))
523         {
524           rt->cp_mode = CP_MEDIAN;
525         }
526       else if (lex_match_id (lexer, "MODE"))
527         {
528           rt->cp_mode = CP_MODE;
529         }
530       else if (lex_is_number (lexer))
531         {
532           rt->cutpoint = lex_number (lexer);
533           rt->cp_mode = CP_CUSTOM;
534           lex_get (lexer);
535         }
536       else
537         {
538           lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
539           return 0;
540         }
541                   
542       lex_force_match (lexer, ')');
543       lex_force_match (lexer, '=');
544       if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
545                                   &tp->vars, &tp->n_vars,
546                                   PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
547         {
548           return 2;
549         }
550     }
551
552   specs->n_tests++;
553   specs->test = pool_realloc (specs->pool,
554                               specs->test,
555                               sizeof (*specs->test) * specs->n_tests);
556
557   specs->test[specs->n_tests - 1] = nt;
558
559   return 1;
560 }
561
562 static int
563 npar_friedman (struct lexer *lexer, struct dataset *ds,
564                struct npar_specs *specs)
565 {
566   struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft)); 
567   struct npar_test *nt = &ft->parent;
568
569   nt->execute = friedman_execute;
570   nt->insert_variables = one_sample_insert_variables;
571
572   lex_match (lexer, '=');
573
574   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
575                                    &ft->vars, &ft->n_vars,
576                                    PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
577     {
578       return 2;
579     }
580
581   specs->n_tests++;
582   specs->test = pool_realloc (specs->pool,
583                               specs->test,
584                               sizeof (*specs->test) * specs->n_tests);
585
586   specs->test[specs->n_tests - 1] = nt;
587
588   return 1;
589 }
590
591
592 static int
593 npar_chisquare (struct lexer *lexer, struct dataset *ds,
594                 struct npar_specs *specs)
595 {
596   struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
597   struct one_sample_test *tp = &cstp->parent;
598   struct npar_test *nt = &tp->parent;
599
600
601   nt->execute = chisquare_execute;
602   nt->insert_variables = one_sample_insert_variables;
603
604   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
605                                    &tp->vars, &tp->n_vars,
606                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
607     {
608       return 2;
609     }
610
611   cstp->ranged = false;
612
613   if ( lex_match (lexer, '('))
614     {
615       cstp->ranged = true;
616       if ( ! lex_force_num (lexer)) return 0;
617       cstp->lo = lex_integer (lexer);
618       lex_get (lexer);
619       lex_force_match (lexer, ',');
620       if (! lex_force_num (lexer) ) return 0;
621       cstp->hi = lex_integer (lexer);
622       if ( cstp->lo >= cstp->hi )
623         {
624           msg (ME,
625               _("The specified value of HI (%d) is "
626                 "lower than the specified value of LO (%d)"),
627               cstp->hi, cstp->lo);
628           return 0;
629         }
630       lex_get (lexer);
631       if (! lex_force_match (lexer, ')')) return 0;
632     }
633
634   cstp->n_expected = 0;
635   cstp->expected = NULL;
636   if ( lex_match (lexer, '/') )
637     {
638       if ( lex_match_id (lexer, "EXPECTED") )
639         {
640           lex_force_match (lexer, '=');
641           if ( ! lex_match_id (lexer, "EQUAL") )
642             {
643               double f;
644               int n;
645               while ( lex_is_number (lexer) )
646                 {
647                   int i;
648                   n = 1;
649                   f = lex_number (lexer);
650                   lex_get (lexer);
651                   if ( lex_match (lexer, '*'))
652                     {
653                       n = f;
654                       f = lex_number (lexer);
655                       lex_get (lexer);
656                     }
657                   lex_match (lexer, ',');
658
659                   cstp->n_expected += n;
660                   cstp->expected = pool_realloc (specs->pool,
661                                                  cstp->expected,
662                                                  sizeof (double) *
663                                                  cstp->n_expected);
664                   for ( i = cstp->n_expected - n ;
665                         i < cstp->n_expected;
666                         ++i )
667                     cstp->expected[i] = f;
668
669                 }
670             }
671         }
672       else
673         lex_put_back (lexer, '/');
674     }
675
676   if ( cstp->ranged && cstp->n_expected > 0 &&
677        cstp->n_expected != cstp->hi - cstp->lo + 1 )
678     {
679       msg (ME,
680           _("%d expected values were given, but the specified "
681             "range (%d-%d) requires exactly %d values."),
682           cstp->n_expected, cstp->lo, cstp->hi,
683           cstp->hi - cstp->lo +1);
684       return 0;
685     }
686
687   specs->n_tests++;
688   specs->test = pool_realloc (specs->pool,
689                               specs->test,
690                               sizeof (*specs->test) * specs->n_tests);
691
692   specs->test[specs->n_tests - 1] = nt;
693
694   return 1;
695 }
696
697
698 static int
699 npar_binomial (struct lexer *lexer, struct dataset *ds,
700                struct npar_specs *specs)
701 {
702   struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
703   struct one_sample_test *tp = &btp->parent;
704   struct npar_test *nt = &tp->parent;
705
706   nt->execute = binomial_execute;
707   nt->insert_variables = one_sample_insert_variables;
708
709   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
710
711   btp->p = 0.5;
712
713   if ( lex_match (lexer, '(') )
714     {
715       if ( lex_force_num (lexer) )
716         {
717           btp->p = lex_number (lexer);
718           lex_get (lexer);
719           lex_force_match (lexer, ')');
720         }
721       else
722         return 0;
723     }
724   else
725     /* Kludge: q2c swallows the '=' so put it back here  */
726      lex_put_back (lexer, '=');
727
728   if (lex_match (lexer, '=') )
729     {
730       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
731                                       &tp->vars, &tp->n_vars,
732                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
733         {
734           if (lex_match (lexer, '('))
735             {
736               lex_force_num (lexer);
737               btp->category1 = lex_number (lexer);
738               lex_get (lexer);
739               if ( lex_match (lexer, ','))
740                 {
741                   if ( ! lex_force_num (lexer) ) return 2;
742                   btp->category2 = lex_number (lexer);
743                   lex_get (lexer);
744                 }
745               else
746                 {
747                   btp->cutpoint = btp->category1;
748                 }
749
750               lex_force_match (lexer, ')');
751             }
752         }
753       else
754         return 2;
755
756     }
757
758   specs->n_tests++;
759   specs->test = pool_realloc (specs->pool,
760                               specs->test,
761                               sizeof (*specs->test) * specs->n_tests);
762
763   specs->test[specs->n_tests - 1] = nt;
764
765   return 1;
766 }
767
768
769 static bool
770 parse_two_sample_related_test (struct lexer *lexer,
771                                     const struct dictionary *dict,
772                                     struct two_sample_test *test_parameters,
773                                     struct pool *pool
774                                     );
775
776
777 static bool
778 parse_two_sample_related_test (struct lexer *lexer,
779                                const struct dictionary *dict,
780                                struct two_sample_test *test_parameters,
781                                struct pool *pool
782                                )
783 {
784   int n = 0;
785   bool paired = false;
786   bool with = false;
787   const struct variable **vlist1;
788   size_t n_vlist1;
789
790   const struct variable **vlist2;
791   size_t n_vlist2;
792
793   test_parameters->parent.insert_variables = two_sample_insert_variables;
794
795   if (!parse_variables_const_pool (lexer, pool,
796                                    dict,
797                                    &vlist1, &n_vlist1,
798                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
799     return false;
800
801   if ( lex_match (lexer, T_WITH))
802     {
803       with = true;
804       if ( !parse_variables_const_pool (lexer, pool, dict,
805                                         &vlist2, &n_vlist2,
806                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
807         return false;
808
809       paired = (lex_match (lexer, '(') &&
810                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
811     }
812
813
814   if ( with )
815     {
816       if (paired)
817         {
818           if ( n_vlist1 != n_vlist2)
819             msg (SE, _("PAIRED was specified but the number of variables "
820                        "preceding WITH (%zu) did not match the number "
821                        "following (%zu)."), n_vlist1, n_vlist2);
822
823           test_parameters->n_pairs = n_vlist1 ;
824         }
825       else
826         {
827           test_parameters->n_pairs = n_vlist1 * n_vlist2;
828         }
829     }
830   else
831     {
832       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
833     }
834
835   test_parameters->pairs =
836     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
837
838   if ( with )
839     {
840       if (paired)
841         {
842           int i;
843           assert (n_vlist1 == n_vlist2);
844           for ( i = 0 ; i < n_vlist1; ++i )
845             {
846               test_parameters->pairs[n][1] = vlist1[i];
847               test_parameters->pairs[n][0] = vlist2[i];
848               n++;
849             }
850         }
851       else
852         {
853           int i,j;
854           for ( i = 0 ; i < n_vlist1; ++i )
855             {
856               for ( j = 0 ; j < n_vlist2; ++j )
857                 {
858                   test_parameters->pairs[n][1] = vlist1[i];
859                   test_parameters->pairs[n][0] = vlist2[j];
860                   n++;
861                 }
862             }
863         }
864     }
865   else
866     {
867       int i,j;
868       for ( i = 0 ; i < n_vlist1 - 1; ++i )
869         {
870           for ( j = i + 1 ; j < n_vlist1; ++j )
871             {
872               assert ( n < test_parameters->n_pairs);
873               test_parameters->pairs[n][1] = vlist1[i];
874               test_parameters->pairs[n][0] = vlist1[j];
875               n++;
876             }
877         }
878     }
879
880   assert ( n == test_parameters->n_pairs);
881
882   return true;
883 }
884
885
886 static bool
887 parse_n_sample_related_test (struct lexer *lexer,
888                              const struct dictionary *dict,
889                              struct n_sample_test *nst,
890                              struct pool *pool
891                              )
892 {
893   if (!parse_variables_const_pool (lexer, pool,
894                                    dict,
895                                    &nst->vars, &nst->n_vars,
896                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
897     return false;
898
899   if ( ! lex_force_match (lexer, T_BY))
900     return false;
901
902   nst->indep_var = parse_variable_const (lexer, dict);
903
904   if ( ! lex_force_match (lexer, '('))
905     return false;
906
907   value_init (&nst->val1, var_get_width (nst->indep_var));
908   if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
909     {
910       value_destroy (&nst->val1, var_get_width (nst->indep_var));
911       return false;
912     }
913
914   if ( ! lex_force_match (lexer, ','))
915     return false;
916
917   value_init (&nst->val2, var_get_width (nst->indep_var));
918   if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
919     {
920       value_destroy (&nst->val2, var_get_width (nst->indep_var));
921       return false;
922     }
923
924   if ( ! lex_force_match (lexer, ')'))
925     return false;
926
927   return true;
928 }
929
930 static int
931 npar_wilcoxon (struct lexer *lexer,
932                struct dataset *ds,
933                struct npar_specs *specs )
934 {
935
936
937   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
938   struct npar_test *nt = &tp->parent;
939   nt->execute = wilcoxon_execute;
940
941   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
942                                       tp, specs->pool) )
943     return 0;
944
945   specs->n_tests++;
946   specs->test = pool_realloc (specs->pool,
947                               specs->test,
948                               sizeof (*specs->test) * specs->n_tests);
949   specs->test[specs->n_tests - 1] = nt;
950
951   return 1;
952 }
953
954 static int
955 npar_sign (struct lexer *lexer, struct dataset *ds,
956            struct npar_specs *specs)
957 {
958   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
959   struct npar_test *nt = &tp->parent;
960
961   nt->execute = sign_execute;
962
963   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
964                                       tp, specs->pool) )
965     return 0;
966
967   specs->n_tests++;
968   specs->test = pool_realloc (specs->pool,
969                               specs->test,
970                               sizeof (*specs->test) * specs->n_tests);
971   specs->test[specs->n_tests - 1] = nt;
972
973   return 1;
974 }
975
976 static int
977 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
978                       struct npar_specs *specs)
979 {
980   struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
981   struct npar_test *nt = &tp->parent;
982
983   nt->insert_variables = n_sample_insert_variables;
984
985   nt->execute = kruskal_wallis_execute;
986
987   if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
988                                       tp, specs->pool) )
989     return 0;
990
991   specs->n_tests++;
992   specs->test = pool_realloc (specs->pool,
993                               specs->test,
994                               sizeof (*specs->test) * specs->n_tests);
995   specs->test[specs->n_tests - 1] = nt;
996
997   return 1;
998 }
999
1000 static void
1001 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1002 {
1003   size_t hash = hash_pointer (var, 0);
1004   struct hmapx_node *node;
1005   const struct variable *v = NULL;
1006       
1007   HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1008     {
1009       if ( v == var)
1010         return ;
1011     }
1012
1013   hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1014 }
1015
1016 /* Insert the variables for TEST into VAR_MAP */
1017 static void
1018 one_sample_insert_variables (const struct npar_test *test,
1019                              struct hmapx *var_map)
1020 {
1021   int i;
1022   const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1023
1024   for ( i = 0 ; i < ost->n_vars ; ++i )
1025     insert_variable_into_map (var_map, ost->vars[i]);
1026 }
1027
1028
1029 static void
1030 two_sample_insert_variables (const struct npar_test *test,
1031                              struct hmapx *var_map)
1032 {
1033   int i;
1034   const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1035
1036   for ( i = 0 ; i < tst->n_pairs ; ++i )
1037     {
1038       variable_pair *pair = &tst->pairs[i];
1039
1040       insert_variable_into_map (var_map, (*pair)[0]);
1041       insert_variable_into_map (var_map, (*pair)[1]);
1042     }
1043 }
1044
1045 static void 
1046 n_sample_insert_variables (const struct npar_test *test,
1047                            struct hmapx *var_map)
1048 {
1049   int i;
1050   const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1051
1052   for ( i = 0 ; i < tst->n_vars ; ++i )
1053     insert_variable_into_map (var_map, tst->vars[i]);
1054
1055   insert_variable_into_map (var_map, tst->indep_var);
1056 }
1057
1058
1059 static int
1060 npar_method (struct lexer *lexer,  struct npar_specs *specs)
1061 {
1062   if ( lex_match_id (lexer, "EXACT") )
1063     {
1064       specs->exact = true;
1065       specs->timer = 0.0;
1066       if (lex_match_id (lexer, "TIMER"))
1067         {
1068           specs->timer = 5.0;
1069
1070           if ( lex_match (lexer, '('))
1071             {
1072               if ( lex_force_num (lexer) )
1073                 {
1074                   specs->timer = lex_number (lexer);
1075                   lex_get (lexer);
1076                 }
1077               lex_force_match (lexer, ')');
1078             }
1079         }
1080     }
1081
1082   return 1;
1083 }