Eliminate casts that can be replaced by uses of the & operator.
[pspp-builds.git] / src / language / stats / npar.q
1 /* PSPP - a program for statistical analysis. -*-c-*-
2    Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <language/stats/npar.h>
20
21 #include <math.h>
22
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>
39
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    method=custom;
60    +statistics[st_]=descriptives,quartiles,all.
61 */
62 /* (declarations) */
63 /* (functions) */
64
65
66 static struct cmd_npar_tests cmd;
67
68
69 struct npar_specs
70 {
71   struct pool *pool;
72   struct npar_test **test;
73   size_t n_tests;
74
75   const struct variable ** vv; /* Compendium of all variables
76                                   (those mentioned on ANY subcommand */
77   int n_vars; /* Number of variables in vv */
78
79   enum mv_class filter;    /* Missing values to filter. */
80
81   bool descriptives;       /* Descriptive statistics should be calculated */
82   bool quartiles;          /* Quartiles should be calculated */
83
84   bool exact;  /* Whether exact calculations have been requested */
85   double timer;   /* Maximum time (in minutes) to wait for exact calculations */
86 };
87
88 static void one_sample_insert_variables (const struct npar_test *test,
89                                          struct const_hsh_table *variables);
90
91 static void two_sample_insert_variables (const struct npar_test *test,
92                                          struct const_hsh_table *variables);
93
94
95
96 static void
97 npar_execute(struct casereader *input,
98              const struct npar_specs *specs,
99              const struct dataset *ds)
100 {
101   int t;
102   struct descriptives *summary_descriptives = NULL;
103
104   for ( t = 0 ; t < specs->n_tests; ++t )
105     {
106       const struct npar_test *test = specs->test[t];
107       if ( NULL == test->execute )
108         {
109           msg (SW, _("NPAR subcommand not currently implemented."));
110           continue;
111         }
112       test->execute (ds, casereader_clone (input), specs->filter, test, specs->exact, specs->timer);
113     }
114
115   if ( specs->descriptives )
116     {
117       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
118                                        specs->n_vars);
119
120       npar_summary_calc_descriptives (summary_descriptives,
121                                       casereader_clone (input),
122                                       dataset_dict (ds),
123                                       specs->vv, specs->n_vars,
124                                       specs->filter);
125     }
126
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 );
130
131   free (summary_descriptives);
132   casereader_destroy (input);
133 }
134
135 int
136 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
137 {
138   bool ok;
139   int i;
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;
144
145   npar_specs.pool = pool_create ();
146
147   var_hash = const_hsh_create_pool (npar_specs.pool, 0,
148                                     compare_vars_by_name, hash_var_by_name,
149                                     NULL, NULL);
150
151   if ( ! parse_npar_tests (lexer, ds, &cmd, &npar_specs) )
152     {
153       pool_destroy (npar_specs.pool);
154       return CMD_FAILURE;
155     }
156
157   for (i = 0; i < npar_specs.n_tests; ++i )
158     {
159       const struct npar_test *test = npar_specs.test[i];
160       test->insert_variables (test, var_hash);
161     }
162
163   npar_specs.vv = (const struct variable **) const_hsh_sort (var_hash);
164   npar_specs.n_vars = const_hsh_count (var_hash);
165
166   if ( cmd.sbc_statistics )
167     {
168       int i;
169
170       for ( i = 0 ; i < NPAR_ST_count; ++i )
171         {
172           if ( cmd.a_statistics[i] )
173             {
174               switch ( i )
175                 {
176                 case NPAR_ST_DESCRIPTIVES:
177                   npar_specs.descriptives = true;
178                   break;
179                 case NPAR_ST_QUARTILES:
180                   npar_specs.quartiles = true;
181                   break;
182                 case NPAR_ST_ALL:
183                   npar_specs.quartiles = true;
184                   npar_specs.descriptives = true;
185                   break;
186                 default:
187                   NOT_REACHED();
188                 };
189             }
190         }
191     }
192
193   npar_specs.filter = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
194
195   input = proc_open (ds);
196   if ( cmd.miss == NPAR_LISTWISE )
197     {
198       input = casereader_create_filter_missing (input,
199                                                 npar_specs.vv,
200                                                 npar_specs.n_vars,
201                                                 npar_specs.filter,
202                                                 NULL, NULL);
203     }
204
205
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;
211
212   const_hsh_destroy (var_hash);
213
214   pool_destroy (npar_specs.pool);
215
216   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
217 }
218
219 int
220 npar_custom_chisquare (struct lexer *lexer, struct dataset *ds,
221                        struct cmd_npar_tests *cmd UNUSED, void *aux )
222 {
223   struct npar_specs *specs = aux;
224
225   struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
226   struct one_sample_test *tp = &cstp->parent;
227   struct npar_test *nt = &tp->parent;
228
229   nt->execute = chisquare_execute;
230   nt->insert_variables = one_sample_insert_variables;
231
232   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
233                                    &tp->vars, &tp->n_vars,
234                                    PV_NO_SCRATCH | PV_NO_DUPLICATE))
235     {
236       return 2;
237     }
238
239   cstp->ranged = false;
240
241   if ( lex_match (lexer, '('))
242     {
243       cstp->ranged = true;
244       if ( ! lex_force_num (lexer)) return 0;
245       cstp->lo = lex_integer (lexer);
246       lex_get (lexer);
247       lex_force_match (lexer, ',');
248       if (! lex_force_num (lexer) ) return 0;
249       cstp->hi = lex_integer (lexer);
250       if ( cstp->lo >= cstp->hi )
251         {
252           msg(ME,
253               _("The specified value of HI (%d) is "
254                 "lower than the specified value of LO (%d)"),
255               cstp->hi, cstp->lo);
256           return 0;
257         }
258       lex_get (lexer);
259       if (! lex_force_match (lexer, ')')) return 0;
260     }
261
262   cstp->n_expected = 0;
263   cstp->expected = NULL;
264   if ( lex_match (lexer, '/') )
265     {
266       if ( lex_match_id (lexer, "EXPECTED") )
267         {
268           lex_force_match (lexer, '=');
269           if ( ! lex_match_id (lexer, "EQUAL") )
270             {
271               double f;
272               int n;
273               while ( lex_is_number(lexer) )
274                 {
275                   int i;
276                   n = 1;
277                   f = lex_number (lexer);
278                   lex_get (lexer);
279                   if ( lex_match (lexer, '*'))
280                     {
281                       n = f;
282                       f = lex_number (lexer);
283                       lex_get (lexer);
284                     }
285                   lex_match (lexer, ',');
286
287                   cstp->n_expected += n;
288                   cstp->expected = pool_realloc (specs->pool,
289                                                  cstp->expected,
290                                                  sizeof(double) *
291                                                  cstp->n_expected);
292                   for ( i = cstp->n_expected - n ;
293                         i < cstp->n_expected;
294                         ++i )
295                     cstp->expected[i] = f;
296
297                 }
298             }
299         }
300       else
301         lex_put_back (lexer, '/');
302     }
303
304   if ( cstp->ranged && cstp->n_expected > 0 &&
305        cstp->n_expected != cstp->hi - cstp->lo + 1 )
306     {
307       msg(ME,
308           _("%d expected values were given, but the specified "
309             "range (%d-%d) requires exactly %d values."),
310           cstp->n_expected, cstp->lo, cstp->hi,
311           cstp->hi - cstp->lo +1);
312       return 0;
313     }
314
315   specs->n_tests++;
316   specs->test = pool_realloc (specs->pool,
317                               specs->test,
318                               sizeof(*specs->test) * specs->n_tests);
319
320   specs->test[specs->n_tests - 1] = nt;
321
322   return 1;
323 }
324
325
326 int
327 npar_custom_binomial (struct lexer *lexer, struct dataset *ds,
328                       struct cmd_npar_tests *cmd UNUSED, void *aux)
329 {
330   struct npar_specs *specs = aux;
331   struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
332   struct one_sample_test *tp = &btp->parent;
333   struct npar_test *nt = &tp->parent;
334
335   nt->execute = binomial_execute;
336   nt->insert_variables = one_sample_insert_variables;
337
338   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
339
340   if ( lex_match(lexer, '(') )
341     {
342       if ( lex_force_num (lexer) )
343         {
344           btp->p = lex_number (lexer);
345           lex_get (lexer);
346           lex_force_match (lexer, ')');
347         }
348       else
349         return 0;
350     }
351
352   if ( lex_match (lexer, '=') )
353     {
354       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
355                                       &tp->vars, &tp->n_vars,
356                                       PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
357         {
358           if ( lex_match (lexer, '('))
359             {
360               lex_force_num (lexer);
361               btp->category1 = lex_number (lexer);
362               lex_get (lexer);
363               if ( lex_match (lexer, ','))
364                 {
365                   if ( ! lex_force_num (lexer) ) return 2;
366                   btp->category2 = lex_number (lexer);
367                   lex_get (lexer);
368                 }
369               else
370                 {
371                   btp->cutpoint = btp->category1;
372                 }
373
374               lex_force_match (lexer, ')');
375             }
376         }
377       else
378         return 2;
379     }
380
381   specs->n_tests++;
382   specs->test = pool_realloc (specs->pool,
383                               specs->test,
384                               sizeof(*specs->test) * specs->n_tests);
385
386   specs->test[specs->n_tests - 1] = nt;
387
388   return 1;
389 }
390
391
392 bool parse_two_sample_related_test (struct lexer *lexer,
393                                     const struct dictionary *dict,
394                                     struct cmd_npar_tests *cmd,
395                                     struct two_sample_test *test_parameters,
396                                     struct pool *pool
397                                     );
398
399
400 bool
401 parse_two_sample_related_test (struct lexer *lexer,
402                                const struct dictionary *dict,
403                                struct cmd_npar_tests *cmd UNUSED,
404                                struct two_sample_test *test_parameters,
405                                struct pool *pool
406                                )
407 {
408   int n = 0;
409   bool paired = false;
410   bool with = false;
411   const struct variable **vlist1;
412   size_t n_vlist1;
413
414   const struct variable **vlist2;
415   size_t n_vlist2;
416
417   test_parameters->parent.insert_variables = two_sample_insert_variables;
418
419   if (!parse_variables_const_pool (lexer, pool,
420                                    dict,
421                                    &vlist1, &n_vlist1,
422                                    PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
423     return false;
424
425   if ( lex_match(lexer, T_WITH))
426     {
427       with = true;
428       if ( !parse_variables_const_pool (lexer, pool, dict,
429                                         &vlist2, &n_vlist2,
430                                         PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
431         return false;
432
433       paired = (lex_match (lexer, '(') &&
434                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
435     }
436
437
438   if ( with )
439     {
440       if (paired)
441         {
442           if ( n_vlist1 != n_vlist2)
443             msg (SE, _("PAIRED was specified but the number of variables "
444                        "preceding WITH (%zu) did not match the number "
445                        "following (%zu)."), n_vlist1, n_vlist2);
446
447           test_parameters->n_pairs = n_vlist1 ;
448         }
449       else
450         {
451           test_parameters->n_pairs = n_vlist1 * n_vlist2;
452         }
453     }
454   else
455     {
456       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
457     }
458
459   test_parameters->pairs =
460     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
461
462   if ( with )
463     {
464       if (paired)
465         {
466           int i;
467           assert (n_vlist1 == n_vlist2);
468           for ( i = 0 ; i < n_vlist1; ++i )
469             {
470               test_parameters->pairs[n][1] = vlist1[i];
471               test_parameters->pairs[n][0] = vlist2[i];
472               n++;
473             }
474         }
475       else
476         {
477           int i,j;
478           for ( i = 0 ; i < n_vlist1; ++i )
479             {
480               for ( j = 0 ; j < n_vlist2; ++j )
481                 {
482                   test_parameters->pairs[n][1] = vlist1[i];
483                   test_parameters->pairs[n][0] = vlist2[j];
484                   n++;
485                 }
486             }
487         }
488     }
489   else
490     {
491       int i,j;
492       for ( i = 0 ; i < n_vlist1 - 1; ++i )
493         {
494           for ( j = i + 1 ; j < n_vlist1; ++j )
495             {
496               assert ( n < test_parameters->n_pairs);
497               test_parameters->pairs[n][1] = vlist1[i];
498               test_parameters->pairs[n][0] = vlist1[j];
499               n++;
500             }
501         }
502     }
503
504   assert ( n == test_parameters->n_pairs);
505
506   return true;
507 }
508
509 int
510 npar_custom_wilcoxon (struct lexer *lexer,
511                       struct dataset *ds,
512                       struct cmd_npar_tests *cmd, void *aux )
513 {
514   struct npar_specs *specs = aux;
515
516   struct two_sample_test *tp = pool_alloc (specs->pool, sizeof(*tp));
517   struct npar_test *nt = &tp->parent;
518   nt->execute = wilcoxon_execute;
519
520   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
521                                       tp, specs->pool) )
522     return 0;
523
524   specs->n_tests++;
525   specs->test = pool_realloc (specs->pool,
526                               specs->test,
527                               sizeof(*specs->test) * specs->n_tests);
528   specs->test[specs->n_tests - 1] = nt;
529
530   return 1;
531 }
532
533 int
534 npar_custom_mcnemar (struct lexer *lexer,
535                      struct dataset *ds,
536                      struct cmd_npar_tests *cmd, void *aux )
537 {
538   struct npar_specs *specs = aux;
539
540   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
541   struct npar_test *nt = &tp->parent;
542   nt->execute = NULL;
543
544
545   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
546                                       cmd, 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] = nt;
554
555   return 1;
556 }
557
558 int
559 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
560                   struct cmd_npar_tests *cmd, void *aux )
561 {
562   struct npar_specs *specs = aux;
563
564   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
565   struct npar_test *nt = &tp->parent;
566
567   nt->execute = sign_execute;
568
569   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
570                                       tp, specs->pool) )
571     return 0;
572
573   specs->n_tests++;
574   specs->test = pool_realloc (specs->pool,
575                               specs->test,
576                               sizeof(*specs->test) * specs->n_tests);
577   specs->test[specs->n_tests - 1] = nt;
578
579   return 1;
580 }
581
582 /* Insert the variables for TEST into VAR_HASH */
583 static void
584 one_sample_insert_variables (const struct npar_test *test,
585                              struct const_hsh_table *var_hash)
586 {
587   int i;
588   struct one_sample_test *ost = (struct one_sample_test *) test;
589
590   for ( i = 0 ; i < ost->n_vars ; ++i )
591     const_hsh_insert (var_hash, ost->vars[i]);
592 }
593
594 static void
595 two_sample_insert_variables (const struct npar_test *test,
596                              struct const_hsh_table *var_hash)
597 {
598   int i;
599
600   const struct two_sample_test *tst = (const struct two_sample_test *) test;
601
602   for ( i = 0 ; i < tst->n_pairs ; ++i )
603     {
604       variable_pair *pair = &tst->pairs[i];
605
606       const_hsh_insert (var_hash, (*pair)[0]);
607       const_hsh_insert (var_hash, (*pair)[1]);
608     }
609
610 }
611
612
613 static int
614 npar_custom_method (struct lexer *lexer, struct dataset *ds UNUSED,
615                     struct cmd_npar_tests *test UNUSED, void *aux)
616 {
617   struct npar_specs *specs = aux;
618
619   if ( lex_match_id (lexer, "EXACT") )
620     {
621       specs->exact = true;
622       specs->timer = 0.0;
623       if (lex_match_id (lexer, "TIMER"))
624         {
625           specs->timer = 5.0;
626
627           if ( lex_match (lexer, '('))
628             {
629               if ( lex_force_num (lexer) )
630                 {
631                   specs->timer = lex_number (lexer);
632                   lex_get (lexer);
633                 }
634               lex_force_match (lexer, ')');
635             }
636         }
637     }
638
639   return 1;
640 }