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/hash.h>
36 #include <libpspp/pool.h>
37 #include <libpspp/taint.h>
38 #include <math/moments.h>
40 #include "npar-summary.h"
43 #define _(msgid) gettext (msgid)
57 missing=miss:!analysis/listwise,
58 incl:include/!exclude;
60 +statistics[st_]=descriptives,quartiles,all.
66 static struct cmd_npar_tests cmd;
72 struct npar_test **test;
75 const struct variable ** vv; /* Compendium of all variables
76 (those mentioned on ANY subcommand */
77 int n_vars; /* Number of variables in vv */
79 enum mv_class filter; /* Missing values to filter. */
81 bool descriptives; /* Descriptive statistics should be calculated */
82 bool quartiles; /* Quartiles should be calculated */
84 bool exact; /* Whether exact calculations have been requested */
85 double timer; /* Maximum time (in minutes) to wait for exact calculations */
88 static void one_sample_insert_variables (const struct npar_test *test,
89 struct const_hsh_table *variables);
91 static void two_sample_insert_variables (const struct npar_test *test,
92 struct const_hsh_table *variables);
97 npar_execute(struct casereader *input,
98 const struct npar_specs *specs,
99 const struct dataset *ds)
102 struct descriptives *summary_descriptives = NULL;
104 for ( t = 0 ; t < specs->n_tests; ++t )
106 const struct npar_test *test = specs->test[t];
107 if ( NULL == test->execute )
109 msg (SW, _("NPAR subcommand not currently implemented."));
112 test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
115 if ( specs->descriptives )
117 summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
120 npar_summary_calc_descriptives (summary_descriptives,
121 casereader_clone (input),
123 specs->vv, specs->n_vars,
127 if ( (specs->descriptives || specs->quartiles)
128 && !taint_has_tainted_successor (casereader_get_taint (input)) )
129 do_summary_box (summary_descriptives, specs->vv, specs->n_vars );
131 free (summary_descriptives);
132 casereader_destroy (input);
136 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
140 struct npar_specs npar_specs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
141 struct const_hsh_table *var_hash;
142 struct casegrouper *grouper;
143 struct casereader *input, *group;
145 npar_specs.pool = pool_create ();
147 var_hash = const_hsh_create_pool (npar_specs.pool, 0,
148 compare_vars_by_name, hash_var_by_name,
151 if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
153 pool_destroy (npar_specs.pool);
157 for (i = 0; i < npar_specs.n_tests; ++i )
159 const struct npar_test *test = npar_specs.test[i];
160 test->insert_variables (test, var_hash);
163 npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
164 npar_specs.n_vars = const_hsh_count (var_hash);
166 if ( cmd.sbc_statistics )
170 for ( i = 0 ; i < NPAR_ST_count; ++i )
172 if ( cmd.a_statistics[i] )
176 case NPAR_ST_DESCRIPTIVES:
177 npar_specs.descriptives = true;
179 case NPAR_ST_QUARTILES:
180 npar_specs.quartiles = true;
183 npar_specs.quartiles = true;
184 npar_specs.descriptives = true;
193 npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
195 input = proc_open (ds);
196 if ( cmd.miss == NPAR_LISTWISE )
198 input = casereader_create_filter_missing (input,
206 grouper = casegrouper_create_splits (input, dataset_dict (ds));
207 while (casegrouper_get_next_group (grouper, &group))
208 npar_execute (group, &npar_specs, ds);
209 ok = casegrouper_destroy (grouper);
210 ok = proc_commit (ds) && ok;
212 const_hsh_destroy (var_hash);
214 pool_destroy (npar_specs.pool);
216 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
220 npar_custom_chisquare (struct lexer *lexer, struct dataset *ds,
221 struct cmd_npar_tests *cmd UNUSED, void *aux )
223 struct npar_specs *specs = aux;
225 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
226 struct one_sample_test *tp = (struct one_sample_test *) cstp;
228 ((struct npar_test *)tp)->execute = chisquare_execute;
229 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
231 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
232 &tp->vars, &tp->n_vars,
233 PV_NO_SCRATCH | PV_NO_DUPLICATE))
238 cstp->ranged = false;
240 if ( lex_match (lexer, '('))
243 if ( ! lex_force_num (lexer)) return 0;
244 cstp->lo = lex_integer (lexer);
246 lex_force_match (lexer, ',');
247 if (! lex_force_num (lexer) ) return 0;
248 cstp->hi = lex_integer (lexer);
249 if ( cstp->lo >= cstp->hi )
252 _("The specified value of HI (%d) is "
253 "lower than the specified value of LO (%d)"),
258 if (! lex_force_match (lexer, ')')) return 0;
261 cstp->n_expected = 0;
262 cstp->expected = NULL;
263 if ( lex_match (lexer, '/') )
265 if ( lex_match_id (lexer, "EXPECTED") )
267 lex_force_match (lexer, '=');
268 if ( ! lex_match_id (lexer, "EQUAL") )
272 while ( lex_is_number(lexer) )
276 f = lex_number (lexer);
278 if ( lex_match (lexer, '*'))
281 f = lex_number (lexer);
284 lex_match (lexer, ',');
286 cstp->n_expected += n;
287 cstp->expected = pool_realloc (specs->pool,
291 for ( i = cstp->n_expected - n ;
292 i < cstp->n_expected;
294 cstp->expected[i] = f;
300 lex_put_back (lexer, '/');
303 if ( cstp->ranged && cstp->n_expected > 0 &&
304 cstp->n_expected != cstp->hi - cstp->lo + 1 )
307 _("%d expected values were given, but the specified "
308 "range (%d-%d) requires exactly %d values."),
309 cstp->n_expected, cstp->lo, cstp->hi,
310 cstp->hi - cstp->lo +1);
315 specs->test = pool_realloc (specs->pool,
317 sizeof(*specs->test) * specs->n_tests);
319 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
326 npar_custom_binomial (struct lexer *lexer, struct dataset *ds,
327 struct cmd_npar_tests *cmd UNUSED, void *aux)
329 struct npar_specs *specs = aux;
330 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
331 struct one_sample_test *tp = (struct one_sample_test *) btp;
333 ((struct npar_test *)tp)->execute = binomial_execute;
334 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
336 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
338 if ( lex_match(lexer, '(') )
340 if ( lex_force_num (lexer) )
342 btp->p = lex_number (lexer);
344 lex_force_match (lexer, ')');
350 if ( lex_match (lexer, '=') )
352 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
353 &tp->vars, &tp->n_vars,
354 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
356 if ( lex_match (lexer, '('))
358 lex_force_num (lexer);
359 btp->category1 = lex_number (lexer);
361 if ( lex_match (lexer, ','))
363 if ( ! lex_force_num (lexer) ) return 2;
364 btp->category2 = lex_number (lexer);
369 btp->cutpoint = btp->category1;
372 lex_force_match (lexer, ')');
380 specs->test = pool_realloc (specs->pool,
382 sizeof(*specs->test) * specs->n_tests);
384 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
390 bool parse_two_sample_related_test (struct lexer *lexer,
391 const struct dictionary *dict,
392 struct cmd_npar_tests *cmd,
393 struct two_sample_test *test_parameters,
399 parse_two_sample_related_test (struct lexer *lexer,
400 const struct dictionary *dict,
401 struct cmd_npar_tests *cmd UNUSED,
402 struct two_sample_test *test_parameters,
409 const struct variable **vlist1;
412 const struct variable **vlist2;
415 ((struct npar_test *)test_parameters)->insert_variables = two_sample_insert_variables;
417 if (!parse_variables_const_pool (lexer, pool,
420 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
423 if ( lex_match(lexer, T_WITH))
426 if ( !parse_variables_const_pool (lexer, pool, dict,
428 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
431 paired = (lex_match (lexer, '(') &&
432 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
440 if ( n_vlist1 != n_vlist2)
441 msg (SE, _("PAIRED was specified but the number of variables "
442 "preceding WITH (%zu) did not match the number "
443 "following (%zu)."), n_vlist1, n_vlist2);
445 test_parameters->n_pairs = n_vlist1 ;
449 test_parameters->n_pairs = n_vlist1 * n_vlist2;
454 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
457 test_parameters->pairs =
458 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
465 assert (n_vlist1 == n_vlist2);
466 for ( i = 0 ; i < n_vlist1; ++i )
468 test_parameters->pairs[n][1] = vlist1[i];
469 test_parameters->pairs[n][0] = vlist2[i];
476 for ( i = 0 ; i < n_vlist1; ++i )
478 for ( j = 0 ; j < n_vlist2; ++j )
480 test_parameters->pairs[n][1] = vlist1[i];
481 test_parameters->pairs[n][0] = vlist2[j];
490 for ( i = 0 ; i < n_vlist1 - 1; ++i )
492 for ( j = i + 1 ; j < n_vlist1; ++j )
494 assert ( n < test_parameters->n_pairs);
495 test_parameters->pairs[n][1] = vlist1[i];
496 test_parameters->pairs[n][0] = vlist1[j];
502 assert ( n == test_parameters->n_pairs);
508 npar_custom_wilcoxon (struct lexer *lexer,
510 struct cmd_npar_tests *cmd, void *aux )
512 struct npar_specs *specs = aux;
514 struct two_sample_test *tp = pool_alloc (specs->pool, sizeof(*tp));
515 ((struct npar_test *)tp)->execute = wilcoxon_execute;
517 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
522 specs->test = pool_realloc (specs->pool,
524 sizeof(*specs->test) * specs->n_tests);
525 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
531 npar_custom_mcnemar (struct lexer *lexer,
533 struct cmd_npar_tests *cmd, void *aux )
535 struct npar_specs *specs = aux;
537 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
538 ((struct npar_test *)tp)->execute = NULL;
541 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
542 cmd, tp, specs->pool) )
546 specs->test = pool_realloc (specs->pool,
548 sizeof(*specs->test) * specs->n_tests);
549 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
555 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
556 struct cmd_npar_tests *cmd, void *aux )
558 struct npar_specs *specs = aux;
560 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
561 ((struct npar_test *) tp)->execute = sign_execute;
563 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
568 specs->test = pool_realloc (specs->pool,
570 sizeof(*specs->test) * specs->n_tests);
571 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
576 /* Insert the variables for TEST into VAR_HASH */
578 one_sample_insert_variables (const struct npar_test *test,
579 struct const_hsh_table *var_hash)
582 struct one_sample_test *ost = (struct one_sample_test *) test;
584 for ( i = 0 ; i < ost->n_vars ; ++i )
585 const_hsh_insert (var_hash, ost->vars[i]);
589 two_sample_insert_variables (const struct npar_test *test,
590 struct const_hsh_table *var_hash)
594 const struct two_sample_test *tst = (const struct two_sample_test *) test;
596 for ( i = 0 ; i < tst->n_pairs ; ++i )
598 variable_pair *pair = &tst->pairs[i];
600 const_hsh_insert (var_hash, (*pair)[0]);
601 const_hsh_insert (var_hash, (*pair)[1]);
608 npar_custom_method (struct lexer *lexer, struct dataset *ds UNUSED,
609 struct cmd_npar_tests *test UNUSED, void *aux)
611 struct npar_specs *specs = aux;
613 if ( lex_match_id (lexer, "EXACT") )
617 if (lex_match_id (lexer, "TIMER"))
621 if ( lex_match (lexer, '('))
623 if ( lex_force_num (lexer) )
625 specs->timer = lex_number (lexer);
628 lex_force_match (lexer, ')');