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/commands/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/commands/binomial.h"
36 #include "language/commands/chisquare.h"
37 #include "language/commands/ks-one-sample.h"
38 #include "language/commands/cochran.h"
39 #include "language/commands/friedman.h"
40 #include "language/commands/jonckheere-terpstra.h"
41 #include "language/commands/kruskal-wallis.h"
42 #include "language/commands/mann-whitney.h"
43 #include "language/commands/mcnemar.h"
44 #include "language/commands/median.h"
45 #include "language/commands/npar-summary.h"
46 #include "language/commands/runs.h"
47 #include "language/commands/sign.h"
48 #include "language/commands/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_phrase (lexer, ")="))
429 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
430 &tp->vars, &tp->n_vars,
431 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
434 add_test (specs, nt);
439 npar_friedman (struct lexer *lexer, struct dataset *ds,
440 struct npar_specs *specs)
442 struct friedman_test *ft = pool_alloc (specs->pool, sizeof (*ft));
443 struct one_sample_test *ost = &ft->parent;
444 struct npar_test *nt = &ost->parent;
446 ft->kendalls_w = false;
447 nt->execute = friedman_execute;
448 nt->insert_variables = one_sample_insert_variables;
450 lex_match (lexer, T_EQUALS);
452 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
453 &ost->vars, &ost->n_vars,
454 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
457 add_test (specs, nt);
462 npar_kendall (struct lexer *lexer, struct dataset *ds,
463 struct npar_specs *specs)
465 struct friedman_test *kt = pool_alloc (specs->pool, sizeof (*kt));
466 struct one_sample_test *ost = &kt->parent;
467 struct npar_test *nt = &ost->parent;
469 kt->kendalls_w = true;
470 nt->execute = friedman_execute;
471 nt->insert_variables = one_sample_insert_variables;
473 lex_match (lexer, T_EQUALS);
475 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
476 &ost->vars, &ost->n_vars,
477 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
480 add_test (specs, nt);
486 npar_cochran (struct lexer *lexer, struct dataset *ds,
487 struct npar_specs *specs)
489 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
490 struct npar_test *nt = &ft->parent;
492 nt->execute = cochran_execute;
493 nt->insert_variables = one_sample_insert_variables;
495 lex_match (lexer, T_EQUALS);
497 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
498 &ft->vars, &ft->n_vars,
499 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
502 add_test (specs, nt);
507 npar_chisquare (struct lexer *lexer, struct dataset *ds,
508 struct npar_specs *specs)
510 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
511 struct one_sample_test *tp = &cstp->parent;
512 struct npar_test *nt = &tp->parent;
514 nt->execute = chisquare_execute;
515 nt->insert_variables = one_sample_insert_variables;
517 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
518 &tp->vars, &tp->n_vars,
519 PV_NO_SCRATCH | PV_NO_DUPLICATE))
522 cstp->ranged = false;
524 if (lex_match (lexer, T_LPAREN))
527 if (!lex_force_num (lexer))
529 cstp->lo = lex_number (lexer);
532 if (!lex_force_match (lexer, T_COMMA))
534 if (!lex_force_num_range_open (lexer, "HI", cstp->lo, DBL_MAX))
536 cstp->hi = lex_number (lexer);
538 if (!lex_force_match (lexer, T_RPAREN))
542 cstp->n_expected = 0;
543 cstp->expected = NULL;
544 int expected_start = 0;
545 int expected_end = 0;
546 if (lex_match_phrase (lexer, "/EXPECTED"))
548 if (!lex_force_match (lexer, T_EQUALS))
551 if (!lex_match_id (lexer, "EQUAL"))
553 expected_start = lex_ofs (lexer);
554 while (lex_is_number (lexer))
557 double f = lex_number (lexer);
559 if (lex_match (lexer, T_ASTERISK))
562 if (!lex_force_num (lexer))
564 f = lex_number (lexer);
567 lex_match (lexer, T_COMMA);
569 cstp->n_expected += n;
570 cstp->expected = pool_realloc (specs->pool,
572 sizeof (double) * cstp->n_expected);
573 for (int i = cstp->n_expected - n; i < cstp->n_expected; ++i)
574 cstp->expected[i] = f;
576 expected_end = lex_ofs (lexer) - 1;
580 if (cstp->ranged && cstp->n_expected > 0 &&
581 cstp->n_expected != cstp->hi - cstp->lo + 1)
583 lex_ofs_error (lexer, expected_start, expected_end,
584 _("%d expected values were given, but the specified "
585 "range (%d-%d) requires exactly %d values."),
586 cstp->n_expected, cstp->lo, cstp->hi,
587 cstp->hi - cstp->lo +1);
591 add_test (specs, nt);
596 npar_binomial (struct lexer *lexer, struct dataset *ds,
597 struct npar_specs *specs)
599 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
600 struct one_sample_test *tp = &btp->parent;
601 struct npar_test *nt = &tp->parent;
603 nt->execute = binomial_execute;
604 nt->insert_variables = one_sample_insert_variables;
606 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
610 if (lex_match (lexer, T_LPAREN))
612 if (!lex_force_num (lexer))
614 btp->p = lex_number (lexer);
616 if (!lex_force_match (lexer, T_RPAREN))
618 if (!lex_force_match (lexer, T_EQUALS))
622 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
623 &tp->vars, &tp->n_vars,
624 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
626 if (lex_match (lexer, T_LPAREN))
628 if (!lex_force_num (lexer))
630 btp->category1 = lex_number (lexer);
632 if (lex_match (lexer, T_COMMA))
634 if (!lex_force_num (lexer))
636 btp->category2 = lex_number (lexer);
640 btp->cutpoint = btp->category1;
642 if (!lex_force_match (lexer, T_RPAREN))
646 add_test (specs, nt);
651 ks_one_sample_parse_params (struct lexer *lexer, struct ks_one_sample_test *kst, int params)
653 assert (params == 1 || params == 2);
655 if (lex_is_number (lexer))
657 kst->p[0] = lex_number (lexer);
662 lex_match (lexer, T_COMMA);
663 if (lex_force_num (lexer))
665 kst->p[1] = lex_number (lexer);
673 npar_ks_one_sample (struct lexer *lexer, struct dataset *ds, struct npar_specs *specs)
675 struct ks_one_sample_test *kst = pool_alloc (specs->pool, sizeof (*kst));
676 struct one_sample_test *tp = &kst->parent;
677 struct npar_test *nt = &tp->parent;
679 nt->execute = ks_one_sample_execute;
680 nt->insert_variables = one_sample_insert_variables;
682 kst->p[0] = kst->p[1] = SYSMIS;
684 if (!lex_force_match (lexer, T_LPAREN))
687 if (lex_match_id (lexer, "NORMAL"))
689 kst->dist = KS_NORMAL;
690 ks_one_sample_parse_params (lexer, kst, 2);
692 else if (lex_match_id (lexer, "POISSON"))
694 kst->dist = KS_POISSON;
695 ks_one_sample_parse_params (lexer, kst, 1);
697 else if (lex_match_id (lexer, "UNIFORM"))
699 kst->dist = KS_UNIFORM;
700 ks_one_sample_parse_params (lexer, kst, 2);
702 else if (lex_match_id (lexer, "EXPONENTIAL"))
704 kst->dist = KS_EXPONENTIAL;
705 ks_one_sample_parse_params (lexer, kst, 1);
709 lex_error_expecting (lexer, "NORMAL", "POISSON", "UNIFORM",
714 if (!lex_force_match (lexer, T_RPAREN))
717 lex_match (lexer, T_EQUALS);
719 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
720 &tp->vars, &tp->n_vars,
721 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
724 add_test (specs, nt);
730 parse_two_sample_related_test (struct lexer *lexer,
731 const struct dictionary *dict,
732 struct two_sample_test *tp,
735 tp->parent.insert_variables = two_sample_insert_variables;
737 const struct variable **v1;
739 int vars_start = lex_ofs (lexer);
740 if (!parse_variables_const_pool (lexer, pool, dict, &v1, &n1,
741 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
746 const struct variable **v2 = NULL;
748 if (lex_match (lexer, T_WITH))
751 if (!parse_variables_const_pool (lexer, pool, dict, &v2, &n2,
752 PV_NUMERIC | PV_NO_SCRATCH | PV_DUPLICATE))
754 int vars_end = lex_ofs (lexer) - 1;
756 if (lex_match (lexer, T_LPAREN))
758 if (!lex_force_match_phrase (lexer, "PAIRED)"))
764 lex_ofs_error (lexer, vars_start, vars_end,
765 _("PAIRED was specified, but the number of "
766 "variables preceding WITH (%zu) does not match "
767 "the number following (%zu)."),
774 tp->n_pairs = (paired ? n1
776 : (n1 * (n1 - 1)) / 2);
777 tp->pairs = pool_alloc (pool, sizeof (variable_pair) * tp->n_pairs);
781 for (size_t i = 0; i < n1 - 1; ++i)
782 for (size_t j = i + 1; j < n1; ++j)
784 assert (n < tp->n_pairs);
785 tp->pairs[n][0] = v1[i];
786 tp->pairs[n][1] = v1[j];
792 for (size_t i = 0; i < n1; ++i)
794 tp->pairs[n][0] = v1[i];
795 tp->pairs[n][1] = v2[i];
801 for (size_t i = 0; i < n1; ++i)
802 for (size_t j = 0; j < n2; ++j)
804 tp->pairs[n][0] = v1[i];
805 tp->pairs[n][1] = v2[j];
809 assert (n == tp->n_pairs);
815 parse_n_sample_related_test (struct lexer *lexer, const struct dictionary *dict,
816 struct n_sample_test *nst, struct pool *pool)
818 if (!parse_variables_const_pool (lexer, pool, dict, &nst->vars, &nst->n_vars,
819 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE))
822 if (!lex_force_match (lexer, T_BY))
825 nst->indep_var = parse_variable_const (lexer, dict);
829 if (!lex_force_match (lexer, T_LPAREN))
832 value_init (&nst->val1, var_get_width (nst->indep_var));
833 if (!parse_value (lexer, &nst->val1, nst->indep_var))
835 value_destroy (&nst->val1, var_get_width (nst->indep_var));
839 lex_match (lexer, T_COMMA);
841 value_init (&nst->val2, var_get_width (nst->indep_var));
842 if (!parse_value (lexer, &nst->val2, nst->indep_var))
844 value_destroy (&nst->val2, var_get_width (nst->indep_var));
848 if (!lex_force_match (lexer, T_RPAREN))
855 npar_wilcoxon (struct lexer *lexer,
857 struct npar_specs *specs)
859 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
860 struct npar_test *nt = &tp->parent;
861 nt->execute = wilcoxon_execute;
863 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
867 add_test (specs, nt);
872 npar_mann_whitney (struct lexer *lexer,
874 struct npar_specs *specs)
876 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
877 struct npar_test *nt = &tp->parent;
879 nt->insert_variables = n_sample_insert_variables;
880 nt->execute = mann_whitney_execute;
882 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
885 add_test (specs, nt);
890 npar_median (struct lexer *lexer,
892 struct npar_specs *specs)
894 struct median_test *mt = pool_alloc (specs->pool, sizeof (*mt));
895 struct n_sample_test *tp = &mt->parent;
896 struct npar_test *nt = &tp->parent;
900 if (lex_match (lexer, T_LPAREN))
902 if (!lex_force_num (lexer))
904 mt->median = lex_number (lexer);
907 if (!lex_force_match (lexer, T_RPAREN))
911 lex_match (lexer, T_EQUALS);
913 nt->insert_variables = n_sample_insert_variables;
914 nt->execute = median_execute;
916 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
919 add_test (specs, nt);
924 npar_sign (struct lexer *lexer, struct dataset *ds,
925 struct npar_specs *specs)
927 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
928 struct npar_test *nt = &tp->parent;
930 nt->execute = sign_execute;
932 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
936 add_test (specs, nt);
941 npar_mcnemar (struct lexer *lexer, struct dataset *ds,
942 struct npar_specs *specs)
944 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
945 struct npar_test *nt = &tp->parent;
947 nt->execute = mcnemar_execute;
949 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
953 add_test (specs, nt);
959 npar_jonckheere_terpstra (struct lexer *lexer, struct dataset *ds,
960 struct npar_specs *specs)
962 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
963 struct npar_test *nt = &tp->parent;
965 nt->insert_variables = n_sample_insert_variables;
966 nt->execute = jonckheere_terpstra_execute;
968 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
971 add_test (specs, nt);
976 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
977 struct npar_specs *specs)
979 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
980 struct npar_test *nt = &tp->parent;
982 nt->insert_variables = n_sample_insert_variables;
984 nt->execute = kruskal_wallis_execute;
986 if (!parse_n_sample_related_test (lexer, dataset_dict (ds), tp, specs->pool))
989 add_test (specs, nt);
994 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
996 size_t hash = hash_pointer (var, 0);
997 struct hmapx_node *node;
998 const struct variable *v = NULL;
1000 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1004 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1007 /* Insert the variables for TEST into VAR_MAP */
1009 one_sample_insert_variables (const struct npar_test *test,
1010 struct hmapx *var_map)
1012 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1014 for (size_t i = 0; i < ost->n_vars; ++i)
1015 insert_variable_into_map (var_map, ost->vars[i]);
1020 two_sample_insert_variables (const struct npar_test *test,
1021 struct hmapx *var_map)
1023 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1025 for (size_t i = 0; i < tst->n_pairs; ++i)
1027 variable_pair *pair = &tst->pairs[i];
1029 insert_variable_into_map (var_map, (*pair)[0]);
1030 insert_variable_into_map (var_map, (*pair)[1]);
1035 n_sample_insert_variables (const struct npar_test *test,
1036 struct hmapx *var_map)
1038 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1040 for (size_t i = 0; i < tst->n_vars; ++i)
1041 insert_variable_into_map (var_map, tst->vars[i]);
1043 insert_variable_into_map (var_map, tst->indep_var);
1047 npar_method (struct lexer *lexer, struct npar_specs *specs)
1049 if (lex_match_id (lexer, "EXACT"))
1051 specs->exact = true;
1053 if (lex_match_id (lexer, "TIMER"))
1057 if (lex_match (lexer, T_LPAREN))
1059 if (!lex_force_num (lexer))
1061 specs->timer = lex_number (lexer);
1063 if (!lex_force_match (lexer, T_RPAREN))