647205fd690a26e5e2a73e8323be872b40efc269
[pspp-builds.git] / src / language / stats / npar.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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 <libpspp/hash.h>
34 #include <libpspp/pool.h>
35 #include <libpspp/taint.h>
36 #include <math/moments.h>
37
38 #include "npar-summary.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 /* (headers) */
44
45 /* (specification)
46    "NPAR TESTS" (npar_):
47    +chisquare=custom;
48    +binomial=custom;
49    +wilcoxon=custom;
50    +mcnemar=custom;
51    +sign=custom;
52    +cochran=varlist;
53    +friedman=varlist;
54    +kendall=varlist;
55    missing=miss:!analysis/listwise,
56            incl:include/!exclude;
57    +statistics[st_]=descriptives,quartiles,all.
58 */
59 /* (declarations) */
60 /* (functions) */
61
62
63 static struct cmd_npar_tests cmd;
64
65
66 struct npar_specs
67 {
68   struct pool *pool;
69   struct npar_test **test;
70   size_t n_tests;
71
72   const struct variable ** vv; /* Compendium of all variables
73                                        (those mentioned on ANY subcommand */
74   int n_vars; /* Number of variables in vv */
75
76   enum mv_class filter;    /* Missing values to filter. */
77
78   bool descriptives;       /* Descriptive statistics should be calculated */
79   bool quartiles;          /* Quartiles should be calculated */
80 };
81
82 void one_sample_insert_variables (const struct npar_test *test,
83                                   struct const_hsh_table *variables);
84
85 static void
86 npar_execute(struct casereader *input,
87              const struct npar_specs *specs,
88              const struct dataset *ds)
89 {
90   int t;
91   struct descriptives *summary_descriptives = NULL;
92
93   for ( t = 0 ; t < specs->n_tests; ++t )
94     {
95       const struct npar_test *test = specs->test[t];
96       if ( NULL == test->execute )
97         {
98           msg (SW, _("NPAR subcommand not currently implemented."));
99           continue;
100         }
101       test->execute (ds, casereader_clone (input), specs->filter, test);
102     }
103
104   if ( specs->descriptives )
105     {
106       summary_descriptives = xnmalloc (sizeof (*summary_descriptives),
107                                        specs->n_vars);
108
109       npar_summary_calc_descriptives (summary_descriptives,
110                                       casereader_clone (input),
111                                       dataset_dict (ds),
112                                       specs->vv, specs->n_vars,
113                                       specs->filter);
114     }
115
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 );
119
120   free (summary_descriptives);
121   casereader_destroy (input);
122 }
123
124 int
125 cmd_npar_tests (struct lexer *lexer, struct dataset *ds)
126 {
127   bool ok;
128   int i;
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;
133
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_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 = cmd.incl == NPAR_EXCLUDE ? MV_ANY : MV_SYSTEM;
183
184   input = proc_open (ds);
185   if ( cmd.miss == NPAR_LISTWISE )
186     input = casereader_create_filter_missing (input,
187                                               npar_specs.vv,
188                                               npar_specs.n_vars,
189                                               npar_specs.filter,
190                                                NULL, NULL);
191
192   grouper = casegrouper_create_splits (input, dataset_dict (ds));
193   while (casegrouper_get_next_group (grouper, &group))
194     npar_execute (group, &npar_specs, ds);
195   ok = casegrouper_destroy (grouper);
196   ok = proc_commit (ds) && ok;
197
198   const_hsh_destroy (var_hash);
199
200   pool_destroy (npar_specs.pool);
201
202   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
203 }
204
205 int
206 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
207 {
208   struct npar_specs *specs = aux;
209
210   struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
211   struct one_sample_test *tp = (struct one_sample_test *) cstp;
212
213   ((struct npar_test *)tp)->execute = chisquare_execute;
214   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
215
216   if (!parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
217                              &tp->vars, &tp->n_vars,
218                              PV_NO_SCRATCH | PV_NO_DUPLICATE))
219     {
220       return 2;
221     }
222
223   cstp->ranged = false;
224
225   if ( lex_match (lexer, '('))
226     {
227       cstp->ranged = true;
228       if ( ! lex_force_num (lexer)) return 0;
229       cstp->lo = lex_integer (lexer);
230       lex_get (lexer);
231       lex_force_match (lexer, ',');
232       if (! lex_force_num (lexer) ) return 0;
233       cstp->hi = lex_integer (lexer);
234       if ( cstp->lo >= cstp->hi )
235         {
236           msg(ME,
237               _("The specified value of HI (%d) is "
238                 "lower than the specified value of LO (%d)"),
239               cstp->hi, cstp->lo);
240           return 0;
241         }
242       lex_get (lexer);
243       if (! lex_force_match (lexer, ')')) return 0;
244     }
245
246   cstp->n_expected = 0;
247   cstp->expected = NULL;
248   if ( lex_match (lexer, '/') )
249     {
250       if ( lex_match_id (lexer, "EXPECTED") )
251         {
252           lex_force_match (lexer, '=');
253           if ( ! lex_match_id (lexer, "EQUAL") )
254             {
255               double f;
256               int n;
257               while ( lex_is_number(lexer) )
258                 {
259                   int i;
260                   n = 1;
261                   f = lex_number (lexer);
262                   lex_get (lexer);
263                   if ( lex_match (lexer, '*'))
264                     {
265                       n = f;
266                       f = lex_number (lexer);
267                       lex_get (lexer);
268                     }
269                   lex_match (lexer, ',');
270
271                   cstp->n_expected += n;
272                   cstp->expected = pool_realloc (specs->pool,
273                                                  cstp->expected,
274                                                  sizeof(double) *
275                                                  cstp->n_expected);
276                   for ( i = cstp->n_expected - n ;
277                         i < cstp->n_expected;
278                         ++i )
279                     cstp->expected[i] = f;
280
281                 }
282             }
283         }
284       else
285         lex_put_back (lexer, '/');
286     }
287
288   if ( cstp->ranged && cstp->n_expected > 0 &&
289        cstp->n_expected != cstp->hi - cstp->lo + 1 )
290     {
291       msg(ME,
292           _("%d expected values were given, but the specified "
293             "range (%d-%d) requires exactly %d values."),
294           cstp->n_expected, cstp->lo, cstp->hi,
295           cstp->hi - cstp->lo +1);
296       return 0;
297     }
298
299   specs->n_tests++;
300   specs->test = pool_realloc (specs->pool,
301                               specs->test,
302                               sizeof(*specs->test) * specs->n_tests);
303
304   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
305
306   return 1;
307 }
308
309
310 int
311 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
312 {
313   struct npar_specs *specs = aux;
314   struct binomial_test *btp = pool_alloc(specs->pool, sizeof(*btp));
315   struct one_sample_test *tp = (struct one_sample_test *) btp;
316
317   ((struct npar_test *)tp)->execute = binomial_execute;
318   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
319
320   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
321
322   if ( lex_match(lexer, '(') )
323     {
324       if ( lex_force_num (lexer) )
325         {
326           btp->p = lex_number (lexer);
327           lex_get (lexer);
328           lex_force_match (lexer, ')');
329         }
330       else
331         return 0;
332     }
333
334   if ( lex_match (lexer, '=') )
335     {
336       if (parse_variables_const_pool (lexer, specs->pool, dataset_dict (ds),
337                                 &tp->vars, &tp->n_vars,
338                                 PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
339         {
340           if ( lex_match (lexer, '('))
341             {
342               lex_force_num (lexer);
343               btp->category1 = lex_number (lexer);
344               lex_get (lexer);
345               if ( ! lex_force_match (lexer, ',')) return 2;
346               if ( ! lex_force_num (lexer) ) return 2;
347               btp->category2 = lex_number (lexer);
348               lex_get (lexer);
349               lex_force_match (lexer, ')');
350             }
351         }
352       else
353         return 2;
354     }
355   else
356     {
357       if ( lex_match (lexer, '(') )
358         {
359           lex_force_num (lexer);
360           btp->cutpoint = lex_number (lexer);
361           lex_get (lexer);
362           lex_force_match (lexer, ')');
363         }
364     }
365
366   specs->n_tests++;
367   specs->test = pool_realloc (specs->pool,
368                               specs->test,
369                               sizeof(*specs->test) * specs->n_tests);
370
371   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
372
373   return 1;
374 }
375
376
377 bool parse_two_sample_related_test (struct lexer *lexer,
378                                     const struct dictionary *dict,
379                                     struct cmd_npar_tests *cmd,
380                                     struct two_sample_test *test_parameters,
381                                     struct pool *pool
382                                     );
383
384
385 bool
386 parse_two_sample_related_test (struct lexer *lexer,
387                                const struct dictionary *dict,
388                                struct cmd_npar_tests *cmd UNUSED,
389                                struct two_sample_test *test_parameters,
390                                struct pool *pool
391                                )
392 {
393   int n = 0;
394   bool paired = false;
395   bool with = false;
396   const struct variable **vlist1;
397   size_t n_vlist1;
398
399   const struct variable **vlist2;
400   size_t n_vlist2;
401
402   if (!parse_variables_const_pool (lexer, pool,
403                              dict,
404                              &vlist1, &n_vlist1,
405                              PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
406     return false;
407
408   if ( lex_match(lexer, T_WITH))
409     {
410       with = true;
411       if ( !parse_variables_const_pool (lexer, pool, dict,
412                                   &vlist2, &n_vlist2,
413                                   PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
414         return false;
415
416       paired = (lex_match (lexer, '(') &&
417                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
418     }
419
420
421   if ( with )
422     {
423       if (paired)
424         {
425           if ( n_vlist1 != n_vlist2)
426             msg (SE, _("PAIRED was specified but the number of variables "
427                        "preceding WITH (%zu) did not match the number "
428                        "following (%zu)."), n_vlist1, n_vlist2);
429
430           test_parameters->n_pairs = n_vlist1 ;
431         }
432       else
433         {
434           test_parameters->n_pairs = n_vlist1 * n_vlist2;
435         }
436     }
437   else
438     {
439       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
440     }
441
442   test_parameters->pairs =
443     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
444
445   if ( with )
446     {
447       if (paired)
448         {
449           int i;
450           assert (n_vlist1 == n_vlist2);
451           for ( i = 0 ; i < n_vlist1; ++i )
452             {
453               test_parameters->pairs[n][0] = vlist1[i];
454               test_parameters->pairs[n][1] = vlist2[i];
455               n++;
456             }
457         }
458       else
459         {
460           int i,j;
461           for ( i = 0 ; i < n_vlist1; ++i )
462             {
463               for ( j = 0 ; j < n_vlist2; ++j )
464                 {
465                   test_parameters->pairs[n][0] = vlist1[i];
466                   test_parameters->pairs[n][1] = vlist2[j];
467                   n++;
468                 }
469             }
470         }
471     }
472   else
473     {
474       int i,j;
475       for ( i = 0 ; i < n_vlist1 - 1; ++i )
476         {
477           for ( j = i + 1 ; j < n_vlist1; ++j )
478             {
479               assert ( n < test_parameters->n_pairs);
480               test_parameters->pairs[n][0] = vlist1[i];
481               test_parameters->pairs[n][1] = vlist1[j];
482               n++;
483             }
484         }
485     }
486
487   assert ( n == test_parameters->n_pairs);
488
489   return true;
490 }
491
492 int
493 npar_custom_wilcoxon (struct lexer *lexer,
494                       struct dataset *ds,
495                       struct cmd_npar_tests *cmd, void *aux )
496 {
497   struct npar_specs *specs = aux;
498
499   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
500   ((struct npar_test *)tp)->execute = NULL;
501
502   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
503                                       tp, specs->pool) )
504     return 0;
505
506   specs->n_tests++;
507   specs->test = pool_realloc (specs->pool,
508                               specs->test,
509                               sizeof(*specs->test) * specs->n_tests);
510   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
511
512   return 1;
513 }
514
515 int
516 npar_custom_mcnemar (struct lexer *lexer,
517                      struct dataset *ds,
518                      struct cmd_npar_tests *cmd, void *aux )
519 {
520   struct npar_specs *specs = aux;
521
522   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
523   ((struct npar_test *)tp)->execute = NULL;
524
525
526   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
527                                       cmd, tp, specs->pool) )
528     return 0;
529
530   specs->n_tests++;
531   specs->test = pool_realloc (specs->pool,
532                               specs->test,
533                               sizeof(*specs->test) * specs->n_tests);
534   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
535
536   return 1;
537 }
538
539 int
540 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
541                   struct cmd_npar_tests *cmd, void *aux )
542 {
543   struct npar_specs *specs = aux;
544
545   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
546   ((struct npar_test *)tp)->execute = NULL;
547
548
549   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
550                                       tp, specs->pool) )
551     return 0;
552
553   specs->n_tests++;
554   specs->test = pool_realloc (specs->pool,
555                               specs->test,
556                               sizeof(*specs->test) * specs->n_tests);
557   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
558
559   return 1;
560 }
561
562 /* Insert the variables for TEST into VAR_HASH */
563 void
564 one_sample_insert_variables (const struct npar_test *test,
565                             struct const_hsh_table *var_hash)
566 {
567   int i;
568   struct one_sample_test *ost = (struct one_sample_test *) test;
569
570   for ( i = 0 ; i < ost->n_vars ; ++i )
571     const_hsh_insert (var_hash, ost->vars[i]);
572 }
573