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,
189 npar_specs.filter, NULL);
191 grouper = casegrouper_create_splits (input, dataset_dict (ds));
192 while (casegrouper_get_next_group (grouper, &group))
193 npar_execute (group, &npar_specs, ds);
194 ok = casegrouper_destroy (grouper);
195 ok = proc_commit (ds) && ok;
197 const_hsh_destroy (var_hash);
199 pool_destroy (npar_specs.pool);
201 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
205 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
207 struct npar_specs *specs = aux;
209 struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
210 struct one_sample_test *tp = (struct one_sample_test *) cstp;
212 ((struct npar_test *)tp)->execute = chisquare_execute;
213 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
215 if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
216 &tp->vars, &tp->n_vars,
217 PV_NO_SCRATCH | PV_NO_DUPLICATE))
222 cstp->ranged = false;
224 if ( lex_match (lexer, '('))
227 if ( ! lex_force_num (lexer)) return 0;
228 cstp->lo = lex_integer (lexer);
230 lex_force_match (lexer, ',');
231 if (! lex_force_num (lexer) ) return 0;
232 cstp->hi = lex_integer (lexer);
233 if ( cstp->lo >= cstp->hi )
236 _("The specified value of HI (%d) is "
237 "lower than the specified value of LO (%d)"),
242 if (! lex_force_match (lexer, ')')) return 0;
245 cstp->n_expected = 0;
246 cstp->expected = NULL;
247 if ( lex_match (lexer, '/') )
249 if ( lex_match_id (lexer, "EXPECTED") )
251 lex_force_match (lexer, '=');
252 if ( ! lex_match_id (lexer, "EQUAL") )
256 while ( lex_is_number(lexer) )
260 f = lex_number (lexer);
262 if ( lex_match (lexer, '*'))
265 f = lex_number (lexer);
268 lex_match (lexer, ',');
270 cstp->n_expected += n;
271 cstp->expected = pool_realloc (specs->pool,
275 for ( i = cstp->n_expected - n ;
276 i < cstp->n_expected;
278 cstp->expected[i] = f;
284 lex_put_back (lexer, '/');
287 if ( cstp->ranged && cstp->n_expected > 0 &&
288 cstp->n_expected != cstp->hi - cstp->lo + 1 )
291 _("%d expected values were given, but the specified "
292 "range (%d-%d) requires exactly %d values."),
293 cstp->n_expected, cstp->lo, cstp->hi,
294 cstp->hi - cstp->lo +1);
299 specs->test = pool_realloc (specs->pool,
301 sizeof(*specs->test) * specs->n_tests);
303 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
310 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
312 struct npar_specs *specs = aux;
313 struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
314 struct one_sample_test *tp = (struct one_sample_test *) btp;
316 ((struct npar_test *)tp)->execute = binomial_execute;
317 ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
319 btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
321 if ( lex_match(lexer, '(') )
323 if ( lex_force_num (lexer) )
325 btp->p = lex_number (lexer);
327 lex_force_match (lexer, ')');
333 if ( lex_match (lexer, '=') )
335 if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
336 &tp->vars, &tp->n_vars,
337 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
339 if ( lex_match (lexer, '('))
341 lex_force_num (lexer);
342 btp->category1 = lex_number (lexer);
344 if ( ! lex_force_match (lexer, ',')) return 2;
345 if ( ! lex_force_num (lexer) ) return 2;
346 btp->category2 = lex_number (lexer);
348 lex_force_match (lexer, ')');
356 if ( lex_match (lexer, '(') )
358 lex_force_num (lexer);
359 btp->cutpoint = lex_number (lexer);
361 lex_force_match (lexer, ')');
366 specs->test = pool_realloc (specs->pool,
368 sizeof(*specs->test) * specs->n_tests);
370 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
376 bool parse_two_sample_related_test (struct lexer *lexer,
377 const struct dictionary *dict,
378 struct cmd_npar_tests *cmd,
379 struct two_sample_test *test_parameters,
385 parse_two_sample_related_test (struct lexer *lexer,
386 const struct dictionary *dict,
387 struct cmd_npar_tests *cmd UNUSED,
388 struct two_sample_test *test_parameters,
395 const struct variable **vlist1;
398 const struct variable **vlist2;
401 if (!parse_variables_const_pool (lexer, pool,
404 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
407 if ( lex_match(lexer, T_WITH))
410 if ( !parse_variables_const_pool (lexer, pool, dict,
412 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
415 paired = (lex_match (lexer, '(') &&
416 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
424 if ( n_vlist1 != n_vlist2)
425 msg (SE, _("PAIRED was specified but the number of variables "
426 "preceding WITH (%zu) did not match the number "
427 "following (%zu)."), n_vlist1, n_vlist2);
429 test_parameters->n_pairs = n_vlist1 ;
433 test_parameters->n_pairs = n_vlist1 * n_vlist2;
438 test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
441 test_parameters->pairs =
442 pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
449 assert (n_vlist1 == n_vlist2);
450 for ( i = 0 ; i < n_vlist1; ++i )
452 test_parameters->pairs[n][0] = vlist1[i];
453 test_parameters->pairs[n][1] = vlist2[i];
460 for ( i = 0 ; i < n_vlist1; ++i )
462 for ( j = 0 ; j < n_vlist2; ++j )
464 test_parameters->pairs[n][0] = vlist1[i];
465 test_parameters->pairs[n][1] = vlist2[j];
474 for ( i = 0 ; i < n_vlist1 - 1; ++i )
476 for ( j = i + 1 ; j < n_vlist1; ++j )
478 assert ( n < test_parameters->n_pairs);
479 test_parameters->pairs[n][0] = vlist1[i];
480 test_parameters->pairs[n][1] = vlist1[j];
486 assert ( n == test_parameters->n_pairs);
492 npar_custom_wilcoxon (struct lexer *lexer,
494 struct cmd_npar_tests *cmd, void *aux )
496 struct npar_specs *specs = aux;
498 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
499 ((struct npar_test *)tp)->execute = NULL;
501 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
506 specs->test = pool_realloc (specs->pool,
508 sizeof(*specs->test) * specs->n_tests);
509 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
515 npar_custom_mcnemar (struct lexer *lexer,
517 struct cmd_npar_tests *cmd, void *aux )
519 struct npar_specs *specs = aux;
521 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
522 ((struct npar_test *)tp)->execute = NULL;
525 if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
526 cmd, tp, specs->pool) )
530 specs->test = pool_realloc (specs->pool,
532 sizeof(*specs->test) * specs->n_tests);
533 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
539 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
540 struct cmd_npar_tests *cmd, void *aux )
542 struct npar_specs *specs = aux;
544 struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
545 ((struct npar_test *)tp)->execute = NULL;
548 if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
553 specs->test = pool_realloc (specs->pool,
555 sizeof(*specs->test) * specs->n_tests);
556 specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
561 /* Insert the variables for TEST into VAR_HASH */
563 one_sample_insert_variables (const struct npar_test *test,
564 struct const_hsh_table *var_hash)
567 struct one_sample_test *ost = (struct one_sample_test *) test;
569 for ( i = 0 ; i < ost->n_vars ; ++i )
570 const_hsh_insert (var_hash, ost->vars[i]);