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"
26 #include <data/case.h>
27 #include <data/casegrouper.h>
28 #include <data/casereader.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <language/lexer/value-parser.h>
37 #include <language/stats/binomial.h>
38 #include <language/stats/chisquare.h>
39 #include <language/stats/wilcoxon.h>
40 #include <language/stats/sign.h>
41 #include <libpspp/assertion.h>
42 #include <libpspp/cast.h>
43 #include <libpspp/hash.h>
44 #include <libpspp/message.h>
45 #include <libpspp/pool.h>
46 #include <libpspp/str.h>
47 #include <libpspp/taint.h>
48 #include <math/moments.h>
51 #define _(msgid) gettext (msgid)
53 /* Settings for subcommand specifiers. */
60 /* Array indices for STATISTICS subcommand. */
63 NPAR_ST_DESCRIPTIVES = 0,
64 NPAR_ST_QUARTILES = 1,
69 /* NPAR TESTS structure. */
72 /* Count variables indicating how many
73 of the subcommands have been given. */
83 /* How missing values should be treated */
86 /* Which statistics have been requested */
87 int a_statistics[NPAR_ST_count];
94 struct npar_test **test;
97 const struct variable **vv; /* Compendium of all variables
98 (those mentioned on ANY subcommand */
99 int n_vars; /* Number of variables in vv */
101 enum mv_class filter; /* Missing values to filter. */
103 bool descriptives; /* Descriptive statistics should be calculated */
104 bool quartiles; /* Quartiles should be calculated */
106 bool exact; /* Whether exact calculations have been requested */
107 double timer; /* Maximum time (in minutes) to wait for exact calculations */
111 /* Prototype for custom subcommands of NPAR TESTS. */
112 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
113 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
114 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
115 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
116 static int npar_kruskal_wallis (struct lexer *, struct dataset *, struct npar_specs *);
117 static int npar_method (struct lexer *, struct npar_specs *);
119 /* Command parsing functions. */
120 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
121 struct npar_specs *npar_specs );
124 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
125 struct npar_specs *nps)
132 npt->miss = MISS_ANALYSIS;
135 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
138 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
140 lex_match (lexer, '=');
142 switch (npar_chisquare (lexer, ds, nps))
149 lex_error (lexer, NULL);
155 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
157 lex_match (lexer, '=');
159 switch (npar_binomial (lexer, ds, nps))
166 lex_error (lexer, NULL);
172 else if (lex_match_hyphenated_word (lexer, "K-S") ||
173 lex_match_hyphenated_word (lexer, "KRUSKAL-WALLIS"))
175 lex_match (lexer, '=');
176 npt->kruskal_wallis++;
177 switch (npar_kruskal_wallis (lexer, ds, nps))
184 lex_error (lexer, NULL);
190 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
192 lex_match (lexer, '=');
194 switch (npar_wilcoxon (lexer, ds, nps))
201 lex_error (lexer, NULL);
207 else if (lex_match_hyphenated_word (lexer, "SIGN"))
209 lex_match (lexer, '=');
211 switch (npar_sign (lexer, ds, nps))
218 lex_error (lexer, NULL);
224 else if (lex_match_hyphenated_word (lexer, "MISSING"))
226 lex_match (lexer, '=');
228 if (npt->missing > 1)
230 msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
233 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
235 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
236 npt->miss = MISS_ANALYSIS;
237 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
238 npt->miss = MISS_LISTWISE;
239 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
240 nps->filter = MV_SYSTEM;
241 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
242 nps->filter = MV_ANY;
245 lex_error (lexer, NULL);
248 lex_match (lexer, ',');
251 else if (lex_match_hyphenated_word (lexer, "METHOD"))
253 lex_match (lexer, '=');
257 msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
260 switch (npar_method (lexer, nps))
267 lex_error (lexer, NULL);
273 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
275 lex_match (lexer, '=');
277 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
279 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
280 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
281 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
282 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
283 else if (lex_match (lexer, T_ALL))
284 npt->a_statistics[NPAR_ST_ALL] = 1;
287 lex_error (lexer, NULL);
290 lex_match (lexer, ',');
293 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
295 lex_match (lexer, '=');
296 if (lex_match_id (lexer, "COMPATIBLE"))
297 settings_set_cmd_algorithm (COMPATIBLE);
298 else if (lex_match_id (lexer, "ENHANCED"))
299 settings_set_cmd_algorithm (ENHANCED);
301 if (!lex_match (lexer, '/'))
305 if (lex_token (lexer) != '.')
307 lex_error (lexer, _ ("expecting end of command"));
320 static void one_sample_insert_variables (const struct npar_test *test,
321 struct const_hsh_table *variables);
323 static void two_sample_insert_variables (const struct npar_test *test,
324 struct const_hsh_table *variables);
327 static void n_sample_insert_variables (const struct npar_test *test,
328 struct const_hsh_table *variables);
333 npar_execute (struct casereader *input,
334 const struct npar_specs *specs,
335 const struct dataset *ds)
338 struct descriptives *summary_descriptives = NULL;
340 for ( t = 0 ; t < specs->n_tests; ++t )
342 const struct npar_test *test = specs->test[t];
343 if ( NULL == test->execute )
345 msg (SW, _ ("NPAR subcommand not currently implemented."));
348 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
351 if ( specs->descriptives )
353 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
356 npar_summary_calc_descriptives (summary_descriptives,
357 casereader_clone (input),
359 specs->vv, specs->n_vars,
363 if ( (specs->descriptives || specs->quartiles)
364 && !taint_has_tainted_successor (casereader_get_taint (input)) )
365 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
367 free (summary_descriptives);
368 casereader_destroy (input);
373 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
375 struct cmd_npar_tests cmd;
378 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
379 struct const_hsh_table *var_hash;
380 struct casegrouper *grouper;
381 struct casereader *input, *group;
383 npar_specs.pool = pool_create ();
384 npar_specs.filter = MV_ANY;
386 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
387 compare_vars_by_name, hash_var_by_name,
390 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
392 pool_destroy (npar_specs.pool);
396 for (i = 0; i < npar_specs.n_tests; ++i )
398 const struct npar_test *test = npar_specs.test[i];
399 test->insert_variables (test, var_hash);
402 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
403 npar_specs.n_vars = const_hsh_count (var_hash);
405 if ( cmd.statistics )
409 for ( i = 0 ; i < NPAR_ST_count; ++i )
411 if ( cmd.a_statistics[i] )
415 case NPAR_ST_DESCRIPTIVES:
416 npar_specs.descriptives = true;
418 case NPAR_ST_QUARTILES:
419 npar_specs.quartiles = true;
422 npar_specs.quartiles = true;
423 npar_specs.descriptives = true;
432 input = proc_open (ds);
433 if ( cmd.miss == MISS_LISTWISE )
435 input = casereader_create_filter_missing (input,
443 grouper = casegrouper_create_splits (input, dataset_dict (ds));
444 while (casegrouper_get_next_group (grouper, &group))
445 npar_execute (group, &npar_specs, ds);
446 ok = casegrouper_destroy (grouper);
447 ok = proc_commit (ds) && ok;
449 const_hsh_destroy (var_hash);
451 pool_destroy (npar_specs.pool);
453 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
457 npar_chisquare (struct lexer *lexer, struct dataset *ds,
458 struct npar_specs *specs)
460 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
461 struct one_sample_test *tp = &cstp->parent;
462 struct npar_test *nt = &tp->parent;
464 nt->execute = chisquare_execute;
465 nt->insert_variables = one_sample_insert_variables;
467 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
468 &tp->vars, &tp->n_vars,
469 PV_NO_SCRATCH | PV_NO_DUPLICATE))
474 cstp->ranged = false;
476 if ( lex_match (lexer, '('))
479 if ( ! lex_force_num (lexer)) return 0;
480 cstp->lo = lex_integer (lexer);
482 lex_force_match (lexer, ',');
483 if (! lex_force_num (lexer) ) return 0;
484 cstp->hi = lex_integer (lexer);
485 if ( cstp->lo >= cstp->hi )
488 _ ("The specified value of HI (%d) is "
489 "lower than the specified value of LO (%d)"),
494 if (! lex_force_match (lexer, ')')) return 0;
497 cstp->n_expected = 0;
498 cstp->expected = NULL;
499 if ( lex_match (lexer, '/') )
501 if ( lex_match_id (lexer, "EXPECTED") )
503 lex_force_match (lexer, '=');
504 if ( ! lex_match_id (lexer, "EQUAL") )
508 while ( lex_is_number (lexer) )
512 f = lex_number (lexer);
514 if ( lex_match (lexer, '*'))
517 f = lex_number (lexer);
520 lex_match (lexer, ',');
522 cstp->n_expected += n;
523 cstp->expected = pool_realloc (specs->pool,
527 for ( i = cstp->n_expected - n ;
528 i < cstp->n_expected;
530 cstp->expected[i] = f;
536 lex_put_back (lexer, '/');
539 if ( cstp->ranged && cstp->n_expected > 0 &&
540 cstp->n_expected != cstp->hi - cstp->lo + 1 )
543 _ ("%d expected values were given, but the specified "
544 "range (%d-%d) requires exactly %d values."),
545 cstp->n_expected, cstp->lo, cstp->hi,
546 cstp->hi - cstp->lo +1);
551 specs->test = pool_realloc (specs->pool,
553 sizeof (*specs->test) * specs->n_tests);
555 specs->test[specs->n_tests - 1] = nt;
562 npar_binomial (struct lexer *lexer, struct dataset *ds,
563 struct npar_specs *specs)
565 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
566 struct one_sample_test *tp = &btp->parent;
567 struct npar_test *nt = &tp->parent;
569 nt->execute = binomial_execute;
570 nt->insert_variables = one_sample_insert_variables;
572 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
576 if ( lex_match (lexer, '(') )
578 if ( lex_force_num (lexer) )
580 btp->p = lex_number (lexer);
582 lex_force_match (lexer, ')');
588 /* Kludge: q2c swallows the '=' so put it back here */
589 lex_put_back (lexer, '=');
591 if (lex_match (lexer, '=') )
593 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
594 &tp->vars, &tp->n_vars,
595 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
597 if (lex_match (lexer, '('))
599 lex_force_num (lexer);
600 btp->category1 = lex_number (lexer);
602 if ( lex_match (lexer, ','))
604 if ( ! lex_force_num (lexer) ) return 2;
605 btp->category2 = lex_number (lexer);
610 btp->cutpoint = btp->category1;
613 lex_force_match (lexer, ')');
622 specs->test = pool_realloc (specs->pool,
624 sizeof (*specs->test) * specs->n_tests);
626 specs->test[specs->n_tests - 1] = nt;
633 parse_two_sample_related_test (struct lexer *lexer,
634 const struct dictionary *dict,
635 struct two_sample_test *test_parameters,
641 parse_two_sample_related_test (struct lexer *lexer,
642 const struct dictionary *dict,
643 struct two_sample_test *test_parameters,
650 const struct variable **vlist1;
653 const struct variable **vlist2;
656 test_parameters->parent.insert_variables = two_sample_insert_variables;
658 if (!parse_variables_const_pool (lexer, pool,
661 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
664 if ( lex_match (lexer, T_WITH))
667 if ( !parse_variables_const_pool (lexer, pool, dict,
669 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
672 paired = (lex_match (lexer, '(') &&
673 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
681 if ( n_vlist1 != n_vlist2)
682 msg (SE, _ ("PAIRED was specified but the number of variables "
683 "preceding WITH (%zu) did not match the number "
684 "following (%zu)."), n_vlist1, n_vlist2);
686 test_parameters->n_pairs = n_vlist1 ;
690 test_parameters->n_pairs = n_vlist1 * n_vlist2;
695 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
698 test_parameters->pairs =
699 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
706 assert (n_vlist1 == n_vlist2);
707 for ( i = 0 ; i < n_vlist1; ++i )
709 test_parameters->pairs[n][1] = vlist1[i];
710 test_parameters->pairs[n][0] = vlist2[i];
717 for ( i = 0 ; i < n_vlist1; ++i )
719 for ( j = 0 ; j < n_vlist2; ++j )
721 test_parameters->pairs[n][1] = vlist1[i];
722 test_parameters->pairs[n][0] = vlist2[j];
731 for ( i = 0 ; i < n_vlist1 - 1; ++i )
733 for ( j = i + 1 ; j < n_vlist1; ++j )
735 assert ( n < test_parameters->n_pairs);
736 test_parameters->pairs[n][1] = vlist1[i];
737 test_parameters->pairs[n][0] = vlist1[j];
743 assert ( n == test_parameters->n_pairs);
750 parse_n_sample_related_test (struct lexer *lexer,
751 const struct dictionary *dict,
752 struct n_sample_test *nst,
756 union value val1, val2;
758 if (!parse_variables_const_pool (lexer, pool,
760 &nst->vars, &nst->n_vars,
761 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
764 if ( ! lex_force_match (lexer, T_BY))
767 nst->indep_var = parse_variable_const (lexer, dict);
769 if ( ! lex_force_match (lexer, '('))
772 value_init (&val1, var_get_width (nst->indep_var));
773 if ( ! parse_value (lexer, &val1, var_get_width (nst->indep_var)))
775 value_destroy (&val1, var_get_width (nst->indep_var));
779 if ( ! lex_force_match (lexer, ','))
782 value_init (&val2, var_get_width (nst->indep_var));
783 if ( ! parse_value (lexer, &val2, var_get_width (nst->indep_var)))
785 value_destroy (&val2, var_get_width (nst->indep_var));
789 if ( ! lex_force_match (lexer, ')'))
796 npar_wilcoxon (struct lexer *lexer,
798 struct npar_specs *specs )
802 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
803 struct npar_test *nt = &tp->parent;
804 nt->execute = wilcoxon_execute;
806 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
811 specs->test = pool_realloc (specs->pool,
813 sizeof (*specs->test) * specs->n_tests);
814 specs->test[specs->n_tests - 1] = nt;
820 npar_sign (struct lexer *lexer, struct dataset *ds,
821 struct npar_specs *specs)
823 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
824 struct npar_test *nt = &tp->parent;
826 nt->execute = sign_execute;
828 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
833 specs->test = pool_realloc (specs->pool,
835 sizeof (*specs->test) * specs->n_tests);
836 specs->test[specs->n_tests - 1] = nt;
842 npar_kruskal_wallis (struct lexer *lexer, struct dataset *ds,
843 struct npar_specs *specs)
845 struct n_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
846 struct npar_test *nt = &tp->parent;
848 nt->insert_variables = n_sample_insert_variables;
850 // nt->execute = kruskall_wallis_execute;
852 if (!parse_n_sample_related_test (lexer, dataset_dict (ds),
857 specs->test = pool_realloc (specs->pool,
859 sizeof (*specs->test) * specs->n_tests);
860 specs->test[specs->n_tests - 1] = nt;
865 /* Insert the variables for TEST into VAR_HASH */
867 one_sample_insert_variables (const struct npar_test *test,
868 struct const_hsh_table *var_hash)
871 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
873 for ( i = 0 ; i < ost->n_vars ; ++i )
874 const_hsh_insert (var_hash, ost->vars[i]);
879 two_sample_insert_variables (const struct npar_test *test,
880 struct const_hsh_table *var_hash)
884 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
886 for ( i = 0 ; i < tst->n_pairs ; ++i )
888 variable_pair *pair = &tst->pairs[i];
890 const_hsh_insert (var_hash, (*pair)[0]);
891 const_hsh_insert (var_hash, (*pair)[1]);
896 n_sample_insert_variables (const struct npar_test *test,
897 struct const_hsh_table *var_hash)
900 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
902 for ( i = 0 ; i < tst->n_vars ; ++i )
903 const_hsh_insert (var_hash, tst->vars[i]);
905 const_hsh_insert (var_hash, tst->indep_var);
909 npar_method (struct lexer *lexer, struct npar_specs *specs)
911 if ( lex_match_id (lexer, "EXACT") )
915 if (lex_match_id (lexer, "TIMER"))
919 if ( lex_match (lexer, '('))
921 if ( lex_force_num (lexer) )
923 specs->timer = lex_number (lexer);
926 lex_force_match (lexer, ')');