1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2015 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)
42 /* Variables pertaining to the paired mode */
43 const struct variable **v1 = NULL;
45 const struct variable **v2 = NULL;
52 double testval = SYSMIS;
54 /* Independent samples mode */
55 const struct variable *gvar;
61 const struct dictionary *dict = dataset_dict (ds);
63 .wv = dict_get_weight (dict),
67 .missing_type = MISS_ANALYSIS,
72 lex_match (lexer, T_EQUALS);
75 while (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 if (!lex_force_num (lexer))
85 testval = lex_number (lexer);
88 else if (lex_match_id (lexer, "GROUPS"))
93 lex_match (lexer, T_EQUALS);
95 int groups_start = lex_ofs (lexer);
96 gvar = parse_variable (lexer, dict);
100 gval_width = var_get_width (gvar);
101 value_init (&gval0, gval_width);
102 value_init (&gval1, gval_width);
105 if (lex_match (lexer, T_LPAREN))
107 if (!parse_value (lexer, &gval0, gvar))
109 if (lex_token (lexer) != T_RPAREN)
111 lex_match (lexer, T_COMMA);
112 if (!parse_value (lexer, &gval1, gvar))
123 if (!lex_force_match (lexer, T_RPAREN))
133 int groups_end = lex_ofs (lexer) - 1;
135 if (n != 2 && var_is_alpha (gvar))
137 lex_ofs_error (lexer, groups_start, groups_end,
138 _("When applying %s to a string variable, two "
139 "values must be specified."), "GROUPS");
143 else if (lex_match_id (lexer, "PAIRS"))
150 lex_next_error (lexer, -1, -1,
151 _("%s subcommand may not be used with %s."),
152 "VARIABLES", "PAIRS");
157 tt.mode = MODE_PAIRED;
158 lex_match (lexer, T_EQUALS);
160 int vars_start = lex_ofs (lexer);
161 if (!parse_variables_const (lexer, dict,
163 PV_NO_DUPLICATE | PV_NUMERIC))
166 if (lex_match (lexer, T_WITH))
169 if (!parse_variables_const (lexer, dict,
171 PV_NO_DUPLICATE | PV_NUMERIC))
173 int vars_end = lex_ofs (lexer) - 1;
175 if (lex_match_phrase (lexer, "(PAIRED)"))
180 lex_ofs_error (lexer, vars_start, vars_end,
181 _("PAIRED was specified, but the number "
182 "of variables preceding WITH (%zu) "
183 "does not match the number following "
191 n_pairs = (paired ? n_v1
193 : (n_v1 * (n_v1 - 1)) / 2.0);
194 pairs = xcalloc (n_pairs, sizeof *pairs);
198 for (size_t i = 0; i < n_v1; ++i)
200 vp *pair = &pairs[i];
208 for (size_t i = 0; i < n_v1; ++i)
209 for (size_t j = 0; j < n_v2; ++j)
211 vp *pair = &pairs[x++];
219 for (size_t i = 0; i < n_v1; ++i)
220 for (size_t j = i + 1; j < n_v1; ++j)
222 vp *pair = &pairs[x++];
228 else if (lex_match_id (lexer, "VARIABLES"))
230 if (mode_count && tt.mode == MODE_PAIRED)
232 lex_next_error (lexer, -1, -1,
233 _("%s subcommand may not be used with %s."),
234 "VARIABLES", "PAIRS");
238 lex_match (lexer, T_EQUALS);
240 if (!parse_variables_const (lexer, dict,
243 PV_NO_DUPLICATE | PV_NUMERIC))
246 else if (lex_match_id (lexer, "MISSING"))
248 lex_match (lexer, T_EQUALS);
249 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
251 if (lex_match_id (lexer, "INCLUDE"))
252 tt.exclude = MV_SYSTEM;
253 else if (lex_match_id (lexer, "EXCLUDE"))
255 else if (lex_match_id (lexer, "LISTWISE"))
256 tt.missing_type = MISS_LISTWISE;
257 else if (lex_match_id (lexer, "ANALYSIS"))
258 tt.missing_type = MISS_ANALYSIS;
261 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE",
262 "LISTWISE", "ANALYSIS");
265 lex_match (lexer, T_COMMA);
268 else if (lex_match_id (lexer, "CRITERIA"))
270 lex_match (lexer, T_EQUALS);
271 if (!lex_match_id (lexer, "CIN") && !lex_match_id (lexer, "CI"))
273 lex_error_expecting (lexer, "CIN", "CI");
276 if (!lex_force_match (lexer, T_LPAREN))
278 if (!lex_force_num (lexer))
280 tt.confidence = lex_number (lexer);
282 if (!lex_force_match (lexer, T_RPAREN))
287 lex_error_expecting (lexer, "TESTVAL", "GROUPS", "PAIRS",
288 "VARIABLES", "MISSING", "CRITERIA");
295 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
296 "must be specified."));
300 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
302 lex_sbc_missing (lexer, "VARIABLES");
306 struct casereader *group;
307 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
308 while (casegrouper_get_next_group (grouper, &group))
312 if (tt.missing_type == MISS_LISTWISE)
313 group = casereader_create_filter_missing (group, tt.vars, tt.n_vars,
314 tt.exclude, NULL, NULL);
315 one_sample_run (&tt, testval, group);
319 if (tt.missing_type == MISS_LISTWISE)
321 group = casereader_create_filter_missing (group, v1, n_v1,
322 tt.exclude, NULL, NULL);
323 group = casereader_create_filter_missing (group, v2, n_v2,
324 tt.exclude, NULL, NULL);
326 paired_run (&tt, n_pairs, pairs, group);
330 if (tt.missing_type == MISS_LISTWISE)
332 group = casereader_create_filter_missing (group, tt.vars, tt.n_vars,
333 tt.exclude, NULL, NULL);
334 group = casereader_create_filter_missing (group, &gvar, 1,
335 tt.exclude, NULL, NULL);
337 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
341 ok = casegrouper_destroy (grouper);
342 ok = proc_commit (ds) && ok;
345 if (gval_width != -1)
347 value_destroy (&gval0, gval_width);
348 value_destroy (&gval1, gval_width);
355 return ok ? CMD_SUCCESS : CMD_FAILURE;