eff5c9195f3eb7f7e7190b4ed65aa363783c1491
[pspp-builds.git] / src / language / stats / npar.q
1 /* PSPP - NPAR TESTS. -*-c-*-
2
3 Copyright (C) 2006 Free Software Foundation, Inc.
4 Author: John Darrington <john@darrington.wattle.id.au>,
5
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.
10
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.
15
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
19 02110-1301, USA. */
20
21 #include <config.h>
22
23 #include <language/stats/npar.h>
24
25 #include <math.h>
26
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>
41
42 #include "npar-summary.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 /* (headers) */
48
49 /* (specification)
50    "NPAR TESTS" (npar_):
51    +chisquare=custom;
52    +binomial=custom;
53    +wilcoxon=custom;
54    +mcnemar=custom;
55    +sign=custom;
56    +cochran=varlist;
57    +friedman=varlist;
58    +kendall=varlist;
59    missing=miss:!analysis/listwise,
60            incl:include/!exclude;
61    +statistics[st_]=descriptives,quartiles,all.
62 */
63 /* (declarations) */
64 /* (functions) */
65
66
67 static struct cmd_npar_tests cmd;
68
69
70 struct npar_specs
71 {
72   struct pool *pool;
73   struct npar_test **test;
74   size_t n_tests;
75
76   const struct variable ** vv; /* Compendium of all variables
77                                        (those mentioned on ANY subcommand */
78   int n_vars; /* Number of variables in vv */
79
80   enum mv_class filter;    /* Missing values to filter. */
81
82   bool descriptives;       /* Descriptive statistics should be calculated */
83   bool quartiles;          /* Quartiles should be calculated */
84 };
85
86 void one_sample_insert_variables (const struct npar_test *test,
87                                   struct const_hsh_table *variables);
88
89 static void
90 npar_execute(struct casereader *input,
91              const struct npar_specs *specs,
92              const struct dataset *ds)
93 {
94   int t;
95   struct descriptives *summary_descriptives = NULL;
96
97   for ( t = 0 ; t < specs->n_tests; ++t )
98     {
99       const struct npar_test *test = specs->test[t];
100       if ( NULL == test->execute )
101         {
102           msg (SW, _("NPAR subcommand not currently implemented."));
103           continue;
104         }
105       test->execute (ds, casereader_clone (input), specs->filter, test);
106     }
107
108   if ( specs->descriptives )
109     {
110       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
111                                        specs->n_vars);
112
113       npar_summary_calc_descriptives (summary_descriptives,
114                                       casereader_clone (input),
115                                       dataset_dict (ds),
116                                       specs->vv, specs->n_vars,
117                                       specs->filter);
118     }
119
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 );
123
124   free (summary_descriptives);
125   casereader_destroy (input);
126 }
127
128 int
129 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
130 {
131   bool ok;
132   int i;
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;
137
138   npar_specs.pool = pool_create ();
139
140   var_hash = const_hsh_create_pool (npar_specs.pool, 0,
141                               compare_vars_by_name, hash_var_by_name,
142                               NULL, NULL);
143
144   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
145     {
146       pool_destroy (npar_specs.pool);
147       return CMD_FAILURE;
148     }
149
150   for (i = 0; i < npar_specs.n_tests; ++i )
151     {
152       const struct npar_test *test = npar_specs.test[i];
153       test->insert_variables (test, var_hash);
154     }
155
156   npar_specs.vv = (const struct variable **) const_hsh_data (var_hash);
157   npar_specs.n_vars = const_hsh_count (var_hash);
158
159   if ( cmd.sbc_statistics )
160     {
161       int i;
162
163       for ( i = 0 ; i < NPAR_ST_count; ++i )
164         {
165           if ( cmd.a_statistics[i] )
166             {
167               switch ( i )
168                 {
169                 case NPAR_ST_DESCRIPTIVES:
170                   npar_specs.descriptives = true;
171                   break;
172                 case NPAR_ST_QUARTILES:
173                   npar_specs.quartiles = true;
174                   break;
175                 case NPAR_ST_ALL:
176                   npar_specs.quartiles = true;
177                   npar_specs.descriptives = true;
178                   break;
179                 default:
180                   NOT_REACHED();
181                 };
182             }
183         }
184     }
185
186   npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
187
188   input = proc_open (ds);
189   if ( cmd.miss == NPAR_LISTWISE )
190     input = casereader_create_filter_missing (input,
191                                               npar_specs.vv,
192                                               npar_specs.n_vars,
193                                               npar_specs.filter, NULL);
194
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;
200
201   const_hsh_destroy (var_hash);
202
203   pool_destroy (npar_specs.pool);
204
205   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
206 }
207
208 int
209 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
210 {
211   struct npar_specs *specs = aux;
212
213   struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
214   struct one_sample_test *tp = (struct one_sample_test *) cstp;
215
216   ((struct npar_test *)tp)->execute = chisquare_execute;
217   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
218
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))
222     {
223       return 2;
224     }
225
226   cstp->ranged = false;
227
228   if ( lex_match (lexer, '('))
229     {
230       cstp->ranged = true;
231       if ( ! lex_force_num (lexer)) return 0;
232       cstp->lo = lex_integer (lexer);
233       lex_get (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 )
238         {
239           msg(ME,
240               _("The specified value of HI (%d) is "
241                 "lower than the specified value of LO (%d)"),
242               cstp->hi, cstp->lo);
243           return 0;
244         }
245       lex_get (lexer);
246       if (! lex_force_match (lexer, ')')) return 0;
247     }
248
249   cstp->n_expected = 0;
250   cstp->expected = NULL;
251   if ( lex_match (lexer, '/') )
252     {
253       if ( lex_match_id (lexer, "EXPECTED") )
254         {
255           lex_force_match (lexer, '=');
256           if ( ! lex_match_id (lexer, "EQUAL") )
257             {
258               double f;
259               int n;
260               while ( lex_is_number(lexer) )
261                 {
262                   int i;
263                   n = 1;
264                   f = lex_number (lexer);
265                   lex_get (lexer);
266                   if ( lex_match (lexer, '*'))
267                     {
268                       n = f;
269                       f = lex_number (lexer);
270                       lex_get (lexer);
271                     }
272                   lex_match (lexer, ',');
273
274                   cstp->n_expected += n;
275                   cstp->expected = pool_realloc (specs->pool,
276                                                  cstp->expected,
277                                                  sizeof(double) *
278                                                  cstp->n_expected);
279                   for ( i = cstp->n_expected - n ;
280                         i < cstp->n_expected;
281                         ++i )
282                     cstp->expected[i] = f;
283
284                 }
285             }
286         }
287       else
288         lex_put_back (lexer, '/');
289     }
290
291   if ( cstp->ranged && cstp->n_expected > 0 &&
292        cstp->n_expected != cstp->hi - cstp->lo + 1 )
293     {
294       msg(ME,
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);
299       return 0;
300     }
301
302   specs->n_tests++;
303   specs->test = pool_realloc (specs->pool,
304                               specs->test,
305                               sizeof(*specs->test) * specs->n_tests);
306
307   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
308
309   return 1;
310 }
311
312
313 int
314 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
315 {
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;
319
320   ((struct npar_test *)tp)->execute = binomial_execute;
321   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
322
323   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
324
325   if ( lex_match(lexer, '(') )
326     {
327       if ( lex_force_num (lexer) )
328         {
329           btp->p = lex_number (lexer);
330           lex_get (lexer);
331           lex_force_match (lexer, ')');
332         }
333       else
334         return 0;
335     }
336
337   if ( lex_match (lexer, '=') )
338     {
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) )
342         {
343           if ( lex_match (lexer, '('))
344             {
345               lex_force_num (lexer);
346               btp->category1 = lex_number (lexer);
347               lex_get (lexer);
348               if ( ! lex_force_match (lexer, ',')) return 2;
349               if ( ! lex_force_num (lexer) ) return 2;
350               btp->category2 = lex_number (lexer);
351               lex_get (lexer);
352               lex_force_match (lexer, ')');
353             }
354         }
355       else
356         return 2;
357     }
358   else
359     {
360       if ( lex_match (lexer, '(') )
361         {
362           lex_force_num (lexer);
363           btp->cutpoint = lex_number (lexer);
364           lex_get (lexer);
365           lex_force_match (lexer, ')');
366         }
367     }
368
369   specs->n_tests++;
370   specs->test = pool_realloc (specs->pool,
371                               specs->test,
372                               sizeof(*specs->test) * specs->n_tests);
373
374   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
375
376   return 1;
377 }
378
379
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,
384                                     struct pool *pool
385                                     );
386
387
388 bool
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,
393                                struct pool *pool
394                                )
395 {
396   int n = 0;
397   bool paired = false;
398   bool with = false;
399   const struct variable **vlist1;
400   size_t n_vlist1;
401
402   const struct variable **vlist2;
403   size_t n_vlist2;
404
405   if (!parse_variables_const_pool (lexer, pool,
406                              dict,
407                              &vlist1, &n_vlist1,
408                              PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
409     return false;
410
411   if ( lex_match(lexer, T_WITH))
412     {
413       with = true;
414       if ( !parse_variables_const_pool (lexer, pool, dict,
415                                   &vlist2, &n_vlist2,
416                                   PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
417         return false;
418
419       paired = (lex_match (lexer, '(') &&
420                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
421     }
422
423
424   if ( with )
425     {
426       if (paired)
427         {
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);
432
433           test_parameters->n_pairs = n_vlist1 ;
434         }
435       else
436         {
437           test_parameters->n_pairs = n_vlist1 * n_vlist2;
438         }
439     }
440   else
441     {
442       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
443     }
444
445   test_parameters->pairs =
446     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
447
448   if ( with )
449     {
450       if (paired)
451         {
452           int i;
453           assert (n_vlist1 == n_vlist2);
454           for ( i = 0 ; i < n_vlist1; ++i )
455             {
456               test_parameters->pairs[n][0] = vlist1[i];
457               test_parameters->pairs[n][1] = vlist2[i];
458               n++;
459             }
460         }
461       else
462         {
463           int i,j;
464           for ( i = 0 ; i < n_vlist1; ++i )
465             {
466               for ( j = 0 ; j < n_vlist2; ++j )
467                 {
468                   test_parameters->pairs[n][0] = vlist1[i];
469                   test_parameters->pairs[n][1] = vlist2[j];
470                   n++;
471                 }
472             }
473         }
474     }
475   else
476     {
477       int i,j;
478       for ( i = 0 ; i < n_vlist1 - 1; ++i )
479         {
480           for ( j = i + 1 ; j < n_vlist1; ++j )
481             {
482               assert ( n < test_parameters->n_pairs);
483               test_parameters->pairs[n][0] = vlist1[i];
484               test_parameters->pairs[n][1] = vlist1[j];
485               n++;
486             }
487         }
488     }
489
490   assert ( n == test_parameters->n_pairs);
491
492   return true;
493 }
494
495 int
496 npar_custom_wilcoxon (struct lexer *lexer,
497                       struct dataset *ds,
498                       struct cmd_npar_tests *cmd, void *aux )
499 {
500   struct npar_specs *specs = aux;
501
502   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
503   ((struct npar_test *)tp)->execute = NULL;
504
505   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
506                                       tp, specs->pool) )
507     return 0;
508
509   specs->n_tests++;
510   specs->test = pool_realloc (specs->pool,
511                               specs->test,
512                               sizeof(*specs->test) * specs->n_tests);
513   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
514
515   return 1;
516 }
517
518 int
519 npar_custom_mcnemar (struct lexer *lexer,
520                      struct dataset *ds,
521                      struct cmd_npar_tests *cmd, void *aux )
522 {
523   struct npar_specs *specs = aux;
524
525   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
526   ((struct npar_test *)tp)->execute = NULL;
527
528
529   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
530                                       cmd, tp, specs->pool) )
531     return 0;
532
533   specs->n_tests++;
534   specs->test = pool_realloc (specs->pool,
535                               specs->test,
536                               sizeof(*specs->test) * specs->n_tests);
537   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
538
539   return 1;
540 }
541
542 int
543 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
544                   struct cmd_npar_tests *cmd, void *aux )
545 {
546   struct npar_specs *specs = aux;
547
548   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
549   ((struct npar_test *)tp)->execute = NULL;
550
551
552   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
553                                       tp, specs->pool) )
554     return 0;
555
556   specs->n_tests++;
557   specs->test = pool_realloc (specs->pool,
558                               specs->test,
559                               sizeof(*specs->test) * specs->n_tests);
560   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
561
562   return 1;
563 }
564
565 /* Insert the variables for TEST into VAR_HASH */
566 void
567 one_sample_insert_variables (const struct npar_test *test,
568                             struct const_hsh_table *var_hash)
569 {
570   int i;
571   struct one_sample_test *ost = (struct one_sample_test *) test;
572
573   for ( i = 0 ; i < ost->n_vars ; ++i )
574     const_hsh_insert (var_hash, ost->vars[i]);
575 }
576