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/lexer/lexer.h>
24 #include <language/lexer/variable-parser.h>
25 #include <language/command.h>
26 #include <data/procedure.h>
27 #include <libpspp/pool.h>
28 #include <libpspp/hash.h>
30 #include <data/casefilter.h>
31 #include <data/case.h>
32 #include <data/casefile.h>
33 #include <math/moments.h>
34 #include <data/dictionary.h>
35 #include <language/stats/chisquare.h>
36 #include <language/stats/binomial.h>
40 #include "npar-summary.h"
43 #define _(msgid) gettext (msgid)
57 missing=miss:!analysis/listwise,
58 incl:include/!exclude;
59 +statistics[st_]=descriptives,quartiles,all.
65 static struct cmd_npar_tests cmd;
71 struct npar_test **test;
74 const struct variable *const* vv; /* Compendium of all variables
75 (those mentioned on ANY subcommand */
76 int n_vars; /* Number of variables in vv */
78 struct casefilter *filter; /* The missing value filter */
80 bool descriptives; /* Descriptive statistics should be calculated */
81 bool quartiles; /* Quartiles should be calculated */
84 void one_sample_insert_variables (const struct npar_test *test,
85 struct hsh_table *variables);
88 npar_execute(const struct ccase *first UNUSED,
89 const struct casefile *cf, void *aux,
90 const struct dataset *ds)
93 const struct npar_specs *specs = aux;
94 struct descriptives *summary_descriptives = NULL;
96 for ( t = 0 ; t < specs->n_tests; ++t )
98 const struct npar_test *test = specs->test[t];
99 if ( NULL == test->execute )
101 msg (SW, _("NPAR subcommand not currently implemented."));
104 test->execute (ds, cf, specs->filter, test);
107 if ( specs->descriptives )
109 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
112 npar_summary_calc_descriptives (summary_descriptives, cf,
115 specs->vv, specs->n_vars);
118 if ( specs->descriptives || specs->quartiles )
119 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
121 free (summary_descriptives);
128 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
132 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0};
133 struct hsh_table *var_hash;
134 npar_specs.pool = pool_create ();
136 var_hash = hsh_create_pool (npar_specs.pool, 0,
137 compare_vars_by_name, hash_var_by_name,
140 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
142 pool_destroy (npar_specs.pool);
146 for (i = 0; i < npar_specs.n_tests; ++i )
148 const struct npar_test *test = npar_specs.test[i];
149 test->insert_variables (test, var_hash);
152 npar_specs.vv = (const struct variable *const *) hsh_data (var_hash);
153 npar_specs.n_vars = hsh_count (var_hash);
155 if ( cmd.sbc_statistics )
159 for ( i = 0 ; i < NPAR_ST_count; ++i )
161 if ( cmd.a_statistics[i] )
165 case NPAR_ST_DESCRIPTIVES:
166 npar_specs.descriptives = true;
168 case NPAR_ST_QUARTILES:
169 npar_specs.quartiles = true;
172 npar_specs.quartiles = true;
173 npar_specs.descriptives = true;
183 casefilter_create (cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM, 0, 0);
185 if ( cmd.miss == NPAR_LISTWISE )
186 casefilter_add_variables (npar_specs.filter,
190 ok = multipass_procedure_with_splits (ds, npar_execute, &npar_specs);
192 casefilter_destroy (npar_specs.filter);
194 hsh_destroy (var_hash);
196 pool_destroy (npar_specs.pool);
198 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
202 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
204 struct npar_specs *specs = aux;
206 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
207 struct one_sample_test *tp = (struct one_sample_test *) cstp;
209 ((struct npar_test *)tp)->execute = chisquare_execute;
210 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
212 if (!parse_variables_pool (lexer, specs->pool, dataset_dict (ds),
213 &tp->vars, &tp->n_vars,
214 PV_NO_SCRATCH | PV_NO_DUPLICATE))
219 cstp->ranged = false;
221 if ( lex_match (lexer, '('))
224 if ( ! lex_force_num (lexer)) return 0;
225 cstp->lo = lex_integer (lexer);
227 lex_force_match (lexer, ',');
228 if (! lex_force_num (lexer) ) return 0;
229 cstp->hi = lex_integer (lexer);
230 if ( cstp->lo >= cstp->hi )
233 _("The specified value of HI (%d) is "
234 "lower than the specified value of LO (%d)"),
239 if (! lex_force_match (lexer, ')')) return 0;
242 cstp->n_expected = 0;
243 cstp->expected = NULL;
244 if ( lex_match (lexer, '/') )
246 if ( lex_match_id (lexer, "EXPECTED") )
248 lex_force_match (lexer, '=');
249 if ( ! lex_match_id (lexer, "EQUAL") )
253 while ( lex_is_number(lexer) )
257 f = lex_number (lexer);
259 if ( lex_match (lexer, '*'))
262 f = lex_number (lexer);
265 lex_match (lexer, ',');
267 cstp->n_expected += n;
268 cstp->expected = pool_realloc (specs->pool,
272 for ( i = cstp->n_expected - n ;
273 i < cstp->n_expected;
275 cstp->expected[i] = f;
281 lex_put_back (lexer, '/');
284 if ( cstp->ranged && cstp->n_expected > 0 &&
285 cstp->n_expected != cstp->hi - cstp->lo + 1 )
288 _("%d expected values were given, but the specified "
289 "range (%d-%d) requires exactly %d values."),
290 cstp->n_expected, cstp->lo, cstp->hi,
291 cstp->hi - cstp->lo +1);
296 specs->test = pool_realloc (specs->pool,
298 sizeof(*specs->test) * specs->n_tests);
300 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
307 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
309 struct npar_specs *specs = aux;
310 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
311 struct one_sample_test *tp = (struct one_sample_test *) btp;
313 ((struct npar_test *)tp)->execute = binomial_execute;
314 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
316 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
318 if ( lex_match(lexer, '(') )
320 if ( lex_force_num (lexer) )
322 btp->p = lex_number (lexer);
324 lex_force_match (lexer, ')');
330 if ( lex_match (lexer, '=') )
332 if (parse_variables_pool (lexer, specs->pool, dataset_dict (ds),
333 &tp->vars, &tp->n_vars,
334 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
336 if ( lex_match (lexer, '('))
338 lex_force_num (lexer);
339 btp->category1 = lex_number (lexer);
341 if ( ! lex_force_match (lexer, ',')) return 2;
342 if ( ! lex_force_num (lexer) ) return 2;
343 btp->category2 = lex_number (lexer);
345 lex_force_match (lexer, ')');
353 if ( lex_match (lexer, '(') )
355 lex_force_num (lexer);
356 btp->cutpoint = lex_number (lexer);
358 lex_force_match (lexer, ')');
363 specs->test = pool_realloc (specs->pool,
365 sizeof(*specs->test) * specs->n_tests);
367 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
373 bool parse_two_sample_related_test (struct lexer *lexer,
374 const struct dictionary *dict,
375 struct cmd_npar_tests *cmd,
376 struct two_sample_test *test_parameters,
382 parse_two_sample_related_test (struct lexer *lexer,
383 const struct dictionary *dict,
384 struct cmd_npar_tests *cmd UNUSED,
385 struct two_sample_test *test_parameters,
392 struct variable **vlist1;
395 struct variable **vlist2;
398 if (!parse_variables_pool (lexer, pool,
401 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
404 if ( lex_match(lexer, T_WITH))
407 if ( !parse_variables_pool (lexer, pool, dict,
409 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
412 paired = (lex_match (lexer, '(') &&
413 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
421 if ( n_vlist1 != n_vlist2)
422 msg (SE, _("PAIRED was specified but the number of variables "
423 "preceding WITH (%d) did not match the number "
424 "following (%d)."), (int) n_vlist1, (int) n_vlist2);
426 test_parameters->n_pairs = n_vlist1 ;
430 test_parameters->n_pairs = n_vlist1 * n_vlist2;
435 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
438 test_parameters->pairs =
439 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
446 assert (n_vlist1 == n_vlist2);
447 for ( i = 0 ; i < n_vlist1; ++i )
449 test_parameters->pairs[n][0] = vlist1[i];
450 test_parameters->pairs[n][1] = vlist2[i];
457 for ( i = 0 ; i < n_vlist1; ++i )
459 for ( j = 0 ; j < n_vlist2; ++j )
461 test_parameters->pairs[n][0] = vlist1[i];
462 test_parameters->pairs[n][1] = vlist2[j];
471 for ( i = 0 ; i < n_vlist1 - 1; ++i )
473 for ( j = i + 1 ; j < n_vlist1; ++j )
475 assert ( n < test_parameters->n_pairs);
476 test_parameters->pairs[n][0] = vlist1[i];
477 test_parameters->pairs[n][1] = vlist1[j];
483 assert ( n == test_parameters->n_pairs);
489 npar_custom_wilcoxon (struct lexer *lexer,
491 struct cmd_npar_tests *cmd, void *aux )
493 struct npar_specs *specs = aux;
495 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
496 ((struct npar_test *)tp)->execute = NULL;
498 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
503 specs->test = pool_realloc (specs->pool,
505 sizeof(*specs->test) * specs->n_tests);
506 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
512 npar_custom_mcnemar (struct lexer *lexer,
514 struct cmd_npar_tests *cmd, void *aux )
516 struct npar_specs *specs = aux;
518 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
519 ((struct npar_test *)tp)->execute = NULL;
522 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
523 cmd, tp, specs->pool) )
527 specs->test = pool_realloc (specs->pool,
529 sizeof(*specs->test) * specs->n_tests);
530 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
536 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
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 *)tp)->execute = NULL;
545 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
550 specs->test = pool_realloc (specs->pool,
552 sizeof(*specs->test) * specs->n_tests);
553 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
558 /* Insert the variables for TEST into VAR_HASH */
560 one_sample_insert_variables (const struct npar_test *test,
561 struct hsh_table *var_hash)
564 struct one_sample_test *ost = (struct one_sample_test *) test;
566 for ( i = 0 ; i < ost->n_vars ; ++i )
567 hsh_insert (var_hash, ost->vars[i]);