1 /* PSPP - a program for statistical analysis. -*-c-*-
2 Copyright (C) 2006, 2008, 2009, 2010, 2011 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)
160 npt->ks_one_sample = 0;
163 npt->kruskal_wallis = 0;
164 npt->mann_whitney = 0;
165 npt->jonckheere_terpstra = 0;
171 npt->miss = MISS_ANALYSIS;
174 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
177 if (lex_match_id (lexer, "COCHRAN"))
180 switch (npar_cochran (lexer, ds, nps))
187 lex_error (lexer, NULL);
193 else if (lex_match_id (lexer, "FRIEDMAN"))
196 switch (npar_friedman (lexer, ds, nps))
203 lex_error (lexer, NULL);
209 else if (lex_match_id (lexer, "KENDALL"))
212 switch (npar_kendall (lexer, ds, nps))
219 lex_error (lexer, NULL);
225 else if (lex_match_id (lexer, "RUNS"))
228 switch (npar_runs (lexer, ds, nps))
235 lex_error (lexer, NULL);
241 else if (lex_match_id (lexer, "CHISQUARE"))
243 lex_match (lexer, T_EQUALS);
245 switch (npar_chisquare (lexer, ds, nps))
252 lex_error (lexer, NULL);
260 else if (lex_match_id (lexer, "BINOMIAL"))
262 lex_match (lexer, T_EQUALS);
264 switch (npar_binomial (lexer, ds, nps))
271 lex_error (lexer, NULL);
277 else if (lex_match_phrase (lexer, "K-S") ||
278 lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
280 lex_match (lexer, T_EQUALS);
281 npt->ks_one_sample++;
282 switch (npar_ks_one_sample (lexer, ds, nps))
289 lex_error (lexer, NULL);
295 else if (lex_match_phrase (lexer, "J-T") ||
296 lex_match_phrase (lexer, "JONCKHEERE-TERPSTRA"))
298 lex_match (lexer, T_EQUALS);
299 npt->jonckheere_terpstra++;
300 switch (npar_jonckheere_terpstra (lexer, ds, nps))
307 lex_error (lexer, NULL);
313 else if (lex_match_phrase (lexer, "K-W") ||
314 lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
316 lex_match (lexer, T_EQUALS);
317 npt->kruskal_wallis++;
318 switch (npar_kruskal_wallis (lexer, ds, nps))
325 lex_error (lexer, NULL);
331 else if (lex_match_phrase (lexer, "MCNEMAR"))
333 lex_match (lexer, T_EQUALS);
335 switch (npar_mcnemar (lexer, ds, nps))
342 lex_error (lexer, NULL);
348 else if (lex_match_phrase (lexer, "M-W") ||
349 lex_match_phrase (lexer, "MANN-WHITNEY"))
351 lex_match (lexer, T_EQUALS);
353 switch (npar_mann_whitney (lexer, ds, nps))
360 lex_error (lexer, NULL);
366 else if (lex_match_phrase (lexer, "MEDIAN"))
370 switch (npar_median (lexer, ds, nps))
377 lex_error (lexer, NULL);
383 else if (lex_match_id (lexer, "WILCOXON"))
385 lex_match (lexer, T_EQUALS);
387 switch (npar_wilcoxon (lexer, ds, nps))
394 lex_error (lexer, NULL);
400 else if (lex_match_id (lexer, "SIGN"))
402 lex_match (lexer, T_EQUALS);
404 switch (npar_sign (lexer, ds, nps))
411 lex_error (lexer, NULL);
417 else if (lex_match_id (lexer, "MISSING"))
419 lex_match (lexer, T_EQUALS);
421 if (npt->missing > 1)
423 lex_sbc_only_once ("MISSING");
426 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
428 if (lex_match_id (lexer, "ANALYSIS"))
429 npt->miss = MISS_ANALYSIS;
430 else if (lex_match_id (lexer, "LISTWISE"))
431 npt->miss = MISS_LISTWISE;
432 else if (lex_match_id (lexer, "INCLUDE"))
433 nps->filter = MV_SYSTEM;
434 else if (lex_match_id (lexer, "EXCLUDE"))
435 nps->filter = MV_ANY;
438 lex_error (lexer, NULL);
441 lex_match (lexer, T_COMMA);
444 else if (lex_match_id (lexer, "METHOD"))
446 lex_match (lexer, T_EQUALS);
450 lex_sbc_only_once ("METHOD");
453 switch (npar_method (lexer, nps))
460 lex_error (lexer, NULL);
466 else if (lex_match_id (lexer, "STATISTICS"))
468 lex_match (lexer, T_EQUALS);
470 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
472 if (lex_match_id (lexer, "DESCRIPTIVES"))
473 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
474 else if (lex_match_id (lexer, "QUARTILES"))
475 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
476 else if (lex_match (lexer, T_ALL))
477 npt->a_statistics[NPAR_ST_ALL] = 1;
480 lex_error (lexer, NULL);
483 lex_match (lexer, T_COMMA);
486 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
488 lex_match (lexer, T_EQUALS);
489 if (lex_match_id (lexer, "COMPATIBLE"))
490 settings_set_cmd_algorithm (COMPATIBLE);
491 else if (lex_match_id (lexer, "ENHANCED"))
492 settings_set_cmd_algorithm (ENHANCED);
494 if (!lex_match (lexer, T_SLASH))
498 if (lex_token (lexer) != T_ENDCMD)
500 lex_error (lexer, _("expecting end of command"));
511 static void one_sample_insert_variables (const struct npar_test *test,
514 static void two_sample_insert_variables (const struct npar_test *test,
517 static void n_sample_insert_variables (const struct npar_test *test,
521 npar_execute (struct casereader *input,
522 const struct npar_specs *specs,
523 const struct dataset *ds)
526 struct descriptives *summary_descriptives = NULL;
528 for ( t = 0 ; t < specs->n_tests; ++t )
530 const struct npar_test *test = specs->test[t];
531 if ( NULL == test->execute )
533 msg (SW, _("NPAR subcommand not currently implemented."));
536 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
539 if ( specs->descriptives )
541 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
544 npar_summary_calc_descriptives (summary_descriptives,
545 casereader_clone (input),
547 specs->vv, specs->n_vars,
551 if ( (specs->descriptives || specs->quartiles)
552 && !taint_has_tainted_successor (casereader_get_taint (input)) )
553 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
555 free (summary_descriptives);
556 casereader_destroy (input);
560 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
562 struct cmd_npar_tests cmd;
565 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
566 struct casegrouper *grouper;
567 struct casereader *input, *group;
568 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
571 npar_specs.pool = pool_create ();
572 npar_specs.filter = MV_ANY;
573 npar_specs.n_vars = -1;
574 npar_specs.vv = NULL;
576 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
578 pool_destroy (npar_specs.pool);
582 for (i = 0; i < npar_specs.n_tests; ++i )
584 const struct npar_test *test = npar_specs.test[i];
585 test->insert_variables (test, &var_map);
589 struct hmapx_node *node;
590 struct variable *var;
591 npar_specs.n_vars = 0;
593 HMAPX_FOR_EACH (var, node, &var_map)
595 npar_specs.n_vars ++;
596 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
597 npar_specs.vv[npar_specs.n_vars - 1] = var;
601 sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
602 compare_var_ptrs_by_name, NULL);
604 if ( cmd.statistics )
608 for ( i = 0 ; i < NPAR_ST_count; ++i )
610 if ( cmd.a_statistics[i] )
614 case NPAR_ST_DESCRIPTIVES:
615 npar_specs.descriptives = true;
617 case NPAR_ST_QUARTILES:
618 npar_specs.quartiles = true;
621 npar_specs.quartiles = true;
622 npar_specs.descriptives = true;
631 input = proc_open (ds);
632 if ( cmd.miss == MISS_LISTWISE )
634 input = casereader_create_filter_missing (input,
642 grouper = casegrouper_create_splits (input, dataset_dict (ds));
643 while (casegrouper_get_next_group (grouper, &group))
644 npar_execute (group, &npar_specs, ds);
645 ok = casegrouper_destroy (grouper);
646 ok = proc_commit (ds) && ok;
648 pool_destroy (npar_specs.pool);
649 hmapx_destroy (&var_map);
651 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
655 npar_runs (struct lexer *lexer, struct dataset *ds,
656 struct npar_specs *specs)
658 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
659 struct one_sample_test *tp = &rt->parent;
660 struct npar_test *nt = &tp->parent;
662 nt->execute = runs_execute;
663 nt->insert_variables = one_sample_insert_variables;
665 if ( lex_force_match (lexer, T_LPAREN) )
667 if ( lex_match_id (lexer, "MEAN"))
669 rt->cp_mode = CP_MEAN;
671 else if (lex_match_id (lexer, "MEDIAN"))
673 rt->cp_mode = CP_MEDIAN;
675 else if (lex_match_id (lexer, "MODE"))
677 rt->cp_mode = CP_MODE;
679 else if (lex_is_number (lexer))
681 rt->cutpoint = lex_number (lexer);
682 rt->cp_mode = CP_CUSTOM;
687 lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
691 lex_force_match (lexer, T_RPAREN);
692 lex_force_match (lexer, T_EQUALS);
693 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
694 &tp->vars, &tp->n_vars,
695 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
702 specs->test = pool_realloc (specs->pool,
704 sizeof (*specs->test) * specs->n_tests);
706 specs->test[specs->n_tests - 1] = nt;
712 npar_friedman (struct lexer *lexer, struct dataset *ds,
713 struct npar_specs *specs)
715 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
716 struct one_sample_test *ost = &ft->parent;
717 struct npar_test *nt = &ost->parent;
719 ft->kendalls_w = false;
720 nt->execute = friedman_execute;
721 nt->insert_variables = one_sample_insert_variables;
723 lex_match (lexer, T_EQUALS);
725 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
726 &ost->vars, &ost->n_vars,
727 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
733 specs->test = pool_realloc (specs->pool,
735 sizeof (*specs->test) * specs->n_tests);
737 specs->test[specs->n_tests - 1] = nt;
743 npar_kendall (struct lexer *lexer, struct dataset *ds,
744 struct npar_specs *specs)
746 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
747 struct one_sample_test *ost = &kt->parent;
748 struct npar_test *nt = &ost->parent;
750 kt->kendalls_w = true;
751 nt->execute = friedman_execute;
752 nt->insert_variables = one_sample_insert_variables;
754 lex_match (lexer, T_EQUALS);
756 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
757 &ost->vars, &ost->n_vars,
758 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
764 specs->test = pool_realloc (specs->pool,
766 sizeof (*specs->test) * specs->n_tests);
768 specs->test[specs->n_tests - 1] = nt;
775 npar_cochran (struct lexer *lexer, struct dataset *ds,
776 struct npar_specs *specs)
778 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
779 struct npar_test *nt = &ft->parent;
781 nt->execute = cochran_execute;
782 nt->insert_variables = one_sample_insert_variables;
784 lex_match (lexer, T_EQUALS);
786 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
787 &ft->vars, &ft->n_vars,
788 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
794 specs->test = pool_realloc (specs->pool,
796 sizeof (*specs->test) * specs->n_tests);
798 specs->test[specs->n_tests - 1] = nt;
805 npar_chisquare (struct lexer *lexer, struct dataset *ds,
806 struct npar_specs *specs)
808 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
809 struct one_sample_test *tp = &cstp->parent;
810 struct npar_test *nt = &tp->parent;
813 nt->execute = chisquare_execute;
814 nt->insert_variables = one_sample_insert_variables;
816 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
817 &tp->vars, &tp->n_vars,
818 PV_NO_SCRATCH | PV_NO_DUPLICATE))
823 cstp->ranged = false;
825 if ( lex_match (lexer, T_LPAREN))
828 if ( ! lex_force_num (lexer)) return 0;
829 cstp->lo = lex_integer (lexer);
831 lex_force_match (lexer, T_COMMA);
832 if (! lex_force_num (lexer) ) return 0;
833 cstp->hi = lex_integer (lexer);
834 if ( cstp->lo >= cstp->hi )
837 _("The specified value of HI (%d) is "
838 "lower than the specified value of LO (%d)"),
843 if (! lex_force_match (lexer, T_RPAREN)) return 0;
846 cstp->n_expected = 0;
847 cstp->expected = NULL;
848 if (lex_match_phrase (lexer, "/EXPECTED"))
850 lex_force_match (lexer, T_EQUALS);
851 if ( ! lex_match_id (lexer, "EQUAL") )
855 while ( lex_is_number (lexer) )
859 f = lex_number (lexer);
861 if ( lex_match (lexer, T_ASTERISK))
864 f = lex_number (lexer);
867 lex_match (lexer, T_COMMA);
869 cstp->n_expected += n;
870 cstp->expected = pool_realloc (specs->pool,
874 for ( i = cstp->n_expected - n ;
875 i < cstp->n_expected;
877 cstp->expected[i] = f;
883 if ( cstp->ranged && cstp->n_expected > 0 &&
884 cstp->n_expected != cstp->hi - cstp->lo + 1 )
887 _("%d expected values were given, but the specified "
888 "range (%d-%d) requires exactly %d values."),
889 cstp->n_expected, cstp->lo, cstp->hi,
890 cstp->hi - cstp->lo +1);
895 specs->test = pool_realloc (specs->pool,
897 sizeof (*specs->test) * specs->n_tests);
899 specs->test[specs->n_tests - 1] = nt;
906 npar_binomial (struct lexer *lexer, struct dataset *ds,
907 struct npar_specs *specs)
909 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
910 struct one_sample_test *tp = &btp->parent;
911 struct npar_test *nt = &tp->parent;
914 nt->execute = binomial_execute;
915 nt->insert_variables = one_sample_insert_variables;
917 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
921 if ( lex_match (lexer, T_LPAREN) )
924 if ( lex_force_num (lexer) )
926 btp->p = lex_number (lexer);
928 lex_force_match (lexer, T_RPAREN);
936 if (equals || lex_match (lexer, T_EQUALS) )
938 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
939 &tp->vars, &tp->n_vars,
940 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
942 if (lex_match (lexer, T_LPAREN))
944 lex_force_num (lexer);
945 btp->category1 = lex_number (lexer);
947 if ( lex_match (lexer, T_COMMA))
949 if ( ! lex_force_num (lexer) ) return 2;
950 btp->category2 = lex_number (lexer);
955 btp->cutpoint = btp->category1;
958 lex_force_match (lexer, T_RPAREN);
967 specs->test = pool_realloc (specs->pool,
969 sizeof (*specs->test) * specs->n_tests);
971 specs->test[specs->n_tests - 1] = nt;
979 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
981 assert (params == 1 || params == 2);
983 if (lex_is_number (lexer))
985 kst->p[0] = lex_number (lexer);
990 lex_match (lexer, T_COMMA);
991 if (lex_force_num (lexer))
993 kst->p[1] = lex_number (lexer);
1001 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
1003 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
1004 struct one_sample_test *tp = &kst->parent;
1005 struct npar_test *nt = &tp->parent;
1007 nt->execute = ks_one_sample_execute;
1008 nt->insert_variables = one_sample_insert_variables;
1010 kst->p[0] = kst->p[1] = SYSMIS;
1012 if (! lex_force_match (lexer, T_LPAREN))
1015 if (lex_match_id (lexer, "NORMAL"))
1017 kst->dist = KS_NORMAL;
1018 ks_one_sample_parse_params (lexer, kst, 2);
1020 else if (lex_match_id (lexer, "POISSON"))
1022 kst->dist = KS_POISSON;
1023 ks_one_sample_parse_params (lexer, kst, 1);
1025 else if (lex_match_id (lexer, "UNIFORM"))
1027 kst->dist = KS_UNIFORM;
1028 ks_one_sample_parse_params (lexer, kst, 2);
1030 else if (lex_match_id (lexer, "EXPONENTIAL"))
1032 kst->dist = KS_EXPONENTIAL;
1033 ks_one_sample_parse_params (lexer, kst, 1);
1038 if (! lex_force_match (lexer, T_RPAREN))
1041 lex_match (lexer, T_EQUALS);
1043 if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1044 &tp->vars, &tp->n_vars,
1045 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1049 specs->test = pool_realloc (specs->pool,
1051 sizeof (*specs->test) * specs->n_tests);
1053 specs->test[specs->n_tests - 1] = nt;
1060 parse_two_sample_related_test (struct lexer *lexer,
1061 const struct dictionary *dict,
1062 struct two_sample_test *test_parameters,
1066 bool paired = false;
1068 const struct variable **vlist1;
1071 const struct variable **vlist2;
1074 test_parameters->parent.insert_variables = two_sample_insert_variables;
1076 if (!parse_variables_const_pool (lexer, pool,
1079 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1082 if ( lex_match (lexer, T_WITH))
1085 if ( !parse_variables_const_pool (lexer, pool, dict,
1087 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1090 paired = (lex_match (lexer, T_LPAREN) &&
1091 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1099 if ( n_vlist1 != n_vlist2)
1100 msg (SE, _("PAIRED was specified but the number of variables "
1101 "preceding WITH (%zu) did not match the number "
1102 "following (%zu)."), n_vlist1, n_vlist2);
1104 test_parameters->n_pairs = n_vlist1 ;
1108 test_parameters->n_pairs = n_vlist1 * n_vlist2;
1113 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1116 test_parameters->pairs =
1117 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
1124 assert (n_vlist1 == n_vlist2);
1125 for ( i = 0 ; i < n_vlist1; ++i )
1127 test_parameters->pairs[n][0] = vlist1[i];
1128 test_parameters->pairs[n][1] = vlist2[i];
1135 for ( i = 0 ; i < n_vlist1; ++i )
1137 for ( j = 0 ; j < n_vlist2; ++j )
1139 test_parameters->pairs[n][0] = vlist1[i];
1140 test_parameters->pairs[n][1] = vlist2[j];
1149 for ( i = 0 ; i < n_vlist1 - 1; ++i )
1151 for ( j = i + 1 ; j < n_vlist1; ++j )
1153 assert ( n < test_parameters->n_pairs);
1154 test_parameters->pairs[n][0] = vlist1[i];
1155 test_parameters->pairs[n][1] = vlist1[j];
1161 assert ( n == test_parameters->n_pairs);
1168 parse_n_sample_related_test (struct lexer *lexer,
1169 const struct dictionary *dict,
1170 struct n_sample_test *nst,
1174 if (!parse_variables_const_pool (lexer, pool,
1176 &nst->vars, &nst->n_vars,
1177 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1180 if ( ! lex_force_match (lexer, T_BY))
1183 nst->indep_var = parse_variable_const (lexer, dict);
1185 if ( ! lex_force_match (lexer, T_LPAREN))
1188 value_init (&nst->val1, var_get_width (nst->indep_var));
1189 if ( ! parse_value (lexer, &nst->val1, nst->indep_var))
1191 value_destroy (&nst->val1, var_get_width (nst->indep_var));
1195 lex_match (lexer, T_COMMA);
1197 value_init (&nst->val2, var_get_width (nst->indep_var));
1198 if ( ! parse_value (lexer, &nst->val2, nst->indep_var))
1200 value_destroy (&nst->val2, var_get_width (nst->indep_var));
1204 if ( ! lex_force_match (lexer, T_RPAREN))
1211 npar_wilcoxon (struct lexer *lexer,
1213 struct npar_specs *specs )
1215 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1216 struct npar_test *nt = &tp->parent;
1217 nt->execute = wilcoxon_execute;
1219 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1224 specs->test = pool_realloc (specs->pool,
1226 sizeof (*specs->test) * specs->n_tests);
1227 specs->test[specs->n_tests - 1] = nt;
1234 npar_mann_whitney (struct lexer *lexer,
1236 struct npar_specs *specs )
1238 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1239 struct npar_test *nt = &tp->parent;
1241 nt->insert_variables = n_sample_insert_variables;
1242 nt->execute = mann_whitney_execute;
1244 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1249 specs->test = pool_realloc (specs->pool,
1251 sizeof (*specs->test) * specs->n_tests);
1252 specs->test[specs->n_tests - 1] = nt;
1259 npar_median (struct lexer *lexer,
1261 struct npar_specs *specs)
1263 struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
1264 struct n_sample_test *tp = &mt->parent;
1265 struct npar_test *nt = &tp->parent;
1267 mt->median = SYSMIS;
1269 if ( lex_match (lexer, T_LPAREN))
1271 lex_force_num (lexer);
1272 mt->median = lex_number (lexer);
1274 lex_force_match (lexer, T_RPAREN);
1277 lex_match (lexer, T_EQUALS);
1279 nt->insert_variables = n_sample_insert_variables;
1280 nt->execute = median_execute;
1282 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1287 specs->test = pool_realloc (specs->pool,
1289 sizeof (*specs->test) * specs->n_tests);
1290 specs->test[specs->n_tests - 1] = nt;
1297 npar_sign (struct lexer *lexer, struct dataset *ds,
1298 struct npar_specs *specs)
1300 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1301 struct npar_test *nt = &tp->parent;
1303 nt->execute = sign_execute;
1305 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1310 specs->test = pool_realloc (specs->pool,
1312 sizeof (*specs->test) * specs->n_tests);
1313 specs->test[specs->n_tests - 1] = nt;
1320 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
1321 struct npar_specs *specs)
1323 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1324 struct npar_test *nt = &tp->parent;
1326 nt->execute = mcnemar_execute;
1328 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1333 specs->test = pool_realloc (specs->pool,
1335 sizeof (*specs->test) * specs->n_tests);
1336 specs->test[specs->n_tests - 1] = nt;
1343 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
1344 struct npar_specs *specs)
1346 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1347 struct npar_test *nt = &tp->parent;
1349 nt->insert_variables = n_sample_insert_variables;
1351 nt->execute = jonckheere_terpstra_execute;
1353 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1358 specs->test = pool_realloc (specs->pool,
1360 sizeof (*specs->test) * specs->n_tests);
1361 specs->test[specs->n_tests - 1] = nt;
1367 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1368 struct npar_specs *specs)
1370 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1371 struct npar_test *nt = &tp->parent;
1373 nt->insert_variables = n_sample_insert_variables;
1375 nt->execute = kruskal_wallis_execute;
1377 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1382 specs->test = pool_realloc (specs->pool,
1384 sizeof (*specs->test) * specs->n_tests);
1385 specs->test[specs->n_tests - 1] = nt;
1391 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1393 size_t hash = hash_pointer (var, 0);
1394 struct hmapx_node *node;
1395 const struct variable *v = NULL;
1397 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1403 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1406 /* Insert the variables for TEST into VAR_MAP */
1408 one_sample_insert_variables (const struct npar_test *test,
1409 struct hmapx *var_map)
1412 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1414 for ( i = 0 ; i < ost->n_vars ; ++i )
1415 insert_variable_into_map (var_map, ost->vars[i]);
1420 two_sample_insert_variables (const struct npar_test *test,
1421 struct hmapx *var_map)
1424 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1426 for ( i = 0 ; i < tst->n_pairs ; ++i )
1428 variable_pair *pair = &tst->pairs[i];
1430 insert_variable_into_map (var_map, (*pair)[0]);
1431 insert_variable_into_map (var_map, (*pair)[1]);
1436 n_sample_insert_variables (const struct npar_test *test,
1437 struct hmapx *var_map)
1440 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1442 for ( i = 0 ; i < tst->n_vars ; ++i )
1443 insert_variable_into_map (var_map, tst->vars[i]);
1445 insert_variable_into_map (var_map, tst->indep_var);
1450 npar_method (struct lexer *lexer, struct npar_specs *specs)
1452 if ( lex_match_id (lexer, "EXACT") )
1454 specs->exact = true;
1456 if (lex_match_id (lexer, "TIMER"))
1460 if ( lex_match (lexer, T_LPAREN))
1462 if ( lex_force_num (lexer) )
1464 specs->timer = lex_number (lexer);
1467 lex_force_match (lexer, T_RPAREN);