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. */
82 /* How missing values should be treated */
85 /* Which statistics have been requested */
86 int a_statistics[NPAR_ST_count];
93 struct npar_test **test;
96 const struct variable **vv; /* Compendium of all variables
97 (those mentioned on ANY subcommand */
98 int n_vars; /* Number of variables in vv */
100 enum mv_class filter; /* Missing values to filter. */
102 bool descriptives; /* Descriptive statistics should be calculated */
103 bool quartiles; /* Quartiles should be calculated */
105 bool exact; /* Whether exact calculations have been requested */
106 double timer; /* Maximum time (in minutes) to wait for exact calculations */
110 /* Prototype for custom subcommands of NPAR TESTS. */
111 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
112 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
113 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
114 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
115 static int npar_method (struct lexer *, struct npar_specs *);
117 /* Command parsing functions. */
118 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
119 struct npar_specs *npar_specs );
122 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
123 struct npar_specs *nps)
130 npt->miss = MISS_ANALYSIS;
133 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
136 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
138 lex_match (lexer, '=');
140 switch (npar_chisquare (lexer, ds, nps))
147 lex_error (lexer, NULL);
153 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
155 lex_match (lexer, '=');
157 switch (npar_binomial (lexer, ds, nps))
164 lex_error (lexer, NULL);
170 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
172 lex_match (lexer, '=');
174 switch (npar_wilcoxon (lexer, ds, nps))
181 lex_error (lexer, NULL);
187 else if (lex_match_hyphenated_word (lexer, "SIGN"))
189 lex_match (lexer, '=');
191 switch (npar_sign (lexer, ds, nps))
198 lex_error (lexer, NULL);
204 else if (lex_match_hyphenated_word (lexer, "MISSING"))
206 lex_match (lexer, '=');
208 if (npt->missing > 1)
210 msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
213 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
215 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
216 npt->miss = MISS_ANALYSIS;
217 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
218 npt->miss = MISS_LISTWISE;
219 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
220 nps->filter = MV_SYSTEM;
221 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
222 nps->filter = MV_ANY;
225 lex_error (lexer, NULL);
228 lex_match (lexer, ',');
231 else if (lex_match_hyphenated_word (lexer, "METHOD"))
233 lex_match (lexer, '=');
237 msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
240 switch (npar_method (lexer, nps))
247 lex_error (lexer, NULL);
253 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
255 lex_match (lexer, '=');
257 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
259 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
260 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
261 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
262 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
263 else if (lex_match (lexer, T_ALL))
264 npt->a_statistics[NPAR_ST_ALL] = 1;
267 lex_error (lexer, NULL);
270 lex_match (lexer, ',');
273 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
275 lex_match (lexer, '=');
276 if (lex_match_id (lexer, "COMPATIBLE"))
277 settings_set_cmd_algorithm (COMPATIBLE);
278 else if (lex_match_id (lexer, "ENHANCED"))
279 settings_set_cmd_algorithm (ENHANCED);
281 if (!lex_match (lexer, '/'))
285 if (lex_token (lexer) != '.')
287 lex_error (lexer, _ ("expecting end of command"));
300 static void one_sample_insert_variables (const struct npar_test *test,
301 struct const_hsh_table *variables);
303 static void two_sample_insert_variables (const struct npar_test *test,
304 struct const_hsh_table *variables);
307 static void n_sample_insert_variables (const struct npar_test *test,
308 struct const_hsh_table *variables);
313 npar_execute (struct casereader *input,
314 const struct npar_specs *specs,
315 const struct dataset *ds)
318 struct descriptives *summary_descriptives = NULL;
320 for ( t = 0 ; t < specs->n_tests; ++t )
322 const struct npar_test *test = specs->test[t];
323 if ( NULL == test->execute )
325 msg (SW, _ ("NPAR subcommand not currently implemented."));
328 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
331 if ( specs->descriptives )
333 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
336 npar_summary_calc_descriptives (summary_descriptives,
337 casereader_clone (input),
339 specs->vv, specs->n_vars,
343 if ( (specs->descriptives || specs->quartiles)
344 && !taint_has_tainted_successor (casereader_get_taint (input)) )
345 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
347 free (summary_descriptives);
348 casereader_destroy (input);
353 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
355 struct cmd_npar_tests cmd;
358 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
359 struct const_hsh_table *var_hash;
360 struct casegrouper *grouper;
361 struct casereader *input, *group;
363 npar_specs.pool = pool_create ();
364 npar_specs.filter = MV_ANY;
366 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
367 compare_vars_by_name, hash_var_by_name,
370 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
372 pool_destroy (npar_specs.pool);
376 for (i = 0; i < npar_specs.n_tests; ++i )
378 const struct npar_test *test = npar_specs.test[i];
379 test->insert_variables (test, var_hash);
382 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
383 npar_specs.n_vars = const_hsh_count (var_hash);
385 if ( cmd.statistics )
389 for ( i = 0 ; i < NPAR_ST_count; ++i )
391 if ( cmd.a_statistics[i] )
395 case NPAR_ST_DESCRIPTIVES:
396 npar_specs.descriptives = true;
398 case NPAR_ST_QUARTILES:
399 npar_specs.quartiles = true;
402 npar_specs.quartiles = true;
403 npar_specs.descriptives = true;
412 input = proc_open (ds);
413 if ( cmd.miss == MISS_LISTWISE )
415 input = casereader_create_filter_missing (input,
423 grouper = casegrouper_create_splits (input, dataset_dict (ds));
424 while (casegrouper_get_next_group (grouper, &group))
425 npar_execute (group, &npar_specs, ds);
426 ok = casegrouper_destroy (grouper);
427 ok = proc_commit (ds) && ok;
429 const_hsh_destroy (var_hash);
431 pool_destroy (npar_specs.pool);
433 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
437 npar_chisquare (struct lexer *lexer, struct dataset *ds,
438 struct npar_specs *specs)
440 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
441 struct one_sample_test *tp = &cstp->parent;
442 struct npar_test *nt = &tp->parent;
444 nt->execute = chisquare_execute;
445 nt->insert_variables = one_sample_insert_variables;
447 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
448 &tp->vars, &tp->n_vars,
449 PV_NO_SCRATCH | PV_NO_DUPLICATE))
454 cstp->ranged = false;
456 if ( lex_match (lexer, '('))
459 if ( ! lex_force_num (lexer)) return 0;
460 cstp->lo = lex_integer (lexer);
462 lex_force_match (lexer, ',');
463 if (! lex_force_num (lexer) ) return 0;
464 cstp->hi = lex_integer (lexer);
465 if ( cstp->lo >= cstp->hi )
468 _ ("The specified value of HI (%d) is "
469 "lower than the specified value of LO (%d)"),
474 if (! lex_force_match (lexer, ')')) return 0;
477 cstp->n_expected = 0;
478 cstp->expected = NULL;
479 if ( lex_match (lexer, '/') )
481 if ( lex_match_id (lexer, "EXPECTED") )
483 lex_force_match (lexer, '=');
484 if ( ! lex_match_id (lexer, "EQUAL") )
488 while ( lex_is_number (lexer) )
492 f = lex_number (lexer);
494 if ( lex_match (lexer, '*'))
497 f = lex_number (lexer);
500 lex_match (lexer, ',');
502 cstp->n_expected += n;
503 cstp->expected = pool_realloc (specs->pool,
507 for ( i = cstp->n_expected - n ;
508 i < cstp->n_expected;
510 cstp->expected[i] = f;
516 lex_put_back (lexer, '/');
519 if ( cstp->ranged && cstp->n_expected > 0 &&
520 cstp->n_expected != cstp->hi - cstp->lo + 1 )
523 _ ("%d expected values were given, but the specified "
524 "range (%d-%d) requires exactly %d values."),
525 cstp->n_expected, cstp->lo, cstp->hi,
526 cstp->hi - cstp->lo +1);
531 specs->test = pool_realloc (specs->pool,
533 sizeof (*specs->test) * specs->n_tests);
535 specs->test[specs->n_tests - 1] = nt;
542 npar_binomial (struct lexer *lexer, struct dataset *ds,
543 struct npar_specs *specs)
545 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
546 struct one_sample_test *tp = &btp->parent;
547 struct npar_test *nt = &tp->parent;
549 nt->execute = binomial_execute;
550 nt->insert_variables = one_sample_insert_variables;
552 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
556 if ( lex_match (lexer, '(') )
558 if ( lex_force_num (lexer) )
560 btp->p = lex_number (lexer);
562 lex_force_match (lexer, ')');
568 /* Kludge: q2c swallows the '=' so put it back here */
569 lex_put_back (lexer, '=');
571 if (lex_match (lexer, '=') )
573 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
574 &tp->vars, &tp->n_vars,
575 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
577 if (lex_match (lexer, '('))
579 lex_force_num (lexer);
580 btp->category1 = lex_number (lexer);
582 if ( lex_match (lexer, ','))
584 if ( ! lex_force_num (lexer) ) return 2;
585 btp->category2 = lex_number (lexer);
590 btp->cutpoint = btp->category1;
593 lex_force_match (lexer, ')');
602 specs->test = pool_realloc (specs->pool,
604 sizeof (*specs->test) * specs->n_tests);
606 specs->test[specs->n_tests - 1] = nt;
613 parse_two_sample_related_test (struct lexer *lexer,
614 const struct dictionary *dict,
615 struct two_sample_test *test_parameters,
621 parse_two_sample_related_test (struct lexer *lexer,
622 const struct dictionary *dict,
623 struct two_sample_test *test_parameters,
630 const struct variable **vlist1;
633 const struct variable **vlist2;
636 test_parameters->parent.insert_variables = two_sample_insert_variables;
638 if (!parse_variables_const_pool (lexer, pool,
641 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
644 if ( lex_match (lexer, T_WITH))
647 if ( !parse_variables_const_pool (lexer, pool, dict,
649 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
652 paired = (lex_match (lexer, '(') &&
653 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
661 if ( n_vlist1 != n_vlist2)
662 msg (SE, _ ("PAIRED was specified but the number of variables "
663 "preceding WITH (%zu) did not match the number "
664 "following (%zu)."), n_vlist1, n_vlist2);
666 test_parameters->n_pairs = n_vlist1 ;
670 test_parameters->n_pairs = n_vlist1 * n_vlist2;
675 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
678 test_parameters->pairs =
679 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
686 assert (n_vlist1 == n_vlist2);
687 for ( i = 0 ; i < n_vlist1; ++i )
689 test_parameters->pairs[n][1] = vlist1[i];
690 test_parameters->pairs[n][0] = vlist2[i];
697 for ( i = 0 ; i < n_vlist1; ++i )
699 for ( j = 0 ; j < n_vlist2; ++j )
701 test_parameters->pairs[n][1] = vlist1[i];
702 test_parameters->pairs[n][0] = vlist2[j];
711 for ( i = 0 ; i < n_vlist1 - 1; ++i )
713 for ( j = i + 1 ; j < n_vlist1; ++j )
715 assert ( n < test_parameters->n_pairs);
716 test_parameters->pairs[n][1] = vlist1[i];
717 test_parameters->pairs[n][0] = vlist1[j];
723 assert ( n == test_parameters->n_pairs);
730 parse_n_sample_related_test (struct lexer *lexer,
731 const struct dictionary *dict,
732 struct n_sample_test *nst,
736 union value val1, val2;
738 if (!parse_variables_const_pool (lexer, pool,
740 &nst->vars, &nst->n_vars,
741 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
744 if ( ! lex_force_match (lexer, T_BY))
747 nst->indep_var = parse_variable_const (lexer, dict);
749 if ( ! lex_force_match (lexer, '('))
752 value_init (&val1, var_get_width (nst->indep_var));
753 if ( ! parse_value (lexer, &val1, var_get_width (nst->indep_var)))
755 value_destroy (&val1, var_get_width (nst->indep_var));
759 if ( ! lex_force_match (lexer, ','))
762 value_init (&val2, var_get_width (nst->indep_var));
763 if ( ! parse_value (lexer, &val2, var_get_width (nst->indep_var)))
765 value_destroy (&val2, var_get_width (nst->indep_var));
769 if ( ! lex_force_match (lexer, ')'))
776 npar_wilcoxon (struct lexer *lexer,
778 struct npar_specs *specs )
782 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
783 struct npar_test *nt = &tp->parent;
784 nt->execute = wilcoxon_execute;
786 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
791 specs->test = pool_realloc (specs->pool,
793 sizeof (*specs->test) * specs->n_tests);
794 specs->test[specs->n_tests - 1] = nt;
800 npar_sign (struct lexer *lexer, struct dataset *ds,
801 struct npar_specs *specs)
803 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
804 struct npar_test *nt = &tp->parent;
806 nt->execute = sign_execute;
808 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
813 specs->test = pool_realloc (specs->pool,
815 sizeof (*specs->test) * specs->n_tests);
816 specs->test[specs->n_tests - 1] = nt;
821 /* Insert the variables for TEST into VAR_HASH */
823 one_sample_insert_variables (const struct npar_test *test,
824 struct const_hsh_table *var_hash)
827 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
829 for ( i = 0 ; i < ost->n_vars ; ++i )
830 const_hsh_insert (var_hash, ost->vars[i]);
835 two_sample_insert_variables (const struct npar_test *test,
836 struct const_hsh_table *var_hash)
840 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
842 for ( i = 0 ; i < tst->n_pairs ; ++i )
844 variable_pair *pair = &tst->pairs[i];
846 const_hsh_insert (var_hash, (*pair)[0]);
847 const_hsh_insert (var_hash, (*pair)[1]);
852 n_sample_insert_variables (const struct npar_test *test,
853 struct const_hsh_table *var_hash)
856 const struct n_sample_test *tst = UP_CAST (test, const struct n_sample_test, parent);
858 for ( i = 0 ; i < tst->n_vars ; ++i )
859 const_hsh_insert (var_hash, tst->vars[i]);
861 const_hsh_insert (var_hash, tst->indep_var);
865 npar_method (struct lexer *lexer, struct npar_specs *specs)
867 if ( lex_match_id (lexer, "EXACT") )
871 if (lex_match_id (lexer, "TIMER"))
875 if ( lex_match (lexer, '('))
877 if ( lex_force_num (lexer) )
879 specs->timer = lex_number (lexer);
882 lex_force_match (lexer, ')');