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/npar-summary.h"
44 #include "language/stats/runs.h"
45 #include "language/stats/sign.h"
46 #include "language/stats/wilcoxon.h"
47 #include "libpspp/array.h"
48 #include "libpspp/assertion.h"
49 #include "libpspp/cast.h"
50 #include "libpspp/hash-functions.h"
51 #include "libpspp/hmapx.h"
52 #include "libpspp/message.h"
53 #include "libpspp/pool.h"
54 #include "libpspp/str.h"
55 #include "libpspp/taint.h"
56 #include "math/moments.h"
58 #include "gl/xalloc.h"
61 #define _(msgid) gettext (msgid)
63 /* Settings for subcommand specifiers. */
70 /* Array indices for STATISTICS subcommand. */
73 NPAR_ST_DESCRIPTIVES = 0,
74 NPAR_ST_QUARTILES = 1,
79 /* NPAR TESTS structure. */
82 /* Count variables indicating how many
83 of the subcommands have been given. */
100 /* How missing values should be treated */
103 /* Which statistics have been requested */
104 int a_statistics[NPAR_ST_count];
111 struct npar_test **test;
114 const struct variable **vv; /* Compendium of all variables
115 (those mentioned on ANY subcommand */
116 int n_vars; /* Number of variables in vv */
118 enum mv_class filter; /* Missing values to filter. */
120 bool descriptives; /* Descriptive statistics should be calculated */
121 bool quartiles; /* Quartiles should be calculated */
123 bool exact; /* Whether exact calculations have been requested */
124 double timer; /* Maximum time (in minutes) to wait for exact calculations */
128 /* Prototype for custom subcommands of NPAR TESTS. */
129 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
130 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
131 static int npar_ks_one_sample (struct lexer *, struct dataset *, struct npar_specs *);
132 static int npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
133 static int npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
134 static int npar_kendall (struct lexer *, struct dataset *, struct npar_specs *);
135 static int npar_cochran (struct lexer *, struct dataset *, struct npar_specs *);
136 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
137 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
138 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
139 static int npar_mann_whitney (struct lexer *, struct dataset *, struct npar_specs *);
140 static int npar_mcnemar (struct lexer *, struct dataset *, struct npar_specs *);
141 static int npar_method (struct lexer *, struct npar_specs *);
143 /* Command parsing functions. */
144 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
145 struct npar_specs *npar_specs );
148 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
149 struct npar_specs *nps)
153 npt->ks_one_sample = 0;
156 npt->kruskal_wallis = 0;
157 npt->mann_whitney = 0;
163 npt->miss = MISS_ANALYSIS;
166 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
169 if (lex_match_id (lexer, "COCHRAN"))
172 switch (npar_cochran (lexer, ds, nps))
179 lex_error (lexer, NULL);
185 else if (lex_match_id (lexer, "FRIEDMAN"))
188 switch (npar_friedman (lexer, ds, nps))
195 lex_error (lexer, NULL);
201 else if (lex_match_id (lexer, "KENDALL"))
204 switch (npar_kendall (lexer, ds, nps))
211 lex_error (lexer, NULL);
217 else if (lex_match_id (lexer, "RUNS"))
220 switch (npar_runs (lexer, ds, nps))
227 lex_error (lexer, NULL);
233 else if (lex_match_id (lexer, "CHISQUARE"))
235 lex_match (lexer, T_EQUALS);
237 switch (npar_chisquare (lexer, ds, nps))
244 lex_error (lexer, NULL);
252 else if (lex_match_id (lexer, "BINOMIAL"))
254 lex_match (lexer, T_EQUALS);
256 switch (npar_binomial (lexer, ds, nps))
263 lex_error (lexer, NULL);
269 else if (lex_match_phrase (lexer, "K-S") ||
270 lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
272 lex_match (lexer, T_EQUALS);
273 npt->ks_one_sample++;
274 switch (npar_ks_one_sample (lexer, ds, nps))
281 lex_error (lexer, NULL);
287 else if (lex_match_phrase (lexer, "K-W") ||
288 lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
290 lex_match (lexer, T_EQUALS);
291 npt->kruskal_wallis++;
292 switch (npar_kruskal_wallis (lexer, ds, nps))
299 lex_error (lexer, NULL);
305 else if (lex_match_phrase (lexer, "MCNEMAR"))
307 lex_match (lexer, T_EQUALS);
309 switch (npar_mcnemar (lexer, ds, nps))
316 lex_error (lexer, NULL);
322 else if (lex_match_phrase (lexer, "M-W") ||
323 lex_match_phrase (lexer, "MANN-WHITNEY"))
325 lex_match (lexer, T_EQUALS);
327 switch (npar_mann_whitney (lexer, ds, nps))
334 lex_error (lexer, NULL);
340 else if (lex_match_id (lexer, "WILCOXON"))
342 lex_match (lexer, T_EQUALS);
344 switch (npar_wilcoxon (lexer, ds, nps))
351 lex_error (lexer, NULL);
357 else if (lex_match_id (lexer, "SIGN"))
359 lex_match (lexer, T_EQUALS);
361 switch (npar_sign (lexer, ds, nps))
368 lex_error (lexer, NULL);
374 else if (lex_match_id (lexer, "MISSING"))
376 lex_match (lexer, T_EQUALS);
378 if (npt->missing > 1)
380 msg (SE, _("The %s subcommand may be given only once."), "MISSING");
383 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
385 if (lex_match_id (lexer, "ANALYSIS"))
386 npt->miss = MISS_ANALYSIS;
387 else if (lex_match_id (lexer, "LISTWISE"))
388 npt->miss = MISS_LISTWISE;
389 else if (lex_match_id (lexer, "INCLUDE"))
390 nps->filter = MV_SYSTEM;
391 else if (lex_match_id (lexer, "EXCLUDE"))
392 nps->filter = MV_ANY;
395 lex_error (lexer, NULL);
398 lex_match (lexer, T_COMMA);
401 else if (lex_match_id (lexer, "METHOD"))
403 lex_match (lexer, T_EQUALS);
407 msg (SE, _("The %s subcommand may be given only once."), "METHOD");
410 switch (npar_method (lexer, nps))
417 lex_error (lexer, NULL);
423 else if (lex_match_id (lexer, "STATISTICS"))
425 lex_match (lexer, T_EQUALS);
427 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
429 if (lex_match_id (lexer, "DESCRIPTIVES"))
430 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
431 else if (lex_match_id (lexer, "QUARTILES"))
432 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
433 else if (lex_match (lexer, T_ALL))
434 npt->a_statistics[NPAR_ST_ALL] = 1;
437 lex_error (lexer, NULL);
440 lex_match (lexer, T_COMMA);
443 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
445 lex_match (lexer, T_EQUALS);
446 if (lex_match_id (lexer, "COMPATIBLE"))
447 settings_set_cmd_algorithm (COMPATIBLE);
448 else if (lex_match_id (lexer, "ENHANCED"))
449 settings_set_cmd_algorithm (ENHANCED);
451 if (!lex_match (lexer, T_SLASH))
455 if (lex_token (lexer) != T_ENDCMD)
457 lex_error (lexer, _("expecting end of command"));
468 static void one_sample_insert_variables (const struct npar_test *test,
471 static void two_sample_insert_variables (const struct npar_test *test,
474 static void n_sample_insert_variables (const struct npar_test *test,
478 npar_execute (struct casereader *input,
479 const struct npar_specs *specs,
480 const struct dataset *ds)
483 struct descriptives *summary_descriptives = NULL;
485 for ( t = 0 ; t < specs->n_tests; ++t )
487 const struct npar_test *test = specs->test[t];
488 if ( NULL == test->execute )
490 msg (SW, _("NPAR subcommand not currently implemented."));
493 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
496 if ( specs->descriptives )
498 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
501 npar_summary_calc_descriptives (summary_descriptives,
502 casereader_clone (input),
504 specs->vv, specs->n_vars,
508 if ( (specs->descriptives || specs->quartiles)
509 && !taint_has_tainted_successor (casereader_get_taint (input)) )
510 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
512 free (summary_descriptives);
513 casereader_destroy (input);
517 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
519 struct cmd_npar_tests cmd;
522 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
523 struct casegrouper *grouper;
524 struct casereader *input, *group;
525 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
528 npar_specs.pool = pool_create ();
529 npar_specs.filter = MV_ANY;
530 npar_specs.n_vars = -1;
531 npar_specs.vv = NULL;
533 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
535 pool_destroy (npar_specs.pool);
539 for (i = 0; i < npar_specs.n_tests; ++i )
541 const struct npar_test *test = npar_specs.test[i];
542 test->insert_variables (test, &var_map);
546 struct hmapx_node *node;
547 struct variable *var;
548 npar_specs.n_vars = 0;
550 HMAPX_FOR_EACH (var, node, &var_map)
552 npar_specs.n_vars ++;
553 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
554 npar_specs.vv[npar_specs.n_vars - 1] = var;
558 sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
559 compare_var_ptrs_by_name, NULL);
561 if ( cmd.statistics )
565 for ( i = 0 ; i < NPAR_ST_count; ++i )
567 if ( cmd.a_statistics[i] )
571 case NPAR_ST_DESCRIPTIVES:
572 npar_specs.descriptives = true;
574 case NPAR_ST_QUARTILES:
575 npar_specs.quartiles = true;
578 npar_specs.quartiles = true;
579 npar_specs.descriptives = true;
588 input = proc_open (ds);
589 if ( cmd.miss == MISS_LISTWISE )
591 input = casereader_create_filter_missing (input,
599 grouper = casegrouper_create_splits (input, dataset_dict (ds));
600 while (casegrouper_get_next_group (grouper, &group))
601 npar_execute (group, &npar_specs, ds);
602 ok = casegrouper_destroy (grouper);
603 ok = proc_commit (ds) && ok;
605 pool_destroy (npar_specs.pool);
606 hmapx_destroy (&var_map);
608 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
612 npar_runs (struct lexer *lexer, struct dataset *ds,
613 struct npar_specs *specs)
615 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
616 struct one_sample_test *tp = &rt->parent;
617 struct npar_test *nt = &tp->parent;
619 nt->execute = runs_execute;
620 nt->insert_variables = one_sample_insert_variables;
622 if ( lex_force_match (lexer, T_LPAREN) )
624 if ( lex_match_id (lexer, "MEAN"))
626 rt->cp_mode = CP_MEAN;
628 else if (lex_match_id (lexer, "MEDIAN"))
630 rt->cp_mode = CP_MEDIAN;
632 else if (lex_match_id (lexer, "MODE"))
634 rt->cp_mode = CP_MODE;
636 else if (lex_is_number (lexer))
638 rt->cutpoint = lex_number (lexer);
639 rt->cp_mode = CP_CUSTOM;
644 lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
648 lex_force_match (lexer, T_RPAREN);
649 lex_force_match (lexer, T_EQUALS);
650 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
651 &tp->vars, &tp->n_vars,
652 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
659 specs->test = pool_realloc (specs->pool,
661 sizeof (*specs->test) * specs->n_tests);
663 specs->test[specs->n_tests - 1] = nt;
669 npar_friedman (struct lexer *lexer, struct dataset *ds,
670 struct npar_specs *specs)
672 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
673 struct one_sample_test *ost = &ft->parent;
674 struct npar_test *nt = &ost->parent;
676 ft->kendalls_w = false;
677 nt->execute = friedman_execute;
678 nt->insert_variables = one_sample_insert_variables;
680 lex_match (lexer, T_EQUALS);
682 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
683 &ost->vars, &ost->n_vars,
684 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
690 specs->test = pool_realloc (specs->pool,
692 sizeof (*specs->test) * specs->n_tests);
694 specs->test[specs->n_tests - 1] = nt;
700 npar_kendall (struct lexer *lexer, struct dataset *ds,
701 struct npar_specs *specs)
703 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
704 struct one_sample_test *ost = &kt->parent;
705 struct npar_test *nt = &ost->parent;
707 kt->kendalls_w = true;
708 nt->execute = friedman_execute;
709 nt->insert_variables = one_sample_insert_variables;
711 lex_match (lexer, T_EQUALS);
713 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
714 &ost->vars, &ost->n_vars,
715 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
721 specs->test = pool_realloc (specs->pool,
723 sizeof (*specs->test) * specs->n_tests);
725 specs->test[specs->n_tests - 1] = nt;
732 npar_cochran (struct lexer *lexer, struct dataset *ds,
733 struct npar_specs *specs)
735 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
736 struct npar_test *nt = &ft->parent;
738 nt->execute = cochran_execute;
739 nt->insert_variables = one_sample_insert_variables;
741 lex_match (lexer, T_EQUALS);
743 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
744 &ft->vars, &ft->n_vars,
745 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
751 specs->test = pool_realloc (specs->pool,
753 sizeof (*specs->test) * specs->n_tests);
755 specs->test[specs->n_tests - 1] = nt;
762 npar_chisquare (struct lexer *lexer, struct dataset *ds,
763 struct npar_specs *specs)
765 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
766 struct one_sample_test *tp = &cstp->parent;
767 struct npar_test *nt = &tp->parent;
770 nt->execute = chisquare_execute;
771 nt->insert_variables = one_sample_insert_variables;
773 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
774 &tp->vars, &tp->n_vars,
775 PV_NO_SCRATCH | PV_NO_DUPLICATE))
780 cstp->ranged = false;
782 if ( lex_match (lexer, T_LPAREN))
785 if ( ! lex_force_num (lexer)) return 0;
786 cstp->lo = lex_integer (lexer);
788 lex_force_match (lexer, T_COMMA);
789 if (! lex_force_num (lexer) ) return 0;
790 cstp->hi = lex_integer (lexer);
791 if ( cstp->lo >= cstp->hi )
794 _("The specified value of HI (%d) is "
795 "lower than the specified value of LO (%d)"),
800 if (! lex_force_match (lexer, T_RPAREN)) return 0;
803 cstp->n_expected = 0;
804 cstp->expected = NULL;
805 if (lex_match_phrase (lexer, "/EXPECTED"))
807 lex_force_match (lexer, T_EQUALS);
808 if ( ! lex_match_id (lexer, "EQUAL") )
812 while ( lex_is_number (lexer) )
816 f = lex_number (lexer);
818 if ( lex_match (lexer, T_ASTERISK))
821 f = lex_number (lexer);
824 lex_match (lexer, T_COMMA);
826 cstp->n_expected += n;
827 cstp->expected = pool_realloc (specs->pool,
831 for ( i = cstp->n_expected - n ;
832 i < cstp->n_expected;
834 cstp->expected[i] = f;
840 if ( cstp->ranged && cstp->n_expected > 0 &&
841 cstp->n_expected != cstp->hi - cstp->lo + 1 )
844 _("%d expected values were given, but the specified "
845 "range (%d-%d) requires exactly %d values."),
846 cstp->n_expected, cstp->lo, cstp->hi,
847 cstp->hi - cstp->lo +1);
852 specs->test = pool_realloc (specs->pool,
854 sizeof (*specs->test) * specs->n_tests);
856 specs->test[specs->n_tests - 1] = nt;
863 npar_binomial (struct lexer *lexer, struct dataset *ds,
864 struct npar_specs *specs)
866 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
867 struct one_sample_test *tp = &btp->parent;
868 struct npar_test *nt = &tp->parent;
871 nt->execute = binomial_execute;
872 nt->insert_variables = one_sample_insert_variables;
874 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
878 if ( lex_match (lexer, T_LPAREN) )
881 if ( lex_force_num (lexer) )
883 btp->p = lex_number (lexer);
885 lex_force_match (lexer, T_RPAREN);
893 if (equals || lex_match (lexer, T_EQUALS) )
895 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
896 &tp->vars, &tp->n_vars,
897 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
899 if (lex_match (lexer, T_LPAREN))
901 lex_force_num (lexer);
902 btp->category1 = lex_number (lexer);
904 if ( lex_match (lexer, T_COMMA))
906 if ( ! lex_force_num (lexer) ) return 2;
907 btp->category2 = lex_number (lexer);
912 btp->cutpoint = btp->category1;
915 lex_force_match (lexer, T_RPAREN);
924 specs->test = pool_realloc (specs->pool,
926 sizeof (*specs->test) * specs->n_tests);
928 specs->test[specs->n_tests - 1] = nt;
936 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
938 assert (params == 1 || params == 2);
940 if (lex_is_number (lexer))
942 kst->p[0] = lex_number (lexer);
947 lex_match (lexer, T_COMMA);
948 if (lex_force_num (lexer))
950 kst->p[1] = lex_number (lexer);
958 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
960 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
961 struct one_sample_test *tp = &kst->parent;
962 struct npar_test *nt = &tp->parent;
964 nt->execute = ks_one_sample_execute;
965 nt->insert_variables = one_sample_insert_variables;
967 kst->p[0] = kst->p[1] = SYSMIS;
969 if (! lex_force_match (lexer, T_LPAREN))
972 if (lex_match_id (lexer, "NORMAL"))
974 kst->dist = KS_NORMAL;
975 ks_one_sample_parse_params (lexer, kst, 2);
977 else if (lex_match_id (lexer, "POISSON"))
979 kst->dist = KS_POISSON;
980 ks_one_sample_parse_params (lexer, kst, 1);
982 else if (lex_match_id (lexer, "UNIFORM"))
984 kst->dist = KS_UNIFORM;
985 ks_one_sample_parse_params (lexer, kst, 2);
987 else if (lex_match_id (lexer, "EXPONENTIAL"))
989 kst->dist = KS_EXPONENTIAL;
990 ks_one_sample_parse_params (lexer, kst, 1);
995 if (! lex_force_match (lexer, T_RPAREN))
998 lex_match (lexer, T_EQUALS);
1000 if (! parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
1001 &tp->vars, &tp->n_vars,
1002 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1006 specs->test = pool_realloc (specs->pool,
1008 sizeof (*specs->test) * specs->n_tests);
1010 specs->test[specs->n_tests - 1] = nt;
1017 parse_two_sample_related_test (struct lexer *lexer,
1018 const struct dictionary *dict,
1019 struct two_sample_test *test_parameters,
1023 bool paired = false;
1025 const struct variable **vlist1;
1028 const struct variable **vlist2;
1031 test_parameters->parent.insert_variables = two_sample_insert_variables;
1033 if (!parse_variables_const_pool (lexer, pool,
1036 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1039 if ( lex_match (lexer, T_WITH))
1042 if ( !parse_variables_const_pool (lexer, pool, dict,
1044 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1047 paired = (lex_match (lexer, T_LPAREN) &&
1048 lex_match_id (lexer, "PAIRED") && lex_match (lexer, T_RPAREN));
1056 if ( n_vlist1 != n_vlist2)
1057 msg (SE, _("PAIRED was specified but the number of variables "
1058 "preceding WITH (%zu) did not match the number "
1059 "following (%zu)."), n_vlist1, n_vlist2);
1061 test_parameters->n_pairs = n_vlist1 ;
1065 test_parameters->n_pairs = n_vlist1 * n_vlist2;
1070 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
1073 test_parameters->pairs =
1074 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
1081 assert (n_vlist1 == n_vlist2);
1082 for ( i = 0 ; i < n_vlist1; ++i )
1084 test_parameters->pairs[n][0] = vlist1[i];
1085 test_parameters->pairs[n][1] = vlist2[i];
1092 for ( i = 0 ; i < n_vlist1; ++i )
1094 for ( j = 0 ; j < n_vlist2; ++j )
1096 test_parameters->pairs[n][0] = vlist1[i];
1097 test_parameters->pairs[n][1] = vlist2[j];
1106 for ( i = 0 ; i < n_vlist1 - 1; ++i )
1108 for ( j = i + 1 ; j < n_vlist1; ++j )
1110 assert ( n < test_parameters->n_pairs);
1111 test_parameters->pairs[n][0] = vlist1[i];
1112 test_parameters->pairs[n][1] = vlist1[j];
1118 assert ( n == test_parameters->n_pairs);
1125 parse_n_sample_related_test (struct lexer *lexer,
1126 const struct dictionary *dict,
1127 struct n_sample_test *nst,
1131 if (!parse_variables_const_pool (lexer, pool,
1133 &nst->vars, &nst->n_vars,
1134 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
1137 if ( ! lex_force_match (lexer, T_BY))
1140 nst->indep_var = parse_variable_const (lexer, dict);
1142 if ( ! lex_force_match (lexer, T_LPAREN))
1145 value_init (&nst->val1, var_get_width (nst->indep_var));
1146 if ( ! parse_value (lexer, &nst->val1, nst->indep_var))
1148 value_destroy (&nst->val1, var_get_width (nst->indep_var));
1152 lex_match (lexer, T_COMMA);
1154 value_init (&nst->val2, var_get_width (nst->indep_var));
1155 if ( ! parse_value (lexer, &nst->val2, nst->indep_var))
1157 value_destroy (&nst->val2, var_get_width (nst->indep_var));
1161 if ( ! lex_force_match (lexer, T_RPAREN))
1168 npar_wilcoxon (struct lexer *lexer,
1170 struct npar_specs *specs )
1172 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1173 struct npar_test *nt = &tp->parent;
1174 nt->execute = wilcoxon_execute;
1176 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1181 specs->test = pool_realloc (specs->pool,
1183 sizeof (*specs->test) * specs->n_tests);
1184 specs->test[specs->n_tests - 1] = nt;
1191 npar_mann_whitney (struct lexer *lexer,
1193 struct npar_specs *specs )
1195 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1196 struct npar_test *nt = &tp->parent;
1198 nt->insert_variables = n_sample_insert_variables;
1199 nt->execute = mann_whitney_execute;
1201 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1206 specs->test = pool_realloc (specs->pool,
1208 sizeof (*specs->test) * specs->n_tests);
1209 specs->test[specs->n_tests - 1] = nt;
1218 npar_sign (struct lexer *lexer, struct dataset *ds,
1219 struct npar_specs *specs)
1221 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1222 struct npar_test *nt = &tp->parent;
1224 nt->execute = sign_execute;
1226 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1231 specs->test = pool_realloc (specs->pool,
1233 sizeof (*specs->test) * specs->n_tests);
1234 specs->test[specs->n_tests - 1] = nt;
1241 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
1242 struct npar_specs *specs)
1244 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1245 struct npar_test *nt = &tp->parent;
1247 nt->execute = mcnemar_execute;
1249 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
1254 specs->test = pool_realloc (specs->pool,
1256 sizeof (*specs->test) * specs->n_tests);
1257 specs->test[specs->n_tests - 1] = nt;
1264 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
1265 struct npar_specs *specs)
1267 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
1268 struct npar_test *nt = &tp->parent;
1270 nt->insert_variables = n_sample_insert_variables;
1272 nt->execute = kruskal_wallis_execute;
1274 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
1279 specs->test = pool_realloc (specs->pool,
1281 sizeof (*specs->test) * specs->n_tests);
1282 specs->test[specs->n_tests - 1] = nt;
1288 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1290 size_t hash = hash_pointer (var, 0);
1291 struct hmapx_node *node;
1292 const struct variable *v = NULL;
1294 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1300 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1303 /* Insert the variables for TEST into VAR_MAP */
1305 one_sample_insert_variables (const struct npar_test *test,
1306 struct hmapx *var_map)
1309 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1311 for ( i = 0 ; i < ost->n_vars ; ++i )
1312 insert_variable_into_map (var_map, ost->vars[i]);
1317 two_sample_insert_variables (const struct npar_test *test,
1318 struct hmapx *var_map)
1321 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1323 for ( i = 0 ; i < tst->n_pairs ; ++i )
1325 variable_pair *pair = &tst->pairs[i];
1327 insert_variable_into_map (var_map, (*pair)[0]);
1328 insert_variable_into_map (var_map, (*pair)[1]);
1333 n_sample_insert_variables (const struct npar_test *test,
1334 struct hmapx *var_map)
1337 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1339 for ( i = 0 ; i < tst->n_vars ; ++i )
1340 insert_variable_into_map (var_map, tst->vars[i]);
1342 insert_variable_into_map (var_map, tst->indep_var);
1347 npar_method (struct lexer *lexer, struct npar_specs *specs)
1349 if ( lex_match_id (lexer, "EXACT") )
1351 specs->exact = true;
1353 if (lex_match_id (lexer, "TIMER"))
1357 if ( lex_match (lexer, T_LPAREN))
1359 if ( lex_force_num (lexer) )
1361 specs->timer = lex_number (lexer);
1364 lex_force_match (lexer, T_RPAREN);