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)
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;
65 tt.wv = dict_get_weight (dict);
69 tt.missing_type = MISS_ANALYSIS;
74 lex_match (lexer, T_EQUALS);
76 for (; lex_token (lexer) != T_ENDCMD;)
78 lex_match (lexer, T_SLASH);
79 if (lex_match_id (lexer, "TESTVAL"))
82 tt.mode = MODE_SINGLE;
83 lex_match (lexer, T_EQUALS);
84 if (!lex_force_num (lexer))
86 testval = lex_number (lexer);
89 else if (lex_match_id (lexer, "GROUPS"))
94 lex_match (lexer, T_EQUALS);
96 int groups_start = lex_ofs (lexer);
97 if (NULL == (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 parse_value (lexer, &gval0, gvar);
108 if (lex_token (lexer) != T_RPAREN)
110 lex_match (lexer, T_COMMA);
111 parse_value (lexer, &gval1, gvar);
121 if (! lex_force_match (lexer, T_RPAREN))
131 int groups_end = lex_ofs (lexer) - 1;
133 if (n != 2 && var_is_alpha (gvar))
135 lex_ofs_error (lexer, groups_start, groups_end,
136 _("When applying %s to a string variable, two "
137 "values must be specified."), "GROUPS");
141 else if (lex_match_id (lexer, "PAIRS"))
148 lex_next_error (lexer, -1, -1,
149 _("%s subcommand may not be used with %s."),
150 "VARIABLES", "PAIRS");
155 tt.mode = MODE_PAIRED;
156 lex_match (lexer, T_EQUALS);
158 if (!parse_variables_const (lexer, dict,
160 PV_NO_DUPLICATE | PV_NUMERIC))
163 if (lex_match (lexer, T_WITH))
166 if (!parse_variables_const (lexer, dict,
168 PV_NO_DUPLICATE | PV_NUMERIC))
171 if (lex_match (lexer, T_LPAREN)
172 && lex_match_id (lexer, "PAIRED")
173 && lex_match (lexer, T_RPAREN))
178 msg (SE, _("PAIRED was specified but the number of variables "
179 "preceding WITH (%zu) did not match the number "
190 n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
194 n_pairs = n_v1 * n_v2;
196 pairs = xcalloc (n_pairs, sizeof *pairs);
203 for (i = 0 ; i < n_v1; ++i)
205 vp *pair = &pairs[i];
212 for (i = 0 ; i < n_v1; ++i)
215 for (j = 0 ; j < n_v2; ++j)
217 vp *pair = &pairs[x++];
227 for (i = 0 ; i < n_v1; ++i)
231 for (j = i + 1 ; j < n_v1; ++j)
233 vp *pair = &pairs[x++];
242 else if (lex_match_id (lexer, "VARIABLES"))
244 if (tt.mode == MODE_PAIRED)
246 lex_next_error (lexer, -1, -1,
247 _("%s subcommand may not be used with %s."),
248 "VARIABLES", "PAIRS");
252 lex_match (lexer, T_EQUALS);
254 if (!parse_variables_const (lexer, dict,
257 PV_NO_DUPLICATE | PV_NUMERIC))
260 else if (lex_match_id (lexer, "MISSING"))
262 lex_match (lexer, T_EQUALS);
263 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
265 if (lex_match_id (lexer, "INCLUDE"))
267 tt.exclude = MV_SYSTEM;
269 else if (lex_match_id (lexer, "EXCLUDE"))
273 else if (lex_match_id (lexer, "LISTWISE"))
275 tt.missing_type = MISS_LISTWISE;
277 else if (lex_match_id (lexer, "ANALYSIS"))
279 tt.missing_type = MISS_ANALYSIS;
283 lex_error (lexer, NULL);
286 lex_match (lexer, T_COMMA);
289 else if (lex_match_id (lexer, "CRITERIA"))
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))
295 if (!lex_force_num (lexer))
297 tt.confidence = lex_number (lexer);
299 if (! lex_force_match (lexer, T_RPAREN))
305 lex_error (lexer, NULL);
312 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
313 "must be specified."));
317 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
319 lex_sbc_missing (lexer, "VARIABLES");
325 /* Deal with splits etc */
327 struct casereader *group;
328 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
330 while (casegrouper_get_next_group (grouper, &group))
332 if (tt.mode == MODE_SINGLE)
334 if (tt.missing_type == MISS_LISTWISE)
335 group = casereader_create_filter_missing (group,
339 one_sample_run (&tt, testval, group);
341 else if (tt.mode == MODE_PAIRED)
343 if (tt.missing_type == MISS_LISTWISE)
345 group = casereader_create_filter_missing (group,
349 group = casereader_create_filter_missing (group,
355 paired_run (&tt, n_pairs, pairs, group);
357 else /* tt.mode == MODE_INDEP */
359 if (tt.missing_type == MISS_LISTWISE)
361 group = casereader_create_filter_missing (group,
366 group = casereader_create_filter_missing (group,
373 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
377 ok = casegrouper_destroy (grouper);
378 ok = proc_commit (ds) && ok;
382 if (gval_width != -1)
384 value_destroy (&gval0, gval_width);
385 value_destroy (&gval1, gval_width);
392 return ok ? CMD_SUCCESS : CMD_FAILURE;