Fixed bug reporting the significance of paired value t-test.
[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, NULL);
190
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;
196
197   const_hsh_destroy (var_hash);
198
199   pool_destroy (npar_specs.pool);
200
201   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
202 }
203
204 int
205 npar_custom_chisquare(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux )
206 {
207   struct npar_specs *specs = aux;
208
209   struct chisquare_test *cstp = pool_alloc(specs->pool, sizeof(*cstp));
210   struct one_sample_test *tp = (struct one_sample_test *) cstp;
211
212   ((struct npar_test *)tp)->execute = chisquare_execute;
213   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
214
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))
218     {
219       return 2;
220     }
221
222   cstp->ranged = false;
223
224   if ( lex_match (lexer, '('))
225     {
226       cstp->ranged = true;
227       if ( ! lex_force_num (lexer)) return 0;
228       cstp->lo = lex_integer (lexer);
229       lex_get (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 )
234         {
235           msg(ME,
236               _("The specified value of HI (%d) is "
237                 "lower than the specified value of LO (%d)"),
238               cstp->hi, cstp->lo);
239           return 0;
240         }
241       lex_get (lexer);
242       if (! lex_force_match (lexer, ')')) return 0;
243     }
244
245   cstp->n_expected = 0;
246   cstp->expected = NULL;
247   if ( lex_match (lexer, '/') )
248     {
249       if ( lex_match_id (lexer, "EXPECTED") )
250         {
251           lex_force_match (lexer, '=');
252           if ( ! lex_match_id (lexer, "EQUAL") )
253             {
254               double f;
255               int n;
256               while ( lex_is_number(lexer) )
257                 {
258                   int i;
259                   n = 1;
260                   f = lex_number (lexer);
261                   lex_get (lexer);
262                   if ( lex_match (lexer, '*'))
263                     {
264                       n = f;
265                       f = lex_number (lexer);
266                       lex_get (lexer);
267                     }
268                   lex_match (lexer, ',');
269
270                   cstp->n_expected += n;
271                   cstp->expected = pool_realloc (specs->pool,
272                                                  cstp->expected,
273                                                  sizeof(double) *
274                                                  cstp->n_expected);
275                   for ( i = cstp->n_expected - n ;
276                         i < cstp->n_expected;
277                         ++i )
278                     cstp->expected[i] = f;
279
280                 }
281             }
282         }
283       else
284         lex_put_back (lexer, '/');
285     }
286
287   if ( cstp->ranged && cstp->n_expected > 0 &&
288        cstp->n_expected != cstp->hi - cstp->lo + 1 )
289     {
290       msg(ME,
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);
295       return 0;
296     }
297
298   specs->n_tests++;
299   specs->test = pool_realloc (specs->pool,
300                               specs->test,
301                               sizeof(*specs->test) * specs->n_tests);
302
303   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
304
305   return 1;
306 }
307
308
309 int
310 npar_custom_binomial(struct lexer *lexer, struct dataset *ds, struct cmd_npar_tests *cmd UNUSED, void *aux)
311 {
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;
315
316   ((struct npar_test *)tp)->execute = binomial_execute;
317   ((struct npar_test *)tp)->insert_variables = one_sample_insert_variables;
318
319   btp->category1 = btp->category2 = btp->cutpoint = SYSMIS;
320
321   if ( lex_match(lexer, '(') )
322     {
323       if ( lex_force_num (lexer) )
324         {
325           btp->p = lex_number (lexer);
326           lex_get (lexer);
327           lex_force_match (lexer, ')');
328         }
329       else
330         return 0;
331     }
332
333   if ( lex_match (lexer, '=') )
334     {
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) )
338         {
339           if ( lex_match (lexer, '('))
340             {
341               lex_force_num (lexer);
342               btp->category1 = lex_number (lexer);
343               lex_get (lexer);
344               if ( ! lex_force_match (lexer, ',')) return 2;
345               if ( ! lex_force_num (lexer) ) return 2;
346               btp->category2 = lex_number (lexer);
347               lex_get (lexer);
348               lex_force_match (lexer, ')');
349             }
350         }
351       else
352         return 2;
353     }
354   else
355     {
356       if ( lex_match (lexer, '(') )
357         {
358           lex_force_num (lexer);
359           btp->cutpoint = lex_number (lexer);
360           lex_get (lexer);
361           lex_force_match (lexer, ')');
362         }
363     }
364
365   specs->n_tests++;
366   specs->test = pool_realloc (specs->pool,
367                               specs->test,
368                               sizeof(*specs->test) * specs->n_tests);
369
370   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
371
372   return 1;
373 }
374
375
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,
380                                     struct pool *pool
381                                     );
382
383
384 bool
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,
389                                struct pool *pool
390                                )
391 {
392   int n = 0;
393   bool paired = false;
394   bool with = false;
395   const struct variable **vlist1;
396   size_t n_vlist1;
397
398   const struct variable **vlist2;
399   size_t n_vlist2;
400
401   if (!parse_variables_const_pool (lexer, pool,
402                              dict,
403                              &vlist1, &n_vlist1,
404                              PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
405     return false;
406
407   if ( lex_match(lexer, T_WITH))
408     {
409       with = true;
410       if ( !parse_variables_const_pool (lexer, pool, dict,
411                                   &vlist2, &n_vlist2,
412                                   PV_NUMERIC | PV_NO_SCRATCH | PV_NO_DUPLICATE) )
413         return false;
414
415       paired = (lex_match (lexer, '(') &&
416                 lex_match_id (lexer, "PAIRED") && lex_match (lexer, ')'));
417     }
418
419
420   if ( with )
421     {
422       if (paired)
423         {
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);
428
429           test_parameters->n_pairs = n_vlist1 ;
430         }
431       else
432         {
433           test_parameters->n_pairs = n_vlist1 * n_vlist2;
434         }
435     }
436   else
437     {
438       test_parameters->n_pairs = (n_vlist1 * (n_vlist1 - 1)) / 2 ;
439     }
440
441   test_parameters->pairs =
442     pool_alloc (pool, sizeof ( variable_pair) * test_parameters->n_pairs);
443
444   if ( with )
445     {
446       if (paired)
447         {
448           int i;
449           assert (n_vlist1 == n_vlist2);
450           for ( i = 0 ; i < n_vlist1; ++i )
451             {
452               test_parameters->pairs[n][0] = vlist1[i];
453               test_parameters->pairs[n][1] = vlist2[i];
454               n++;
455             }
456         }
457       else
458         {
459           int i,j;
460           for ( i = 0 ; i < n_vlist1; ++i )
461             {
462               for ( j = 0 ; j < n_vlist2; ++j )
463                 {
464                   test_parameters->pairs[n][0] = vlist1[i];
465                   test_parameters->pairs[n][1] = vlist2[j];
466                   n++;
467                 }
468             }
469         }
470     }
471   else
472     {
473       int i,j;
474       for ( i = 0 ; i < n_vlist1 - 1; ++i )
475         {
476           for ( j = i + 1 ; j < n_vlist1; ++j )
477             {
478               assert ( n < test_parameters->n_pairs);
479               test_parameters->pairs[n][0] = vlist1[i];
480               test_parameters->pairs[n][1] = vlist1[j];
481               n++;
482             }
483         }
484     }
485
486   assert ( n == test_parameters->n_pairs);
487
488   return true;
489 }
490
491 int
492 npar_custom_wilcoxon (struct lexer *lexer,
493                       struct dataset *ds,
494                       struct cmd_npar_tests *cmd, void *aux )
495 {
496   struct npar_specs *specs = aux;
497
498   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
499   ((struct npar_test *)tp)->execute = NULL;
500
501   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
502                                       tp, specs->pool) )
503     return 0;
504
505   specs->n_tests++;
506   specs->test = pool_realloc (specs->pool,
507                               specs->test,
508                               sizeof(*specs->test) * specs->n_tests);
509   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
510
511   return 1;
512 }
513
514 int
515 npar_custom_mcnemar (struct lexer *lexer,
516                      struct dataset *ds,
517                      struct cmd_npar_tests *cmd, void *aux )
518 {
519   struct npar_specs *specs = aux;
520
521   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
522   ((struct npar_test *)tp)->execute = NULL;
523
524
525   if (!parse_two_sample_related_test (lexer, dataset_dict (ds),
526                                       cmd, tp, specs->pool) )
527     return 0;
528
529   specs->n_tests++;
530   specs->test = pool_realloc (specs->pool,
531                               specs->test,
532                               sizeof(*specs->test) * specs->n_tests);
533   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
534
535   return 1;
536 }
537
538 int
539 npar_custom_sign (struct lexer *lexer, struct dataset *ds,
540                   struct cmd_npar_tests *cmd, void *aux )
541 {
542   struct npar_specs *specs = aux;
543
544   struct two_sample_test *tp = pool_alloc(specs->pool, sizeof(*tp));
545   ((struct npar_test *)tp)->execute = NULL;
546
547
548   if (!parse_two_sample_related_test (lexer, dataset_dict (ds), cmd,
549                                       tp, specs->pool) )
550     return 0;
551
552   specs->n_tests++;
553   specs->test = pool_realloc (specs->pool,
554                               specs->test,
555                               sizeof(*specs->test) * specs->n_tests);
556   specs->test[specs->n_tests - 1] = (struct npar_test *) tp;
557
558   return 1;
559 }
560
561 /* Insert the variables for TEST into VAR_HASH */
562 void
563 one_sample_insert_variables (const struct npar_test *test,
564                             struct const_hsh_table *var_hash)
565 {
566   int i;
567   struct one_sample_test *ost = (struct one_sample_test *) test;
568
569   for ( i = 0 ; i < ost->n_vars ; ++i )
570     const_hsh_insert (var_hash, ost->vars[i]);
571 }
572