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 /* Count variables indicating how many
72 of the subcommands have been given. */
81 /* How missing values should be treated */
84 /* Which statistics have been requested */
85 int a_statistics[NPAR_ST_count];
92 struct npar_test **test;
95 const struct variable **vv; /* Compendium of all variables
96 (those mentioned on ANY subcommand */
97 int n_vars; /* Number of variables in vv */
99 enum mv_class filter; /* Missing values to filter. */
101 bool descriptives; /* Descriptive statistics should be calculated */
102 bool quartiles; /* Quartiles should be calculated */
104 bool exact; /* Whether exact calculations have been requested */
105 double timer; /* Maximum time (in minutes) to wait for exact calculations */
109 /* Prototype for custom subcommands of NPAR TESTS. */
110 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
111 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
112 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
113 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
114 static int npar_method (struct lexer *, struct npar_specs *);
116 /* Command parsing functions. */
117 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
118 struct npar_specs *npar_specs );
121 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
122 struct npar_specs *nps)
129 npt->miss = MISS_ANALYSIS;
132 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
135 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
137 lex_match (lexer, '=');
139 switch (npar_chisquare (lexer, ds, nps))
146 lex_error (lexer, NULL);
152 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
154 lex_match (lexer, '=');
156 switch (npar_binomial (lexer, ds, nps))
163 lex_error (lexer, NULL);
169 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
171 lex_match (lexer, '=');
173 switch (npar_wilcoxon (lexer, ds, nps))
180 lex_error (lexer, NULL);
186 else if (lex_match_hyphenated_word (lexer, "SIGN"))
188 lex_match (lexer, '=');
190 switch (npar_sign (lexer, ds, nps))
197 lex_error (lexer, NULL);
203 else if (lex_match_hyphenated_word (lexer, "MISSING"))
205 lex_match (lexer, '=');
207 if (npt->missing > 1)
209 msg (SE, _ ("The %s subcommand may be given only once."), "MISSING");
212 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
214 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
215 npt->miss = MISS_ANALYSIS;
216 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
217 npt->miss = MISS_LISTWISE;
218 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
219 nps->filter = MV_SYSTEM;
220 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
221 nps->filter = MV_ANY;
224 lex_error (lexer, NULL);
227 lex_match (lexer, ',');
230 else if (lex_match_hyphenated_word (lexer, "METHOD"))
232 lex_match (lexer, '=');
236 msg (SE, _ ("The %s subcommand may be given only once."), "METHOD");
239 switch (npar_method (lexer, nps))
246 lex_error (lexer, NULL);
252 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
254 lex_match (lexer, '=');
256 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
258 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
259 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
260 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
261 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
262 else if (lex_match (lexer, T_ALL))
263 npt->a_statistics[NPAR_ST_ALL] = 1;
266 lex_error (lexer, NULL);
269 lex_match (lexer, ',');
272 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
274 lex_match (lexer, '=');
275 if (lex_match_id (lexer, "COMPATIBLE"))
276 settings_set_cmd_algorithm (COMPATIBLE);
277 else if (lex_match_id (lexer, "ENHANCED"))
278 settings_set_cmd_algorithm (ENHANCED);
280 if (!lex_match (lexer, '/'))
284 if (lex_token (lexer) != '.')
286 lex_error (lexer, _ ("expecting end of command"));
299 static void one_sample_insert_variables (const struct npar_test *test,
300 struct const_hsh_table *variables);
302 static void two_sample_insert_variables (const struct npar_test *test,
303 struct const_hsh_table *variables);
307 npar_execute (struct casereader *input,
308 const struct npar_specs *specs,
309 const struct dataset *ds)
312 struct descriptives *summary_descriptives = NULL;
314 for ( t = 0 ; t < specs->n_tests; ++t )
316 const struct npar_test *test = specs->test[t];
317 if ( NULL == test->execute )
319 msg (SW, _ ("NPAR subcommand not currently implemented."));
322 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
325 if ( specs->descriptives )
327 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
330 npar_summary_calc_descriptives (summary_descriptives,
331 casereader_clone (input),
333 specs->vv, specs->n_vars,
337 if ( (specs->descriptives || specs->quartiles)
338 && !taint_has_tainted_successor (casereader_get_taint (input)) )
339 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
341 free (summary_descriptives);
342 casereader_destroy (input);
347 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
349 struct cmd_npar_tests cmd;
352 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
353 struct const_hsh_table *var_hash;
354 struct casegrouper *grouper;
355 struct casereader *input, *group;
357 npar_specs.pool = pool_create ();
358 npar_specs.filter = MV_ANY;
360 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
361 compare_vars_by_name, hash_var_by_name,
364 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
366 pool_destroy (npar_specs.pool);
370 for (i = 0; i < npar_specs.n_tests; ++i )
372 const struct npar_test *test = npar_specs.test[i];
373 test->insert_variables (test, var_hash);
376 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
377 npar_specs.n_vars = const_hsh_count (var_hash);
379 if ( cmd.statistics )
383 for ( i = 0 ; i < NPAR_ST_count; ++i )
385 if ( cmd.a_statistics[i] )
389 case NPAR_ST_DESCRIPTIVES:
390 npar_specs.descriptives = true;
392 case NPAR_ST_QUARTILES:
393 npar_specs.quartiles = true;
396 npar_specs.quartiles = true;
397 npar_specs.descriptives = true;
406 input = proc_open (ds);
407 if ( cmd.miss == MISS_LISTWISE )
409 input = casereader_create_filter_missing (input,
417 grouper = casegrouper_create_splits (input, dataset_dict (ds));
418 while (casegrouper_get_next_group (grouper, &group))
419 npar_execute (group, &npar_specs, ds);
420 ok = casegrouper_destroy (grouper);
421 ok = proc_commit (ds) && ok;
423 const_hsh_destroy (var_hash);
425 pool_destroy (npar_specs.pool);
427 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
431 npar_chisquare (struct lexer *lexer, struct dataset *ds,
432 struct npar_specs *specs)
434 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
435 struct one_sample_test *tp = &cstp->parent;
436 struct npar_test *nt = &tp->parent;
438 nt->execute = chisquare_execute;
439 nt->insert_variables = one_sample_insert_variables;
441 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
442 &tp->vars, &tp->n_vars,
443 PV_NO_SCRATCH | PV_NO_DUPLICATE))
448 cstp->ranged = false;
450 if ( lex_match (lexer, '('))
453 if ( ! lex_force_num (lexer)) return 0;
454 cstp->lo = lex_integer (lexer);
456 lex_force_match (lexer, ',');
457 if (! lex_force_num (lexer) ) return 0;
458 cstp->hi = lex_integer (lexer);
459 if ( cstp->lo >= cstp->hi )
462 _ ("The specified value of HI (%d) is "
463 "lower than the specified value of LO (%d)"),
468 if (! lex_force_match (lexer, ')')) return 0;
471 cstp->n_expected = 0;
472 cstp->expected = NULL;
473 if ( lex_match (lexer, '/') )
475 if ( lex_match_id (lexer, "EXPECTED") )
477 lex_force_match (lexer, '=');
478 if ( ! lex_match_id (lexer, "EQUAL") )
482 while ( lex_is_number (lexer) )
486 f = lex_number (lexer);
488 if ( lex_match (lexer, '*'))
491 f = lex_number (lexer);
494 lex_match (lexer, ',');
496 cstp->n_expected += n;
497 cstp->expected = pool_realloc (specs->pool,
501 for ( i = cstp->n_expected - n ;
502 i < cstp->n_expected;
504 cstp->expected[i] = f;
510 lex_put_back (lexer, '/');
513 if ( cstp->ranged && cstp->n_expected > 0 &&
514 cstp->n_expected != cstp->hi - cstp->lo + 1 )
517 _ ("%d expected values were given, but the specified "
518 "range (%d-%d) requires exactly %d values."),
519 cstp->n_expected, cstp->lo, cstp->hi,
520 cstp->hi - cstp->lo +1);
525 specs->test = pool_realloc (specs->pool,
527 sizeof (*specs->test) * specs->n_tests);
529 specs->test[specs->n_tests - 1] = nt;
536 npar_binomial (struct lexer *lexer, struct dataset *ds,
537 struct npar_specs *specs)
539 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
540 struct one_sample_test *tp = &btp->parent;
541 struct npar_test *nt = &tp->parent;
543 nt->execute = binomial_execute;
544 nt->insert_variables = one_sample_insert_variables;
546 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
550 if ( lex_match (lexer, '(') )
552 if ( lex_force_num (lexer) )
554 btp->p = lex_number (lexer);
556 lex_force_match (lexer, ')');
562 /* Kludge: q2c swallows the '=' so put it back here */
563 lex_put_back (lexer, '=');
565 if (lex_match (lexer, '=') )
567 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
568 &tp->vars, &tp->n_vars,
569 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
571 if (lex_match (lexer, '('))
573 lex_force_num (lexer);
574 btp->category1 = lex_number (lexer);
576 if ( lex_match (lexer, ','))
578 if ( ! lex_force_num (lexer) ) return 2;
579 btp->category2 = lex_number (lexer);
584 btp->cutpoint = btp->category1;
587 lex_force_match (lexer, ')');
596 specs->test = pool_realloc (specs->pool,
598 sizeof (*specs->test) * specs->n_tests);
600 specs->test[specs->n_tests - 1] = nt;
607 parse_two_sample_related_test (struct lexer *lexer,
608 const struct dictionary *dict,
609 struct two_sample_test *test_parameters,
615 parse_two_sample_related_test (struct lexer *lexer,
616 const struct dictionary *dict,
617 struct two_sample_test *test_parameters,
624 const struct variable **vlist1;
627 const struct variable **vlist2;
630 test_parameters->parent.insert_variables = two_sample_insert_variables;
632 if (!parse_variables_const_pool (lexer, pool,
635 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
638 if ( lex_match (lexer, T_WITH))
641 if ( !parse_variables_const_pool (lexer, pool, dict,
643 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
646 paired = (lex_match (lexer, '(') &&
647 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
655 if ( n_vlist1 != n_vlist2)
656 msg (SE, _ ("PAIRED was specified but the number of variables "
657 "preceding WITH (%zu) did not match the number "
658 "following (%zu)."), n_vlist1, n_vlist2);
660 test_parameters->n_pairs = n_vlist1 ;
664 test_parameters->n_pairs = n_vlist1 * n_vlist2;
669 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
672 test_parameters->pairs =
673 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
680 assert (n_vlist1 == n_vlist2);
681 for ( i = 0 ; i < n_vlist1; ++i )
683 test_parameters->pairs[n][1] = vlist1[i];
684 test_parameters->pairs[n][0] = vlist2[i];
691 for ( i = 0 ; i < n_vlist1; ++i )
693 for ( j = 0 ; j < n_vlist2; ++j )
695 test_parameters->pairs[n][1] = vlist1[i];
696 test_parameters->pairs[n][0] = vlist2[j];
705 for ( i = 0 ; i < n_vlist1 - 1; ++i )
707 for ( j = i + 1 ; j < n_vlist1; ++j )
709 assert ( n < test_parameters->n_pairs);
710 test_parameters->pairs[n][1] = vlist1[i];
711 test_parameters->pairs[n][0] = vlist1[j];
717 assert ( n == test_parameters->n_pairs);
724 npar_wilcoxon (struct lexer *lexer,
726 struct npar_specs *specs )
730 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
731 struct npar_test *nt = &tp->parent;
732 nt->execute = wilcoxon_execute;
734 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
739 specs->test = pool_realloc (specs->pool,
741 sizeof (*specs->test) * specs->n_tests);
742 specs->test[specs->n_tests - 1] = nt;
748 npar_sign (struct lexer *lexer, struct dataset *ds,
749 struct npar_specs *specs)
751 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
752 struct npar_test *nt = &tp->parent;
754 nt->execute = sign_execute;
756 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
761 specs->test = pool_realloc (specs->pool,
763 sizeof (*specs->test) * specs->n_tests);
764 specs->test[specs->n_tests - 1] = nt;
770 /* Insert the variables for TEST into VAR_HASH */
772 one_sample_insert_variables (const struct npar_test *test,
773 struct const_hsh_table *var_hash)
776 const struct one_sample_test *ost = UP_CAST (test, const struct one_sample_test, parent);
778 for ( i = 0 ; i < ost->n_vars ; ++i )
779 const_hsh_insert (var_hash, ost->vars[i]);
784 two_sample_insert_variables (const struct npar_test *test,
785 struct const_hsh_table *var_hash)
789 const struct two_sample_test *tst = UP_CAST (test, const struct two_sample_test, parent);
791 for ( i = 0 ; i < tst->n_pairs ; ++i )
793 variable_pair *pair = &tst->pairs[i];
795 const_hsh_insert (var_hash, (*pair)[0]);
796 const_hsh_insert (var_hash, (*pair)[1]);
801 npar_method (struct lexer *lexer, struct npar_specs *specs)
803 if ( lex_match_id (lexer, "EXACT") )
807 if (lex_match_id (lexer, "TIMER"))
811 if ( lex_match (lexer, '('))
813 if ( lex_force_num (lexer) )
815 specs->timer = lex_number (lexer);
818 lex_force_match (lexer, ')');