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