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