1 /* PSPP - a program for statistical analysis. -*-c-*-
2 Copyright (C) 2006, 2008, 2009, 2010 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>
20 #include "npar-summary.h"
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/settings.h>
33 #include <data/variable.h>
34 #include <libpspp/array.h>
35 #include <libpspp/assertion.h>
36 #include <libpspp/cast.h>
37 #include <libpspp/hmapx.h>
38 #include <libpspp/hash-functions.h>
39 #include <libpspp/message.h>
40 #include <libpspp/pool.h>
41 #include <libpspp/str.h>
42 #include <libpspp/taint.h>
43 #include <language/command.h>
44 #include <language/lexer/lexer.h>
45 #include <language/lexer/variable-parser.h>
46 #include <language/lexer/value-parser.h>
47 #include <language/stats/binomial.h>
48 #include <language/stats/chisquare.h>
49 #include <language/stats/runs.h>
50 #include <language/stats/friedman.h>
51 #include <language/stats/kruskal-wallis.h>
52 #include <language/stats/wilcoxon.h>
53 #include <language/stats/sign.h>
54 #include <math/moments.h>
57 #define _(msgid) gettext (msgid)
59 /* Settings for subcommand specifiers. */
66 /* Array indices for STATISTICS subcommand. */
69 NPAR_ST_DESCRIPTIVES = 0,
70 NPAR_ST_QUARTILES = 1,
75 /* NPAR TESTS structure. */
78 /* Count variables indicating how many
79 of the subcommands have been given. */
91 /* How missing values should be treated */
94 /* Which statistics have been requested */
95 int a_statistics[NPAR_ST_count];
102 struct npar_test **test;
105 const struct variable **vv; /* Compendium of all variables
106 (those mentioned on ANY subcommand */
107 int n_vars; /* Number of variables in vv */
109 enum mv_class filter; /* Missing values to filter. */
111 bool descriptives; /* Descriptive statistics should be calculated */
112 bool quartiles; /* Quartiles should be calculated */
114 bool exact; /* Whether exact calculations have been requested */
115 double timer; /* Maximum time (in minutes) to wait for exact calculations */
119 /* Prototype for custom subcommands of NPAR TESTS. */
120 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
121 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
122 static int npar_runs (struct lexer *, struct dataset *, struct npar_specs *);
123 static int npar_friedman (struct lexer *, struct dataset *, struct npar_specs *);
124 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
125 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
126 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
127 static int npar_method (struct lexer *, struct npar_specs *);
129 /* Command parsing functions. */
130 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
131 struct npar_specs *npar_specs );
134 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
135 struct npar_specs *nps)
144 npt->miss = MISS_ANALYSIS;
147 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
150 if (lex_match_hyphenated_word (lexer, "FRIEDMAN"))
153 switch (npar_friedman (lexer, ds, nps))
160 lex_error (lexer, NULL);
166 else if (lex_match_hyphenated_word (lexer, "RUNS"))
169 switch (npar_runs (lexer, ds, nps))
176 lex_error (lexer, NULL);
182 else if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
184 lex_match (lexer, '=');
186 switch (npar_chisquare (lexer, ds, nps))
193 lex_error (lexer, NULL);
199 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
201 lex_match (lexer, '=');
203 switch (npar_binomial (lexer, ds, nps))
210 lex_error (lexer, NULL);
216 else if (lex_match_hyphenated_word (lexer, "K-W") ||
217 lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
219 lex_match (lexer, '=');
220 npt->kruskal_wallis++;
221 switch (npar_kruskal_wallis (lexer, ds, nps))
228 lex_error (lexer, NULL);
234 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
236 lex_match (lexer, '=');
238 switch (npar_wilcoxon (lexer, ds, nps))
245 lex_error (lexer, NULL);
251 else if (lex_match_hyphenated_word (lexer, "SIGN"))
253 lex_match (lexer, '=');
255 switch (npar_sign (lexer, ds, nps))
262 lex_error (lexer, NULL);
268 else if (lex_match_hyphenated_word (lexer, "MISSING"))
270 lex_match (lexer, '=');
272 if (npt->missing > 1)
274 msg (SE, _("The %s subcommand may be given only once."), "MISSING");
277 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
279 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
280 npt->miss = MISS_ANALYSIS;
281 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
282 npt->miss = MISS_LISTWISE;
283 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
284 nps->filter = MV_SYSTEM;
285 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
286 nps->filter = MV_ANY;
289 lex_error (lexer, NULL);
292 lex_match (lexer, ',');
295 else if (lex_match_hyphenated_word (lexer, "METHOD"))
297 lex_match (lexer, '=');
301 msg (SE, _("The %s subcommand may be given only once."), "METHOD");
304 switch (npar_method (lexer, nps))
311 lex_error (lexer, NULL);
317 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
319 lex_match (lexer, '=');
321 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
323 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
324 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
325 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
326 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
327 else if (lex_match (lexer, T_ALL))
328 npt->a_statistics[NPAR_ST_ALL] = 1;
331 lex_error (lexer, NULL);
334 lex_match (lexer, ',');
337 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
339 lex_match (lexer, '=');
340 if (lex_match_id (lexer, "COMPATIBLE"))
341 settings_set_cmd_algorithm (COMPATIBLE);
342 else if (lex_match_id (lexer, "ENHANCED"))
343 settings_set_cmd_algorithm (ENHANCED);
345 if (!lex_match (lexer, '/'))
349 if (lex_token (lexer) != '.')
351 lex_error (lexer, _("expecting end of command"));
362 static void one_sample_insert_variables (const struct npar_test *test,
365 static void two_sample_insert_variables (const struct npar_test *test,
368 static void n_sample_insert_variables (const struct npar_test *test,
372 npar_execute (struct casereader *input,
373 const struct npar_specs *specs,
374 const struct dataset *ds)
377 struct descriptives *summary_descriptives = NULL;
379 for ( t = 0 ; t < specs->n_tests; ++t )
381 const struct npar_test *test = specs->test[t];
382 if ( NULL == test->execute )
384 msg (SW, _("NPAR subcommand not currently implemented."));
387 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
390 if ( specs->descriptives )
392 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
395 npar_summary_calc_descriptives (summary_descriptives,
396 casereader_clone (input),
398 specs->vv, specs->n_vars,
402 if ( (specs->descriptives || specs->quartiles)
403 && !taint_has_tainted_successor (casereader_get_taint (input)) )
404 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
406 free (summary_descriptives);
407 casereader_destroy (input);
411 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
413 struct cmd_npar_tests cmd;
416 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
417 struct casegrouper *grouper;
418 struct casereader *input, *group;
419 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
422 npar_specs.pool = pool_create ();
423 npar_specs.filter = MV_ANY;
424 npar_specs.n_vars = -1;
425 npar_specs.vv = NULL;
427 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
429 pool_destroy (npar_specs.pool);
433 for (i = 0; i < npar_specs.n_tests; ++i )
435 const struct npar_test *test = npar_specs.test[i];
436 test->insert_variables (test, &var_map);
440 struct hmapx_node *node;
441 struct variable *var;
442 npar_specs.n_vars = 0;
444 HMAPX_FOR_EACH (var, node, &var_map)
446 npar_specs.n_vars ++;
447 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
448 npar_specs.vv[npar_specs.n_vars - 1] = var;
452 sort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
453 compare_var_ptrs_by_name, NULL);
455 if ( cmd.statistics )
459 for ( i = 0 ; i < NPAR_ST_count; ++i )
461 if ( cmd.a_statistics[i] )
465 case NPAR_ST_DESCRIPTIVES:
466 npar_specs.descriptives = true;
468 case NPAR_ST_QUARTILES:
469 npar_specs.quartiles = true;
472 npar_specs.quartiles = true;
473 npar_specs.descriptives = true;
482 input = proc_open (ds);
483 if ( cmd.miss == MISS_LISTWISE )
485 input = casereader_create_filter_missing (input,
493 grouper = casegrouper_create_splits (input, dataset_dict (ds));
494 while (casegrouper_get_next_group (grouper, &group))
495 npar_execute (group, &npar_specs, ds);
496 ok = casegrouper_destroy (grouper);
497 ok = proc_commit (ds) && ok;
499 pool_destroy (npar_specs.pool);
500 hmapx_destroy (&var_map);
502 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
506 npar_runs (struct lexer *lexer, struct dataset *ds,
507 struct npar_specs *specs)
509 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
510 struct one_sample_test *tp = &rt->parent;
511 struct npar_test *nt = &tp->parent;
513 nt->execute = runs_execute;
514 nt->insert_variables = one_sample_insert_variables;
516 if ( lex_force_match (lexer, '(') )
518 if ( lex_match_id (lexer, "MEAN"))
520 rt->cp_mode = CP_MEAN;
522 else if (lex_match_id (lexer, "MEDIAN"))
524 rt->cp_mode = CP_MEDIAN;
526 else if (lex_match_id (lexer, "MODE"))
528 rt->cp_mode = CP_MODE;
530 else if (lex_is_number (lexer))
532 rt->cutpoint = lex_number (lexer);
533 rt->cp_mode = CP_CUSTOM;
538 lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
542 lex_force_match (lexer, ')');
543 lex_force_match (lexer, '=');
544 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
545 &tp->vars, &tp->n_vars,
546 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
553 specs->test = pool_realloc (specs->pool,
555 sizeof (*specs->test) * specs->n_tests);
557 specs->test[specs->n_tests - 1] = nt;
563 npar_friedman (struct lexer *lexer, struct dataset *ds,
564 struct npar_specs *specs)
566 struct one_sample_test *ft = pool_alloc (specs->pool, sizeof (*ft));
567 struct npar_test *nt = &ft->parent;
569 nt->execute = friedman_execute;
570 nt->insert_variables = one_sample_insert_variables;
572 lex_match (lexer, '=');
574 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
575 &ft->vars, &ft->n_vars,
576 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
582 specs->test = pool_realloc (specs->pool,
584 sizeof (*specs->test) * specs->n_tests);
586 specs->test[specs->n_tests - 1] = nt;
593 npar_chisquare (struct lexer *lexer, struct dataset *ds,
594 struct npar_specs *specs)
596 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
597 struct one_sample_test *tp = &cstp->parent;
598 struct npar_test *nt = &tp->parent;
601 nt->execute = chisquare_execute;
602 nt->insert_variables = one_sample_insert_variables;
604 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
605 &tp->vars, &tp->n_vars,
606 PV_NO_SCRATCH | PV_NO_DUPLICATE))
611 cstp->ranged = false;
613 if ( lex_match (lexer, '('))
616 if ( ! lex_force_num (lexer)) return 0;
617 cstp->lo = lex_integer (lexer);
619 lex_force_match (lexer, ',');
620 if (! lex_force_num (lexer) ) return 0;
621 cstp->hi = lex_integer (lexer);
622 if ( cstp->lo >= cstp->hi )
625 _("The specified value of HI (%d) is "
626 "lower than the specified value of LO (%d)"),
631 if (! lex_force_match (lexer, ')')) return 0;
634 cstp->n_expected = 0;
635 cstp->expected = NULL;
636 if ( lex_match (lexer, '/') )
638 if ( lex_match_id (lexer, "EXPECTED") )
640 lex_force_match (lexer, '=');
641 if ( ! lex_match_id (lexer, "EQUAL") )
645 while ( lex_is_number (lexer) )
649 f = lex_number (lexer);
651 if ( lex_match (lexer, '*'))
654 f = lex_number (lexer);
657 lex_match (lexer, ',');
659 cstp->n_expected += n;
660 cstp->expected = pool_realloc (specs->pool,
664 for ( i = cstp->n_expected - n ;
665 i < cstp->n_expected;
667 cstp->expected[i] = f;
673 lex_put_back (lexer, '/');
676 if ( cstp->ranged && cstp->n_expected > 0 &&
677 cstp->n_expected != cstp->hi - cstp->lo + 1 )
680 _("%d expected values were given, but the specified "
681 "range (%d-%d) requires exactly %d values."),
682 cstp->n_expected, cstp->lo, cstp->hi,
683 cstp->hi - cstp->lo +1);
688 specs->test = pool_realloc (specs->pool,
690 sizeof (*specs->test) * specs->n_tests);
692 specs->test[specs->n_tests - 1] = nt;
699 npar_binomial (struct lexer *lexer, struct dataset *ds,
700 struct npar_specs *specs)
702 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
703 struct one_sample_test *tp = &btp->parent;
704 struct npar_test *nt = &tp->parent;
706 nt->execute = binomial_execute;
707 nt->insert_variables = one_sample_insert_variables;
709 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
713 if ( lex_match (lexer, '(') )
715 if ( lex_force_num (lexer) )
717 btp->p = lex_number (lexer);
719 lex_force_match (lexer, ')');
725 /* Kludge: q2c swallows the '=' so put it back here */
726 lex_put_back (lexer, '=');
728 if (lex_match (lexer, '=') )
730 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
731 &tp->vars, &tp->n_vars,
732 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
734 if (lex_match (lexer, '('))
736 lex_force_num (lexer);
737 btp->category1 = lex_number (lexer);
739 if ( lex_match (lexer, ','))
741 if ( ! lex_force_num (lexer) ) return 2;
742 btp->category2 = lex_number (lexer);
747 btp->cutpoint = btp->category1;
750 lex_force_match (lexer, ')');
759 specs->test = pool_realloc (specs->pool,
761 sizeof (*specs->test) * specs->n_tests);
763 specs->test[specs->n_tests - 1] = nt;
770 parse_two_sample_related_test (struct lexer *lexer,
771 const struct dictionary *dict,
772 struct two_sample_test *test_parameters,
778 parse_two_sample_related_test (struct lexer *lexer,
779 const struct dictionary *dict,
780 struct two_sample_test *test_parameters,
787 const struct variable **vlist1;
790 const struct variable **vlist2;
793 test_parameters->parent.insert_variables = two_sample_insert_variables;
795 if (!parse_variables_const_pool (lexer, pool,
798 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
801 if ( lex_match (lexer, T_WITH))
804 if ( !parse_variables_const_pool (lexer, pool, dict,
806 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
809 paired = (lex_match (lexer, '(') &&
810 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
818 if ( n_vlist1 != n_vlist2)
819 msg (SE, _("PAIRED was specified but the number of variables "
820 "preceding WITH (%zu) did not match the number "
821 "following (%zu)."), n_vlist1, n_vlist2);
823 test_parameters->n_pairs = n_vlist1 ;
827 test_parameters->n_pairs = n_vlist1 * n_vlist2;
832 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
835 test_parameters->pairs =
836 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
843 assert (n_vlist1 == n_vlist2);
844 for ( i = 0 ; i < n_vlist1; ++i )
846 test_parameters->pairs[n][1] = vlist1[i];
847 test_parameters->pairs[n][0] = vlist2[i];
854 for ( i = 0 ; i < n_vlist1; ++i )
856 for ( j = 0 ; j < n_vlist2; ++j )
858 test_parameters->pairs[n][1] = vlist1[i];
859 test_parameters->pairs[n][0] = vlist2[j];
868 for ( i = 0 ; i < n_vlist1 - 1; ++i )
870 for ( j = i + 1 ; j < n_vlist1; ++j )
872 assert ( n < test_parameters->n_pairs);
873 test_parameters->pairs[n][1] = vlist1[i];
874 test_parameters->pairs[n][0] = vlist1[j];
880 assert ( n == test_parameters->n_pairs);
887 parse_n_sample_related_test (struct lexer *lexer,
888 const struct dictionary *dict,
889 struct n_sample_test *nst,
893 if (!parse_variables_const_pool (lexer, pool,
895 &nst->vars, &nst->n_vars,
896 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
899 if ( ! lex_force_match (lexer, T_BY))
902 nst->indep_var = parse_variable_const (lexer, dict);
904 if ( ! lex_force_match (lexer, '('))
907 value_init (&nst->val1, var_get_width (nst->indep_var));
908 if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
910 value_destroy (&nst->val1, var_get_width (nst->indep_var));
914 if ( ! lex_force_match (lexer, ','))
917 value_init (&nst->val2, var_get_width (nst->indep_var));
918 if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
920 value_destroy (&nst->val2, var_get_width (nst->indep_var));
924 if ( ! lex_force_match (lexer, ')'))
931 npar_wilcoxon (struct lexer *lexer,
933 struct npar_specs *specs )
937 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
938 struct npar_test *nt = &tp->parent;
939 nt->execute = wilcoxon_execute;
941 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
946 specs->test = pool_realloc (specs->pool,
948 sizeof (*specs->test) * specs->n_tests);
949 specs->test[specs->n_tests - 1] = nt;
955 npar_sign (struct lexer *lexer, struct dataset *ds,
956 struct npar_specs *specs)
958 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
959 struct npar_test *nt = &tp->parent;
961 nt->execute = sign_execute;
963 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
968 specs->test = pool_realloc (specs->pool,
970 sizeof (*specs->test) * specs->n_tests);
971 specs->test[specs->n_tests - 1] = nt;
977 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
978 struct npar_specs *specs)
980 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
981 struct npar_test *nt = &tp->parent;
983 nt->insert_variables = n_sample_insert_variables;
985 nt->execute = kruskal_wallis_execute;
987 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
992 specs->test = pool_realloc (specs->pool,
994 sizeof (*specs->test) * specs->n_tests);
995 specs->test[specs->n_tests - 1] = nt;
1001 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
1003 size_t hash = hash_pointer (var, 0);
1004 struct hmapx_node *node;
1005 const struct variable *v = NULL;
1007 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
1013 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
1016 /* Insert the variables for TEST into VAR_MAP */
1018 one_sample_insert_variables (const struct npar_test *test,
1019 struct hmapx *var_map)
1022 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
1024 for ( i = 0 ; i < ost->n_vars ; ++i )
1025 insert_variable_into_map (var_map, ost->vars[i]);
1030 two_sample_insert_variables (const struct npar_test *test,
1031 struct hmapx *var_map)
1034 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
1036 for ( i = 0 ; i < tst->n_pairs ; ++i )
1038 variable_pair *pair = &tst->pairs[i];
1040 insert_variable_into_map (var_map, (*pair)[0]);
1041 insert_variable_into_map (var_map, (*pair)[1]);
1046 n_sample_insert_variables (const struct npar_test *test,
1047 struct hmapx *var_map)
1050 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1052 for ( i = 0 ; i < tst->n_vars ; ++i )
1053 insert_variable_into_map (var_map, tst->vars[i]);
1055 insert_variable_into_map (var_map, tst->indep_var);
1060 npar_method (struct lexer *lexer, struct npar_specs *specs)
1062 if ( lex_match_id (lexer, "EXACT") )
1064 specs->exact = true;
1066 if (lex_match_id (lexer, "TIMER"))
1070 if ( lex_match (lexer, '('))
1072 if ( lex_force_num (lexer) )
1074 specs->timer = lex_number (lexer);
1077 lex_force_match (lexer, ')');