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. */
59 /* Array indices for STATISTICS subcommand. */
62 NPAR_ST_DESCRIPTIVES = 0,
63 NPAR_ST_QUARTILES = 1,
68 /* NPAR TESTS structure. */
71 /* CHISQUARE subcommand. */
74 /* BINOMIAL subcommand. */
77 /* WILCOXON subcommand. */
80 /* SIGN subcommand. */
83 /* MISSING subcommand. */
87 /* METHOD subcommand. */
90 /* STATISTICS subcommand. */
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_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
120 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
121 static int npar_method (struct lexer *, struct npar_specs *);
123 /* Command parsing functions. */
124 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
125 struct npar_specs *npar_specs );
128 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
129 struct npar_specs *nps)
136 npt->miss = MISS_ANALYSIS;
139 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
142 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
144 lex_match (lexer, '=');
146 switch (npar_chisquare (lexer, ds, nps))
153 lex_error (lexer, NULL);
159 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
161 lex_match (lexer, '=');
163 switch (npar_binomial (lexer, ds, nps))
170 lex_error (lexer, NULL);
176 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
178 lex_match (lexer, '=');
180 switch (npar_wilcoxon (lexer, ds, nps))
187 lex_error (lexer, NULL);
193 else if (lex_match_hyphenated_word (lexer, "SIGN"))
195 lex_match (lexer, '=');
197 switch (npar_sign (lexer, ds, nps))
204 lex_error (lexer, NULL);
210 else if (lex_match_hyphenated_word (lexer, "MISSING"))
212 lex_match (lexer, '=');
214 if (npt->missing > 1)
216 msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
219 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
221 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
222 npt->miss = MISS_ANALYSIS;
223 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
224 npt->miss = MISS_LISTWISE;
225 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
226 nps->filter = MV_SYSTEM;
227 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
228 nps->filter = MV_ANY;
231 lex_error (lexer, NULL);
234 lex_match (lexer, ',');
237 else if (lex_match_hyphenated_word (lexer, "METHOD"))
239 lex_match (lexer, '=');
243 msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
246 switch (npar_method (lexer, nps))
253 lex_error (lexer, NULL);
259 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
261 lex_match (lexer, '=');
263 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
265 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
266 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
267 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
268 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
269 else if (lex_match (lexer, T_ALL))
270 npt->a_statistics[NPAR_ST_ALL] = 1;
273 lex_error (lexer, NULL);
276 lex_match (lexer, ',');
279 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
281 lex_match (lexer, '=');
282 if (lex_match_id (lexer, "COMPATIBLE"))
283 settings_set_cmd_algorithm (COMPATIBLE);
284 else if (lex_match_id (lexer, "ENHANCED"))
285 settings_set_cmd_algorithm (ENHANCED);
287 if (!lex_match (lexer, '/'))
291 if (lex_token (lexer) != '.')
293 lex_error (lexer, _ ("expecting end of command"));
306 static void one_sample_insert_variables (const struct npar_test *test,
307 struct const_hsh_table *variables);
309 static void two_sample_insert_variables (const struct npar_test *test,
310 struct const_hsh_table *variables);
314 npar_execute (struct casereader *input,
315 const struct npar_specs *specs,
316 const struct dataset *ds)
319 struct descriptives *summary_descriptives = NULL;
321 for ( t = 0 ; t < specs->n_tests; ++t )
323 const struct npar_test *test = specs->test[t];
324 if ( NULL == test->execute )
326 msg (SW, _ ("NPAR subcommand not currently implemented."));
329 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
332 if ( specs->descriptives )
334 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
337 npar_summary_calc_descriptives (summary_descriptives,
338 casereader_clone (input),
340 specs->vv, specs->n_vars,
344 if ( (specs->descriptives || specs->quartiles)
345 && !taint_has_tainted_successor (casereader_get_taint (input)) )
346 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
348 free (summary_descriptives);
349 casereader_destroy (input);
354 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
356 struct cmd_npar_tests cmd;
359 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
360 struct const_hsh_table *var_hash;
361 struct casegrouper *grouper;
362 struct casereader *input, *group;
364 npar_specs.pool = pool_create ();
365 npar_specs.filter = MV_ANY;
367 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
368 compare_vars_by_name, hash_var_by_name,
371 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
373 pool_destroy (npar_specs.pool);
377 for (i = 0; i < npar_specs.n_tests; ++i )
379 const struct npar_test *test = npar_specs.test[i];
380 test->insert_variables (test, var_hash);
383 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
384 npar_specs.n_vars = const_hsh_count (var_hash);
386 if ( cmd.statistics )
390 for ( i = 0 ; i < NPAR_ST_count; ++i )
392 if ( cmd.a_statistics[i] )
396 case NPAR_ST_DESCRIPTIVES:
397 npar_specs.descriptives = true;
399 case NPAR_ST_QUARTILES:
400 npar_specs.quartiles = true;
403 npar_specs.quartiles = true;
404 npar_specs.descriptives = true;
413 input = proc_open (ds);
414 if ( cmd.miss == MISS_LISTWISE )
416 input = casereader_create_filter_missing (input,
424 grouper = casegrouper_create_splits (input, dataset_dict (ds));
425 while (casegrouper_get_next_group (grouper, &group))
426 npar_execute (group, &npar_specs, ds);
427 ok = casegrouper_destroy (grouper);
428 ok = proc_commit (ds) && ok;
430 const_hsh_destroy (var_hash);
432 pool_destroy (npar_specs.pool);
434 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
438 npar_chisquare (struct lexer *lexer, struct dataset *ds,
439 struct npar_specs *specs)
441 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
442 struct one_sample_test *tp = &cstp->parent;
443 struct npar_test *nt = &tp->parent;
445 nt->execute = chisquare_execute;
446 nt->insert_variables = one_sample_insert_variables;
448 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
449 &tp->vars, &tp->n_vars,
450 PV_NO_SCRATCH | PV_NO_DUPLICATE))
455 cstp->ranged = false;
457 if ( lex_match (lexer, '('))
460 if ( ! lex_force_num (lexer)) return 0;
461 cstp->lo = lex_integer (lexer);
463 lex_force_match (lexer, ',');
464 if (! lex_force_num (lexer) ) return 0;
465 cstp->hi = lex_integer (lexer);
466 if ( cstp->lo >= cstp->hi )
469 _ ("The specified value of HI (%d) is "
470 "lower than the specified value of LO (%d)"),
475 if (! lex_force_match (lexer, ')')) return 0;
478 cstp->n_expected = 0;
479 cstp->expected = NULL;
480 if ( lex_match (lexer, '/') )
482 if ( lex_match_id (lexer, "EXPECTED") )
484 lex_force_match (lexer, '=');
485 if ( ! lex_match_id (lexer, "EQUAL") )
489 while ( lex_is_number (lexer) )
493 f = lex_number (lexer);
495 if ( lex_match (lexer, '*'))
498 f = lex_number (lexer);
501 lex_match (lexer, ',');
503 cstp->n_expected += n;
504 cstp->expected = pool_realloc (specs->pool,
508 for ( i = cstp->n_expected - n ;
509 i < cstp->n_expected;
511 cstp->expected[i] = f;
517 lex_put_back (lexer, '/');
520 if ( cstp->ranged && cstp->n_expected > 0 &&
521 cstp->n_expected != cstp->hi - cstp->lo + 1 )
524 _ ("%d expected values were given, but the specified "
525 "range (%d-%d) requires exactly %d values."),
526 cstp->n_expected, cstp->lo, cstp->hi,
527 cstp->hi - cstp->lo +1);
532 specs->test = pool_realloc (specs->pool,
534 sizeof (*specs->test) * specs->n_tests);
536 specs->test[specs->n_tests - 1] = nt;
543 npar_binomial (struct lexer *lexer, struct dataset *ds,
544 struct npar_specs *specs)
546 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
547 struct one_sample_test *tp = &btp->parent;
548 struct npar_test *nt = &tp->parent;
550 nt->execute = binomial_execute;
551 nt->insert_variables = one_sample_insert_variables;
553 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
557 if ( lex_match (lexer, '(') )
559 if ( lex_force_num (lexer) )
561 btp->p = lex_number (lexer);
563 lex_force_match (lexer, ')');
569 /* Kludge: q2c swallows the '=' so put it back here */
570 lex_put_back (lexer, '=');
572 if (lex_match (lexer, '=') )
574 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
575 &tp->vars, &tp->n_vars,
576 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
578 if (lex_match (lexer, '('))
580 lex_force_num (lexer);
581 btp->category1 = lex_number (lexer);
583 if ( lex_match (lexer, ','))
585 if ( ! lex_force_num (lexer) ) return 2;
586 btp->category2 = lex_number (lexer);
591 btp->cutpoint = btp->category1;
594 lex_force_match (lexer, ')');
603 specs->test = pool_realloc (specs->pool,
605 sizeof (*specs->test) * specs->n_tests);
607 specs->test[specs->n_tests - 1] = nt;
614 parse_two_sample_related_test (struct lexer *lexer,
615 const struct dictionary *dict,
616 struct two_sample_test *test_parameters,
622 parse_two_sample_related_test (struct lexer *lexer,
623 const struct dictionary *dict,
624 struct two_sample_test *test_parameters,
631 const struct variable **vlist1;
634 const struct variable **vlist2;
637 test_parameters->parent.insert_variables = two_sample_insert_variables;
639 if (!parse_variables_const_pool (lexer, pool,
642 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
645 if ( lex_match (lexer, T_WITH))
648 if ( !parse_variables_const_pool (lexer, pool, dict,
650 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
653 paired = (lex_match (lexer, '(') &&
654 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
662 if ( n_vlist1 != n_vlist2)
663 msg (SE, _ ("PAIRED was specified but the number of variables "
664 "preceding WITH (%zu) did not match the number "
665 "following (%zu)."), n_vlist1, n_vlist2);
667 test_parameters->n_pairs = n_vlist1 ;
671 test_parameters->n_pairs = n_vlist1 * n_vlist2;
676 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
679 test_parameters->pairs =
680 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
687 assert (n_vlist1 == n_vlist2);
688 for ( i = 0 ; i < n_vlist1; ++i )
690 test_parameters->pairs[n][1] = vlist1[i];
691 test_parameters->pairs[n][0] = vlist2[i];
698 for ( i = 0 ; i < n_vlist1; ++i )
700 for ( j = 0 ; j < n_vlist2; ++j )
702 test_parameters->pairs[n][1] = vlist1[i];
703 test_parameters->pairs[n][0] = vlist2[j];
712 for ( i = 0 ; i < n_vlist1 - 1; ++i )
714 for ( j = i + 1 ; j < n_vlist1; ++j )
716 assert ( n < test_parameters->n_pairs);
717 test_parameters->pairs[n][1] = vlist1[i];
718 test_parameters->pairs[n][0] = vlist1[j];
724 assert ( n == test_parameters->n_pairs);
730 npar_wilcoxon (struct lexer *lexer,
732 struct npar_specs *specs )
736 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
737 struct npar_test *nt = &tp->parent;
738 nt->execute = wilcoxon_execute;
740 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
745 specs->test = pool_realloc (specs->pool,
747 sizeof (*specs->test) * specs->n_tests);
748 specs->test[specs->n_tests - 1] = nt;
754 npar_sign (struct lexer *lexer, struct dataset *ds,
755 struct npar_specs *specs)
757 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
758 struct npar_test *nt = &tp->parent;
760 nt->execute = sign_execute;
762 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
767 specs->test = pool_realloc (specs->pool,
769 sizeof (*specs->test) * specs->n_tests);
770 specs->test[specs->n_tests - 1] = nt;
775 /* Insert the variables for TEST into VAR_HASH */
777 one_sample_insert_variables (const struct npar_test *test,
778 struct const_hsh_table *var_hash)
781 struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
783 for ( i = 0 ; i < ost->n_vars ; ++i )
784 const_hsh_insert (var_hash, ost->vars[i]);
788 two_sample_insert_variables (const struct npar_test *test,
789 struct const_hsh_table *var_hash)
793 const struct two_sample_test *tst = (const struct two_sample_test *) test;
795 for ( i = 0 ; i < tst->n_pairs ; ++i )
797 variable_pair *pair = &tst->pairs[i];
799 const_hsh_insert (var_hash, (*pair)[0]);
800 const_hsh_insert (var_hash, (*pair)[1]);
805 npar_method (struct lexer *lexer, struct npar_specs *specs)
807 if ( lex_match_id (lexer, "EXACT") )
811 if (lex_match_id (lexer, "TIMER"))
815 if ( lex_match (lexer, '('))
817 if ( lex_force_num (lexer) )
819 specs->timer = lex_number (lexer);
822 lex_force_match (lexer, ')');