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