1 /* PSPP - NPAR TESTS. -*-c-*-
3 Copyright (C) 2006 Free Software Foundation, Inc.
4 Author: John Darrington <john@darrington.wattle.id.au>,
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <language/stats/npar.h>
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <language/command.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <language/stats/binomial.h>
36 #include <language/stats/chisquare.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;
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 */
86 void one_sample_insert_variables (const struct npar_test *test,
87 struct const_hsh_table *variables);
90 npar_execute(struct casereader *input,
91 const struct npar_specs *specs,
92 const struct dataset *ds)
95 struct descriptives *summary_descriptives = NULL;
97 for ( t = 0 ; t < specs->n_tests; ++t )
99 const struct npar_test *test = specs->test[t];
100 if ( NULL == test->execute )
102 msg (SW, _("NPAR subcommand not currently implemented."));
105 test->execute (ds, casereader_clone (input), specs->filter, test);
108 if ( specs->descriptives )
110 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
113 npar_summary_calc_descriptives (summary_descriptives,
114 casereader_clone (input),
116 specs->vv, specs->n_vars,
120 if ( (specs->descriptives || specs->quartiles)
121 && !taint_has_tainted_successor (casereader_get_taint (input)) )
122 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
124 free (summary_descriptives);
125 casereader_destroy (input);
129 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
133 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0};
134 struct const_hsh_table *var_hash;
135 struct casegrouper *grouper;
136 struct casereader *input, *group;
138 npar_specs.pool = pool_create ();
140 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
141 compare_vars_by_name, hash_var_by_name,
144 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
146 pool_destroy (npar_specs.pool);
150 for (i = 0; i < npar_specs.n_tests; ++i )
152 const struct npar_test *test = npar_specs.test[i];
153 test->insert_variables (test, var_hash);
156 npar_specs.vv = (const struct variable **) const_hsh_data (var_hash);
157 npar_specs.n_vars = const_hsh_count (var_hash);
159 if ( cmd.sbc_statistics )
163 for ( i = 0 ; i < NPAR_ST_count; ++i )
165 if ( cmd.a_statistics[i] )
169 case NPAR_ST_DESCRIPTIVES:
170 npar_specs.descriptives = true;
172 case NPAR_ST_QUARTILES:
173 npar_specs.quartiles = true;
176 npar_specs.quartiles = true;
177 npar_specs.descriptives = true;
186 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
188 input = proc_open (ds);
189 if ( cmd.miss == NPAR_LISTWISE )
190 input = casereader_create_filter_missing (input,
193 npar_specs.filter, NULL);
195 grouper = casegrouper_create_splits (input, dataset_dict (ds));
196 while (casegrouper_get_next_group (grouper, &group))
197 npar_execute (group, &npar_specs, ds);
198 ok = casegrouper_destroy (grouper);
199 ok = proc_commit (ds) && ok;
201 const_hsh_destroy (var_hash);
203 pool_destroy (npar_specs.pool);
205 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
209 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
211 struct npar_specs *specs = aux;
213 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
214 struct one_sample_test *tp = (struct one_sample_test *) cstp;
216 ((struct npar_test *)tp)->execute = chisquare_execute;
217 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
219 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
220 &tp->vars, &tp->n_vars,
221 PV_NO_SCRATCH | PV_NO_DUPLICATE))
226 cstp->ranged = false;
228 if ( lex_match (lexer, '('))
231 if ( ! lex_force_num (lexer)) return 0;
232 cstp->lo = lex_integer (lexer);
234 lex_force_match (lexer, ',');
235 if (! lex_force_num (lexer) ) return 0;
236 cstp->hi = lex_integer (lexer);
237 if ( cstp->lo >= cstp->hi )
240 _("The specified value of HI (%d) is "
241 "lower than the specified value of LO (%d)"),
246 if (! lex_force_match (lexer, ')')) return 0;
249 cstp->n_expected = 0;
250 cstp->expected = NULL;
251 if ( lex_match (lexer, '/') )
253 if ( lex_match_id (lexer, "EXPECTED") )
255 lex_force_match (lexer, '=');
256 if ( ! lex_match_id (lexer, "EQUAL") )
260 while ( lex_is_number(lexer) )
264 f = lex_number (lexer);
266 if ( lex_match (lexer, '*'))
269 f = lex_number (lexer);
272 lex_match (lexer, ',');
274 cstp->n_expected += n;
275 cstp->expected = pool_realloc (specs->pool,
279 for ( i = cstp->n_expected - n ;
280 i < cstp->n_expected;
282 cstp->expected[i] = f;
288 lex_put_back (lexer, '/');
291 if ( cstp->ranged && cstp->n_expected > 0 &&
292 cstp->n_expected != cstp->hi - cstp->lo + 1 )
295 _("%d expected values were given, but the specified "
296 "range (%d-%d) requires exactly %d values."),
297 cstp->n_expected, cstp->lo, cstp->hi,
298 cstp->hi - cstp->lo +1);
303 specs->test = pool_realloc (specs->pool,
305 sizeof(*specs->test) * specs->n_tests);
307 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
314 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
316 struct npar_specs *specs = aux;
317 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
318 struct one_sample_test *tp = (struct one_sample_test *) btp;
320 ((struct npar_test *)tp)->execute = binomial_execute;
321 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
323 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
325 if ( lex_match(lexer, '(') )
327 if ( lex_force_num (lexer) )
329 btp->p = lex_number (lexer);
331 lex_force_match (lexer, ')');
337 if ( lex_match (lexer, '=') )
339 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
340 &tp->vars, &tp->n_vars,
341 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
343 if ( lex_match (lexer, '('))
345 lex_force_num (lexer);
346 btp->category1 = lex_number (lexer);
348 if ( ! lex_force_match (lexer, ',')) return 2;
349 if ( ! lex_force_num (lexer) ) return 2;
350 btp->category2 = lex_number (lexer);
352 lex_force_match (lexer, ')');
360 if ( lex_match (lexer, '(') )
362 lex_force_num (lexer);
363 btp->cutpoint = lex_number (lexer);
365 lex_force_match (lexer, ')');
370 specs->test = pool_realloc (specs->pool,
372 sizeof(*specs->test) * specs->n_tests);
374 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
380 bool parse_two_sample_related_test (struct lexer *lexer,
381 const struct dictionary *dict,
382 struct cmd_npar_tests *cmd,
383 struct two_sample_test *test_parameters,
389 parse_two_sample_related_test (struct lexer *lexer,
390 const struct dictionary *dict,
391 struct cmd_npar_tests *cmd UNUSED,
392 struct two_sample_test *test_parameters,
399 const struct variable **vlist1;
402 const struct variable **vlist2;
405 if (!parse_variables_const_pool (lexer, pool,
408 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
411 if ( lex_match(lexer, T_WITH))
414 if ( !parse_variables_const_pool (lexer, pool, dict,
416 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
419 paired = (lex_match (lexer, '(') &&
420 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
428 if ( n_vlist1 != n_vlist2)
429 msg (SE, _("PAIRED was specified but the number of variables "
430 "preceding WITH (%d) did not match the number "
431 "following (%d)."), (int) n_vlist1, (int) n_vlist2);
433 test_parameters->n_pairs = n_vlist1 ;
437 test_parameters->n_pairs = n_vlist1 * n_vlist2;
442 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
445 test_parameters->pairs =
446 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
453 assert (n_vlist1 == n_vlist2);
454 for ( i = 0 ; i < n_vlist1; ++i )
456 test_parameters->pairs[n][0] = vlist1[i];
457 test_parameters->pairs[n][1] = vlist2[i];
464 for ( i = 0 ; i < n_vlist1; ++i )
466 for ( j = 0 ; j < n_vlist2; ++j )
468 test_parameters->pairs[n][0] = vlist1[i];
469 test_parameters->pairs[n][1] = vlist2[j];
478 for ( i = 0 ; i < n_vlist1 - 1; ++i )
480 for ( j = i + 1 ; j < n_vlist1; ++j )
482 assert ( n < test_parameters->n_pairs);
483 test_parameters->pairs[n][0] = vlist1[i];
484 test_parameters->pairs[n][1] = vlist1[j];
490 assert ( n == test_parameters->n_pairs);
496 npar_custom_wilcoxon (struct lexer *lexer,
498 struct cmd_npar_tests *cmd, void *aux )
500 struct npar_specs *specs = aux;
502 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
503 ((struct npar_test *)tp)->execute = NULL;
505 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
510 specs->test = pool_realloc (specs->pool,
512 sizeof(*specs->test) * specs->n_tests);
513 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
519 npar_custom_mcnemar (struct lexer *lexer,
521 struct cmd_npar_tests *cmd, void *aux )
523 struct npar_specs *specs = aux;
525 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
526 ((struct npar_test *)tp)->execute = NULL;
529 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
530 cmd, tp, specs->pool) )
534 specs->test = pool_realloc (specs->pool,
536 sizeof(*specs->test) * specs->n_tests);
537 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
543 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
544 struct cmd_npar_tests *cmd, void *aux )
546 struct npar_specs *specs = aux;
548 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
549 ((struct npar_test *)tp)->execute = NULL;
552 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
557 specs->test = pool_realloc (specs->pool,
559 sizeof(*specs->test) * specs->n_tests);
560 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
565 /* Insert the variables for TEST into VAR_HASH */
567 one_sample_insert_variables (const struct npar_test *test,
568 struct const_hsh_table *var_hash)
571 struct one_sample_test *ost = (struct one_sample_test *) test;
573 for ( i = 0 ; i < ost->n_vars ; ++i )
574 const_hsh_insert (var_hash, ost->vars[i]);