1 /* PSPP - a program for statistical analysis. -*-c-*-
2 Copyright (C) 2006, 2008, 2009, 2010, 2011, 2016 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "language/stats/npar.h"
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"
60 #include "gl/xalloc.h"
63 #define _(msgid) gettext (msgid)
65 /* Settings for subcommand specifiers. */
72 /* Array indices for STATISTICS subcommand. */
75 NPAR_ST_DESCRIPTIVES = 0,
76 NPAR_ST_QUARTILES = 1,
81 /* NPAR TESTS structure. */
84 /* Count variables indicating how many
85 of the subcommands have been given. */
99 int jonckheere_terpstra;
104 /* How missing values should be treated */
107 /* Which statistics have been requested */
108 int a_statistics[NPAR_ST_count];
115 struct npar_test **test;
118 const struct variable **vv; /* Compendium of all variables
119 (those mentioned on ANY subcommand */
120 int n_vars; /* Number of variables in vv */
122 enum mv_class filter; /* Missing values to filter. */
124 bool descriptives; /* Descriptive statistics should be calculated */
125 bool quartiles; /* Quartiles should be calculated */
127 bool exact; /* Whether exact calculations have been requested */
128 double timer; /* Maximum time (in minutes) to wait for exact calculations */
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 *);
148 static int npar_method (struct lexer *, struct npar_specs *);
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);
155 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
156 struct npar_specs *nps)
161 npt->ks_one_sample = 0;
167 npt->kruskal_wallis = 0;
168 npt->mann_whitney = 0;
171 npt->jonckheere_terpstra = 0;
173 npt->miss = MISS_ANALYSIS;
178 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
181 if (lex_match_id (lexer, "COCHRAN"))
184 switch (npar_cochran (lexer, ds, nps))
191 lex_error (lexer, NULL);
197 else if (lex_match_id (lexer, "FRIEDMAN"))
200 switch (npar_friedman (lexer, ds, nps))
207 lex_error (lexer, NULL);
213 else if (lex_match_id (lexer, "KENDALL"))
216 switch (npar_kendall (lexer, ds, nps))
223 lex_error (lexer, NULL);
229 else if (lex_match_id (lexer, "RUNS"))
232 switch (npar_runs (lexer, ds, nps))
239 lex_error (lexer, NULL);
245 else if (lex_match_id (lexer, "CHISQUARE"))
247 lex_match (lexer, T_EQUALS);
249 switch (npar_chisquare (lexer, ds, nps))
256 lex_error (lexer, NULL);
264 else if (lex_match_id (lexer, "BINOMIAL"))
266 lex_match (lexer, T_EQUALS);
268 switch (npar_binomial (lexer, ds, nps))
275 lex_error (lexer, NULL);
281 else if (lex_match_phrase (lexer, "K-S") ||
282 lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
284 lex_match (lexer, T_EQUALS);
285 npt->ks_one_sample++;
286 switch (npar_ks_one_sample (lexer, ds, nps))
293 lex_error (lexer, NULL);
299 else if (lex_match_phrase (lexer, "J-T") ||
300 lex_match_phrase (lexer, "JONCKHEERE-TERPSTRA"))
302 lex_match (lexer, T_EQUALS);
303 npt->jonckheere_terpstra++;
304 switch (npar_jonckheere_terpstra (lexer, ds, nps))
311 lex_error (lexer, NULL);
317 else if (lex_match_phrase (lexer, "K-W") ||
318 lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
320 lex_match (lexer, T_EQUALS);
321 npt->kruskal_wallis++;
322 switch (npar_kruskal_wallis (lexer, ds, nps))
329 lex_error (lexer, NULL);
335 else if (lex_match_phrase (lexer, "MCNEMAR"))
337 lex_match (lexer, T_EQUALS);
339 switch (npar_mcnemar (lexer, ds, nps))
346 lex_error (lexer, NULL);
352 else if (lex_match_phrase (lexer, "M-W") ||
353 lex_match_phrase (lexer, "MANN-WHITNEY"))
355 lex_match (lexer, T_EQUALS);
357 switch (npar_mann_whitney (lexer, ds, nps))
364 lex_error (lexer, NULL);
370 else if (lex_match_phrase (lexer, "MEDIAN"))
374 switch (npar_median (lexer, ds, nps))
381 lex_error (lexer, NULL);
387 else if (lex_match_id (lexer, "WILCOXON"))
389 lex_match (lexer, T_EQUALS);
391 switch (npar_wilcoxon (lexer, ds, nps))
398 lex_error (lexer, NULL);
404 else if (lex_match_id (lexer, "SIGN"))
406 lex_match (lexer, T_EQUALS);
408 switch (npar_sign (lexer, ds, nps))
415 lex_error (lexer, NULL);
421 else if (lex_match_id (lexer, "MISSING"))
423 lex_match (lexer, T_EQUALS);
425 if (npt->missing > 1)
427 lex_sbc_only_once ("MISSING");
430 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
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;
442 lex_error (lexer, NULL);
445 lex_match (lexer, T_COMMA);
448 else if (lex_match_id (lexer, "METHOD"))
450 lex_match (lexer, T_EQUALS);
454 lex_sbc_only_once ("METHOD");
457 switch (npar_method (lexer, nps))
464 lex_error (lexer, NULL);
470 else if (lex_match_id (lexer, "STATISTICS"))
472 lex_match (lexer, T_EQUALS);
474 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
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;
484 lex_error (lexer, NULL);
487 lex_match (lexer, T_COMMA);
490 else if (settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
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);
498 if (!lex_match (lexer, T_SLASH))
501 if (lex_token (lexer) != T_ENDCMD)
503 lex_error (lexer, _("expecting end of command"));
514 static void one_sample_insert_variables (const struct npar_test *test,
517 static void two_sample_insert_variables (const struct npar_test *test,
520 static void n_sample_insert_variables (const struct npar_test *test,
524 npar_execute (struct casereader *input,
525 const struct npar_specs *specs,
526 const struct dataset *ds)
529 struct descriptives *summary_descriptives = NULL;
531 for (t = 0 ; t < specs->n_tests; ++t)
533 const struct npar_test *test = specs->test[t];
534 if (NULL == test->execute)
536 msg (SW, _("%s subcommand not currently implemented."), "NPAR");
539 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
542 if (specs->descriptives && specs->n_vars > 0)
544 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
547 npar_summary_calc_descriptives (summary_descriptives,
548 casereader_clone (input),
550 specs->vv, specs->n_vars,
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 dict_get_weight_format (dataset_dict (ds)));
559 free (summary_descriptives);
560 casereader_destroy (input);
564 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
566 struct cmd_npar_tests cmd;
569 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
570 struct casegrouper *grouper;
571 struct casereader *input, *group;
572 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
575 npar_specs.pool = pool_create ();
576 npar_specs.filter = MV_ANY;
577 npar_specs.n_vars = -1;
578 npar_specs.vv = NULL;
580 if (! parse_npar_tests (lexer, ds, &cmd, &npar_specs))
582 pool_destroy (npar_specs.pool);
586 for (i = 0; i < npar_specs.n_tests; ++i)
588 const struct npar_test *test = npar_specs.test[i];
589 test->insert_variables (test, &var_map);
593 struct hmapx_node *node;
594 struct variable *var;
595 npar_specs.n_vars = 0;
597 HMAPX_FOR_EACH (var, node, &var_map)
599 npar_specs.n_vars ++;
600 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
601 npar_specs.vv[npar_specs.n_vars - 1] = var;
605 sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
606 compare_var_ptrs_by_name, NULL);
612 for (i = 0 ; i < NPAR_ST_count; ++i)
614 if (cmd.a_statistics[i])
618 case NPAR_ST_DESCRIPTIVES:
619 npar_specs.descriptives = true;
621 case NPAR_ST_QUARTILES:
622 npar_specs.quartiles = true;
625 npar_specs.quartiles = true;
626 npar_specs.descriptives = true;
635 input = proc_open (ds);
636 if (cmd.miss == MISS_LISTWISE)
638 input = casereader_create_filter_missing (input,
646 grouper = casegrouper_create_splits (input, dataset_dict (ds));
647 while (casegrouper_get_next_group (grouper, &group))
648 npar_execute (group, &npar_specs, ds);
649 ok = casegrouper_destroy (grouper);
650 ok = proc_commit (ds) && ok;
652 pool_destroy (npar_specs.pool);
653 hmapx_destroy (&var_map);
655 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
659 npar_runs (struct lexer *lexer, struct dataset *ds,
660 struct npar_specs *specs)
662 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
663 struct one_sample_test *tp = &rt->parent;
664 struct npar_test *nt = &tp->parent;
666 nt->execute = runs_execute;
667 nt->insert_variables = one_sample_insert_variables;
669 if (lex_force_match (lexer, T_LPAREN))
671 if (lex_match_id (lexer, "MEAN"))
673 rt->cp_mode = CP_MEAN;
675 else if (lex_match_id (lexer, "MEDIAN"))
677 rt->cp_mode = CP_MEDIAN;
679 else if (lex_match_id (lexer, "MODE"))
681 rt->cp_mode = CP_MODE;
683 else if (lex_is_number (lexer))
685 rt->cutpoint = lex_number (lexer);
686 rt->cp_mode = CP_CUSTOM;
691 lex_error (lexer, _("Expecting %s, %s, %s or a number."), "MEAN", "MEDIAN", "MODE");
695 if (! lex_force_match (lexer, T_RPAREN))
698 if (! lex_force_match (lexer, T_EQUALS))
701 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
702 &tp->vars, &tp->n_vars,
703 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
710 specs->test = pool_realloc (specs->pool,
712 sizeof (*specs->test) * specs->n_tests);
714 specs->test[specs->n_tests - 1] = nt;
720 npar_friedman (struct lexer *lexer, struct dataset *ds,
721 struct npar_specs *specs)
723 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
724 struct one_sample_test *ost = &ft->parent;
725 struct npar_test *nt = &ost->parent;
727 ft->kendalls_w = false;
728 nt->execute = friedman_execute;
729 nt->insert_variables = one_sample_insert_variables;
731 lex_match (lexer, T_EQUALS);
733 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
734 &ost->vars, &ost->n_vars,
735 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
741 specs->test = pool_realloc (specs->pool,
743 sizeof (*specs->test) * specs->n_tests);
745 specs->test[specs->n_tests - 1] = nt;
751 npar_kendall (struct lexer *lexer, struct dataset *ds,
752 struct npar_specs *specs)
754 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
755 struct one_sample_test *ost = &kt->parent;
756 struct npar_test *nt = &ost->parent;
758 kt->kendalls_w = true;
759 nt->execute = friedman_execute;
760 nt->insert_variables = one_sample_insert_variables;
762 lex_match (lexer, T_EQUALS);
764 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
765 &ost->vars, &ost->n_vars,
766 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
772 specs->test = pool_realloc (specs->pool,
774 sizeof (*specs->test) * specs->n_tests);
776 specs->test[specs->n_tests - 1] = nt;
783 npar_cochran (struct lexer *lexer, struct dataset *ds,
784 struct npar_specs *specs)
786 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
787 struct npar_test *nt = &ft->parent;
789 nt->execute = cochran_execute;
790 nt->insert_variables = one_sample_insert_variables;
792 lex_match (lexer, T_EQUALS);
794 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
795 &ft->vars, &ft->n_vars,
796 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
802 specs->test = pool_realloc (specs->pool,
804 sizeof (*specs->test) * specs->n_tests);
806 specs->test[specs->n_tests - 1] = nt;
813 npar_chisquare (struct lexer *lexer, struct dataset *ds,
814 struct npar_specs *specs)
816 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
817 struct one_sample_test *tp = &cstp->parent;
818 struct npar_test *nt = &tp->parent;
821 nt->execute = chisquare_execute;
822 nt->insert_variables = one_sample_insert_variables;
824 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
825 &tp->vars, &tp->n_vars,
826 PV_NO_SCRATCH | PV_NO_DUPLICATE))
831 cstp->ranged = false;
833 if (lex_match (lexer, T_LPAREN))
836 if (! lex_force_num (lexer)) return 0;
837 cstp->lo = lex_number (lexer);
839 if (! lex_force_match (lexer, T_COMMA)) return 0;
840 if (! lex_force_num (lexer)) return 0;
841 cstp->hi = lex_number (lexer);
842 if (cstp->lo >= cstp->hi)
845 _("The specified value of HI (%d) is "
846 "lower than the specified value of LO (%d)"),
851 if (! lex_force_match (lexer, T_RPAREN)) return 0;
854 cstp->n_expected = 0;
855 cstp->expected = NULL;
856 if (lex_match_phrase (lexer, "/EXPECTED"))
858 if (! lex_force_match (lexer, T_EQUALS)) return 0;
859 if (! lex_match_id (lexer, "EQUAL"))
863 while (lex_is_number (lexer))
867 f = lex_number (lexer);
869 if (lex_match (lexer, T_ASTERISK))
872 if (!lex_force_num (lexer))
874 f = lex_number (lexer);
877 lex_match (lexer, T_COMMA);
879 cstp->n_expected += n;
880 cstp->expected = pool_realloc (specs->pool,
884 for (i = cstp->n_expected - n ;
885 i < cstp->n_expected;
887 cstp->expected[i] = f;
893 if (cstp->ranged && cstp->n_expected > 0 &&
894 cstp->n_expected != cstp->hi - cstp->lo + 1)
897 _("%d expected values were given, but the specified "
898 "range (%d-%d) requires exactly %d values."),
899 cstp->n_expected, cstp->lo, cstp->hi,
900 cstp->hi - cstp->lo +1);
905 specs->test = pool_realloc (specs->pool,
907 sizeof (*specs->test) * specs->n_tests);
909 specs->test[specs->n_tests - 1] = nt;
916 npar_binomial (struct lexer *lexer, struct dataset *ds,
917 struct npar_specs *specs)
919 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
920 struct one_sample_test *tp = &btp->parent;
921 struct npar_test *nt = &tp->parent;
924 nt->execute = binomial_execute;
925 nt->insert_variables = one_sample_insert_variables;
927 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
931 if (lex_match (lexer, T_LPAREN))
934 if (lex_force_num (lexer))
936 btp->p = lex_number (lexer);
938 if (!lex_force_match (lexer, T_RPAREN))
948 if (!lex_force_match (lexer, T_EQUALS))
952 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
953 &tp->vars, &tp->n_vars,
954 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
956 if (lex_match (lexer, T_LPAREN))
958 if (! lex_force_num (lexer))
960 btp->category1 = lex_number (lexer);
962 if (lex_match (lexer, T_COMMA))
964 if (! lex_force_num (lexer)) return 2;
965 btp->category2 = lex_number (lexer);
970 btp->cutpoint = btp->category1;
973 if (! lex_force_match (lexer, T_RPAREN))
984 specs->test = pool_realloc (specs->pool,
986 sizeof (*specs->test) * specs->n_tests);
988 specs->test[specs->n_tests - 1] = nt;
996 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
998 assert (params == 1 || params == 2);
1000 if (lex_is_number (lexer))
1002 kst->p[0] = lex_number (lexer);
1007 lex_match (lexer, T_COMMA);
1008 if (lex_force_num (lexer))
1010 kst->p[1] = lex_number (lexer);
1018 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
1020 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
1021 struct one_sample_test *tp = &kst->parent;
1022 struct npar_test *nt = &tp->parent;
1024 nt->execute = ks_one_sample_execute;
1025 nt->insert_variables = one_sample_insert_variables;
1027 kst->p[0] = kst->p[1] = SYSMIS;
1029 if (! lex_force_match (lexer, T_LPAREN))
1032 if (lex_match_id (lexer, "NORMAL"))
1034 kst->dist = KS_NORMAL;
1035 ks_one_sample_parse_params (lexer, kst, 2);
1037 else if (lex_match_id (lexer, "POISSON"))
1039 kst->dist = KS_POISSON;
1040 ks_one_sample_parse_params (lexer, kst, 1);
1042 else if (lex_match_id (lexer, "UNIFORM"))
1044 kst->dist = KS_UNIFORM;
1045 ks_one_sample_parse_params (lexer, kst, 2);
1047 else if (lex_match_id (lexer, "EXPONENTIAL"))
1049 kst->dist = KS_EXPONENTIAL;
1050 ks_one_sample_parse_params (lexer, kst, 1);
1055 if (! lex_force_match (lexer, T_RPAREN))
1058 lex_match (lexer, T_EQUALS);
1060 if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1061 &tp->vars, &tp->n_vars,
1062 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
1066 specs->test = pool_realloc (specs->pool,
1068 sizeof (*specs->test) * specs->n_tests);
1070 specs->test[specs->n_tests - 1] = nt;
1077 parse_two_sample_related_test (struct lexer *lexer,
1078 const struct dictionary *dict,
1079 struct two_sample_test *test_parameters,
1083 bool paired = false;
1085 const struct variable **vlist1;
1088 const struct variable **vlist2;
1091 test_parameters->parent.insert_variables = two_sample_insert_variables;
1093 if (!parse_variables_const_pool (lexer, pool,
1096 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
1099 if (lex_match (lexer, T_WITH))
1102 if (!parse_variables_const_pool (lexer, pool, dict,
1104 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
1107 paired = (lex_match (lexer, T_LPAREN) &&
1108 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1116 if (n_vlist1 != n_vlist2)
1118 msg (SE, _("PAIRED was specified but the number of variables "
1119 "preceding WITH (%zu) did not match the number "
1120 "following (%zu)."), n_vlist1, n_vlist2);
1124 test_parameters->n_pairs = n_vlist1 ;
1128 test_parameters->n_pairs = n_vlist1 * n_vlist2;
1133 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1136 test_parameters->pairs =
1137 pool_alloc (pool, sizeof (variable_pair) * test_parameters->n_pairs);
1144 assert (n_vlist1 == n_vlist2);
1145 for (i = 0 ; i < n_vlist1; ++i)
1147 test_parameters->pairs[n][0] = vlist1[i];
1148 test_parameters->pairs[n][1] = vlist2[i];
1155 for (i = 0 ; i < n_vlist1; ++i)
1157 for (j = 0 ; j < n_vlist2; ++j)
1159 test_parameters->pairs[n][0] = vlist1[i];
1160 test_parameters->pairs[n][1] = vlist2[j];
1169 for (i = 0 ; i < n_vlist1 - 1; ++i)
1171 for (j = i + 1 ; j < n_vlist1; ++j)
1173 assert (n < test_parameters->n_pairs);
1174 test_parameters->pairs[n][0] = vlist1[i];
1175 test_parameters->pairs[n][1] = vlist1[j];
1181 assert (n == test_parameters->n_pairs);
1188 parse_n_sample_related_test (struct lexer *lexer,
1189 const struct dictionary *dict,
1190 struct n_sample_test *nst,
1194 if (!parse_variables_const_pool (lexer, pool,
1196 &nst->vars, &nst->n_vars,
1197 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
1200 if (! lex_force_match (lexer, T_BY))
1203 nst->indep_var = parse_variable_const (lexer, dict);
1204 if (!nst->indep_var)
1207 if (! lex_force_match (lexer, T_LPAREN))
1210 value_init (&nst->val1, var_get_width (nst->indep_var));
1211 if (! parse_value (lexer, &nst->val1, nst->indep_var))
1213 value_destroy (&nst->val1, var_get_width (nst->indep_var));
1217 lex_match (lexer, T_COMMA);
1219 value_init (&nst->val2, var_get_width (nst->indep_var));
1220 if (! parse_value (lexer, &nst->val2, nst->indep_var))
1222 value_destroy (&nst->val2, var_get_width (nst->indep_var));
1226 if (! lex_force_match (lexer, T_RPAREN))
1233 npar_wilcoxon (struct lexer *lexer,
1235 struct npar_specs *specs)
1237 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1238 struct npar_test *nt = &tp->parent;
1239 nt->execute = wilcoxon_execute;
1241 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1246 specs->test = pool_realloc (specs->pool,
1248 sizeof (*specs->test) * specs->n_tests);
1249 specs->test[specs->n_tests - 1] = nt;
1256 npar_mann_whitney (struct lexer *lexer,
1258 struct npar_specs *specs)
1260 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1261 struct npar_test *nt = &tp->parent;
1263 nt->insert_variables = n_sample_insert_variables;
1264 nt->execute = mann_whitney_execute;
1266 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1271 specs->test = pool_realloc (specs->pool,
1273 sizeof (*specs->test) * specs->n_tests);
1274 specs->test[specs->n_tests - 1] = nt;
1281 npar_median (struct lexer *lexer,
1283 struct npar_specs *specs)
1285 struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
1286 struct n_sample_test *tp = &mt->parent;
1287 struct npar_test *nt = &tp->parent;
1289 mt->median = SYSMIS;
1291 if (lex_match (lexer, T_LPAREN) && lex_force_num (lexer))
1293 mt->median = lex_number (lexer);
1295 if (! lex_force_match (lexer, T_RPAREN))
1299 lex_match (lexer, T_EQUALS);
1301 nt->insert_variables = n_sample_insert_variables;
1302 nt->execute = median_execute;
1304 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1309 specs->test = pool_realloc (specs->pool,
1311 sizeof (*specs->test) * specs->n_tests);
1312 specs->test[specs->n_tests - 1] = nt;
1319 npar_sign (struct lexer *lexer, struct dataset *ds,
1320 struct npar_specs *specs)
1322 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1323 struct npar_test *nt = &tp->parent;
1325 nt->execute = sign_execute;
1327 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1332 specs->test = pool_realloc (specs->pool,
1334 sizeof (*specs->test) * specs->n_tests);
1335 specs->test[specs->n_tests - 1] = nt;
1342 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
1343 struct npar_specs *specs)
1345 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1346 struct npar_test *nt = &tp->parent;
1348 nt->execute = mcnemar_execute;
1350 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1355 specs->test = pool_realloc (specs->pool,
1357 sizeof (*specs->test) * specs->n_tests);
1358 specs->test[specs->n_tests - 1] = nt;
1365 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
1366 struct npar_specs *specs)
1368 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1369 struct npar_test *nt = &tp->parent;
1371 nt->insert_variables = n_sample_insert_variables;
1373 nt->execute = jonckheere_terpstra_execute;
1375 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1380 specs->test = pool_realloc (specs->pool,
1382 sizeof (*specs->test) * specs->n_tests);
1383 specs->test[specs->n_tests - 1] = nt;
1389 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1390 struct npar_specs *specs)
1392 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1393 struct npar_test *nt = &tp->parent;
1395 nt->insert_variables = n_sample_insert_variables;
1397 nt->execute = kruskal_wallis_execute;
1399 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1404 specs->test = pool_realloc (specs->pool,
1406 sizeof (*specs->test) * specs->n_tests);
1407 specs->test[specs->n_tests - 1] = nt;
1413 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1415 size_t hash = hash_pointer (var, 0);
1416 struct hmapx_node *node;
1417 const struct variable *v = NULL;
1419 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1425 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1428 /* Insert the variables for TEST into VAR_MAP */
1430 one_sample_insert_variables (const struct npar_test *test,
1431 struct hmapx *var_map)
1434 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1436 for (i = 0 ; i < ost->n_vars ; ++i)
1437 insert_variable_into_map (var_map, ost->vars[i]);
1442 two_sample_insert_variables (const struct npar_test *test,
1443 struct hmapx *var_map)
1446 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1448 for (i = 0 ; i < tst->n_pairs ; ++i)
1450 variable_pair *pair = &tst->pairs[i];
1452 insert_variable_into_map (var_map, (*pair)[0]);
1453 insert_variable_into_map (var_map, (*pair)[1]);
1458 n_sample_insert_variables (const struct npar_test *test,
1459 struct hmapx *var_map)
1462 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1464 for (i = 0 ; i < tst->n_vars ; ++i)
1465 insert_variable_into_map (var_map, tst->vars[i]);
1467 insert_variable_into_map (var_map, tst->indep_var);
1472 npar_method (struct lexer *lexer, struct npar_specs *specs)
1474 if (lex_match_id (lexer, "EXACT"))
1476 specs->exact = true;
1478 if (lex_match_id (lexer, "TIMER"))
1482 if (lex_match (lexer, T_LPAREN))
1484 if (lex_force_num (lexer))
1486 specs->timer = lex_number (lexer);
1489 if (lex_force_match (lexer, T_RPAREN))