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/kruskal-wallis.h"
41 #include "language/stats/mann-whitney.h"
42 #include "language/stats/mcnemar.h"
43 #include "language/stats/median.h"
44 #include "language/stats/npar-summary.h"
45 #include "language/stats/runs.h"
46 #include "language/stats/sign.h"
47 #include "language/stats/wilcoxon.h"
48 #include "libpspp/array.h"
49 #include "libpspp/assertion.h"
50 #include "libpspp/cast.h"
51 #include "libpspp/hash-functions.h"
52 #include "libpspp/hmapx.h"
53 #include "libpspp/message.h"
54 #include "libpspp/pool.h"
55 #include "libpspp/str.h"
56 #include "libpspp/taint.h"
57 #include "math/moments.h"
59 #include "gl/xalloc.h"
62 #define _(msgid) gettext (msgid)
64 /* Settings for subcommand specifiers. */
71 /* Array indices for STATISTICS subcommand. */
74 NPAR_ST_DESCRIPTIVES = 0,
75 NPAR_ST_QUARTILES = 1,
80 /* NPAR TESTS structure. */
83 /* Count variables indicating how many
84 of the subcommands have been given. */
102 /* How missing values should be treated */
105 /* Which statistics have been requested */
106 int a_statistics[NPAR_ST_count];
113 struct npar_test **test;
116 const struct variable **vv; /* Compendium of all variables
117 (those mentioned on ANY subcommand */
118 int n_vars; /* Number of variables in vv */
120 enum mv_class filter; /* Missing values to filter. */
122 bool descriptives; /* Descriptive statistics should be calculated */
123 bool quartiles; /* Quartiles should be calculated */
125 bool exact; /* Whether exact calculations have been requested */
126 double timer; /* Maximum time (in minutes) to wait for exact calculations */
130 /* Prototype for custom subcommands of NPAR TESTS. */
131 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
132 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
133 static int npar_ks_one_sample (struct lexer *, struct dataset *, struct npar_specs *);
134 static int npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
135 static int npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
136 static int npar_kendall (struct lexer *, struct dataset *, struct npar_specs *);
137 static int npar_cochran (struct lexer *, struct dataset *, struct npar_specs *);
138 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
139 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
140 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
141 static int npar_mann_whitney (struct lexer *, struct dataset *, struct npar_specs *);
142 static int npar_mcnemar (struct lexer *, struct dataset *, struct npar_specs *);
143 static int npar_median (struct lexer *, struct dataset *, struct npar_specs *);
145 static int npar_method (struct lexer *, struct npar_specs *);
147 /* Command parsing functions. */
148 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
149 struct npar_specs *npar_specs );
152 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
153 struct npar_specs *nps)
157 npt->ks_one_sample = 0;
160 npt->kruskal_wallis = 0;
161 npt->mann_whitney = 0;
167 npt->miss = MISS_ANALYSIS;
170 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
173 if (lex_match_id (lexer, "COCHRAN"))
176 switch (npar_cochran (lexer, ds, nps))
183 lex_error (lexer, NULL);
189 else if (lex_match_id (lexer, "FRIEDMAN"))
192 switch (npar_friedman (lexer, ds, nps))
199 lex_error (lexer, NULL);
205 else if (lex_match_id (lexer, "KENDALL"))
208 switch (npar_kendall (lexer, ds, nps))
215 lex_error (lexer, NULL);
221 else if (lex_match_id (lexer, "RUNS"))
224 switch (npar_runs (lexer, ds, nps))
231 lex_error (lexer, NULL);
237 else if (lex_match_id (lexer, "CHISQUARE"))
239 lex_match (lexer, T_EQUALS);
241 switch (npar_chisquare (lexer, ds, nps))
248 lex_error (lexer, NULL);
256 else if (lex_match_id (lexer, "BINOMIAL"))
258 lex_match (lexer, T_EQUALS);
260 switch (npar_binomial (lexer, ds, nps))
267 lex_error (lexer, NULL);
273 else if (lex_match_phrase (lexer, "K-S") ||
274 lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
276 lex_match (lexer, T_EQUALS);
277 npt->ks_one_sample++;
278 switch (npar_ks_one_sample (lexer, ds, nps))
285 lex_error (lexer, NULL);
291 else if (lex_match_phrase (lexer, "K-W") ||
292 lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
294 lex_match (lexer, T_EQUALS);
295 npt->kruskal_wallis++;
296 switch (npar_kruskal_wallis (lexer, ds, nps))
303 lex_error (lexer, NULL);
309 else if (lex_match_phrase (lexer, "MCNEMAR"))
311 lex_match (lexer, T_EQUALS);
313 switch (npar_mcnemar (lexer, ds, nps))
320 lex_error (lexer, NULL);
326 else if (lex_match_phrase (lexer, "M-W") ||
327 lex_match_phrase (lexer, "MANN-WHITNEY"))
329 lex_match (lexer, T_EQUALS);
331 switch (npar_mann_whitney (lexer, ds, nps))
338 lex_error (lexer, NULL);
344 else if (lex_match_phrase (lexer, "MEDIAN"))
348 switch (npar_median (lexer, ds, nps))
355 lex_error (lexer, NULL);
361 else if (lex_match_id (lexer, "WILCOXON"))
363 lex_match (lexer, T_EQUALS);
365 switch (npar_wilcoxon (lexer, ds, nps))
372 lex_error (lexer, NULL);
378 else if (lex_match_id (lexer, "SIGN"))
380 lex_match (lexer, T_EQUALS);
382 switch (npar_sign (lexer, ds, nps))
389 lex_error (lexer, NULL);
395 else if (lex_match_id (lexer, "MISSING"))
397 lex_match (lexer, T_EQUALS);
399 if (npt->missing > 1)
401 lex_sbc_only_once ("MISSING");
404 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
406 if (lex_match_id (lexer, "ANALYSIS"))
407 npt->miss = MISS_ANALYSIS;
408 else if (lex_match_id (lexer, "LISTWISE"))
409 npt->miss = MISS_LISTWISE;
410 else if (lex_match_id (lexer, "INCLUDE"))
411 nps->filter = MV_SYSTEM;
412 else if (lex_match_id (lexer, "EXCLUDE"))
413 nps->filter = MV_ANY;
416 lex_error (lexer, NULL);
419 lex_match (lexer, T_COMMA);
422 else if (lex_match_id (lexer, "METHOD"))
424 lex_match (lexer, T_EQUALS);
428 lex_sbc_only_once ("METHOD");
431 switch (npar_method (lexer, nps))
438 lex_error (lexer, NULL);
444 else if (lex_match_id (lexer, "STATISTICS"))
446 lex_match (lexer, T_EQUALS);
448 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
450 if (lex_match_id (lexer, "DESCRIPTIVES"))
451 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
452 else if (lex_match_id (lexer, "QUARTILES"))
453 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
454 else if (lex_match (lexer, T_ALL))
455 npt->a_statistics[NPAR_ST_ALL] = 1;
458 lex_error (lexer, NULL);
461 lex_match (lexer, T_COMMA);
464 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
466 lex_match (lexer, T_EQUALS);
467 if (lex_match_id (lexer, "COMPATIBLE"))
468 settings_set_cmd_algorithm (COMPATIBLE);
469 else if (lex_match_id (lexer, "ENHANCED"))
470 settings_set_cmd_algorithm (ENHANCED);
472 if (!lex_match (lexer, T_SLASH))
476 if (lex_token (lexer) != T_ENDCMD)
478 lex_error (lexer, _("expecting end of command"));
489 static void one_sample_insert_variables (const struct npar_test *test,
492 static void two_sample_insert_variables (const struct npar_test *test,
495 static void n_sample_insert_variables (const struct npar_test *test,
499 npar_execute (struct casereader *input,
500 const struct npar_specs *specs,
501 const struct dataset *ds)
504 struct descriptives *summary_descriptives = NULL;
506 for ( t = 0 ; t < specs->n_tests; ++t )
508 const struct npar_test *test = specs->test[t];
509 if ( NULL == test->execute )
511 msg (SW, _("NPAR subcommand not currently implemented."));
514 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
517 if ( specs->descriptives )
519 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
522 npar_summary_calc_descriptives (summary_descriptives,
523 casereader_clone (input),
525 specs->vv, specs->n_vars,
529 if ( (specs->descriptives || specs->quartiles)
530 && !taint_has_tainted_successor (casereader_get_taint (input)) )
531 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
533 free (summary_descriptives);
534 casereader_destroy (input);
538 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
540 struct cmd_npar_tests cmd;
543 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
544 struct casegrouper *grouper;
545 struct casereader *input, *group;
546 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
549 npar_specs.pool = pool_create ();
550 npar_specs.filter = MV_ANY;
551 npar_specs.n_vars = -1;
552 npar_specs.vv = NULL;
554 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
556 pool_destroy (npar_specs.pool);
560 for (i = 0; i < npar_specs.n_tests; ++i )
562 const struct npar_test *test = npar_specs.test[i];
563 test->insert_variables (test, &var_map);
567 struct hmapx_node *node;
568 struct variable *var;
569 npar_specs.n_vars = 0;
571 HMAPX_FOR_EACH (var, node, &var_map)
573 npar_specs.n_vars ++;
574 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
575 npar_specs.vv[npar_specs.n_vars - 1] = var;
579 sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
580 compare_var_ptrs_by_name, NULL);
582 if ( cmd.statistics )
586 for ( i = 0 ; i < NPAR_ST_count; ++i )
588 if ( cmd.a_statistics[i] )
592 case NPAR_ST_DESCRIPTIVES:
593 npar_specs.descriptives = true;
595 case NPAR_ST_QUARTILES:
596 npar_specs.quartiles = true;
599 npar_specs.quartiles = true;
600 npar_specs.descriptives = true;
609 input = proc_open (ds);
610 if ( cmd.miss == MISS_LISTWISE )
612 input = casereader_create_filter_missing (input,
620 grouper = casegrouper_create_splits (input, dataset_dict (ds));
621 while (casegrouper_get_next_group (grouper, &group))
622 npar_execute (group, &npar_specs, ds);
623 ok = casegrouper_destroy (grouper);
624 ok = proc_commit (ds) && ok;
626 pool_destroy (npar_specs.pool);
627 hmapx_destroy (&var_map);
629 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
633 npar_runs (struct lexer *lexer, struct dataset *ds,
634 struct npar_specs *specs)
636 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
637 struct one_sample_test *tp = &rt->parent;
638 struct npar_test *nt = &tp->parent;
640 nt->execute = runs_execute;
641 nt->insert_variables = one_sample_insert_variables;
643 if ( lex_force_match (lexer, T_LPAREN) )
645 if ( lex_match_id (lexer, "MEAN"))
647 rt->cp_mode = CP_MEAN;
649 else if (lex_match_id (lexer, "MEDIAN"))
651 rt->cp_mode = CP_MEDIAN;
653 else if (lex_match_id (lexer, "MODE"))
655 rt->cp_mode = CP_MODE;
657 else if (lex_is_number (lexer))
659 rt->cutpoint = lex_number (lexer);
660 rt->cp_mode = CP_CUSTOM;
665 lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
669 lex_force_match (lexer, T_RPAREN);
670 lex_force_match (lexer, T_EQUALS);
671 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
672 &tp->vars, &tp->n_vars,
673 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
680 specs->test = pool_realloc (specs->pool,
682 sizeof (*specs->test) * specs->n_tests);
684 specs->test[specs->n_tests - 1] = nt;
690 npar_friedman (struct lexer *lexer, struct dataset *ds,
691 struct npar_specs *specs)
693 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
694 struct one_sample_test *ost = &ft->parent;
695 struct npar_test *nt = &ost->parent;
697 ft->kendalls_w = false;
698 nt->execute = friedman_execute;
699 nt->insert_variables = one_sample_insert_variables;
701 lex_match (lexer, T_EQUALS);
703 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
704 &ost->vars, &ost->n_vars,
705 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
711 specs->test = pool_realloc (specs->pool,
713 sizeof (*specs->test) * specs->n_tests);
715 specs->test[specs->n_tests - 1] = nt;
721 npar_kendall (struct lexer *lexer, struct dataset *ds,
722 struct npar_specs *specs)
724 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
725 struct one_sample_test *ost = &kt->parent;
726 struct npar_test *nt = &ost->parent;
728 kt->kendalls_w = true;
729 nt->execute = friedman_execute;
730 nt->insert_variables = one_sample_insert_variables;
732 lex_match (lexer, T_EQUALS);
734 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
735 &ost->vars, &ost->n_vars,
736 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
742 specs->test = pool_realloc (specs->pool,
744 sizeof (*specs->test) * specs->n_tests);
746 specs->test[specs->n_tests - 1] = nt;
753 npar_cochran (struct lexer *lexer, struct dataset *ds,
754 struct npar_specs *specs)
756 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
757 struct npar_test *nt = &ft->parent;
759 nt->execute = cochran_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 &ft->vars, &ft->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_chisquare (struct lexer *lexer, struct dataset *ds,
784 struct npar_specs *specs)
786 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
787 struct one_sample_test *tp = &cstp->parent;
788 struct npar_test *nt = &tp->parent;
791 nt->execute = chisquare_execute;
792 nt->insert_variables = one_sample_insert_variables;
794 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
795 &tp->vars, &tp->n_vars,
796 PV_NO_SCRATCH | PV_NO_DUPLICATE))
801 cstp->ranged = false;
803 if ( lex_match (lexer, T_LPAREN))
806 if ( ! lex_force_num (lexer)) return 0;
807 cstp->lo = lex_integer (lexer);
809 lex_force_match (lexer, T_COMMA);
810 if (! lex_force_num (lexer) ) return 0;
811 cstp->hi = lex_integer (lexer);
812 if ( cstp->lo >= cstp->hi )
815 _("The specified value of HI (%d) is "
816 "lower than the specified value of LO (%d)"),
821 if (! lex_force_match (lexer, T_RPAREN)) return 0;
824 cstp->n_expected = 0;
825 cstp->expected = NULL;
826 if (lex_match_phrase (lexer, "/EXPECTED"))
828 lex_force_match (lexer, T_EQUALS);
829 if ( ! lex_match_id (lexer, "EQUAL") )
833 while ( lex_is_number (lexer) )
837 f = lex_number (lexer);
839 if ( lex_match (lexer, T_ASTERISK))
842 f = lex_number (lexer);
845 lex_match (lexer, T_COMMA);
847 cstp->n_expected += n;
848 cstp->expected = pool_realloc (specs->pool,
852 for ( i = cstp->n_expected - n ;
853 i < cstp->n_expected;
855 cstp->expected[i] = f;
861 if ( cstp->ranged && cstp->n_expected > 0 &&
862 cstp->n_expected != cstp->hi - cstp->lo + 1 )
865 _("%d expected values were given, but the specified "
866 "range (%d-%d) requires exactly %d values."),
867 cstp->n_expected, cstp->lo, cstp->hi,
868 cstp->hi - cstp->lo +1);
873 specs->test = pool_realloc (specs->pool,
875 sizeof (*specs->test) * specs->n_tests);
877 specs->test[specs->n_tests - 1] = nt;
884 npar_binomial (struct lexer *lexer, struct dataset *ds,
885 struct npar_specs *specs)
887 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
888 struct one_sample_test *tp = &btp->parent;
889 struct npar_test *nt = &tp->parent;
892 nt->execute = binomial_execute;
893 nt->insert_variables = one_sample_insert_variables;
895 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
899 if ( lex_match (lexer, T_LPAREN) )
902 if ( lex_force_num (lexer) )
904 btp->p = lex_number (lexer);
906 lex_force_match (lexer, T_RPAREN);
914 if (equals || lex_match (lexer, T_EQUALS) )
916 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
917 &tp->vars, &tp->n_vars,
918 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
920 if (lex_match (lexer, T_LPAREN))
922 lex_force_num (lexer);
923 btp->category1 = lex_number (lexer);
925 if ( lex_match (lexer, T_COMMA))
927 if ( ! lex_force_num (lexer) ) return 2;
928 btp->category2 = lex_number (lexer);
933 btp->cutpoint = btp->category1;
936 lex_force_match (lexer, T_RPAREN);
945 specs->test = pool_realloc (specs->pool,
947 sizeof (*specs->test) * specs->n_tests);
949 specs->test[specs->n_tests - 1] = nt;
957 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
959 assert (params == 1 || params == 2);
961 if (lex_is_number (lexer))
963 kst->p[0] = lex_number (lexer);
968 lex_match (lexer, T_COMMA);
969 if (lex_force_num (lexer))
971 kst->p[1] = lex_number (lexer);
979 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
981 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
982 struct one_sample_test *tp = &kst->parent;
983 struct npar_test *nt = &tp->parent;
985 nt->execute = ks_one_sample_execute;
986 nt->insert_variables = one_sample_insert_variables;
988 kst->p[0] = kst->p[1] = SYSMIS;
990 if (! lex_force_match (lexer, T_LPAREN))
993 if (lex_match_id (lexer, "NORMAL"))
995 kst->dist = KS_NORMAL;
996 ks_one_sample_parse_params (lexer, kst, 2);
998 else if (lex_match_id (lexer, "POISSON"))
1000 kst->dist = KS_POISSON;
1001 ks_one_sample_parse_params (lexer, kst, 1);
1003 else if (lex_match_id (lexer, "UNIFORM"))
1005 kst->dist = KS_UNIFORM;
1006 ks_one_sample_parse_params (lexer, kst, 2);
1008 else if (lex_match_id (lexer, "EXPONENTIAL"))
1010 kst->dist = KS_EXPONENTIAL;
1011 ks_one_sample_parse_params (lexer, kst, 1);
1016 if (! lex_force_match (lexer, T_RPAREN))
1019 lex_match (lexer, T_EQUALS);
1021 if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1022 &tp->vars, &tp->n_vars,
1023 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1027 specs->test = pool_realloc (specs->pool,
1029 sizeof (*specs->test) * specs->n_tests);
1031 specs->test[specs->n_tests - 1] = nt;
1038 parse_two_sample_related_test (struct lexer *lexer,
1039 const struct dictionary *dict,
1040 struct two_sample_test *test_parameters,
1044 bool paired = false;
1046 const struct variable **vlist1;
1049 const struct variable **vlist2;
1052 test_parameters->parent.insert_variables = two_sample_insert_variables;
1054 if (!parse_variables_const_pool (lexer, pool,
1057 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1060 if ( lex_match (lexer, T_WITH))
1063 if ( !parse_variables_const_pool (lexer, pool, dict,
1065 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1068 paired = (lex_match (lexer, T_LPAREN) &&
1069 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1077 if ( n_vlist1 != n_vlist2)
1078 msg (SE, _("PAIRED was specified but the number of variables "
1079 "preceding WITH (%zu) did not match the number "
1080 "following (%zu)."), n_vlist1, n_vlist2);
1082 test_parameters->n_pairs = n_vlist1 ;
1086 test_parameters->n_pairs = n_vlist1 * n_vlist2;
1091 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1094 test_parameters->pairs =
1095 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
1102 assert (n_vlist1 == n_vlist2);
1103 for ( i = 0 ; i < n_vlist1; ++i )
1105 test_parameters->pairs[n][0] = vlist1[i];
1106 test_parameters->pairs[n][1] = vlist2[i];
1113 for ( i = 0 ; i < n_vlist1; ++i )
1115 for ( j = 0 ; j < n_vlist2; ++j )
1117 test_parameters->pairs[n][0] = vlist1[i];
1118 test_parameters->pairs[n][1] = vlist2[j];
1127 for ( i = 0 ; i < n_vlist1 - 1; ++i )
1129 for ( j = i + 1 ; j < n_vlist1; ++j )
1131 assert ( n < test_parameters->n_pairs);
1132 test_parameters->pairs[n][0] = vlist1[i];
1133 test_parameters->pairs[n][1] = vlist1[j];
1139 assert ( n == test_parameters->n_pairs);
1146 parse_n_sample_related_test (struct lexer *lexer,
1147 const struct dictionary *dict,
1148 struct n_sample_test *nst,
1152 if (!parse_variables_const_pool (lexer, pool,
1154 &nst->vars, &nst->n_vars,
1155 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1158 if ( ! lex_force_match (lexer, T_BY))
1161 nst->indep_var = parse_variable_const (lexer, dict);
1163 if ( ! lex_force_match (lexer, T_LPAREN))
1166 value_init (&nst->val1, var_get_width (nst->indep_var));
1167 if ( ! parse_value (lexer, &nst->val1, nst->indep_var))
1169 value_destroy (&nst->val1, var_get_width (nst->indep_var));
1173 lex_match (lexer, T_COMMA);
1175 value_init (&nst->val2, var_get_width (nst->indep_var));
1176 if ( ! parse_value (lexer, &nst->val2, nst->indep_var))
1178 value_destroy (&nst->val2, var_get_width (nst->indep_var));
1182 if ( ! lex_force_match (lexer, T_RPAREN))
1189 npar_wilcoxon (struct lexer *lexer,
1191 struct npar_specs *specs )
1193 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1194 struct npar_test *nt = &tp->parent;
1195 nt->execute = wilcoxon_execute;
1197 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1202 specs->test = pool_realloc (specs->pool,
1204 sizeof (*specs->test) * specs->n_tests);
1205 specs->test[specs->n_tests - 1] = nt;
1212 npar_mann_whitney (struct lexer *lexer,
1214 struct npar_specs *specs )
1216 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1217 struct npar_test *nt = &tp->parent;
1219 nt->insert_variables = n_sample_insert_variables;
1220 nt->execute = mann_whitney_execute;
1222 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1227 specs->test = pool_realloc (specs->pool,
1229 sizeof (*specs->test) * specs->n_tests);
1230 specs->test[specs->n_tests - 1] = nt;
1237 npar_median (struct lexer *lexer,
1239 struct npar_specs *specs)
1241 struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
1242 struct n_sample_test *tp = &mt->parent;
1243 struct npar_test *nt = &tp->parent;
1245 mt->median = SYSMIS;
1247 if ( lex_match (lexer, T_LPAREN))
1249 lex_force_num (lexer);
1250 mt->median = lex_number (lexer);
1252 lex_force_match (lexer, T_RPAREN);
1255 lex_match (lexer, T_EQUALS);
1257 nt->insert_variables = n_sample_insert_variables;
1258 nt->execute = median_execute;
1260 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1265 specs->test = pool_realloc (specs->pool,
1267 sizeof (*specs->test) * specs->n_tests);
1268 specs->test[specs->n_tests - 1] = nt;
1275 npar_sign (struct lexer *lexer, struct dataset *ds,
1276 struct npar_specs *specs)
1278 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1279 struct npar_test *nt = &tp->parent;
1281 nt->execute = sign_execute;
1283 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1288 specs->test = pool_realloc (specs->pool,
1290 sizeof (*specs->test) * specs->n_tests);
1291 specs->test[specs->n_tests - 1] = nt;
1298 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
1299 struct npar_specs *specs)
1301 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1302 struct npar_test *nt = &tp->parent;
1304 nt->execute = mcnemar_execute;
1306 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1311 specs->test = pool_realloc (specs->pool,
1313 sizeof (*specs->test) * specs->n_tests);
1314 specs->test[specs->n_tests - 1] = nt;
1321 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1322 struct npar_specs *specs)
1324 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1325 struct npar_test *nt = &tp->parent;
1327 nt->insert_variables = n_sample_insert_variables;
1329 nt->execute = kruskal_wallis_execute;
1331 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1336 specs->test = pool_realloc (specs->pool,
1338 sizeof (*specs->test) * specs->n_tests);
1339 specs->test[specs->n_tests - 1] = nt;
1345 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1347 size_t hash = hash_pointer (var, 0);
1348 struct hmapx_node *node;
1349 const struct variable *v = NULL;
1351 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1357 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1360 /* Insert the variables for TEST into VAR_MAP */
1362 one_sample_insert_variables (const struct npar_test *test,
1363 struct hmapx *var_map)
1366 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1368 for ( i = 0 ; i < ost->n_vars ; ++i )
1369 insert_variable_into_map (var_map, ost->vars[i]);
1374 two_sample_insert_variables (const struct npar_test *test,
1375 struct hmapx *var_map)
1378 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1380 for ( i = 0 ; i < tst->n_pairs ; ++i )
1382 variable_pair *pair = &tst->pairs[i];
1384 insert_variable_into_map (var_map, (*pair)[0]);
1385 insert_variable_into_map (var_map, (*pair)[1]);
1390 n_sample_insert_variables (const struct npar_test *test,
1391 struct hmapx *var_map)
1394 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1396 for ( i = 0 ; i < tst->n_vars ; ++i )
1397 insert_variable_into_map (var_map, tst->vars[i]);
1399 insert_variable_into_map (var_map, tst->indep_var);
1404 npar_method (struct lexer *lexer, struct npar_specs *specs)
1406 if ( lex_match_id (lexer, "EXACT") )
1408 specs->exact = true;
1410 if (lex_match_id (lexer, "TIMER"))
1414 if ( lex_match (lexer, T_LPAREN))
1416 if ( lex_force_num (lexer) )
1418 specs->timer = lex_number (lexer);
1421 lex_force_match (lexer, T_RPAREN);