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)
53 /* Settings for subcommand specifiers. */
66 /* Array indices for STATISTICS subcommand. */
69 NPAR_ST_DESCRIPTIVES = 0,
70 NPAR_ST_QUARTILES = 1,
75 /* NPAR TESTS structure. */
78 /* CHISQUARE subcommand. */
81 /* BINOMIAL subcommand. */
84 /* WILCOXON subcommand. */
87 /* SIGN subcommand. */
90 /* MISSING subcommand. */
95 /* METHOD subcommand. */
98 /* STATISTICS subcommand. */
100 int a_statistics[NPAR_ST_count];
107 struct npar_test **test;
110 const struct variable **vv; /* Compendium of all variables
111 (those mentioned on ANY subcommand */
112 int n_vars; /* Number of variables in vv */
114 enum mv_class filter; /* Missing values to filter. */
116 bool descriptives; /* Descriptive statistics should be calculated */
117 bool quartiles; /* Quartiles should be calculated */
119 bool exact; /* Whether exact calculations have been requested */
120 double timer; /* Maximum time (in minutes) to wait for exact calculations */
124 /* Prototype for custom subcommands of NPAR TESTS. */
125 static int npar_chisquare (struct lexer *, struct dataset *, struct npar_specs *);
126 static int npar_binomial (struct lexer *, struct dataset *, struct npar_specs *);
127 static int npar_wilcoxon (struct lexer *, struct dataset *, struct npar_specs *);
128 static int npar_sign (struct lexer *, struct dataset *, struct npar_specs *);
129 static int npar_method (struct lexer *, struct npar_specs *);
131 /* Command parsing functions. */
132 static int parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *p,
133 struct npar_specs *npar_specs );
136 parse_npar_tests (struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *npt,
137 struct npar_specs *nps)
144 npt->miss = NPAR_ANALYSIS;
145 npt->incl = NPAR_EXCLUDE;
148 memset (npt->a_statistics, 0, sizeof npt->a_statistics);
151 if (lex_match_hyphenated_word (lexer, "CHISQUARE"))
153 lex_match (lexer, '=');
155 switch (npar_chisquare (lexer, ds, nps))
162 lex_error (lexer, NULL);
168 else if (lex_match_hyphenated_word (lexer, "BINOMIAL"))
170 lex_match (lexer, '=');
172 switch (npar_binomial (lexer, ds, nps))
179 lex_error (lexer, NULL);
185 else if (lex_match_hyphenated_word (lexer, "WILCOXON"))
187 lex_match (lexer, '=');
189 switch (npar_wilcoxon (lexer, ds, nps))
196 lex_error (lexer, NULL);
202 else if (lex_match_hyphenated_word (lexer, "SIGN"))
204 lex_match (lexer, '=');
206 switch (npar_sign (lexer, ds, nps))
213 lex_error (lexer, NULL);
219 else if (lex_match_hyphenated_word (lexer, "MISSING"))
221 lex_match (lexer, '=');
223 if (npt->missing > 1)
225 msg (SE, _ ("MISSING subcommand may be given only once."));
228 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
230 if (lex_match_hyphenated_word (lexer, "ANALYSIS"))
231 npt->miss = NPAR_ANALYSIS;
232 else if (lex_match_hyphenated_word (lexer, "LISTWISE"))
233 npt->miss = NPAR_LISTWISE;
234 else if (lex_match_hyphenated_word (lexer, "INCLUDE"))
235 npt->incl = NPAR_INCLUDE;
236 else if (lex_match_hyphenated_word (lexer, "EXCLUDE"))
237 npt->incl = NPAR_EXCLUDE;
240 lex_error (lexer, NULL);
243 lex_match (lexer, ',');
246 else if (lex_match_hyphenated_word (lexer, "METHOD"))
248 lex_match (lexer, '=');
252 msg (SE, _ ("METHOD subcommand may be given only once."));
255 switch (npar_method (lexer, nps))
262 lex_error (lexer, NULL);
268 else if (lex_match_hyphenated_word (lexer, "STATISTICS"))
270 lex_match (lexer, '=');
272 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
274 if (lex_match_hyphenated_word (lexer, "DESCRIPTIVES"))
275 npt->a_statistics[NPAR_ST_DESCRIPTIVES] = 1;
276 else if (lex_match_hyphenated_word (lexer, "QUARTILES"))
277 npt->a_statistics[NPAR_ST_QUARTILES] = 1;
278 else if (lex_match (lexer, T_ALL))
279 npt->a_statistics[NPAR_ST_ALL] = 1;
282 lex_error (lexer, NULL);
285 lex_match (lexer, ',');
288 else if ( settings_get_syntax () != COMPATIBLE && lex_match_id (lexer, "ALGORITHM"))
290 lex_match (lexer, '=');
291 if (lex_match_id (lexer, "COMPATIBLE"))
292 settings_set_cmd_algorithm (COMPATIBLE);
293 else if (lex_match_id (lexer, "ENHANCED"))
294 settings_set_cmd_algorithm (ENHANCED);
296 if (!lex_match (lexer, '/'))
300 if (lex_token (lexer) != '.')
302 lex_error (lexer, _ ("expecting end of command"));
315 static void one_sample_insert_variables (const struct npar_test *test,
316 struct const_hsh_table *variables);
318 static void two_sample_insert_variables (const struct npar_test *test,
319 struct const_hsh_table *variables);
323 npar_execute (struct casereader *input,
324 const struct npar_specs *specs,
325 const struct dataset *ds)
328 struct descriptives *summary_descriptives = NULL;
330 for ( t = 0 ; t < specs->n_tests; ++t )
332 const struct npar_test *test = specs->test[t];
333 if ( NULL == test->execute )
335 msg (SW, _ ("NPAR subcommand not currently implemented."));
338 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
341 if ( specs->descriptives )
343 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
346 npar_summary_calc_descriptives (summary_descriptives,
347 casereader_clone (input),
349 specs->vv, specs->n_vars,
353 if ( (specs->descriptives || specs->quartiles)
354 && !taint_has_tainted_successor (casereader_get_taint (input)) )
355 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
357 free (summary_descriptives);
358 casereader_destroy (input);
363 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
365 struct cmd_npar_tests cmd;
368 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
369 struct const_hsh_table *var_hash;
370 struct casegrouper *grouper;
371 struct casereader *input, *group;
373 npar_specs.pool = pool_create ();
375 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
376 compare_vars_by_name, hash_var_by_name,
379 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
381 pool_destroy (npar_specs.pool);
385 for (i = 0; i < npar_specs.n_tests; ++i )
387 const struct npar_test *test = npar_specs.test[i];
388 test->insert_variables (test, var_hash);
391 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
392 npar_specs.n_vars = const_hsh_count (var_hash);
394 if ( cmd.statistics )
398 for ( i = 0 ; i < NPAR_ST_count; ++i )
400 if ( cmd.a_statistics[i] )
404 case NPAR_ST_DESCRIPTIVES:
405 npar_specs.descriptives = true;
407 case NPAR_ST_QUARTILES:
408 npar_specs.quartiles = true;
411 npar_specs.quartiles = true;
412 npar_specs.descriptives = true;
421 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
423 input = proc_open (ds);
424 if ( cmd.miss == NPAR_LISTWISE )
426 input = casereader_create_filter_missing (input,
434 grouper = casegrouper_create_splits (input, dataset_dict (ds));
435 while (casegrouper_get_next_group (grouper, &group))
436 npar_execute (group, &npar_specs, ds);
437 ok = casegrouper_destroy (grouper);
438 ok = proc_commit (ds) && ok;
440 const_hsh_destroy (var_hash);
442 pool_destroy (npar_specs.pool);
444 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
448 npar_chisquare (struct lexer *lexer, struct dataset *ds,
449 struct npar_specs *specs)
451 struct chisquare_test *cstp = pool_alloc (specs->pool, sizeof (*cstp));
452 struct one_sample_test *tp = &cstp->parent;
453 struct npar_test *nt = &tp->parent;
455 nt->execute = chisquare_execute;
456 nt->insert_variables = one_sample_insert_variables;
458 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
459 &tp->vars, &tp->n_vars,
460 PV_NO_SCRATCH | PV_NO_DUPLICATE))
465 cstp->ranged = false;
467 if ( lex_match (lexer, '('))
470 if ( ! lex_force_num (lexer)) return 0;
471 cstp->lo = lex_integer (lexer);
473 lex_force_match (lexer, ',');
474 if (! lex_force_num (lexer) ) return 0;
475 cstp->hi = lex_integer (lexer);
476 if ( cstp->lo >= cstp->hi )
479 _ ("The specified value of HI (%d) is "
480 "lower than the specified value of LO (%d)"),
485 if (! lex_force_match (lexer, ')')) return 0;
488 cstp->n_expected = 0;
489 cstp->expected = NULL;
490 if ( lex_match (lexer, '/') )
492 if ( lex_match_id (lexer, "EXPECTED") )
494 lex_force_match (lexer, '=');
495 if ( ! lex_match_id (lexer, "EQUAL") )
499 while ( lex_is_number (lexer) )
503 f = lex_number (lexer);
505 if ( lex_match (lexer, '*'))
508 f = lex_number (lexer);
511 lex_match (lexer, ',');
513 cstp->n_expected += n;
514 cstp->expected = pool_realloc (specs->pool,
518 for ( i = cstp->n_expected - n ;
519 i < cstp->n_expected;
521 cstp->expected[i] = f;
527 lex_put_back (lexer, '/');
530 if ( cstp->ranged && cstp->n_expected > 0 &&
531 cstp->n_expected != cstp->hi - cstp->lo + 1 )
534 _ ("%d expected values were given, but the specified "
535 "range (%d-%d) requires exactly %d values."),
536 cstp->n_expected, cstp->lo, cstp->hi,
537 cstp->hi - cstp->lo +1);
542 specs->test = pool_realloc (specs->pool,
544 sizeof (*specs->test) * specs->n_tests);
546 specs->test[specs->n_tests - 1] = nt;
553 npar_binomial (struct lexer *lexer, struct dataset *ds,
554 struct npar_specs *specs)
556 struct binomial_test *btp = pool_alloc (specs->pool, sizeof (*btp));
557 struct one_sample_test *tp = &btp->parent;
558 struct npar_test *nt = &tp->parent;
560 nt->execute = binomial_execute;
561 nt->insert_variables = one_sample_insert_variables;
563 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
567 if ( lex_match (lexer, '(') )
569 if ( lex_force_num (lexer) )
571 btp->p = lex_number (lexer);
573 lex_force_match (lexer, ')');
579 /* Kludge: q2c swallows the '=' so put it back here */
580 lex_put_back (lexer, '=');
582 if (lex_match (lexer, '=') )
584 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
585 &tp->vars, &tp->n_vars,
586 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
588 if (lex_match (lexer, '('))
590 lex_force_num (lexer);
591 btp->category1 = lex_number (lexer);
593 if ( lex_match (lexer, ','))
595 if ( ! lex_force_num (lexer) ) return 2;
596 btp->category2 = lex_number (lexer);
601 btp->cutpoint = btp->category1;
604 lex_force_match (lexer, ')');
613 specs->test = pool_realloc (specs->pool,
615 sizeof (*specs->test) * specs->n_tests);
617 specs->test[specs->n_tests - 1] = nt;
624 parse_two_sample_related_test (struct lexer *lexer,
625 const struct dictionary *dict,
626 struct two_sample_test *test_parameters,
632 parse_two_sample_related_test (struct lexer *lexer,
633 const struct dictionary *dict,
634 struct two_sample_test *test_parameters,
641 const struct variable **vlist1;
644 const struct variable **vlist2;
647 test_parameters->parent.insert_variables = two_sample_insert_variables;
649 if (!parse_variables_const_pool (lexer, pool,
652 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
655 if ( lex_match (lexer, T_WITH))
658 if ( !parse_variables_const_pool (lexer, pool, dict,
660 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
663 paired = (lex_match (lexer, '(') &&
664 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
672 if ( n_vlist1 != n_vlist2)
673 msg (SE, _ ("PAIRED was specified but the number of variables "
674 "preceding WITH (%zu) did not match the number "
675 "following (%zu)."), n_vlist1, n_vlist2);
677 test_parameters->n_pairs = n_vlist1 ;
681 test_parameters->n_pairs = n_vlist1 * n_vlist2;
686 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
689 test_parameters->pairs =
690 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
697 assert (n_vlist1 == n_vlist2);
698 for ( i = 0 ; i < n_vlist1; ++i )
700 test_parameters->pairs[n][1] = vlist1[i];
701 test_parameters->pairs[n][0] = vlist2[i];
708 for ( i = 0 ; i < n_vlist1; ++i )
710 for ( j = 0 ; j < n_vlist2; ++j )
712 test_parameters->pairs[n][1] = vlist1[i];
713 test_parameters->pairs[n][0] = vlist2[j];
722 for ( i = 0 ; i < n_vlist1 - 1; ++i )
724 for ( j = i + 1 ; j < n_vlist1; ++j )
726 assert ( n < test_parameters->n_pairs);
727 test_parameters->pairs[n][1] = vlist1[i];
728 test_parameters->pairs[n][0] = vlist1[j];
734 assert ( n == test_parameters->n_pairs);
740 npar_wilcoxon (struct lexer *lexer,
742 struct npar_specs *specs )
746 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
747 struct npar_test *nt = &tp->parent;
748 nt->execute = wilcoxon_execute;
750 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
755 specs->test = pool_realloc (specs->pool,
757 sizeof (*specs->test) * specs->n_tests);
758 specs->test[specs->n_tests - 1] = nt;
764 npar_sign (struct lexer *lexer, struct dataset *ds,
765 struct npar_specs *specs)
767 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof (*tp));
768 struct npar_test *nt = &tp->parent;
770 nt->execute = sign_execute;
772 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
777 specs->test = pool_realloc (specs->pool,
779 sizeof (*specs->test) * specs->n_tests);
780 specs->test[specs->n_tests - 1] = nt;
785 /* Insert the variables for TEST into VAR_HASH */
787 one_sample_insert_variables (const struct npar_test *test,
788 struct const_hsh_table *var_hash)
791 struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
793 for ( i = 0 ; i < ost->n_vars ; ++i )
794 const_hsh_insert (var_hash, ost->vars[i]);
798 two_sample_insert_variables (const struct npar_test *test,
799 struct const_hsh_table *var_hash)
803 const struct two_sample_test *tst = (const struct two_sample_test *) test;
805 for ( i = 0 ; i < tst->n_pairs ; ++i )
807 variable_pair *pair = &tst->pairs[i];
809 const_hsh_insert (var_hash, (*pair)[0]);
810 const_hsh_insert (var_hash, (*pair)[1]);
815 npar_method (struct lexer *lexer, struct npar_specs *specs)
817 if ( lex_match_id (lexer, "EXACT") )
821 if (lex_match_id (lexer, "TIMER"))
825 if ( lex_match (lexer, '('))
827 if ( lex_force_num (lexer) )
829 specs->timer = lex_number (lexer);
832 lex_force_match (lexer, ')');