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