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/kruskal-wallis.h>
49 #include <language/stats/wilcoxon.h>
50 #include <language/stats/sign.h>
51 #include <math/moments.h>
54 #define _(msgid) gettext (msgid)
56 /* Settings for subcommand specifiers. */
63 /* Array indices for STATISTICS subcommand. */
66 NPAR_ST_DESCRIPTIVES = 0,
67 NPAR_ST_QUARTILES = 1,
72 /* NPAR TESTS structure. */
75 /* Count variables indicating how many
76 of the subcommands have been given. */
86 /* How missing values should be treated */
89 /* Which statistics have been requested */
90 int a_statistics[NPAR_ST_count];
97 struct npar_test **test;
100 const struct variable **vv; /* Compendium of all variables
101 (those mentioned on ANY subcommand */
102 int n_vars; /* Number of variables in vv */
104 enum mv_class filter; /* Missing values to filter. */
106 bool descriptives; /* Descriptive statistics should be calculated */
107 bool quartiles; /* Quartiles should be calculated */
109 bool exact; /* Whether exact calculations have been requested */
110 double timer; /* Maximum time (in minutes) to wait for exact calculations */
114 /* Prototype for custom subcommands of NPAR TESTS. */
115 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
116 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
117 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
118 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
119 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
120 static int npar_method (struct lexer *, struct npar_specs *);
122 /* Command parsing functions. */
123 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
124 struct npar_specs *npar_specs );
127 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
128 struct npar_specs *nps)
135 npt->miss = MISS_ANALYSIS;
138 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
141 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
143 lex_match (lexer, '=');
145 switch (npar_chisquare (lexer, ds, nps))
152 lex_error (lexer, NULL);
158 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
160 lex_match (lexer, '=');
162 switch (npar_binomial (lexer, ds, nps))
169 lex_error (lexer, NULL);
175 else if (lex_match_hyphenated_word (lexer, "K-W") ||
176 lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
178 lex_match (lexer, '=');
179 npt->kruskal_wallis++;
180 switch (npar_kruskal_wallis (lexer, ds, nps))
187 lex_error (lexer, NULL);
193 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
195 lex_match (lexer, '=');
197 switch (npar_wilcoxon (lexer, ds, nps))
204 lex_error (lexer, NULL);
210 else if (lex_match_hyphenated_word (lexer, "SIGN"))
212 lex_match (lexer, '=');
214 switch (npar_sign (lexer, ds, nps))
221 lex_error (lexer, NULL);
227 else if (lex_match_hyphenated_word (lexer, "MISSING"))
229 lex_match (lexer, '=');
231 if (npt->missing > 1)
233 msg (SE, _("The %s subcommand may be given only once."), "MISSING");
236 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
238 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
239 npt->miss = MISS_ANALYSIS;
240 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
241 npt->miss = MISS_LISTWISE;
242 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
243 nps->filter = MV_SYSTEM;
244 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
245 nps->filter = MV_ANY;
248 lex_error (lexer, NULL);
251 lex_match (lexer, ',');
254 else if (lex_match_hyphenated_word (lexer, "METHOD"))
256 lex_match (lexer, '=');
260 msg (SE, _("The %s subcommand may be given only once."), "METHOD");
263 switch (npar_method (lexer, nps))
270 lex_error (lexer, NULL);
276 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
278 lex_match (lexer, '=');
280 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
282 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
283 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
284 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
285 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
286 else if (lex_match (lexer, T_ALL))
287 npt->a_statistics[NPAR_ST_ALL] = 1;
290 lex_error (lexer, NULL);
293 lex_match (lexer, ',');
296 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
298 lex_match (lexer, '=');
299 if (lex_match_id (lexer, "COMPATIBLE"))
300 settings_set_cmd_algorithm (COMPATIBLE);
301 else if (lex_match_id (lexer, "ENHANCED"))
302 settings_set_cmd_algorithm (ENHANCED);
304 if (!lex_match (lexer, '/'))
308 if (lex_token (lexer) != '.')
310 lex_error (lexer, _("expecting end of command"));
321 static void one_sample_insert_variables (const struct npar_test *test,
324 static void two_sample_insert_variables (const struct npar_test *test,
327 static void n_sample_insert_variables (const struct npar_test *test,
331 npar_execute (struct casereader *input,
332 const struct npar_specs *specs,
333 const struct dataset *ds)
336 struct descriptives *summary_descriptives = NULL;
338 for ( t = 0 ; t < specs->n_tests; ++t )
340 const struct npar_test *test = specs->test[t];
341 if ( NULL == test->execute )
343 msg (SW, _("NPAR subcommand not currently implemented."));
346 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
349 if ( specs->descriptives )
351 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
354 npar_summary_calc_descriptives (summary_descriptives,
355 casereader_clone (input),
357 specs->vv, specs->n_vars,
361 if ( (specs->descriptives || specs->quartiles)
362 && !taint_has_tainted_successor (casereader_get_taint (input)) )
363 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
365 free (summary_descriptives);
366 casereader_destroy (input);
370 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
372 struct cmd_npar_tests cmd;
375 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
376 struct casegrouper *grouper;
377 struct casereader *input, *group;
378 struct hmapx var_map = HMAPX_INITIALIZER (var_map);
381 npar_specs.pool = pool_create ();
382 npar_specs.filter = MV_ANY;
383 npar_specs.n_vars = -1;
384 npar_specs.vv = NULL;
386 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
388 pool_destroy (npar_specs.pool);
392 for (i = 0; i < npar_specs.n_tests; ++i )
394 const struct npar_test *test = npar_specs.test[i];
395 test->insert_variables (test, &var_map);
399 struct hmapx_node *node;
400 struct variable *var;
401 npar_specs.n_vars = 0;
403 HMAPX_FOR_EACH (var, node, &var_map)
405 npar_specs.n_vars ++;
406 npar_specs.vv = pool_nrealloc (npar_specs.pool, npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv));
407 npar_specs.vv[npar_specs.n_vars - 1] = var;
411 qsort (npar_specs.vv, npar_specs.n_vars, sizeof (*npar_specs.vv),
412 compare_var_ptrs_by_name);
414 if ( cmd.statistics )
418 for ( i = 0 ; i < NPAR_ST_count; ++i )
420 if ( cmd.a_statistics[i] )
424 case NPAR_ST_DESCRIPTIVES:
425 npar_specs.descriptives = true;
427 case NPAR_ST_QUARTILES:
428 npar_specs.quartiles = true;
431 npar_specs.quartiles = true;
432 npar_specs.descriptives = true;
441 input = proc_open (ds);
442 if ( cmd.miss == MISS_LISTWISE )
444 input = casereader_create_filter_missing (input,
452 grouper = casegrouper_create_splits (input, dataset_dict (ds));
453 while (casegrouper_get_next_group (grouper, &group))
454 npar_execute (group, &npar_specs, ds);
455 ok = casegrouper_destroy (grouper);
456 ok = proc_commit (ds) && ok;
458 pool_destroy (npar_specs.pool);
459 hmapx_destroy (&var_map);
461 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
465 npar_chisquare (struct lexer *lexer, struct dataset *ds,
466 struct npar_specs *specs)
468 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
469 struct one_sample_test *tp = &cstp->parent;
470 struct npar_test *nt = &tp->parent;
472 nt->execute = chisquare_execute;
473 nt->insert_variables = one_sample_insert_variables;
475 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
476 &tp->vars, &tp->n_vars,
477 PV_NO_SCRATCH | PV_NO_DUPLICATE))
482 cstp->ranged = false;
484 if ( lex_match (lexer, '('))
487 if ( ! lex_force_num (lexer)) return 0;
488 cstp->lo = lex_integer (lexer);
490 lex_force_match (lexer, ',');
491 if (! lex_force_num (lexer) ) return 0;
492 cstp->hi = lex_integer (lexer);
493 if ( cstp->lo >= cstp->hi )
496 _("The specified value of HI (%d) is "
497 "lower than the specified value of LO (%d)"),
502 if (! lex_force_match (lexer, ')')) return 0;
505 cstp->n_expected = 0;
506 cstp->expected = NULL;
507 if ( lex_match (lexer, '/') )
509 if ( lex_match_id (lexer, "EXPECTED") )
511 lex_force_match (lexer, '=');
512 if ( ! lex_match_id (lexer, "EQUAL") )
516 while ( lex_is_number (lexer) )
520 f = lex_number (lexer);
522 if ( lex_match (lexer, '*'))
525 f = lex_number (lexer);
528 lex_match (lexer, ',');
530 cstp->n_expected += n;
531 cstp->expected = pool_realloc (specs->pool,
535 for ( i = cstp->n_expected - n ;
536 i < cstp->n_expected;
538 cstp->expected[i] = f;
544 lex_put_back (lexer, '/');
547 if ( cstp->ranged && cstp->n_expected > 0 &&
548 cstp->n_expected != cstp->hi - cstp->lo + 1 )
551 _("%d expected values were given, but the specified "
552 "range (%d-%d) requires exactly %d values."),
553 cstp->n_expected, cstp->lo, cstp->hi,
554 cstp->hi - cstp->lo +1);
559 specs->test = pool_realloc (specs->pool,
561 sizeof (*specs->test) * specs->n_tests);
563 specs->test[specs->n_tests - 1] = nt;
570 npar_binomial (struct lexer *lexer, struct dataset *ds,
571 struct npar_specs *specs)
573 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
574 struct one_sample_test *tp = &btp->parent;
575 struct npar_test *nt = &tp->parent;
577 nt->execute = binomial_execute;
578 nt->insert_variables = one_sample_insert_variables;
580 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
584 if ( lex_match (lexer, '(') )
586 if ( lex_force_num (lexer) )
588 btp->p = lex_number (lexer);
590 lex_force_match (lexer, ')');
596 /* Kludge: q2c swallows the '=' so put it back here */
597 lex_put_back (lexer, '=');
599 if (lex_match (lexer, '=') )
601 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
602 &tp->vars, &tp->n_vars,
603 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
605 if (lex_match (lexer, '('))
607 lex_force_num (lexer);
608 btp->category1 = lex_number (lexer);
610 if ( lex_match (lexer, ','))
612 if ( ! lex_force_num (lexer) ) return 2;
613 btp->category2 = lex_number (lexer);
618 btp->cutpoint = btp->category1;
621 lex_force_match (lexer, ')');
630 specs->test = pool_realloc (specs->pool,
632 sizeof (*specs->test) * specs->n_tests);
634 specs->test[specs->n_tests - 1] = nt;
641 parse_two_sample_related_test (struct lexer *lexer,
642 const struct dictionary *dict,
643 struct two_sample_test *test_parameters,
649 parse_two_sample_related_test (struct lexer *lexer,
650 const struct dictionary *dict,
651 struct two_sample_test *test_parameters,
658 const struct variable **vlist1;
661 const struct variable **vlist2;
664 test_parameters->parent.insert_variables = two_sample_insert_variables;
666 if (!parse_variables_const_pool (lexer, pool,
669 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
672 if ( lex_match (lexer, T_WITH))
675 if ( !parse_variables_const_pool (lexer, pool, dict,
677 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
680 paired = (lex_match (lexer, '(') &&
681 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
689 if ( n_vlist1 != n_vlist2)
690 msg (SE, _("PAIRED was specified but the number of variables "
691 "preceding WITH (%zu) did not match the number "
692 "following (%zu)."), n_vlist1, n_vlist2);
694 test_parameters->n_pairs = n_vlist1 ;
698 test_parameters->n_pairs = n_vlist1 * n_vlist2;
703 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
706 test_parameters->pairs =
707 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
714 assert (n_vlist1 == n_vlist2);
715 for ( i = 0 ; i < n_vlist1; ++i )
717 test_parameters->pairs[n][1] = vlist1[i];
718 test_parameters->pairs[n][0] = vlist2[i];
725 for ( i = 0 ; i < n_vlist1; ++i )
727 for ( j = 0 ; j < n_vlist2; ++j )
729 test_parameters->pairs[n][1] = vlist1[i];
730 test_parameters->pairs[n][0] = vlist2[j];
739 for ( i = 0 ; i < n_vlist1 - 1; ++i )
741 for ( j = i + 1 ; j < n_vlist1; ++j )
743 assert ( n < test_parameters->n_pairs);
744 test_parameters->pairs[n][1] = vlist1[i];
745 test_parameters->pairs[n][0] = vlist1[j];
751 assert ( n == test_parameters->n_pairs);
758 parse_n_sample_related_test (struct lexer *lexer,
759 const struct dictionary *dict,
760 struct n_sample_test *nst,
764 if (!parse_variables_const_pool (lexer, pool,
766 &nst->vars, &nst->n_vars,
767 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
770 if ( ! lex_force_match (lexer, T_BY))
773 nst->indep_var = parse_variable_const (lexer, dict);
775 if ( ! lex_force_match (lexer, '('))
778 value_init (&nst->val1, var_get_width (nst->indep_var));
779 if ( ! parse_value (lexer, &nst->val1, var_get_width (nst->indep_var)))
781 value_destroy (&nst->val1, var_get_width (nst->indep_var));
785 if ( ! lex_force_match (lexer, ','))
788 value_init (&nst->val2, var_get_width (nst->indep_var));
789 if ( ! parse_value (lexer, &nst->val2, var_get_width (nst->indep_var)))
791 value_destroy (&nst->val2, var_get_width (nst->indep_var));
795 if ( ! lex_force_match (lexer, ')'))
802 npar_wilcoxon (struct lexer *lexer,
804 struct npar_specs *specs )
808 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
809 struct npar_test *nt = &tp->parent;
810 nt->execute = wilcoxon_execute;
812 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
817 specs->test = pool_realloc (specs->pool,
819 sizeof (*specs->test) * specs->n_tests);
820 specs->test[specs->n_tests - 1] = nt;
826 npar_sign (struct lexer *lexer, struct dataset *ds,
827 struct npar_specs *specs)
829 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
830 struct npar_test *nt = &tp->parent;
832 nt->execute = sign_execute;
834 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
839 specs->test = pool_realloc (specs->pool,
841 sizeof (*specs->test) * specs->n_tests);
842 specs->test[specs->n_tests - 1] = nt;
848 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
849 struct npar_specs *specs)
851 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
852 struct npar_test *nt = &tp->parent;
854 nt->insert_variables = n_sample_insert_variables;
856 nt->execute = kruskal_wallis_execute;
858 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
863 specs->test = pool_realloc (specs->pool,
865 sizeof (*specs->test) * specs->n_tests);
866 specs->test[specs->n_tests - 1] = nt;
872 insert_variable_into_map (struct hmapx *var_map, const struct variable *var)
874 size_t hash = hash_pointer (var, 0);
875 struct hmapx_node *node;
876 const struct variable *v = NULL;
878 HMAPX_FOR_EACH_WITH_HASH (v, node, hash, var_map)
884 hmapx_insert (var_map, CONST_CAST (struct variable *, var), hash);
887 /* Insert the variables for TEST into VAR_MAP */
889 one_sample_insert_variables (const struct npar_test *test,
890 struct hmapx *var_map)
893 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
895 for ( i = 0 ; i < ost->n_vars ; ++i )
896 insert_variable_into_map (var_map, ost->vars[i]);
901 two_sample_insert_variables (const struct npar_test *test,
902 struct hmapx *var_map)
905 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
907 for ( i = 0 ; i < tst->n_pairs ; ++i )
909 variable_pair *pair = &tst->pairs[i];
911 insert_variable_into_map (var_map, (*pair)[0]);
912 insert_variable_into_map (var_map, (*pair)[1]);
917 n_sample_insert_variables (const struct npar_test *test,
918 struct hmapx *var_map)
921 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
923 for ( i = 0 ; i < tst->n_vars ; ++i )
924 insert_variable_into_map (var_map, tst->vars[i]);
926 insert_variable_into_map (var_map, tst->indep_var);
931 npar_method (struct lexer *lexer, struct npar_specs *specs)
933 if ( lex_match_id (lexer, "EXACT") )
937 if (lex_match_id (lexer, "TIMER"))
941 if ( lex_match (lexer, '('))
943 if ( lex_force_num (lexer) )
945 specs->timer = lex_number (lexer);
948 lex_force_match (lexer, ')');