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