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/assertion.h>
35 #include <libpspp/cast.h>
36 #include <libpspp/hmapx.h>
37 #include <libpspp/hash-functions.h>
38 #include <libpspp/message.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
41 #include <libpspp/taint.h>
42 #include <language/command.h>
43 #include <language/lexer/lexer.h>
44 #include <language/lexer/variable-parser.h>
45 #include <language/lexer/value-parser.h>
46 #include <language/stats/binomial.h>
47 #include <language/stats/chisquare.h>
48 #include <language/stats/runs.h>
49 #include <language/stats/kruskal-wallis.h>
50 #include <language/stats/wilcoxon.h>
51 #include <language/stats/sign.h>
52 #include <math/moments.h>
55 #define _(msgid) gettext (msgid)
57 /* Settings for subcommand specifiers. */
64 /* Array indices for STATISTICS subcommand. */
67 NPAR_ST_DESCRIPTIVES = 0,
68 NPAR_ST_QUARTILES = 1,
73 /* NPAR TESTS structure. */
76 /* Count variables indicating how many
77 of the subcommands have been given. */
88 /* How missing values should be treated */
91 /* Which statistics have been requested */
92 int a_statistics[NPAR_ST_count];
99 struct npar_test **test;
102 const struct variable **vv; /* Compendium of all variables
103 (those mentioned on ANY subcommand */
104 int n_vars; /* Number of variables in vv */
106 enum mv_class filter; /* Missing values to filter. */
108 bool descriptives; /* Descriptive statistics should be calculated */
109 bool quartiles; /* Quartiles should be calculated */
111 bool exact; /* Whether exact calculations have been requested */
112 double timer; /* Maximum time (in minutes) to wait for exact calculations */
116 /* Prototype for custom subcommands of NPAR TESTS. */
117 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
118 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
119 static int npar_runs (struct lexer *lexer, struct dataset *, struct npar_specs *);
121 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
122 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
123 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
124 static int npar_method (struct lexer *, struct npar_specs *);
126 /* Command parsing functions. */
127 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
128 struct npar_specs *npar_specs );
131 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
132 struct npar_specs *nps)
140 npt->miss = MISS_ANALYSIS;
143 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
146 if (lex_match_hyphenated_word (lexer, "RUNS"))
149 switch (npar_runs (lexer, ds, nps))
156 lex_error (lexer, NULL);
163 else if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
165 lex_match (lexer, '=');
167 switch (npar_chisquare (lexer, ds, nps))
174 lex_error (lexer, NULL);
180 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
182 lex_match (lexer, '=');
184 switch (npar_binomial (lexer, ds, nps))
191 lex_error (lexer, NULL);
197 else if (lex_match_hyphenated_word (lexer, "K-W") ||
198 lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
200 lex_match (lexer, '=');
201 npt->kruskal_wallis++;
202 switch (npar_kruskal_wallis (lexer, ds, nps))
209 lex_error (lexer, NULL);
215 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
217 lex_match (lexer, '=');
219 switch (npar_wilcoxon (lexer, ds, nps))
226 lex_error (lexer, NULL);
232 else if (lex_match_hyphenated_word (lexer, "SIGN"))
234 lex_match (lexer, '=');
236 switch (npar_sign (lexer, ds, nps))
243 lex_error (lexer, NULL);
249 else if (lex_match_hyphenated_word (lexer, "MISSING"))
251 lex_match (lexer, '=');
253 if (npt->missing > 1)
255 msg (SE, _("The %s subcommand may be given only once."), "MISSING");
258 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
260 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
261 npt->miss = MISS_ANALYSIS;
262 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
263 npt->miss = MISS_LISTWISE;
264 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
265 nps->filter = MV_SYSTEM;
266 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
267 nps->filter = MV_ANY;
270 lex_error (lexer, NULL);
273 lex_match (lexer, ',');
276 else if (lex_match_hyphenated_word (lexer, "METHOD"))
278 lex_match (lexer, '=');
282 msg (SE, _("The %s subcommand may be given only once."), "METHOD");
285 switch (npar_method (lexer, nps))
292 lex_error (lexer, NULL);
298 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
300 lex_match (lexer, '=');
302 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
304 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
305 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
306 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
307 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
308 else if (lex_match (lexer, T_ALL))
309 npt->a_statistics[NPAR_ST_ALL] = 1;
312 lex_error (lexer, NULL);
315 lex_match (lexer, ',');
318 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
320 lex_match (lexer, '=');
321 if (lex_match_id (lexer, "COMPATIBLE"))
322 settings_set_cmd_algorithm (COMPATIBLE);
323 else if (lex_match_id (lexer, "ENHANCED"))
324 settings_set_cmd_algorithm (ENHANCED);
326 if (!lex_match (lexer, '/'))
330 if (lex_token (lexer) != '.')
332 lex_error (lexer, _("expecting end of command"));
343 static void one_sample_insert_variables (const struct npar_test *test,
346 static void two_sample_insert_variables (const struct npar_test *test,
349 static void n_sample_insert_variables (const struct npar_test *test,
353 npar_execute (struct casereader *input,
354 const struct npar_specs *specs,
355 const struct dataset *ds)
358 struct descriptives *summary_descriptives = NULL;
360 for ( t = 0 ; t < specs->n_tests; ++t )
362 const struct npar_test *test = specs->test[t];
363 if ( NULL == test->execute )
365 msg (SW, _("NPAR subcommand not currently implemented."));
368 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
371 if ( specs->descriptives )
373 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
376 npar_summary_calc_descriptives (summary_descriptives,
377 casereader_clone (input),
379 specs->vv, specs->n_vars,
383 if ( (specs->descriptives || specs->quartiles)
384 && !taint_has_tainted_successor (casereader_get_taint (input)) )
385 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
387 free (summary_descriptives);
388 casereader_destroy (input);
392 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
394 struct cmd_npar_tests cmd;
397 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
398 struct casegrouper *grouper;
399 struct casereader *input, *group;
400 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
403 npar_specs.pool = pool_create ();
404 npar_specs.filter = MV_ANY;
405 npar_specs.n_vars = -1;
406 npar_specs.vv = NULL;
408 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
410 pool_destroy (npar_specs.pool);
414 for (i = 0; i < npar_specs.n_tests; ++i )
416 const struct npar_test *test = npar_specs.test[i];
417 test->insert_variables (test, &var_map);
421 struct hmapx_node *node;
422 struct variable *var;
423 npar_specs.n_vars = 0;
425 HMAPX_FOR_EACH (var, node, &var_map)
427 npar_specs.n_vars ++;
428 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
429 npar_specs.vv[npar_specs.n_vars - 1] = var;
433 qsort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
434 compare_var_ptrs_by_name);
436 if ( cmd.statistics )
440 for ( i = 0 ; i < NPAR_ST_count; ++i )
442 if ( cmd.a_statistics[i] )
446 case NPAR_ST_DESCRIPTIVES:
447 npar_specs.descriptives = true;
449 case NPAR_ST_QUARTILES:
450 npar_specs.quartiles = true;
453 npar_specs.quartiles = true;
454 npar_specs.descriptives = true;
463 input = proc_open (ds);
464 if ( cmd.miss == MISS_LISTWISE )
466 input = casereader_create_filter_missing (input,
474 grouper = casegrouper_create_splits (input, dataset_dict (ds));
475 while (casegrouper_get_next_group (grouper, &group))
476 npar_execute (group, &npar_specs, ds);
477 ok = casegrouper_destroy (grouper);
478 ok = proc_commit (ds) && ok;
480 pool_destroy (npar_specs.pool);
481 hmapx_destroy (&var_map);
483 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
487 npar_runs (struct lexer *lexer, struct dataset *ds,
488 struct npar_specs *specs)
490 struct runs_test *rt = pool_alloc (specs->pool, sizeof (*rt));
491 struct one_sample_test *tp = &rt->parent;
492 struct npar_test *nt = &tp->parent;
494 nt->execute = runs_execute;
495 nt->insert_variables = one_sample_insert_variables;
497 if ( lex_force_match (lexer, '(') )
499 if ( lex_match_id (lexer, "MEAN"))
501 rt->cp_mode = CP_MEAN;
503 else if (lex_match_id (lexer, "MEDIAN"))
505 rt->cp_mode = CP_MEDIAN;
507 else if (lex_match_id (lexer, "MODE"))
509 rt->cp_mode = CP_MODE;
511 else if (lex_is_number (lexer))
513 rt->cutpoint = lex_number (lexer);
514 rt->cp_mode = CP_CUSTOM;
519 lex_error (lexer, _("Expecting MEAN, MEDIAN, MODE or number"));
523 lex_force_match (lexer, ')');
524 lex_force_match (lexer, '=');
525 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
526 &tp->vars, &tp->n_vars,
527 PV_NO_SCRATCH | PV_NO_DUPLICATE | PV_NUMERIC))
534 specs->test = pool_realloc (specs->pool,
536 sizeof (*specs->test) * specs->n_tests);
538 specs->test[specs->n_tests - 1] = nt;
545 npar_chisquare (struct lexer *lexer, struct dataset *ds,
546 struct npar_specs *specs)
548 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
549 struct one_sample_test *tp = &cstp->parent;
550 struct npar_test *nt = &tp->parent;
553 nt->execute = chisquare_execute;
554 nt->insert_variables = one_sample_insert_variables;
556 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
557 &tp->vars, &tp->n_vars,
558 PV_NO_SCRATCH | PV_NO_DUPLICATE))
563 cstp->ranged = false;
565 if ( lex_match (lexer, '('))
568 if ( ! lex_force_num (lexer)) return 0;
569 cstp->lo = lex_integer (lexer);
571 lex_force_match (lexer, ',');
572 if (! lex_force_num (lexer) ) return 0;
573 cstp->hi = lex_integer (lexer);
574 if ( cstp->lo >= cstp->hi )
577 _("The specified value of HI (%d) is "
578 "lower than the specified value of LO (%d)"),
583 if (! lex_force_match (lexer, ')')) return 0;
586 cstp->n_expected = 0;
587 cstp->expected = NULL;
588 if ( lex_match (lexer, '/') )
590 if ( lex_match_id (lexer, "EXPECTED") )
592 lex_force_match (lexer, '=');
593 if ( ! lex_match_id (lexer, "EQUAL") )
597 while ( lex_is_number (lexer) )
601 f = lex_number (lexer);
603 if ( lex_match (lexer, '*'))
606 f = lex_number (lexer);
609 lex_match (lexer, ',');
611 cstp->n_expected += n;
612 cstp->expected = pool_realloc (specs->pool,
616 for ( i = cstp->n_expected - n ;
617 i < cstp->n_expected;
619 cstp->expected[i] = f;
625 lex_put_back (lexer, '/');
628 if ( cstp->ranged && cstp->n_expected > 0 &&
629 cstp->n_expected != cstp->hi - cstp->lo + 1 )
632 _("%d expected values were given, but the specified "
633 "range (%d-%d) requires exactly %d values."),
634 cstp->n_expected, cstp->lo, cstp->hi,
635 cstp->hi - cstp->lo +1);
640 specs->test = pool_realloc (specs->pool,
642 sizeof (*specs->test) * specs->n_tests);
644 specs->test[specs->n_tests - 1] = nt;
651 npar_binomial (struct lexer *lexer, struct dataset *ds,
652 struct npar_specs *specs)
654 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
655 struct one_sample_test *tp = &btp->parent;
656 struct npar_test *nt = &tp->parent;
658 nt->execute = binomial_execute;
659 nt->insert_variables = one_sample_insert_variables;
661 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
665 if ( lex_match (lexer, '(') )
667 if ( lex_force_num (lexer) )
669 btp->p = lex_number (lexer);
671 lex_force_match (lexer, ')');
677 /* Kludge: q2c swallows the '=' so put it back here */
678 lex_put_back (lexer, '=');
680 if (lex_match (lexer, '=') )
682 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
683 &tp->vars, &tp->n_vars,
684 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
686 if (lex_match (lexer, '('))
688 lex_force_num (lexer);
689 btp->category1 = lex_number (lexer);
691 if ( lex_match (lexer, ','))
693 if ( ! lex_force_num (lexer) ) return 2;
694 btp->category2 = lex_number (lexer);
699 btp->cutpoint = btp->category1;
702 lex_force_match (lexer, ')');
711 specs->test = pool_realloc (specs->pool,
713 sizeof (*specs->test) * specs->n_tests);
715 specs->test[specs->n_tests - 1] = nt;
722 parse_two_sample_related_test (struct lexer *lexer,
723 const struct dictionary *dict,
724 struct two_sample_test *test_parameters,
730 parse_two_sample_related_test (struct lexer *lexer,
731 const struct dictionary *dict,
732 struct two_sample_test *test_parameters,
739 const struct variable **vlist1;
742 const struct variable **vlist2;
745 test_parameters->parent.insert_variables = two_sample_insert_variables;
747 if (!parse_variables_const_pool (lexer, pool,
750 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
753 if ( lex_match (lexer, T_WITH))
756 if ( !parse_variables_const_pool (lexer, pool, dict,
758 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
761 paired = (lex_match (lexer, '(') &&
762 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
770 if ( n_vlist1 != n_vlist2)
771 msg (SE, _("PAIRED was specified but the number of variables "
772 "preceding WITH (%zu) did not match the number "
773 "following (%zu)."), n_vlist1, n_vlist2);
775 test_parameters->n_pairs = n_vlist1 ;
779 test_parameters->n_pairs = n_vlist1 * n_vlist2;
784 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
787 test_parameters->pairs =
788 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
795 assert (n_vlist1 == n_vlist2);
796 for ( i = 0 ; i < n_vlist1; ++i )
798 test_parameters->pairs[n][1] = vlist1[i];
799 test_parameters->pairs[n][0] = vlist2[i];
806 for ( i = 0 ; i < n_vlist1; ++i )
808 for ( j = 0 ; j < n_vlist2; ++j )
810 test_parameters->pairs[n][1] = vlist1[i];
811 test_parameters->pairs[n][0] = vlist2[j];
820 for ( i = 0 ; i < n_vlist1 - 1; ++i )
822 for ( j = i + 1 ; j < n_vlist1; ++j )
824 assert ( n < test_parameters->n_pairs);
825 test_parameters->pairs[n][1] = vlist1[i];
826 test_parameters->pairs[n][0] = vlist1[j];
832 assert ( n == test_parameters->n_pairs);
839 parse_n_sample_related_test (struct lexer *lexer,
840 const struct dictionary *dict,
841 struct n_sample_test *nst,
845 if (!parse_variables_const_pool (lexer, pool,
847 &nst->vars, &nst->n_vars,
848 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
851 if ( ! lex_force_match (lexer, T_BY))
854 nst->indep_var = parse_variable_const (lexer, dict);
856 if ( ! lex_force_match (lexer, '('))
859 value_init (&nst->val1, var_get_width (nst->indep_var));
860 if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
862 value_destroy (&nst->val1, var_get_width (nst->indep_var));
866 if ( ! lex_force_match (lexer, ','))
869 value_init (&nst->val2, var_get_width (nst->indep_var));
870 if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
872 value_destroy (&nst->val2, var_get_width (nst->indep_var));
876 if ( ! lex_force_match (lexer, ')'))
883 npar_wilcoxon (struct lexer *lexer,
885 struct npar_specs *specs )
889 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
890 struct npar_test *nt = &tp->parent;
891 nt->execute = wilcoxon_execute;
893 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
898 specs->test = pool_realloc (specs->pool,
900 sizeof (*specs->test) * specs->n_tests);
901 specs->test[specs->n_tests - 1] = nt;
907 npar_sign (struct lexer *lexer, struct dataset *ds,
908 struct npar_specs *specs)
910 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
911 struct npar_test *nt = &tp->parent;
913 nt->execute = sign_execute;
915 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
920 specs->test = pool_realloc (specs->pool,
922 sizeof (*specs->test) * specs->n_tests);
923 specs->test[specs->n_tests - 1] = nt;
929 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
930 struct npar_specs *specs)
932 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
933 struct npar_test *nt = &tp->parent;
935 nt->insert_variables = n_sample_insert_variables;
937 nt->execute = kruskal_wallis_execute;
939 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
944 specs->test = pool_realloc (specs->pool,
946 sizeof (*specs->test) * specs->n_tests);
947 specs->test[specs->n_tests - 1] = nt;
953 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
955 size_t hash = hash_pointer (var, 0);
956 struct hmapx_node *node;
957 const struct variable *v = NULL;
959 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
965 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
968 /* Insert the variables for TEST into VAR_MAP */
970 one_sample_insert_variables (const struct npar_test *test,
971 struct hmapx *var_map)
974 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
976 for ( i = 0 ; i < ost->n_vars ; ++i )
977 insert_variable_into_map (var_map, ost->vars[i]);
982 two_sample_insert_variables (const struct npar_test *test,
983 struct hmapx *var_map)
986 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
988 for ( i = 0 ; i < tst->n_pairs ; ++i )
990 variable_pair *pair = &tst->pairs[i];
992 insert_variable_into_map (var_map, (*pair)[0]);
993 insert_variable_into_map (var_map, (*pair)[1]);
998 n_sample_insert_variables (const struct npar_test *test,
999 struct hmapx *var_map)
1002 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
1004 for ( i = 0 ; i < tst->n_vars ; ++i )
1005 insert_variable_into_map (var_map, tst->vars[i]);
1007 insert_variable_into_map (var_map, tst->indep_var);
1012 npar_method (struct lexer *lexer, struct npar_specs *specs)
1014 if ( lex_match_id (lexer, "EXACT") )
1016 specs->exact = true;
1018 if (lex_match_id (lexer, "TIMER"))
1022 if ( lex_match (lexer, '('))
1024 if ( lex_force_num (lexer) )
1026 specs->timer = lex_number (lexer);
1029 lex_force_match (lexer, ')');