T-TEST: Allow comma to be omitted in GROUPS subcommand.
[pspp] / src / language / stats / t-test-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011, 2015 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 "libpspp/message.h"
20
21 #include "data/casegrouper.h"
22 #include "data/casereader.h"
23 #include "data/dictionary.h"
24 #include "data/dataset.h"
25 #include "data/missing-values.h"
26
27 #include "language/lexer/lexer.h"
28 #include "language/command.h"
29 #include "language/lexer/variable-parser.h"
30 #include "language/lexer/value-parser.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 #include "t-test.h"
36
37 int
38 cmd_t_test (struct lexer *lexer, struct dataset *ds)
39 {
40   bool ok;
41   const struct dictionary *dict = dataset_dict (ds);
42   struct tt tt;
43   int mode_count = 0;
44
45   /* Variables pertaining to the paired mode */
46   const struct variable **v1 = NULL;
47   size_t n_v1;
48   const struct variable **v2 = NULL;
49   size_t n_v2;
50           
51   size_t n_pairs = 0;
52   vp *pairs = NULL;
53
54
55   /* One sample mode */
56   double testval = SYSMIS;
57
58   /* Independent samples mode */
59   const struct variable *gvar;
60   union value gval0;
61   union value gval1;
62   bool cut = false;
63
64   tt.wv = dict_get_weight (dict);
65   tt.dict = dict;
66   tt.confidence = 0.95;
67   tt.exclude = MV_ANY;
68   tt.missing_type = MISS_ANALYSIS;
69   tt.n_vars = 0;
70   tt.vars = NULL;
71   tt.mode = MODE_undef;
72
73   lex_match (lexer, T_EQUALS);
74
75   for (; lex_token (lexer) != T_ENDCMD; )
76     {
77       lex_match (lexer, T_SLASH);
78       if (lex_match_id (lexer, "TESTVAL"))
79         {
80           mode_count++;
81           tt.mode = MODE_SINGLE;
82           lex_match (lexer, T_EQUALS);
83           lex_force_num (lexer);
84           testval = lex_number (lexer);
85           lex_get (lexer);
86         }
87       else if (lex_match_id (lexer, "GROUPS"))
88         {
89           mode_count++;
90           cut = false;
91           tt.mode = MODE_INDEP;
92           lex_match (lexer, T_EQUALS);
93
94           if (NULL == (gvar = parse_variable (lexer, dict)))
95             goto parse_failed;
96       
97           if (lex_match (lexer, T_LPAREN))
98             {
99
100               value_init (&gval0, var_get_width (gvar));
101               parse_value (lexer, &gval0, gvar);
102               cut = true;
103               if (lex_token (lexer) != T_RPAREN)
104                 {
105                   lex_match (lexer, T_COMMA);
106                   value_init (&gval1, var_get_width (gvar));
107                   parse_value (lexer, &gval1, gvar);
108                   cut = false;
109                 }
110
111               lex_force_match (lexer, T_RPAREN);
112             }
113           else
114             {
115               value_init (&gval0, 0);
116               value_init (&gval1, 0);
117               gval0.f = 1.0;
118               gval1.f = 2.0;
119               cut = false;
120             }
121
122           if ( cut == true && var_is_alpha (gvar))
123             {
124               msg (SE, _("When applying %s to a string variable, two "
125                          "values must be specified."), "GROUPS");
126               goto parse_failed;
127             }
128         }
129       else if (lex_match_id (lexer, "PAIRS"))
130         {
131           bool with = false;
132           bool paired = false;
133
134           if (tt.n_vars > 0)
135             {
136               msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
137               goto parse_failed;
138             }
139
140           mode_count++;
141           tt.mode = MODE_PAIRED;
142           lex_match (lexer, T_EQUALS);
143
144           if (!parse_variables_const (lexer, dict,
145                                       &v1, &n_v1,
146                                       PV_NO_DUPLICATE | PV_NUMERIC))
147             goto parse_failed;
148
149           if ( lex_match (lexer, T_WITH))
150             {
151               with = true;
152               if (!parse_variables_const (lexer, dict,
153                                           &v2, &n_v2,
154                                           PV_NO_DUPLICATE | PV_NUMERIC))
155                 goto parse_failed;
156
157               if (lex_match (lexer, T_LPAREN)
158                   && lex_match_id (lexer, "PAIRED")
159                   && lex_match (lexer, T_RPAREN))
160                 {
161                   paired = true;
162                   if (n_v1 != n_v2)
163                     {
164                       msg (SE, _("PAIRED was specified but the number of variables "
165                                  "preceding WITH (%zu) did not match the number "
166                                  "following (%zu)."),
167                            n_v1, n_v2);
168                       goto parse_failed;
169                     }
170                 }
171             }
172           {
173             int i;
174
175             if ( !with )
176               n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
177             else if ( paired )
178               n_pairs = n_v1;
179             else
180               n_pairs = n_v1 * n_v2;
181           
182             pairs = xcalloc (n_pairs, sizeof *pairs);
183
184             if ( with)
185               {
186                 int x = 0;
187                 if (paired)
188                   {
189                     for (i = 0 ; i < n_v1; ++i)
190                       {
191                         vp *pair = &pairs[i];
192                         (*pair)[0] = v1[i];
193                         (*pair)[1] = v2[i];
194                       } 
195                   }
196                 else
197                   {
198                     for (i = 0 ; i < n_v1; ++i)
199                       {
200                         int j;
201                         for (j = 0 ; j < n_v2; ++j)
202                           {
203                             vp *pair = &pairs[x++];
204                             (*pair)[0] = v1[i];
205                             (*pair)[1] = v2[j];
206                           }
207                       }
208                   }
209               }
210             else
211               {
212                 int x = 0;
213                 for (i = 0 ; i < n_v1; ++i)
214                   {
215                     int j;
216
217                     for (j = i + 1 ; j < n_v1; ++j)
218                       {
219                         vp *pair = &pairs[x++];
220                         (*pair)[0] = v1[i];
221                         (*pair)[1] = v1[j];
222                       }
223                   }
224               }
225
226           }
227         }
228       else if (lex_match_id (lexer, "VARIABLES"))
229         {
230           if ( tt.mode == MODE_PAIRED)
231             {
232               msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
233               goto parse_failed;
234             }
235
236           lex_match (lexer, T_EQUALS);
237
238           if (!parse_variables_const (lexer, dict,
239                                       &tt.vars,
240                                       &tt.n_vars,
241                                       PV_NO_DUPLICATE | PV_NUMERIC))
242             goto parse_failed;
243         }
244       else if ( lex_match_id (lexer, "MISSING"))
245         {
246           lex_match (lexer, T_EQUALS);
247           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
248             {
249               if (lex_match_id (lexer, "INCLUDE"))
250                 {
251                   tt.exclude = MV_SYSTEM;
252                 }
253               else if (lex_match_id (lexer, "EXCLUDE"))
254                 {
255                   tt.exclude = MV_ANY;
256                 }
257               else if (lex_match_id (lexer, "LISTWISE"))
258                 {
259                   tt.missing_type = MISS_LISTWISE;
260                 }
261               else if (lex_match_id (lexer, "ANALYSIS"))
262                 {
263                   tt.missing_type = MISS_ANALYSIS;
264                 }
265               else
266                 {
267                   lex_error (lexer, NULL);
268                   goto parse_failed;
269                 }
270               lex_match (lexer, T_COMMA);
271             }
272         }
273       else if (lex_match_id (lexer, "CRITERIA"))
274         {
275           lex_match (lexer, T_EQUALS);
276           if ( lex_match_id (lexer, "CIN") || lex_force_match_id (lexer, "CI"))
277             if ( lex_force_match (lexer, T_LPAREN))
278               {
279                 lex_force_num (lexer);
280                 tt.confidence = lex_number (lexer);
281                 lex_get (lexer);
282                 lex_force_match (lexer, T_RPAREN);
283               }
284         }
285       else 
286         {
287           lex_error (lexer, NULL);
288           goto parse_failed;
289         }
290     }
291
292   if ( mode_count != 1)
293     {
294       msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
295                  "must be specified."));
296       goto parse_failed;
297     }
298
299   if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
300     {
301       lex_sbc_missing ("VARIABLES");
302       goto parse_failed;
303     }
304
305
306
307   /* Deal with splits etc */
308   {
309     struct casereader *group;
310     struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
311
312     while (casegrouper_get_next_group (grouper, &group))
313       {
314         if ( tt.mode == MODE_SINGLE)
315           {
316             if ( tt.missing_type == MISS_LISTWISE )
317               group  = casereader_create_filter_missing (group,
318                                                          tt.vars, tt.n_vars,
319                                                          tt.exclude,
320                                                          NULL,  NULL);
321             one_sample_run (&tt, testval, group);
322           }
323         else if ( tt.mode == MODE_PAIRED)
324           {
325             if ( tt.missing_type == MISS_LISTWISE )
326               {
327                 group  = casereader_create_filter_missing (group,
328                                                            v1, n_v1,
329                                                            tt.exclude,
330                                                            NULL,  NULL);
331                 group  = casereader_create_filter_missing (group,
332                                                            v2, n_v2,
333                                                            tt.exclude,
334                                                            NULL,  NULL);
335               }
336
337             paired_run (&tt, n_pairs, pairs, group);
338           }
339         else /* tt.mode == MODE_INDEP */
340           {
341             if ( tt.missing_type == MISS_LISTWISE )
342               {
343                 group  = casereader_create_filter_missing (group,
344                                                            tt.vars, tt.n_vars,
345                                                            tt.exclude,
346                                                            NULL,  NULL);
347
348                 group  = casereader_create_filter_missing (group,
349                                                            &gvar, 1,
350                                                            tt.exclude,
351                                                            NULL,  NULL);
352
353               }
354
355             indep_run (&tt, gvar, cut, &gval0, &gval1, group);
356           }
357       }
358
359     ok = casegrouper_destroy (grouper);
360     ok = proc_commit (ds) && ok;
361   }
362
363   free (pairs);
364   free (v1);
365   free (v2);
366
367   free (tt.vars);
368
369   return ok ? CMD_SUCCESS : CMD_FAILURE;
370
371  parse_failed:
372   return CMD_FAILURE;
373 }
374