1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 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 <libpspp/hash.h>
34 #include <libpspp/pool.h>
35 #include <libpspp/taint.h>
36 #include <math/moments.h>
38 #include "npar-summary.h"
41 #define _(msgid) gettext (msgid)
55 missing=miss:!analysis/listwise,
56 incl:include/!exclude;
57 +statistics[st_]=descriptives,quartiles,all.
63 static struct cmd_npar_tests cmd;
69 struct npar_test **test;
72 const struct variable ** vv; /* Compendium of all variables
73 (those mentioned on ANY subcommand */
74 int n_vars; /* Number of variables in vv */
76 enum mv_class filter; /* Missing values to filter. */
78 bool descriptives; /* Descriptive statistics should be calculated */
79 bool quartiles; /* Quartiles should be calculated */
82 void one_sample_insert_variables (const struct npar_test *test,
83 struct const_hsh_table *variables);
86 npar_execute(struct casereader *input,
87 const struct npar_specs *specs,
88 const struct dataset *ds)
91 struct descriptives *summary_descriptives = NULL;
93 for ( t = 0 ; t < specs->n_tests; ++t )
95 const struct npar_test *test = specs->test[t];
96 if ( NULL == test->execute )
98 msg (SW, _("NPAR subcommand not currently implemented."));
101 test->execute (ds, casereader_clone (input), specs->filter, test);
104 if ( specs->descriptives )
106 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
109 npar_summary_calc_descriptives (summary_descriptives,
110 casereader_clone (input),
112 specs->vv, specs->n_vars,
116 if ( (specs->descriptives || specs->quartiles)
117 && !taint_has_tainted_successor (casereader_get_taint (input)) )
118 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
120 free (summary_descriptives);
121 casereader_destroy (input);
125 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
129 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0};
130 struct const_hsh_table *var_hash;
131 struct casegrouper *grouper;
132 struct casereader *input, *group;
134 npar_specs.pool = pool_create ();
136 var_hash = const_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 = const_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;
182 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
184 input = proc_open (ds);
185 if ( cmd.miss == NPAR_LISTWISE )
186 input = casereader_create_filter_missing (input,
192 grouper = casegrouper_create_splits (input, dataset_dict (ds));
193 while (casegrouper_get_next_group (grouper, &group))
194 npar_execute (group, &npar_specs, ds);
195 ok = casegrouper_destroy (grouper);
196 ok = proc_commit (ds) && ok;
198 const_hsh_destroy (var_hash);
200 pool_destroy (npar_specs.pool);
202 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
206 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
208 struct npar_specs *specs = aux;
210 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
211 struct one_sample_test *tp = (struct one_sample_test *) cstp;
213 ((struct npar_test *)tp)->execute = chisquare_execute;
214 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
216 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
217 &tp->vars, &tp->n_vars,
218 PV_NO_SCRATCH | PV_NO_DUPLICATE))
223 cstp->ranged = false;
225 if ( lex_match (lexer, '('))
228 if ( ! lex_force_num (lexer)) return 0;
229 cstp->lo = lex_integer (lexer);
231 lex_force_match (lexer, ',');
232 if (! lex_force_num (lexer) ) return 0;
233 cstp->hi = lex_integer (lexer);
234 if ( cstp->lo >= cstp->hi )
237 _("The specified value of HI (%d) is "
238 "lower than the specified value of LO (%d)"),
243 if (! lex_force_match (lexer, ')')) return 0;
246 cstp->n_expected = 0;
247 cstp->expected = NULL;
248 if ( lex_match (lexer, '/') )
250 if ( lex_match_id (lexer, "EXPECTED") )
252 lex_force_match (lexer, '=');
253 if ( ! lex_match_id (lexer, "EQUAL") )
257 while ( lex_is_number(lexer) )
261 f = lex_number (lexer);
263 if ( lex_match (lexer, '*'))
266 f = lex_number (lexer);
269 lex_match (lexer, ',');
271 cstp->n_expected += n;
272 cstp->expected = pool_realloc (specs->pool,
276 for ( i = cstp->n_expected - n ;
277 i < cstp->n_expected;
279 cstp->expected[i] = f;
285 lex_put_back (lexer, '/');
288 if ( cstp->ranged && cstp->n_expected > 0 &&
289 cstp->n_expected != cstp->hi - cstp->lo + 1 )
292 _("%d expected values were given, but the specified "
293 "range (%d-%d) requires exactly %d values."),
294 cstp->n_expected, cstp->lo, cstp->hi,
295 cstp->hi - cstp->lo +1);
300 specs->test = pool_realloc (specs->pool,
302 sizeof(*specs->test) * specs->n_tests);
304 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
311 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
313 struct npar_specs *specs = aux;
314 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
315 struct one_sample_test *tp = (struct one_sample_test *) btp;
317 ((struct npar_test *)tp)->execute = binomial_execute;
318 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
320 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
322 if ( lex_match(lexer, '(') )
324 if ( lex_force_num (lexer) )
326 btp->p = lex_number (lexer);
328 lex_force_match (lexer, ')');
334 if ( lex_match (lexer, '=') )
336 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
337 &tp->vars, &tp->n_vars,
338 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
340 if ( lex_match (lexer, '('))
342 lex_force_num (lexer);
343 btp->category1 = lex_number (lexer);
345 if ( ! lex_force_match (lexer, ',')) return 2;
346 if ( ! lex_force_num (lexer) ) return 2;
347 btp->category2 = lex_number (lexer);
349 lex_force_match (lexer, ')');
357 if ( lex_match (lexer, '(') )
359 lex_force_num (lexer);
360 btp->cutpoint = lex_number (lexer);
362 lex_force_match (lexer, ')');
367 specs->test = pool_realloc (specs->pool,
369 sizeof(*specs->test) * specs->n_tests);
371 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
377 bool parse_two_sample_related_test (struct lexer *lexer,
378 const struct dictionary *dict,
379 struct cmd_npar_tests *cmd,
380 struct two_sample_test *test_parameters,
386 parse_two_sample_related_test (struct lexer *lexer,
387 const struct dictionary *dict,
388 struct cmd_npar_tests *cmd UNUSED,
389 struct two_sample_test *test_parameters,
396 const struct variable **vlist1;
399 const struct variable **vlist2;
402 if (!parse_variables_const_pool (lexer, pool,
405 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
408 if ( lex_match(lexer, T_WITH))
411 if ( !parse_variables_const_pool (lexer, pool, dict,
413 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
416 paired = (lex_match (lexer, '(') &&
417 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
425 if ( n_vlist1 != n_vlist2)
426 msg (SE, _("PAIRED was specified but the number of variables "
427 "preceding WITH (%zu) did not match the number "
428 "following (%zu)."), n_vlist1, n_vlist2);
430 test_parameters->n_pairs = n_vlist1 ;
434 test_parameters->n_pairs = n_vlist1 * n_vlist2;
439 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
442 test_parameters->pairs =
443 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
450 assert (n_vlist1 == n_vlist2);
451 for ( i = 0 ; i < n_vlist1; ++i )
453 test_parameters->pairs[n][0] = vlist1[i];
454 test_parameters->pairs[n][1] = vlist2[i];
461 for ( i = 0 ; i < n_vlist1; ++i )
463 for ( j = 0 ; j < n_vlist2; ++j )
465 test_parameters->pairs[n][0] = vlist1[i];
466 test_parameters->pairs[n][1] = vlist2[j];
475 for ( i = 0 ; i < n_vlist1 - 1; ++i )
477 for ( j = i + 1 ; j < n_vlist1; ++j )
479 assert ( n < test_parameters->n_pairs);
480 test_parameters->pairs[n][0] = vlist1[i];
481 test_parameters->pairs[n][1] = vlist1[j];
487 assert ( n == test_parameters->n_pairs);
493 npar_custom_wilcoxon (struct lexer *lexer,
495 struct cmd_npar_tests *cmd, void *aux )
497 struct npar_specs *specs = aux;
499 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
500 ((struct npar_test *)tp)->execute = NULL;
502 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
507 specs->test = pool_realloc (specs->pool,
509 sizeof(*specs->test) * specs->n_tests);
510 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
516 npar_custom_mcnemar (struct lexer *lexer,
518 struct cmd_npar_tests *cmd, void *aux )
520 struct npar_specs *specs = aux;
522 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
523 ((struct npar_test *)tp)->execute = NULL;
526 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
527 cmd, tp, specs->pool) )
531 specs->test = pool_realloc (specs->pool,
533 sizeof(*specs->test) * specs->n_tests);
534 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
540 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
541 struct cmd_npar_tests *cmd, void *aux )
543 struct npar_specs *specs = aux;
545 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
546 ((struct npar_test *)tp)->execute = NULL;
549 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
554 specs->test = pool_realloc (specs->pool,
556 sizeof(*specs->test) * specs->n_tests);
557 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
562 /* Insert the variables for TEST into VAR_HASH */
564 one_sample_insert_variables (const struct npar_test *test,
565 struct const_hsh_table *var_hash)
568 struct one_sample_test *ost = (struct one_sample_test *) test;
570 for ( i = 0 ; i < ost->n_vars ; ++i )
571 const_hsh_insert (var_hash, ost->vars[i]);