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