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/stats/binomial.h>
37 #include <language/stats/chisquare.h>
38 #include <language/stats/wilcoxon.h>
39 #include <language/stats/sign.h>
40 #include <libpspp/assertion.h>
41 #include <libpspp/cast.h>
42 #include <libpspp/hash.h>
43 #include <libpspp/message.h>
44 #include <libpspp/pool.h>
45 #include <libpspp/str.h>
46 #include <libpspp/taint.h>
47 #include <math/moments.h>
50 #define _(msgid) gettext (msgid)
52 /* Settings for subcommand specifiers. */
65 /* Array indices for STATISTICS subcommand. */
68 NPAR_ST_DESCRIPTIVES = 0,
69 NPAR_ST_QUARTILES = 1,
74 /* NPAR TESTS structure. */
77 /* CHISQUARE subcommand. */
80 /* BINOMIAL subcommand. */
83 /* WILCOXON subcommand. */
86 /* SIGN subcommand. */
89 /* MISSING subcommand. */
94 /* METHOD subcommand. */
97 /* STATISTICS subcommand. */
99 int a_statistics[NPAR_ST_count];
106 struct npar_test **test;
109 const struct variable **vv; /* Compendium of all variables
110 (those mentioned on ANY subcommand */
111 int n_vars; /* Number of variables in vv */
113 enum mv_class filter; /* Missing values to filter. */
115 bool descriptives; /* Descriptive statistics should be calculated */
116 bool quartiles; /* Quartiles should be calculated */
118 bool exact; /* Whether exact calculations have been requested */
119 double timer; /* Maximum time (in minutes) to wait for exact calculations */
123 /* Prototype for custom subcommands of NPAR TESTS. */
124 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
125 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
126 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
127 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
128 static int npar_method (struct lexer *, struct npar_specs *);
130 /* Command parsing functions. */
131 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
132 struct npar_specs *npar_specs );
135 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
136 struct npar_specs *nps)
143 npt->miss = MISS_ANALYSIS;
144 npt->incl = NPAR_EXCLUDE;
147 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
150 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
152 lex_match (lexer, '=');
154 switch (npar_chisquare (lexer, ds, nps))
161 lex_error (lexer, NULL);
167 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
169 lex_match (lexer, '=');
171 switch (npar_binomial (lexer, ds, nps))
178 lex_error (lexer, NULL);
184 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
186 lex_match (lexer, '=');
188 switch (npar_wilcoxon (lexer, ds, nps))
195 lex_error (lexer, NULL);
201 else if (lex_match_hyphenated_word (lexer, "SIGN"))
203 lex_match (lexer, '=');
205 switch (npar_sign (lexer, ds, nps))
212 lex_error (lexer, NULL);
218 else if (lex_match_hyphenated_word (lexer, "MISSING"))
220 lex_match (lexer, '=');
222 if (npt->missing > 1)
224 msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
227 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
229 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
230 npt->miss = MISS_ANALYSIS;
231 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
232 npt->miss = MISS_LISTWISE;
233 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
234 npt->incl = NPAR_INCLUDE;
235 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
236 npt->incl = NPAR_EXCLUDE;
239 lex_error (lexer, NULL);
242 lex_match (lexer, ',');
245 else if (lex_match_hyphenated_word (lexer, "METHOD"))
247 lex_match (lexer, '=');
251 msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
254 switch (npar_method (lexer, nps))
261 lex_error (lexer, NULL);
267 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
269 lex_match (lexer, '=');
271 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
273 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
274 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
275 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
276 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
277 else if (lex_match (lexer, T_ALL))
278 npt->a_statistics[NPAR_ST_ALL] = 1;
281 lex_error (lexer, NULL);
284 lex_match (lexer, ',');
287 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
289 lex_match (lexer, '=');
290 if (lex_match_id (lexer, "COMPATIBLE"))
291 settings_set_cmd_algorithm (COMPATIBLE);
292 else if (lex_match_id (lexer, "ENHANCED"))
293 settings_set_cmd_algorithm (ENHANCED);
295 if (!lex_match (lexer, '/'))
299 if (lex_token (lexer) != '.')
301 lex_error (lexer, _ ("expecting end of command"));
314 static void one_sample_insert_variables (const struct npar_test *test,
315 struct const_hsh_table *variables);
317 static void two_sample_insert_variables (const struct npar_test *test,
318 struct const_hsh_table *variables);
322 npar_execute (struct casereader *input,
323 const struct npar_specs *specs,
324 const struct dataset *ds)
327 struct descriptives *summary_descriptives = NULL;
329 for ( t = 0 ; t < specs->n_tests; ++t )
331 const struct npar_test *test = specs->test[t];
332 if ( NULL == test->execute )
334 msg (SW, _ ("NPAR subcommand not currently implemented."));
337 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
340 if ( specs->descriptives )
342 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
345 npar_summary_calc_descriptives (summary_descriptives,
346 casereader_clone (input),
348 specs->vv, specs->n_vars,
352 if ( (specs->descriptives || specs->quartiles)
353 && !taint_has_tainted_successor (casereader_get_taint (input)) )
354 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
356 free (summary_descriptives);
357 casereader_destroy (input);
362 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
364 struct cmd_npar_tests cmd;
367 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
368 struct const_hsh_table *var_hash;
369 struct casegrouper *grouper;
370 struct casereader *input, *group;
372 npar_specs.pool = pool_create ();
374 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
375 compare_vars_by_name, hash_var_by_name,
378 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
380 pool_destroy (npar_specs.pool);
384 for (i = 0; i < npar_specs.n_tests; ++i )
386 const struct npar_test *test = npar_specs.test[i];
387 test->insert_variables (test, var_hash);
390 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
391 npar_specs.n_vars = const_hsh_count (var_hash);
393 if ( cmd.statistics )
397 for ( i = 0 ; i < NPAR_ST_count; ++i )
399 if ( cmd.a_statistics[i] )
403 case NPAR_ST_DESCRIPTIVES:
404 npar_specs.descriptives = true;
406 case NPAR_ST_QUARTILES:
407 npar_specs.quartiles = true;
410 npar_specs.quartiles = true;
411 npar_specs.descriptives = true;
420 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
422 input = proc_open (ds);
423 if ( cmd.miss == MISS_LISTWISE )
425 input = casereader_create_filter_missing (input,
433 grouper = casegrouper_create_splits (input, dataset_dict (ds));
434 while (casegrouper_get_next_group (grouper, &group))
435 npar_execute (group, &npar_specs, ds);
436 ok = casegrouper_destroy (grouper);
437 ok = proc_commit (ds) && ok;
439 const_hsh_destroy (var_hash);
441 pool_destroy (npar_specs.pool);
443 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
447 npar_chisquare (struct lexer *lexer, struct dataset *ds,
448 struct npar_specs *specs)
450 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
451 struct one_sample_test *tp = &cstp->parent;
452 struct npar_test *nt = &tp->parent;
454 nt->execute = chisquare_execute;
455 nt->insert_variables = one_sample_insert_variables;
457 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
458 &tp->vars, &tp->n_vars,
459 PV_NO_SCRATCH | PV_NO_DUPLICATE))
464 cstp->ranged = false;
466 if ( lex_match (lexer, '('))
469 if ( ! lex_force_num (lexer)) return 0;
470 cstp->lo = lex_integer (lexer);
472 lex_force_match (lexer, ',');
473 if (! lex_force_num (lexer) ) return 0;
474 cstp->hi = lex_integer (lexer);
475 if ( cstp->lo >= cstp->hi )
478 _ ("The specified value of HI (%d) is "
479 "lower than the specified value of LO (%d)"),
484 if (! lex_force_match (lexer, ')')) return 0;
487 cstp->n_expected = 0;
488 cstp->expected = NULL;
489 if ( lex_match (lexer, '/') )
491 if ( lex_match_id (lexer, "EXPECTED") )
493 lex_force_match (lexer, '=');
494 if ( ! lex_match_id (lexer, "EQUAL") )
498 while ( lex_is_number (lexer) )
502 f = lex_number (lexer);
504 if ( lex_match (lexer, '*'))
507 f = lex_number (lexer);
510 lex_match (lexer, ',');
512 cstp->n_expected += n;
513 cstp->expected = pool_realloc (specs->pool,
517 for ( i = cstp->n_expected - n ;
518 i < cstp->n_expected;
520 cstp->expected[i] = f;
526 lex_put_back (lexer, '/');
529 if ( cstp->ranged && cstp->n_expected > 0 &&
530 cstp->n_expected != cstp->hi - cstp->lo + 1 )
533 _ ("%d expected values were given, but the specified "
534 "range (%d-%d) requires exactly %d values."),
535 cstp->n_expected, cstp->lo, cstp->hi,
536 cstp->hi - cstp->lo +1);
541 specs->test = pool_realloc (specs->pool,
543 sizeof (*specs->test) * specs->n_tests);
545 specs->test[specs->n_tests - 1] = nt;
552 npar_binomial (struct lexer *lexer, struct dataset *ds,
553 struct npar_specs *specs)
555 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
556 struct one_sample_test *tp = &btp->parent;
557 struct npar_test *nt = &tp->parent;
559 nt->execute = binomial_execute;
560 nt->insert_variables = one_sample_insert_variables;
562 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
566 if ( lex_match (lexer, '(') )
568 if ( lex_force_num (lexer) )
570 btp->p = lex_number (lexer);
572 lex_force_match (lexer, ')');
578 /* Kludge: q2c swallows the '=' so put it back here */
579 lex_put_back (lexer, '=');
581 if (lex_match (lexer, '=') )
583 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
584 &tp->vars, &tp->n_vars,
585 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
587 if (lex_match (lexer, '('))
589 lex_force_num (lexer);
590 btp->category1 = lex_number (lexer);
592 if ( lex_match (lexer, ','))
594 if ( ! lex_force_num (lexer) ) return 2;
595 btp->category2 = lex_number (lexer);
600 btp->cutpoint = btp->category1;
603 lex_force_match (lexer, ')');
612 specs->test = pool_realloc (specs->pool,
614 sizeof (*specs->test) * specs->n_tests);
616 specs->test[specs->n_tests - 1] = nt;
623 parse_two_sample_related_test (struct lexer *lexer,
624 const struct dictionary *dict,
625 struct two_sample_test *test_parameters,
631 parse_two_sample_related_test (struct lexer *lexer,
632 const struct dictionary *dict,
633 struct two_sample_test *test_parameters,
640 const struct variable **vlist1;
643 const struct variable **vlist2;
646 test_parameters->parent.insert_variables = two_sample_insert_variables;
648 if (!parse_variables_const_pool (lexer, pool,
651 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
654 if ( lex_match (lexer, T_WITH))
657 if ( !parse_variables_const_pool (lexer, pool, dict,
659 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
662 paired = (lex_match (lexer, '(') &&
663 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
671 if ( n_vlist1 != n_vlist2)
672 msg (SE, _ ("PAIRED was specified but the number of variables "
673 "preceding WITH (%zu) did not match the number "
674 "following (%zu)."), n_vlist1, n_vlist2);
676 test_parameters->n_pairs = n_vlist1 ;
680 test_parameters->n_pairs = n_vlist1 * n_vlist2;
685 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
688 test_parameters->pairs =
689 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
696 assert (n_vlist1 == n_vlist2);
697 for ( i = 0 ; i < n_vlist1; ++i )
699 test_parameters->pairs[n][1] = vlist1[i];
700 test_parameters->pairs[n][0] = vlist2[i];
707 for ( i = 0 ; i < n_vlist1; ++i )
709 for ( j = 0 ; j < n_vlist2; ++j )
711 test_parameters->pairs[n][1] = vlist1[i];
712 test_parameters->pairs[n][0] = vlist2[j];
721 for ( i = 0 ; i < n_vlist1 - 1; ++i )
723 for ( j = i + 1 ; j < n_vlist1; ++j )
725 assert ( n < test_parameters->n_pairs);
726 test_parameters->pairs[n][1] = vlist1[i];
727 test_parameters->pairs[n][0] = vlist1[j];
733 assert ( n == test_parameters->n_pairs);
739 npar_wilcoxon (struct lexer *lexer,
741 struct npar_specs *specs )
745 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
746 struct npar_test *nt = &tp->parent;
747 nt->execute = wilcoxon_execute;
749 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
754 specs->test = pool_realloc (specs->pool,
756 sizeof (*specs->test) * specs->n_tests);
757 specs->test[specs->n_tests - 1] = nt;
763 npar_sign (struct lexer *lexer, struct dataset *ds,
764 struct npar_specs *specs)
766 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
767 struct npar_test *nt = &tp->parent;
769 nt->execute = sign_execute;
771 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
776 specs->test = pool_realloc (specs->pool,
778 sizeof (*specs->test) * specs->n_tests);
779 specs->test[specs->n_tests - 1] = nt;
784 /* Insert the variables for TEST into VAR_HASH */
786 one_sample_insert_variables (const struct npar_test *test,
787 struct const_hsh_table *var_hash)
790 struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
792 for ( i = 0 ; i < ost->n_vars ; ++i )
793 const_hsh_insert (var_hash, ost->vars[i]);
797 two_sample_insert_variables (const struct npar_test *test,
798 struct const_hsh_table *var_hash)
802 const struct two_sample_test *tst = (const struct two_sample_test *) test;
804 for ( i = 0 ; i < tst->n_pairs ; ++i )
806 variable_pair *pair = &tst->pairs[i];
808 const_hsh_insert (var_hash, (*pair)[0]);
809 const_hsh_insert (var_hash, (*pair)[1]);
814 npar_method (struct lexer *lexer, struct npar_specs *specs)
816 if ( lex_match_id (lexer, "EXACT") )
820 if (lex_match_id (lexer, "TIMER"))
824 if ( lex_match (lexer, '('))
826 if ( lex_force_num (lexer) )
828 specs->timer = lex_number (lexer);
831 lex_force_match (lexer, ')');