1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "libpspp/message.h"
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"
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"
33 #define _(msgid) gettext (msgid)
38 cmd_t_test (struct lexer *lexer, struct dataset *ds)
41 const struct dictionary *dict = dataset_dict (ds);
45 /* Variables pertaining to the paired mode */
46 const struct variable **v1 = NULL;
48 const struct variable **v2 = NULL;
56 double testval = SYSMIS;
58 /* Independent samples mode */
59 const struct variable *gvar;
64 tt.wv = dict_get_weight (dict);
68 tt.missing_type = MISS_ANALYSIS;
73 lex_match (lexer, T_EQUALS);
75 for (; lex_token (lexer) != T_ENDCMD; )
77 lex_match (lexer, T_SLASH);
78 if (lex_match_id (lexer, "TESTVAL"))
81 tt.mode = MODE_SINGLE;
82 lex_match (lexer, T_EQUALS);
83 lex_force_num (lexer);
84 testval = lex_number (lexer);
87 else if (lex_match_id (lexer, "GROUPS"))
92 lex_match (lexer, T_EQUALS);
94 if (NULL == (gvar = parse_variable (lexer, dict)))
97 if (lex_match (lexer, T_LPAREN))
100 value_init (&gval0, var_get_width (gvar));
101 parse_value (lexer, &gval0, gvar);
103 if (lex_match (lexer, T_COMMA))
105 value_init (&gval1, var_get_width (gvar));
106 parse_value (lexer, &gval1, gvar);
110 lex_force_match (lexer, T_RPAREN);
114 value_init (&gval0, 0);
115 value_init (&gval1, 0);
121 if ( cut == true && var_is_alpha (gvar))
123 msg (SE, _("When applying GROUPS to a string variable, two "
124 "values must be specified."));
128 else if (lex_match_id (lexer, "PAIRS"))
135 msg (SE, _("VARIABLES subcommand may not be used with PAIRS."));
140 tt.mode = MODE_PAIRED;
141 lex_match (lexer, T_EQUALS);
143 if (!parse_variables_const (lexer, dict,
145 PV_NO_DUPLICATE | PV_NUMERIC))
148 if ( lex_match (lexer, T_WITH))
151 if (!parse_variables_const (lexer, dict,
153 PV_NO_DUPLICATE | PV_NUMERIC))
156 if (lex_match (lexer, T_LPAREN)
157 && lex_match_id (lexer, "PAIRED")
158 && lex_match (lexer, T_RPAREN))
163 msg (SE, _("PAIRED was specified but the number of variables "
164 "preceding WITH (%zu) did not match the number "
175 n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
179 n_pairs = n_v1 * n_v2;
181 pairs = xcalloc (n_pairs, sizeof *pairs);
188 for (i = 0 ; i < n_v1; ++i)
190 vp *pair = &pairs[i];
197 for (i = 0 ; i < n_v1; ++i)
200 for (j = 0 ; j < n_v2; ++j)
202 vp *pair = &pairs[x++];
212 for (i = 0 ; i < n_v1; ++i)
216 for (j = i + 1 ; j < n_v1; ++j)
218 vp *pair = &pairs[x++];
227 else if (lex_match_id (lexer, "VARIABLES"))
229 if ( tt.mode == MODE_PAIRED)
231 msg (SE, _("VARIABLES subcommand may not be used with PAIRS."));
235 lex_match (lexer, T_EQUALS);
237 if (!parse_variables_const (lexer, dict,
240 PV_NO_DUPLICATE | PV_NUMERIC))
243 else if ( lex_match_id (lexer, "MISSING"))
245 lex_match (lexer, T_EQUALS);
246 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
248 if (lex_match_id (lexer, "INCLUDE"))
250 tt.exclude = MV_SYSTEM;
252 else if (lex_match_id (lexer, "EXCLUDE"))
256 else if (lex_match_id (lexer, "LISTWISE"))
258 tt.missing_type = MISS_LISTWISE;
260 else if (lex_match_id (lexer, "ANALYSIS"))
262 tt.missing_type = MISS_ANALYSIS;
266 lex_error (lexer, NULL);
269 lex_match (lexer, T_COMMA);
272 else if (lex_match_id (lexer, "CRITERIA"))
274 lex_match (lexer, T_EQUALS);
275 if ( lex_force_match_id (lexer, "CIN"))
276 if ( lex_force_match (lexer, T_LPAREN))
278 lex_force_num (lexer);
279 tt.confidence = lex_number (lexer);
281 lex_force_match (lexer, T_RPAREN);
286 lex_error (lexer, NULL);
291 if ( mode_count != 1)
293 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
294 "must be specified."));
298 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
300 lex_sbc_missing ("VARIABLES");
306 /* Deal with splits etc */
308 struct casereader *group;
309 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
311 while (casegrouper_get_next_group (grouper, &group))
313 if ( tt.mode == MODE_SINGLE)
315 if ( tt.missing_type == MISS_LISTWISE )
316 group = casereader_create_filter_missing (group,
320 one_sample_run (&tt, testval, group);
322 else if ( tt.mode == MODE_PAIRED)
324 if ( tt.missing_type == MISS_LISTWISE )
326 group = casereader_create_filter_missing (group,
330 group = casereader_create_filter_missing (group,
336 paired_run (&tt, n_pairs, pairs, group);
338 else /* tt.mode == MODE_INDEP */
340 if ( tt.missing_type == MISS_LISTWISE )
342 group = casereader_create_filter_missing (group,
347 group = casereader_create_filter_missing (group,
354 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
358 ok = casegrouper_destroy (grouper);
359 ok = proc_commit (ds) && ok;
368 return ok ? CMD_SUCCESS : CMD_FAILURE;