1 /* PSPP - a program for statistical analysis. -*-c-*-
2 Copyright (C) 2006, 2008, 2009 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/>. */
20 #include <language/stats/npar.h>
24 #include <data/case.h>
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <language/stats/binomial.h>
33 #include <language/stats/chisquare.h>
34 #include <language/stats/wilcoxon.h>
35 #include <language/stats/sign.h>
36 #include <libpspp/cast.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/pool.h>
39 #include <libpspp/taint.h>
40 #include <math/moments.h>
42 #include "npar-summary.h"
45 #define _(msgid) gettext (msgid)
59 missing=miss:!analysis/listwise,
60 incl:include/!exclude;
62 +statistics[st_]=descriptives,quartiles,all.
68 static struct cmd_npar_tests cmd;
74 struct npar_test **test;
77 const struct variable ** vv; /* Compendium of all variables
78 (those mentioned on ANY subcommand */
79 int n_vars; /* Number of variables in vv */
81 enum mv_class filter; /* Missing values to filter. */
83 bool descriptives; /* Descriptive statistics should be calculated */
84 bool quartiles; /* Quartiles should be calculated */
86 bool exact; /* Whether exact calculations have been requested */
87 double timer; /* Maximum time (in minutes) to wait for exact calculations */
90 static void one_sample_insert_variables (const struct npar_test *test,
91 struct const_hsh_table *variables);
93 static void two_sample_insert_variables (const struct npar_test *test,
94 struct const_hsh_table *variables);
99 npar_execute(struct casereader *input,
100 const struct npar_specs *specs,
101 const struct dataset *ds)
104 struct descriptives *summary_descriptives = NULL;
106 for ( t = 0 ; t < specs->n_tests; ++t )
108 const struct npar_test *test = specs->test[t];
109 if ( NULL == test->execute )
111 msg (SW, _("NPAR subcommand not currently implemented."));
114 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
117 if ( specs->descriptives )
119 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
122 npar_summary_calc_descriptives (summary_descriptives,
123 casereader_clone (input),
125 specs->vv, specs->n_vars,
129 if ( (specs->descriptives || specs->quartiles)
130 && !taint_has_tainted_successor (casereader_get_taint (input)) )
131 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
133 free (summary_descriptives);
134 casereader_destroy (input);
139 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
143 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
144 struct const_hsh_table *var_hash;
145 struct casegrouper *grouper;
146 struct casereader *input, *group;
148 npar_specs.pool = pool_create ();
150 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
151 compare_vars_by_name, hash_var_by_name,
154 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
156 pool_destroy (npar_specs.pool);
160 for (i = 0; i < npar_specs.n_tests; ++i )
162 const struct npar_test *test = npar_specs.test[i];
163 test->insert_variables (test, var_hash);
166 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
167 npar_specs.n_vars = const_hsh_count (var_hash);
169 if ( cmd.sbc_statistics )
173 for ( i = 0 ; i < NPAR_ST_count; ++i )
175 if ( cmd.a_statistics[i] )
179 case NPAR_ST_DESCRIPTIVES:
180 npar_specs.descriptives = true;
182 case NPAR_ST_QUARTILES:
183 npar_specs.quartiles = true;
186 npar_specs.quartiles = true;
187 npar_specs.descriptives = true;
196 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
198 input = proc_open (ds);
199 if ( cmd.miss == NPAR_LISTWISE )
201 input = casereader_create_filter_missing (input,
209 grouper = casegrouper_create_splits (input, dataset_dict (ds));
210 while (casegrouper_get_next_group (grouper, &group))
211 npar_execute (group, &npar_specs, ds);
212 ok = casegrouper_destroy (grouper);
213 ok = proc_commit (ds) && ok;
215 const_hsh_destroy (var_hash);
217 pool_destroy (npar_specs.pool);
219 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
223 npar_custom_chisquare (struct lexer *lexer, struct dataset *ds,
224 struct cmd_npar_tests *cmd UNUSED, void *aux )
226 struct npar_specs *specs = aux;
228 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
229 struct one_sample_test *tp = &cstp->parent;
230 struct npar_test *nt = &tp->parent;
232 nt->execute = chisquare_execute;
233 nt->insert_variables = one_sample_insert_variables;
235 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
236 &tp->vars, &tp->n_vars,
237 PV_NO_SCRATCH | PV_NO_DUPLICATE))
242 cstp->ranged = false;
244 if ( lex_match (lexer, '('))
247 if ( ! lex_force_num (lexer)) return 0;
248 cstp->lo = lex_integer (lexer);
250 lex_force_match (lexer, ',');
251 if (! lex_force_num (lexer) ) return 0;
252 cstp->hi = lex_integer (lexer);
253 if ( cstp->lo >= cstp->hi )
256 _("The specified value of HI (%d) is "
257 "lower than the specified value of LO (%d)"),
262 if (! lex_force_match (lexer, ')')) return 0;
265 cstp->n_expected = 0;
266 cstp->expected = NULL;
267 if ( lex_match (lexer, '/') )
269 if ( lex_match_id (lexer, "EXPECTED") )
271 lex_force_match (lexer, '=');
272 if ( ! lex_match_id (lexer, "EQUAL") )
276 while ( lex_is_number(lexer) )
280 f = lex_number (lexer);
282 if ( lex_match (lexer, '*'))
285 f = lex_number (lexer);
288 lex_match (lexer, ',');
290 cstp->n_expected += n;
291 cstp->expected = pool_realloc (specs->pool,
295 for ( i = cstp->n_expected - n ;
296 i < cstp->n_expected;
298 cstp->expected[i] = f;
304 lex_put_back (lexer, '/');
307 if ( cstp->ranged && cstp->n_expected > 0 &&
308 cstp->n_expected != cstp->hi - cstp->lo + 1 )
311 _("%d expected values were given, but the specified "
312 "range (%d-%d) requires exactly %d values."),
313 cstp->n_expected, cstp->lo, cstp->hi,
314 cstp->hi - cstp->lo +1);
319 specs->test = pool_realloc (specs->pool,
321 sizeof(*specs->test) * specs->n_tests);
323 specs->test[specs->n_tests - 1] = nt;
330 npar_custom_binomial (struct lexer *lexer, struct dataset *ds,
331 struct cmd_npar_tests *cmd UNUSED, void *aux)
333 struct npar_specs *specs = aux;
334 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
335 struct one_sample_test *tp = &btp->parent;
336 struct npar_test *nt = &tp->parent;
338 nt->execute = binomial_execute;
339 nt->insert_variables = one_sample_insert_variables;
341 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
345 if ( lex_match (lexer, '(') )
347 if ( lex_force_num (lexer) )
349 btp->p = lex_number (lexer);
351 lex_force_match (lexer, ')');
357 /* Kludge: q2c swallows the '=' so put it back here */
358 lex_put_back (lexer, '=');
361 if ( lex_match (lexer, '=') )
363 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
364 &tp->vars, &tp->n_vars,
365 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
367 if ( lex_match (lexer, '('))
369 lex_force_num (lexer);
370 btp->category1 = lex_number (lexer);
372 if ( lex_match (lexer, ','))
374 if ( ! lex_force_num (lexer) ) return 2;
375 btp->category2 = lex_number (lexer);
380 btp->cutpoint = btp->category1;
383 lex_force_match (lexer, ')');
392 specs->test = pool_realloc (specs->pool,
394 sizeof(*specs->test) * specs->n_tests);
396 specs->test[specs->n_tests - 1] = nt;
402 bool parse_two_sample_related_test (struct lexer *lexer,
403 const struct dictionary *dict,
404 struct cmd_npar_tests *cmd,
405 struct two_sample_test *test_parameters,
411 parse_two_sample_related_test (struct lexer *lexer,
412 const struct dictionary *dict,
413 struct cmd_npar_tests *cmd UNUSED,
414 struct two_sample_test *test_parameters,
421 const struct variable **vlist1;
424 const struct variable **vlist2;
427 test_parameters->parent.insert_variables = two_sample_insert_variables;
429 if (!parse_variables_const_pool (lexer, pool,
432 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
435 if ( lex_match(lexer, T_WITH))
438 if ( !parse_variables_const_pool (lexer, pool, dict,
440 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
443 paired = (lex_match (lexer, '(') &&
444 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
452 if ( n_vlist1 != n_vlist2)
453 msg (SE, _("PAIRED was specified but the number of variables "
454 "preceding WITH (%zu) did not match the number "
455 "following (%zu)."), n_vlist1, n_vlist2);
457 test_parameters->n_pairs = n_vlist1 ;
461 test_parameters->n_pairs = n_vlist1 * n_vlist2;
466 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
469 test_parameters->pairs =
470 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
477 assert (n_vlist1 == n_vlist2);
478 for ( i = 0 ; i < n_vlist1; ++i )
480 test_parameters->pairs[n][1] = vlist1[i];
481 test_parameters->pairs[n][0] = vlist2[i];
488 for ( i = 0 ; i < n_vlist1; ++i )
490 for ( j = 0 ; j < n_vlist2; ++j )
492 test_parameters->pairs[n][1] = vlist1[i];
493 test_parameters->pairs[n][0] = vlist2[j];
502 for ( i = 0 ; i < n_vlist1 - 1; ++i )
504 for ( j = i + 1 ; j < n_vlist1; ++j )
506 assert ( n < test_parameters->n_pairs);
507 test_parameters->pairs[n][1] = vlist1[i];
508 test_parameters->pairs[n][0] = vlist1[j];
514 assert ( n == test_parameters->n_pairs);
520 npar_custom_wilcoxon (struct lexer *lexer,
522 struct cmd_npar_tests *cmd, void *aux )
524 struct npar_specs *specs = aux;
526 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof(*tp));
527 struct npar_test *nt = &tp->parent;
528 nt->execute = wilcoxon_execute;
530 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
535 specs->test = pool_realloc (specs->pool,
537 sizeof(*specs->test) * specs->n_tests);
538 specs->test[specs->n_tests - 1] = nt;
544 npar_custom_mcnemar (struct lexer *lexer,
546 struct cmd_npar_tests *cmd, void *aux )
548 struct npar_specs *specs = aux;
550 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
551 struct npar_test *nt = &tp->parent;
555 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
556 cmd, tp, specs->pool) )
560 specs->test = pool_realloc (specs->pool,
562 sizeof(*specs->test) * specs->n_tests);
563 specs->test[specs->n_tests - 1] = nt;
569 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
570 struct cmd_npar_tests *cmd, void *aux )
572 struct npar_specs *specs = aux;
574 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
575 struct npar_test *nt = &tp->parent;
577 nt->execute = sign_execute;
579 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
584 specs->test = pool_realloc (specs->pool,
586 sizeof(*specs->test) * specs->n_tests);
587 specs->test[specs->n_tests - 1] = nt;
592 /* Insert the variables for TEST into VAR_HASH */
594 one_sample_insert_variables (const struct npar_test *test,
595 struct const_hsh_table *var_hash)
598 struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
600 for ( i = 0 ; i < ost->n_vars ; ++i )
601 const_hsh_insert (var_hash, ost->vars[i]);
605 two_sample_insert_variables (const struct npar_test *test,
606 struct const_hsh_table *var_hash)
610 const struct two_sample_test *tst = (const struct two_sample_test *) test;
612 for ( i = 0 ; i < tst->n_pairs ; ++i )
614 variable_pair *pair = &tst->pairs[i];
616 const_hsh_insert (var_hash, (*pair)[0]);
617 const_hsh_insert (var_hash, (*pair)[1]);
624 npar_custom_method (struct lexer *lexer, struct dataset *ds UNUSED,
625 struct cmd_npar_tests *test UNUSED, void *aux)
627 struct npar_specs *specs = aux;
629 if ( lex_match_id (lexer, "EXACT") )
633 if (lex_match_id (lexer, "TIMER"))
637 if ( lex_match (lexer, '('))
639 if ( lex_force_num (lexer) )
641 specs->timer = lex_number (lexer);
644 lex_force_match (lexer, ')');