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 if (NULL == (gvar = parse_variable (lexer, dict)))
99 gval_width = var_get_width (gvar);
100 value_init (&gval0, gval_width);
101 value_init (&gval1, gval_width);
103 if (lex_match (lexer, T_LPAREN))
105 parse_value (lexer, &gval0, gvar);
107 if (lex_token (lexer) != T_RPAREN)
109 lex_match (lexer, T_COMMA);
110 parse_value (lexer, &gval1, gvar);
114 if (! lex_force_match (lexer, T_RPAREN))
124 if ( cut == true && var_is_alpha (gvar))
126 msg (SE, _("When applying %s to a string variable, two "
127 "values must be specified."), "GROUPS");
131 else if (lex_match_id (lexer, "PAIRS"))
138 msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
143 tt.mode = MODE_PAIRED;
144 lex_match (lexer, T_EQUALS);
146 if (!parse_variables_const (lexer, dict,
148 PV_NO_DUPLICATE | PV_NUMERIC))
151 if ( lex_match (lexer, T_WITH))
154 if (!parse_variables_const (lexer, dict,
156 PV_NO_DUPLICATE | PV_NUMERIC))
159 if (lex_match (lexer, T_LPAREN)
160 && lex_match_id (lexer, "PAIRED")
161 && lex_match (lexer, T_RPAREN))
166 msg (SE, _("PAIRED was specified but the number of variables "
167 "preceding WITH (%zu) did not match the number "
178 n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
182 n_pairs = n_v1 * n_v2;
184 pairs = xcalloc (n_pairs, sizeof *pairs);
191 for (i = 0 ; i < n_v1; ++i)
193 vp *pair = &pairs[i];
200 for (i = 0 ; i < n_v1; ++i)
203 for (j = 0 ; j < n_v2; ++j)
205 vp *pair = &pairs[x++];
215 for (i = 0 ; i < n_v1; ++i)
219 for (j = i + 1 ; j < n_v1; ++j)
221 vp *pair = &pairs[x++];
230 else if (lex_match_id (lexer, "VARIABLES"))
232 if ( tt.mode == MODE_PAIRED)
234 msg (SE, _("%s subcommand may not be used with %s."), "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"))
253 tt.exclude = MV_SYSTEM;
255 else if (lex_match_id (lexer, "EXCLUDE"))
259 else if (lex_match_id (lexer, "LISTWISE"))
261 tt.missing_type = MISS_LISTWISE;
263 else if (lex_match_id (lexer, "ANALYSIS"))
265 tt.missing_type = MISS_ANALYSIS;
269 lex_error (lexer, NULL);
272 lex_match (lexer, T_COMMA);
275 else if (lex_match_id (lexer, "CRITERIA"))
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))
281 if (!lex_force_num (lexer))
283 tt.confidence = lex_number (lexer);
285 if (! lex_force_match (lexer, T_RPAREN))
291 lex_error (lexer, NULL);
296 if ( mode_count != 1)
298 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
299 "must be specified."));
303 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
305 lex_sbc_missing ("VARIABLES");
311 /* Deal with splits etc */
313 struct casereader *group;
314 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
316 while (casegrouper_get_next_group (grouper, &group))
318 if ( tt.mode == MODE_SINGLE)
320 if ( tt.missing_type == MISS_LISTWISE )
321 group = casereader_create_filter_missing (group,
325 one_sample_run (&tt, testval, group);
327 else if ( tt.mode == MODE_PAIRED)
329 if ( tt.missing_type == MISS_LISTWISE )
331 group = casereader_create_filter_missing (group,
335 group = casereader_create_filter_missing (group,
341 paired_run (&tt, n_pairs, pairs, group);
343 else /* tt.mode == MODE_INDEP */
345 if ( tt.missing_type == MISS_LISTWISE )
347 group = casereader_create_filter_missing (group,
352 group = casereader_create_filter_missing (group,
359 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
363 ok = casegrouper_destroy (grouper);
364 ok = proc_commit (ds) && ok;
368 if (gval_width != -1)
370 value_destroy (&gval0, gval_width);
371 value_destroy (&gval1, gval_width);
378 return ok ? CMD_SUCCESS : CMD_FAILURE;