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