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/>. */
19 #include <language/stats/npar.h>
23 #include <data/case.h>
24 #include <data/casegrouper.h>
25 #include <data/casereader.h>
26 #include <data/dictionary.h>
27 #include <data/procedure.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <language/stats/binomial.h>
32 #include <language/stats/chisquare.h>
33 #include <language/stats/wilcoxon.h>
34 #include <language/stats/sign.h>
35 #include <libpspp/cast.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/pool.h>
38 #include <libpspp/taint.h>
39 #include <math/moments.h>
41 #include "npar-summary.h"
44 #define _(msgid) gettext (msgid)
58 missing=miss:!analysis/listwise,
59 incl:include/!exclude;
61 +statistics[st_]=descriptives,quartiles,all.
67 static struct cmd_npar_tests cmd;
73 struct npar_test **test;
76 const struct variable ** vv; /* Compendium of all variables
77 (those mentioned on ANY subcommand */
78 int n_vars; /* Number of variables in vv */
80 enum mv_class filter; /* Missing values to filter. */
82 bool descriptives; /* Descriptive statistics should be calculated */
83 bool quartiles; /* Quartiles should be calculated */
85 bool exact; /* Whether exact calculations have been requested */
86 double timer; /* Maximum time (in minutes) to wait for exact calculations */
89 static void one_sample_insert_variables (const struct npar_test *test,
90 struct const_hsh_table *variables);
92 static void two_sample_insert_variables (const struct npar_test *test,
93 struct const_hsh_table *variables);
98 npar_execute(struct casereader *input,
99 const struct npar_specs *specs,
100 const struct dataset *ds)
103 struct descriptives *summary_descriptives = NULL;
105 for ( t = 0 ; t < specs->n_tests; ++t )
107 const struct npar_test *test = specs->test[t];
108 if ( NULL == test->execute )
110 msg (SW, _("NPAR subcommand not currently implemented."));
113 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
116 if ( specs->descriptives )
118 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
121 npar_summary_calc_descriptives (summary_descriptives,
122 casereader_clone (input),
124 specs->vv, specs->n_vars,
128 if ( (specs->descriptives || specs->quartiles)
129 && !taint_has_tainted_successor (casereader_get_taint (input)) )
130 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
132 free (summary_descriptives);
133 casereader_destroy (input);
137 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
141 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
142 struct const_hsh_table *var_hash;
143 struct casegrouper *grouper;
144 struct casereader *input, *group;
146 npar_specs.pool = pool_create ();
148 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
149 compare_vars_by_name, hash_var_by_name,
152 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
154 pool_destroy (npar_specs.pool);
158 for (i = 0; i < npar_specs.n_tests; ++i )
160 const struct npar_test *test = npar_specs.test[i];
161 test->insert_variables (test, var_hash);
164 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
165 npar_specs.n_vars = const_hsh_count (var_hash);
167 if ( cmd.sbc_statistics )
171 for ( i = 0 ; i < NPAR_ST_count; ++i )
173 if ( cmd.a_statistics[i] )
177 case NPAR_ST_DESCRIPTIVES:
178 npar_specs.descriptives = true;
180 case NPAR_ST_QUARTILES:
181 npar_specs.quartiles = true;
184 npar_specs.quartiles = true;
185 npar_specs.descriptives = true;
194 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
196 input = proc_open (ds);
197 if ( cmd.miss == NPAR_LISTWISE )
199 input = casereader_create_filter_missing (input,
207 grouper = casegrouper_create_splits (input, dataset_dict (ds));
208 while (casegrouper_get_next_group (grouper, &group))
209 npar_execute (group, &npar_specs, ds);
210 ok = casegrouper_destroy (grouper);
211 ok = proc_commit (ds) && ok;
213 const_hsh_destroy (var_hash);
215 pool_destroy (npar_specs.pool);
217 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
221 npar_custom_chisquare (struct lexer *lexer, struct dataset *ds,
222 struct cmd_npar_tests *cmd UNUSED, void *aux )
224 struct npar_specs *specs = aux;
226 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
227 struct one_sample_test *tp = &cstp->parent;
228 struct npar_test *nt = &tp->parent;
230 nt->execute = chisquare_execute;
231 nt->insert_variables = one_sample_insert_variables;
233 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
234 &tp->vars, &tp->n_vars,
235 PV_NO_SCRATCH | PV_NO_DUPLICATE))
240 cstp->ranged = false;
242 if ( lex_match (lexer, '('))
245 if ( ! lex_force_num (lexer)) return 0;
246 cstp->lo = lex_integer (lexer);
248 lex_force_match (lexer, ',');
249 if (! lex_force_num (lexer) ) return 0;
250 cstp->hi = lex_integer (lexer);
251 if ( cstp->lo >= cstp->hi )
254 _("The specified value of HI (%d) is "
255 "lower than the specified value of LO (%d)"),
260 if (! lex_force_match (lexer, ')')) return 0;
263 cstp->n_expected = 0;
264 cstp->expected = NULL;
265 if ( lex_match (lexer, '/') )
267 if ( lex_match_id (lexer, "EXPECTED") )
269 lex_force_match (lexer, '=');
270 if ( ! lex_match_id (lexer, "EQUAL") )
274 while ( lex_is_number(lexer) )
278 f = lex_number (lexer);
280 if ( lex_match (lexer, '*'))
283 f = lex_number (lexer);
286 lex_match (lexer, ',');
288 cstp->n_expected += n;
289 cstp->expected = pool_realloc (specs->pool,
293 for ( i = cstp->n_expected - n ;
294 i < cstp->n_expected;
296 cstp->expected[i] = f;
302 lex_put_back (lexer, '/');
305 if ( cstp->ranged && cstp->n_expected > 0 &&
306 cstp->n_expected != cstp->hi - cstp->lo + 1 )
309 _("%d expected values were given, but the specified "
310 "range (%d-%d) requires exactly %d values."),
311 cstp->n_expected, cstp->lo, cstp->hi,
312 cstp->hi - cstp->lo +1);
317 specs->test = pool_realloc (specs->pool,
319 sizeof(*specs->test) * specs->n_tests);
321 specs->test[specs->n_tests - 1] = nt;
328 npar_custom_binomial (struct lexer *lexer, struct dataset *ds,
329 struct cmd_npar_tests *cmd UNUSED, void *aux)
331 struct npar_specs *specs = aux;
332 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
333 struct one_sample_test *tp = &btp->parent;
334 struct npar_test *nt = &tp->parent;
336 nt->execute = binomial_execute;
337 nt->insert_variables = one_sample_insert_variables;
339 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
341 if ( lex_match(lexer, '(') )
343 if ( lex_force_num (lexer) )
345 btp->p = lex_number (lexer);
347 lex_force_match (lexer, ')');
353 if ( lex_match (lexer, '=') )
355 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
356 &tp->vars, &tp->n_vars,
357 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
359 if ( lex_match (lexer, '('))
361 lex_force_num (lexer);
362 btp->category1 = lex_number (lexer);
364 if ( lex_match (lexer, ','))
366 if ( ! lex_force_num (lexer) ) return 2;
367 btp->category2 = lex_number (lexer);
372 btp->cutpoint = btp->category1;
375 lex_force_match (lexer, ')');
383 specs->test = pool_realloc (specs->pool,
385 sizeof(*specs->test) * specs->n_tests);
387 specs->test[specs->n_tests - 1] = nt;
393 bool parse_two_sample_related_test (struct lexer *lexer,
394 const struct dictionary *dict,
395 struct cmd_npar_tests *cmd,
396 struct two_sample_test *test_parameters,
402 parse_two_sample_related_test (struct lexer *lexer,
403 const struct dictionary *dict,
404 struct cmd_npar_tests *cmd UNUSED,
405 struct two_sample_test *test_parameters,
412 const struct variable **vlist1;
415 const struct variable **vlist2;
418 test_parameters->parent.insert_variables = two_sample_insert_variables;
420 if (!parse_variables_const_pool (lexer, pool,
423 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
426 if ( lex_match(lexer, T_WITH))
429 if ( !parse_variables_const_pool (lexer, pool, dict,
431 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
434 paired = (lex_match (lexer, '(') &&
435 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
443 if ( n_vlist1 != n_vlist2)
444 msg (SE, _("PAIRED was specified but the number of variables "
445 "preceding WITH (%zu) did not match the number "
446 "following (%zu)."), n_vlist1, n_vlist2);
448 test_parameters->n_pairs = n_vlist1 ;
452 test_parameters->n_pairs = n_vlist1 * n_vlist2;
457 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
460 test_parameters->pairs =
461 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
468 assert (n_vlist1 == n_vlist2);
469 for ( i = 0 ; i < n_vlist1; ++i )
471 test_parameters->pairs[n][1] = vlist1[i];
472 test_parameters->pairs[n][0] = vlist2[i];
479 for ( i = 0 ; i < n_vlist1; ++i )
481 for ( j = 0 ; j < n_vlist2; ++j )
483 test_parameters->pairs[n][1] = vlist1[i];
484 test_parameters->pairs[n][0] = vlist2[j];
493 for ( i = 0 ; i < n_vlist1 - 1; ++i )
495 for ( j = i + 1 ; j < n_vlist1; ++j )
497 assert ( n < test_parameters->n_pairs);
498 test_parameters->pairs[n][1] = vlist1[i];
499 test_parameters->pairs[n][0] = vlist1[j];
505 assert ( n == test_parameters->n_pairs);
511 npar_custom_wilcoxon (struct lexer *lexer,
513 struct cmd_npar_tests *cmd, void *aux )
515 struct npar_specs *specs = aux;
517 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof(*tp));
518 struct npar_test *nt = &tp->parent;
519 nt->execute = wilcoxon_execute;
521 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
526 specs->test = pool_realloc (specs->pool,
528 sizeof(*specs->test) * specs->n_tests);
529 specs->test[specs->n_tests - 1] = nt;
535 npar_custom_mcnemar (struct lexer *lexer,
537 struct cmd_npar_tests *cmd, void *aux )
539 struct npar_specs *specs = aux;
541 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
542 struct npar_test *nt = &tp->parent;
546 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
547 cmd, tp, specs->pool) )
551 specs->test = pool_realloc (specs->pool,
553 sizeof(*specs->test) * specs->n_tests);
554 specs->test[specs->n_tests - 1] = nt;
560 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
561 struct cmd_npar_tests *cmd, void *aux )
563 struct npar_specs *specs = aux;
565 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
566 struct npar_test *nt = &tp->parent;
568 nt->execute = sign_execute;
570 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
575 specs->test = pool_realloc (specs->pool,
577 sizeof(*specs->test) * specs->n_tests);
578 specs->test[specs->n_tests - 1] = nt;
583 /* Insert the variables for TEST into VAR_HASH */
585 one_sample_insert_variables (const struct npar_test *test,
586 struct const_hsh_table *var_hash)
589 struct one_sample_test *ost = UP_CAST (test, struct one_sample_test, parent);
591 for ( i = 0 ; i < ost->n_vars ; ++i )
592 const_hsh_insert (var_hash, ost->vars[i]);
596 two_sample_insert_variables (const struct npar_test *test,
597 struct const_hsh_table *var_hash)
601 const struct two_sample_test *tst = (const struct two_sample_test *) test;
603 for ( i = 0 ; i < tst->n_pairs ; ++i )
605 variable_pair *pair = &tst->pairs[i];
607 const_hsh_insert (var_hash, (*pair)[0]);
608 const_hsh_insert (var_hash, (*pair)[1]);
615 npar_custom_method (struct lexer *lexer, struct dataset *ds UNUSED,
616 struct cmd_npar_tests *test UNUSED, void *aux)
618 struct npar_specs *specs = aux;
620 if ( lex_match_id (lexer, "EXACT") )
624 if (lex_match_id (lexer, "TIMER"))
628 if ( lex_match (lexer, '('))
630 if ( lex_force_num (lexer) )
632 specs->timer = lex_number (lexer);
635 lex_force_match (lexer, ')');