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;
58 /* Independent samples mode */
59 const struct variable *gvar;
64 tt.wv = dict_get_weight (dict);
68 tt.missing_type = MISS_ANALYSIS;
72 lex_match (lexer, T_EQUALS);
74 for (; lex_token (lexer) != T_ENDCMD; )
76 lex_match (lexer, T_SLASH);
77 if (lex_match_id (lexer, "TESTVAL"))
80 tt.mode = MODE_SINGLE;
81 lex_match (lexer, T_EQUALS);
82 lex_force_num (lexer);
83 testval = lex_number (lexer);
86 else if (lex_match_id (lexer, "GROUPS"))
91 lex_match (lexer, T_EQUALS);
93 if (NULL == (gvar = parse_variable (lexer, dict)))
96 if (lex_match (lexer, T_LPAREN))
99 value_init (&gval0, var_get_width (gvar));
100 parse_value (lexer, &gval0, gvar);
102 if (lex_match (lexer, T_COMMA))
104 value_init (&gval1, var_get_width (gvar));
105 parse_value (lexer, &gval1, gvar);
109 lex_force_match (lexer, T_RPAREN);
113 value_init (&gval0, 0);
114 value_init (&gval1, 0);
120 if ( cut == true && var_is_alpha (gvar))
122 msg (SE, _("When applying GROUPS to a string variable, two "
123 "values must be specified."));
127 else if (lex_match_id (lexer, "PAIRS"))
132 tt.mode = MODE_PAIRED;
133 lex_match (lexer, T_EQUALS);
135 if (!parse_variables_const (lexer, dict,
137 PV_NO_DUPLICATE | PV_NUMERIC))
140 if ( lex_match (lexer, T_WITH))
143 if (!parse_variables_const (lexer, dict,
145 PV_NO_DUPLICATE | PV_NUMERIC))
148 if (lex_match (lexer, T_LPAREN)
149 && lex_match_id (lexer, "PAIRED")
150 && lex_match (lexer, T_RPAREN))
155 msg (SE, _("PAIRED was specified but the number of variables "
156 "preceding WITH (%zu) did not match the number "
167 n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
171 n_pairs = n_v1 * n_v2;
173 pairs = xcalloc (sizeof *pairs, n_pairs);
181 for (i = 0 ; i < n_v1; ++i)
183 vp *pair = &pairs[i];
190 for (i = 0 ; i < n_v1; ++i)
193 for (j = 0 ; j < n_v2; ++j)
195 vp *pair = &pairs[x++];
205 for (i = 0 ; i < n_v1; ++i)
209 for (j = i + 1 ; j < n_v1; ++j)
211 vp *pair = &pairs[x++];
220 else if (lex_match_id (lexer, "VARIABLES"))
222 if ( tt.mode == MODE_PAIRED)
224 msg (SE, _("VARIABLES subcommand may not be used with PAIRS."));
228 lex_match (lexer, T_EQUALS);
230 if (!parse_variables_const (lexer, dict,
233 PV_NO_DUPLICATE | PV_NUMERIC))
236 else if ( lex_match_id (lexer, "MISSING"))
238 lex_match (lexer, T_EQUALS);
239 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
241 if (lex_match_id (lexer, "INCLUDE"))
243 tt.exclude = MV_SYSTEM;
245 else if (lex_match_id (lexer, "EXCLUDE"))
249 else if (lex_match_id (lexer, "LISTWISE"))
251 tt.missing_type = MISS_LISTWISE;
253 else if (lex_match_id (lexer, "ANALYSIS"))
255 tt.missing_type = MISS_ANALYSIS;
259 lex_error (lexer, NULL);
262 lex_match (lexer, T_COMMA);
265 else if (lex_match_id (lexer, "CRITERIA"))
267 lex_match (lexer, T_EQUALS);
268 if ( lex_force_match_id (lexer, "CIN"))
269 if ( lex_force_match (lexer, T_LPAREN))
271 lex_force_num (lexer);
272 tt.confidence = lex_number (lexer);
274 lex_force_match (lexer, T_RPAREN);
279 lex_error (lexer, NULL);
284 if ( mode_count != 1)
286 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
287 "must be specified."));
291 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
293 msg (SE, _("One or more VARIABLES must be specified."));
299 /* Deal with splits etc */
301 struct casereader *group;
302 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
304 while (casegrouper_get_next_group (grouper, &group))
306 if ( tt.mode == MODE_SINGLE)
308 if ( tt.missing_type == MISS_LISTWISE )
309 group = casereader_create_filter_missing (group,
313 one_sample_run (&tt, testval, group);
315 else if ( tt.mode == MODE_PAIRED)
317 if ( tt.missing_type == MISS_LISTWISE )
319 group = casereader_create_filter_missing (group,
323 group = casereader_create_filter_missing (group,
329 paired_run (&tt, n_pairs, pairs, group);
331 else /* tt.mode == MODE_INDEP */
333 if ( tt.missing_type == MISS_LISTWISE )
335 group = casereader_create_filter_missing (group,
340 group = casereader_create_filter_missing (group,
347 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
351 ok = casegrouper_destroy (grouper);
352 ok = proc_commit (ds) && ok;
361 return ok ? CMD_SUCCESS : CMD_FAILURE;