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 /* NPAR TESTS structure. */
69 struct npar_test **test;
72 const struct variable **vv; /* Compendium of all variables
73 (those mentioned on ANY subcommand */
74 int n_vars; /* Number of variables in vv */
76 enum mv_class filter; /* Missing values to filter. */
77 bool listwise_missing;
79 bool descriptives; /* Descriptive statistics should be calculated */
80 bool quartiles; /* Quartiles should be calculated */
82 bool exact; /* Whether exact calculations have been requested */
83 double timer; /* Maximum time (in minutes) to wait for exact calculations */
87 /* Prototype for custom subcommands of NPAR TESTS. */
88 static bool npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
89 static bool npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
90 static bool npar_ks_one_sample (struct lexer *, struct dataset *, struct npar_specs *);
91 static bool npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
92 static bool npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
93 static bool npar_kendall (struct lexer *, struct dataset *, struct npar_specs *);
94 static bool npar_cochran (struct lexer *, struct dataset *, struct npar_specs *);
95 static bool npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
96 static bool npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
97 static bool npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
98 static bool npar_jonckheere_terpstra (struct lexer *, struct dataset *, struct npar_specs *);
99 static bool npar_mann_whitney (struct lexer *, struct dataset *, struct npar_specs *);
100 static bool npar_mcnemar (struct lexer *, struct dataset *, struct npar_specs *);
101 static bool npar_median (struct lexer *, struct dataset *, struct npar_specs *);
102 static bool npar_method (struct lexer *, struct npar_specs *);
104 /* Command parsing functions. */
107 parse_npar_tests (struct lexer *lexer, struct dataset *ds,
108 struct npar_specs *nps)
110 bool seen_missing = false;
111 bool seen_method = false;
112 lex_match (lexer, T_SLASH);
115 if (lex_match_id (lexer, "COCHRAN"))
117 if (!npar_cochran (lexer, ds, nps))
120 else if (lex_match_id (lexer, "FRIEDMAN"))
122 if (!npar_friedman (lexer, ds, nps))
125 else if (lex_match_id (lexer, "KENDALL"))
127 if (!npar_kendall (lexer, ds, nps))
130 else if (lex_match_id (lexer, "RUNS"))
132 if (!npar_runs (lexer, ds, nps))
135 else if (lex_match_id (lexer, "CHISQUARE"))
137 lex_match (lexer, T_EQUALS);
138 if (!npar_chisquare (lexer, ds, nps))
141 else if (lex_match_id (lexer, "BINOMIAL"))
143 lex_match (lexer, T_EQUALS);
144 if (!npar_binomial (lexer, ds, nps))
147 else if (lex_match_phrase (lexer, "K-S") ||
148 lex_match_phrase (lexer, "KOLMOGOROV-SMIRNOV"))
150 lex_match (lexer, T_EQUALS);
151 if (!npar_ks_one_sample (lexer, ds, nps))
154 else if (lex_match_phrase (lexer, "J-T") ||
155 lex_match_phrase (lexer, "JONCKHEERE-TERPSTRA"))
157 lex_match (lexer, T_EQUALS);
158 if (!npar_jonckheere_terpstra (lexer, ds, nps))
161 else if (lex_match_phrase (lexer, "K-W") ||
162 lex_match_phrase (lexer, "KRUSKAL-WALLIS"))
164 lex_match (lexer, T_EQUALS);
165 if (!npar_kruskal_wallis (lexer, ds, nps))
168 else if (lex_match_phrase (lexer, "MCNEMAR"))
170 lex_match (lexer, T_EQUALS);
171 if (!npar_mcnemar (lexer, ds, nps))
174 else if (lex_match_phrase (lexer, "M-W") ||
175 lex_match_phrase (lexer, "MANN-WHITNEY"))
177 lex_match (lexer, T_EQUALS);
178 if (!npar_mann_whitney (lexer, ds, nps))
181 else if (lex_match_phrase (lexer, "MEDIAN"))
183 if (!npar_median (lexer, ds, nps))
186 else if (lex_match_id (lexer, "WILCOXON"))
188 lex_match (lexer, T_EQUALS);
189 if (!npar_wilcoxon (lexer, ds, nps))
192 else if (lex_match_id (lexer, "SIGN"))
194 lex_match (lexer, T_EQUALS);
195 if (!npar_sign (lexer, ds, nps))
198 else if (lex_match_id (lexer, "MISSING"))
200 lex_match (lexer, T_EQUALS);
203 lex_sbc_only_once (lexer, "MISSING");
207 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
209 if (lex_match_id (lexer, "ANALYSIS"))
210 nps->listwise_missing = false;
211 else if (lex_match_id (lexer, "LISTWISE"))
212 nps->listwise_missing = true;
213 else if (lex_match_id (lexer, "INCLUDE"))
214 nps->filter = MV_SYSTEM;
215 else if (lex_match_id (lexer, "EXCLUDE"))
216 nps->filter = MV_ANY;
219 lex_error_expecting (lexer, "ANALYSIS", "LISTWISE",
220 "INCLUDE", "EXCLUDE");
223 lex_match (lexer, T_COMMA);
226 else if (lex_match_id (lexer, "METHOD"))
228 lex_match (lexer, T_EQUALS);
231 lex_sbc_only_once (lexer, "METHOD");
235 if (!npar_method (lexer, nps))
238 else if (lex_match_id (lexer, "STATISTICS"))
240 lex_match (lexer, T_EQUALS);
241 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
243 if (lex_match_id (lexer, "DESCRIPTIVES"))
244 nps->descriptives = true;
245 else if (lex_match_id (lexer, "QUARTILES"))
246 nps->quartiles = true;
247 else if (lex_match (lexer, T_ALL))
248 nps->descriptives = nps->quartiles = true;
251 lex_error_expecting (lexer, "DESCRIPTIVES", "QUARTILES",
255 lex_match (lexer, T_COMMA);
258 else if (lex_match_id (lexer, "ALGORITHM"))
260 lex_match (lexer, T_EQUALS);
261 if (lex_match_id (lexer, "COMPATIBLE"))
262 settings_set_cmd_algorithm (COMPATIBLE);
263 else if (lex_match_id (lexer, "ENHANCED"))
264 settings_set_cmd_algorithm (ENHANCED);
267 lex_error_expecting (lexer, "COMPATIBLE", "ENHANCED");
273 lex_error_expecting (lexer, "COCHRAN", "FRIEDMAN", "KENDALL", "RUNS",
274 "CHISQUARE", "BINOMIAL", "K-S", "J-T", "K-W",
275 "MCNEMAR", "M-W", "MEDIAN", "WILCOXON",
276 "SIGN", "MISSING", "METHOD", "STATISTICS",
281 while (lex_match (lexer, T_SLASH));
286 static void one_sample_insert_variables (const struct npar_test *test,
289 static void two_sample_insert_variables (const struct npar_test *test,
292 static void n_sample_insert_variables (const struct npar_test *test,
296 npar_execute (struct casereader *input,
297 const struct npar_specs *specs,
298 const struct dataset *ds)
300 struct descriptives *summary_descriptives = NULL;
302 for (size_t t = 0; t < specs->n_tests; ++t)
304 const struct npar_test *test = specs->test[t];
305 test->execute (ds, casereader_clone (input), specs->filter,
306 test, specs->exact, specs->timer);
309 if (specs->descriptives && specs->n_vars > 0)
311 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
314 npar_summary_calc_descriptives (summary_descriptives,
315 casereader_clone (input),
317 specs->vv, specs->n_vars,
321 if ((specs->descriptives || specs->quartiles)
322 && !taint_has_tainted_successor (casereader_get_taint (input)))
323 do_summary_box (summary_descriptives, specs->vv, specs->n_vars,
324 dict_get_weight_format (dataset_dict (ds)));
326 free (summary_descriptives);
327 casereader_destroy (input);
331 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
333 struct npar_specs npar_specs = {
334 .pool = pool_create (),
336 .listwise_missing = false,
339 if (!parse_npar_tests (lexer, ds, &npar_specs))
341 pool_destroy (npar_specs.pool);
345 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
346 for (size_t i = 0; i < npar_specs.n_tests; ++i)
348 const struct npar_test *test = npar_specs.test[i];
349 test->insert_variables (test, &var_map);
352 struct hmapx_node *node;
353 struct variable *var;
354 npar_specs.vv = pool_alloc (npar_specs.pool,
355 hmapx_count (&var_map) * sizeof *npar_specs.vv);
356 HMAPX_FOR_EACH (var, node, &var_map)
357 npar_specs.vv[npar_specs.n_vars++] = var;
358 assert (npar_specs.n_vars == hmapx_count (&var_map));
360 sort (npar_specs.vv, npar_specs.n_vars, sizeof *npar_specs.vv,
361 compare_var_ptrs_by_name, NULL);
363 struct casereader *input = proc_open (ds);
364 if (npar_specs.listwise_missing)
365 input = casereader_create_filter_missing (input,
371 struct casegrouper *grouper = casegrouper_create_splits (input, dataset_dict (ds));
372 struct casereader *group;
373 while (casegrouper_get_next_group (grouper, &group))
374 npar_execute (group, &npar_specs, ds);
375 bool ok = casegrouper_destroy (grouper);
376 ok = proc_commit (ds) && ok;
378 pool_destroy (npar_specs.pool);
379 hmapx_destroy (&var_map);
381 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
385 add_test (struct npar_specs *specs, struct npar_test *nt)
387 specs->test = pool_realloc (specs->pool, specs->test,
388 (specs->n_tests + 1) * sizeof *specs->test);
390 specs->test[specs->n_tests++] = nt;
394 npar_runs (struct lexer *lexer, struct dataset *ds,
395 struct npar_specs *specs)
397 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
398 struct one_sample_test *tp = &rt->parent;
399 struct npar_test *nt = &tp->parent;
401 nt->execute = runs_execute;
402 nt->insert_variables = one_sample_insert_variables;
404 if (!lex_force_match (lexer, T_LPAREN))
407 if (lex_match_id (lexer, "MEAN"))
408 rt->cp_mode = CP_MEAN;
409 else if (lex_match_id (lexer, "MEDIAN"))
410 rt->cp_mode = CP_MEDIAN;
411 else if (lex_match_id (lexer, "MODE"))
412 rt->cp_mode = CP_MODE;
413 else if (lex_is_number (lexer))
415 rt->cutpoint = lex_number (lexer);
416 rt->cp_mode = CP_CUSTOM;
421 lex_error (lexer, _("Syntax error expecting %s, %s, %s or a number."),
422 "MEAN", "MEDIAN", "MODE");
426 if (!lex_force_match (lexer, T_RPAREN)
427 || !lex_force_match (lexer, T_EQUALS))
430 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
431 &tp->vars, &tp->n_vars,
432 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
435 add_test (specs, nt);
440 npar_friedman (struct lexer *lexer, struct dataset *ds,
441 struct npar_specs *specs)
443 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
444 struct one_sample_test *ost = &ft->parent;
445 struct npar_test *nt = &ost->parent;
447 ft->kendalls_w = false;
448 nt->execute = friedman_execute;
449 nt->insert_variables = one_sample_insert_variables;
451 lex_match (lexer, T_EQUALS);
453 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
454 &ost->vars, &ost->n_vars,
455 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
458 add_test (specs, nt);
463 npar_kendall (struct lexer *lexer, struct dataset *ds,
464 struct npar_specs *specs)
466 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
467 struct one_sample_test *ost = &kt->parent;
468 struct npar_test *nt = &ost->parent;
470 kt->kendalls_w = true;
471 nt->execute = friedman_execute;
472 nt->insert_variables = one_sample_insert_variables;
474 lex_match (lexer, T_EQUALS);
476 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
477 &ost->vars, &ost->n_vars,
478 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
481 add_test (specs, nt);
487 npar_cochran (struct lexer *lexer, struct dataset *ds,
488 struct npar_specs *specs)
490 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
491 struct npar_test *nt = &ft->parent;
493 nt->execute = cochran_execute;
494 nt->insert_variables = one_sample_insert_variables;
496 lex_match (lexer, T_EQUALS);
498 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
499 &ft->vars, &ft->n_vars,
500 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
503 add_test (specs, nt);
508 npar_chisquare (struct lexer *lexer, struct dataset *ds,
509 struct npar_specs *specs)
511 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
512 struct one_sample_test *tp = &cstp->parent;
513 struct npar_test *nt = &tp->parent;
515 nt->execute = chisquare_execute;
516 nt->insert_variables = one_sample_insert_variables;
518 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
519 &tp->vars, &tp->n_vars,
520 PV_NO_SCRATCH | PV_NO_DUPLICATE))
523 cstp->ranged = false;
525 if (lex_match (lexer, T_LPAREN))
528 if (!lex_force_num (lexer))
530 cstp->lo = lex_number (lexer);
533 if (!lex_force_match (lexer, T_COMMA))
535 if (!lex_force_num_range_open (lexer, "HI", cstp->lo, DBL_MAX))
537 cstp->hi = lex_number (lexer);
539 if (!lex_force_match (lexer, T_RPAREN))
543 cstp->n_expected = 0;
544 cstp->expected = NULL;
545 int expected_start = 0;
546 int expected_end = 0;
547 if (lex_match_phrase (lexer, "/EXPECTED"))
549 if (!lex_force_match (lexer, T_EQUALS))
552 if (!lex_match_id (lexer, "EQUAL"))
554 expected_start = lex_ofs (lexer);
555 while (lex_is_number (lexer))
558 double f = lex_number (lexer);
560 if (lex_match (lexer, T_ASTERISK))
563 if (!lex_force_num (lexer))
565 f = lex_number (lexer);
568 lex_match (lexer, T_COMMA);
570 cstp->n_expected += n;
571 cstp->expected = pool_realloc (specs->pool,
573 sizeof (double) * cstp->n_expected);
574 for (int i = cstp->n_expected - n; i < cstp->n_expected; ++i)
575 cstp->expected[i] = f;
577 expected_end = lex_ofs (lexer) - 1;
581 if (cstp->ranged && cstp->n_expected > 0 &&
582 cstp->n_expected != cstp->hi - cstp->lo + 1)
584 lex_ofs_error (lexer, expected_start, expected_end,
585 _("%d expected values were given, but the specified "
586 "range (%d-%d) requires exactly %d values."),
587 cstp->n_expected, cstp->lo, cstp->hi,
588 cstp->hi - cstp->lo +1);
592 add_test (specs, nt);
597 npar_binomial (struct lexer *lexer, struct dataset *ds,
598 struct npar_specs *specs)
600 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
601 struct one_sample_test *tp = &btp->parent;
602 struct npar_test *nt = &tp->parent;
604 nt->execute = binomial_execute;
605 nt->insert_variables = one_sample_insert_variables;
607 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
611 if (lex_match (lexer, T_LPAREN))
613 if (!lex_force_num (lexer))
615 btp->p = lex_number (lexer);
617 if (!lex_force_match (lexer, T_RPAREN))
619 if (!lex_force_match (lexer, T_EQUALS))
623 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
624 &tp->vars, &tp->n_vars,
625 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
627 if (lex_match (lexer, T_LPAREN))
629 if (!lex_force_num (lexer))
631 btp->category1 = lex_number (lexer);
633 if (lex_match (lexer, T_COMMA))
635 if (!lex_force_num (lexer))
637 btp->category2 = lex_number (lexer);
641 btp->cutpoint = btp->category1;
643 if (!lex_force_match (lexer, T_RPAREN))
647 add_test (specs, nt);
652 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
654 assert (params == 1 || params == 2);
656 if (lex_is_number (lexer))
658 kst->p[0] = lex_number (lexer);
663 lex_match (lexer, T_COMMA);
664 if (lex_force_num (lexer))
666 kst->p[1] = lex_number (lexer);
674 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
676 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
677 struct one_sample_test *tp = &kst->parent;
678 struct npar_test *nt = &tp->parent;
680 nt->execute = ks_one_sample_execute;
681 nt->insert_variables = one_sample_insert_variables;
683 kst->p[0] = kst->p[1] = SYSMIS;
685 if (!lex_force_match (lexer, T_LPAREN))
688 if (lex_match_id (lexer, "NORMAL"))
690 kst->dist = KS_NORMAL;
691 ks_one_sample_parse_params (lexer, kst, 2);
693 else if (lex_match_id (lexer, "POISSON"))
695 kst->dist = KS_POISSON;
696 ks_one_sample_parse_params (lexer, kst, 1);
698 else if (lex_match_id (lexer, "UNIFORM"))
700 kst->dist = KS_UNIFORM;
701 ks_one_sample_parse_params (lexer, kst, 2);
703 else if (lex_match_id (lexer, "EXPONENTIAL"))
705 kst->dist = KS_EXPONENTIAL;
706 ks_one_sample_parse_params (lexer, kst, 1);
710 lex_error_expecting (lexer, "NORMAL", "POISSON", "UNIFORM",
715 if (!lex_force_match (lexer, T_RPAREN))
718 lex_match (lexer, T_EQUALS);
720 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
721 &tp->vars, &tp->n_vars,
722 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
725 add_test (specs, nt);
731 parse_two_sample_related_test (struct lexer *lexer,
732 const struct dictionary *dict,
733 struct two_sample_test *tp,
736 tp->parent.insert_variables = two_sample_insert_variables;
738 const struct variable **v1;
740 int vars_start = lex_ofs (lexer);
741 if (!parse_variables_const_pool (lexer, pool, dict, &v1, &n1,
742 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
747 const struct variable **v2 = NULL;
749 if (lex_match (lexer, T_WITH))
752 if (!parse_variables_const_pool (lexer, pool, dict, &v2, &n2,
753 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
755 int vars_end = lex_ofs (lexer) - 1;
757 if (lex_match (lexer, T_LPAREN))
759 if (!lex_force_match_id (lexer, "PAIRED")
760 || !lex_force_match (lexer, T_RPAREN))
766 lex_ofs_error (lexer, vars_start, vars_end,
767 _("PAIRED was specified, but the number of "
768 "variables preceding WITH (%zu) does not match "
769 "the number following (%zu)."),
776 tp->n_pairs = (paired ? n1
778 : (n1 * (n1 - 1)) / 2);
779 tp->pairs = pool_alloc (pool, sizeof (variable_pair) * tp->n_pairs);
783 for (size_t i = 0; i < n1 - 1; ++i)
784 for (size_t j = i + 1; j < n1; ++j)
786 assert (n < tp->n_pairs);
787 tp->pairs[n][0] = v1[i];
788 tp->pairs[n][1] = v1[j];
794 for (size_t i = 0; i < n1; ++i)
796 tp->pairs[n][0] = v1[i];
797 tp->pairs[n][1] = v2[i];
803 for (size_t i = 0; i < n1; ++i)
804 for (size_t j = 0; j < n2; ++j)
806 tp->pairs[n][0] = v1[i];
807 tp->pairs[n][1] = v2[j];
811 assert (n == tp->n_pairs);
817 parse_n_sample_related_test (struct lexer *lexer, const struct dictionary *dict,
818 struct n_sample_test *nst, struct pool *pool)
820 if (!parse_variables_const_pool (lexer, pool, dict, &nst->vars, &nst->n_vars,
821 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
824 if (!lex_force_match (lexer, T_BY))
827 nst->indep_var = parse_variable_const (lexer, dict);
831 if (!lex_force_match (lexer, T_LPAREN))
834 value_init (&nst->val1, var_get_width (nst->indep_var));
835 if (!parse_value (lexer, &nst->val1, nst->indep_var))
837 value_destroy (&nst->val1, var_get_width (nst->indep_var));
841 lex_match (lexer, T_COMMA);
843 value_init (&nst->val2, var_get_width (nst->indep_var));
844 if (!parse_value (lexer, &nst->val2, nst->indep_var))
846 value_destroy (&nst->val2, var_get_width (nst->indep_var));
850 if (!lex_force_match (lexer, T_RPAREN))
857 npar_wilcoxon (struct lexer *lexer,
859 struct npar_specs *specs)
861 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
862 struct npar_test *nt = &tp->parent;
863 nt->execute = wilcoxon_execute;
865 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
869 add_test (specs, nt);
874 npar_mann_whitney (struct lexer *lexer,
876 struct npar_specs *specs)
878 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
879 struct npar_test *nt = &tp->parent;
881 nt->insert_variables = n_sample_insert_variables;
882 nt->execute = mann_whitney_execute;
884 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
887 add_test (specs, nt);
892 npar_median (struct lexer *lexer,
894 struct npar_specs *specs)
896 struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
897 struct n_sample_test *tp = &mt->parent;
898 struct npar_test *nt = &tp->parent;
902 if (lex_match (lexer, T_LPAREN))
904 if (!lex_force_num (lexer))
906 mt->median = lex_number (lexer);
909 if (!lex_force_match (lexer, T_RPAREN))
913 lex_match (lexer, T_EQUALS);
915 nt->insert_variables = n_sample_insert_variables;
916 nt->execute = median_execute;
918 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
921 add_test (specs, nt);
926 npar_sign (struct lexer *lexer, struct dataset *ds,
927 struct npar_specs *specs)
929 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
930 struct npar_test *nt = &tp->parent;
932 nt->execute = sign_execute;
934 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
938 add_test (specs, nt);
943 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
944 struct npar_specs *specs)
946 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
947 struct npar_test *nt = &tp->parent;
949 nt->execute = mcnemar_execute;
951 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
955 add_test (specs, nt);
961 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
962 struct npar_specs *specs)
964 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
965 struct npar_test *nt = &tp->parent;
967 nt->insert_variables = n_sample_insert_variables;
968 nt->execute = jonckheere_terpstra_execute;
970 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
973 add_test (specs, nt);
978 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
979 struct npar_specs *specs)
981 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
982 struct npar_test *nt = &tp->parent;
984 nt->insert_variables = n_sample_insert_variables;
986 nt->execute = kruskal_wallis_execute;
988 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
991 add_test (specs, nt);
996 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
998 size_t hash = hash_pointer (var, 0);
999 struct hmapx_node *node;
1000 const struct variable *v = NULL;
1002 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1006 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1009 /* Insert the variables for TEST into VAR_MAP */
1011 one_sample_insert_variables (const struct npar_test *test,
1012 struct hmapx *var_map)
1014 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1016 for (size_t i = 0; i < ost->n_vars; ++i)
1017 insert_variable_into_map (var_map, ost->vars[i]);
1022 two_sample_insert_variables (const struct npar_test *test,
1023 struct hmapx *var_map)
1025 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1027 for (size_t i = 0; i < tst->n_pairs; ++i)
1029 variable_pair *pair = &tst->pairs[i];
1031 insert_variable_into_map (var_map, (*pair)[0]);
1032 insert_variable_into_map (var_map, (*pair)[1]);
1037 n_sample_insert_variables (const struct npar_test *test,
1038 struct hmapx *var_map)
1040 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1042 for (size_t i = 0; i < tst->n_vars; ++i)
1043 insert_variable_into_map (var_map, tst->vars[i]);
1045 insert_variable_into_map (var_map, tst->indep_var);
1049 npar_method (struct lexer *lexer, struct npar_specs *specs)
1051 if (lex_match_id (lexer, "EXACT"))
1053 specs->exact = true;
1055 if (lex_match_id (lexer, "TIMER"))
1059 if (lex_match (lexer, T_LPAREN))
1061 if (!lex_force_num (lexer))
1063 specs->timer = lex_number (lexer);
1065 if (!lex_force_match (lexer, T_RPAREN))