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;
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 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 if (NULL == (gvar = parse_variable (lexer, dict)))
98 if (lex_match (lexer, T_LPAREN))
101 value_init (&gval0, var_get_width (gvar));
102 parse_value (lexer, &gval0, gvar);
104 if (lex_token (lexer) != T_RPAREN)
106 lex_match (lexer, T_COMMA);
107 value_init (&gval1, var_get_width (gvar));
108 parse_value (lexer, &gval1, gvar);
112 lex_force_match (lexer, T_RPAREN);
116 value_init (&gval0, 0);
117 value_init (&gval1, 0);
123 if ( cut == true && var_is_alpha (gvar))
125 msg (SE, _("When applying %s to a string variable, two "
126 "values must be specified."), "GROUPS");
130 else if (lex_match_id (lexer, "PAIRS"))
137 msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
142 tt.mode = MODE_PAIRED;
143 lex_match (lexer, T_EQUALS);
145 if (!parse_variables_const (lexer, dict,
147 PV_NO_DUPLICATE | PV_NUMERIC))
150 if ( lex_match (lexer, T_WITH))
153 if (!parse_variables_const (lexer, dict,
155 PV_NO_DUPLICATE | PV_NUMERIC))
158 if (lex_match (lexer, T_LPAREN)
159 && lex_match_id (lexer, "PAIRED")
160 && lex_match (lexer, T_RPAREN))
165 msg (SE, _("PAIRED was specified but the number of variables "
166 "preceding WITH (%zu) did not match the number "
177 n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
181 n_pairs = n_v1 * n_v2;
183 pairs = xcalloc (n_pairs, sizeof *pairs);
190 for (i = 0 ; i < n_v1; ++i)
192 vp *pair = &pairs[i];
199 for (i = 0 ; i < n_v1; ++i)
202 for (j = 0 ; j < n_v2; ++j)
204 vp *pair = &pairs[x++];
214 for (i = 0 ; i < n_v1; ++i)
218 for (j = i + 1 ; j < n_v1; ++j)
220 vp *pair = &pairs[x++];
229 else if (lex_match_id (lexer, "VARIABLES"))
231 if ( tt.mode == MODE_PAIRED)
233 msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
237 lex_match (lexer, T_EQUALS);
239 if (!parse_variables_const (lexer, dict,
242 PV_NO_DUPLICATE | PV_NUMERIC))
245 else if ( lex_match_id (lexer, "MISSING"))
247 lex_match (lexer, T_EQUALS);
248 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
250 if (lex_match_id (lexer, "INCLUDE"))
252 tt.exclude = MV_SYSTEM;
254 else if (lex_match_id (lexer, "EXCLUDE"))
258 else if (lex_match_id (lexer, "LISTWISE"))
260 tt.missing_type = MISS_LISTWISE;
262 else if (lex_match_id (lexer, "ANALYSIS"))
264 tt.missing_type = MISS_ANALYSIS;
268 lex_error (lexer, NULL);
271 lex_match (lexer, T_COMMA);
274 else if (lex_match_id (lexer, "CRITERIA"))
276 lex_match (lexer, T_EQUALS);
277 if ( lex_match_id (lexer, "CIN") || lex_force_match_id (lexer, "CI"))
278 if ( lex_force_match (lexer, T_LPAREN))
280 if (!lex_force_num (lexer))
282 tt.confidence = lex_number (lexer);
284 lex_force_match (lexer, T_RPAREN);
289 lex_error (lexer, NULL);
294 if ( mode_count != 1)
296 msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
297 "must be specified."));
301 if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
303 lex_sbc_missing ("VARIABLES");
309 /* Deal with splits etc */
311 struct casereader *group;
312 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
314 while (casegrouper_get_next_group (grouper, &group))
316 if ( tt.mode == MODE_SINGLE)
318 if ( tt.missing_type == MISS_LISTWISE )
319 group = casereader_create_filter_missing (group,
323 one_sample_run (&tt, testval, group);
325 else if ( tt.mode == MODE_PAIRED)
327 if ( tt.missing_type == MISS_LISTWISE )
329 group = casereader_create_filter_missing (group,
333 group = casereader_create_filter_missing (group,
339 paired_run (&tt, n_pairs, pairs, group);
341 else /* tt.mode == MODE_INDEP */
343 if ( tt.missing_type == MISS_LISTWISE )
345 group = casereader_create_filter_missing (group,
350 group = casereader_create_filter_missing (group,
357 indep_run (&tt, gvar, cut, &gval0, &gval1, group);
361 ok = casegrouper_destroy (grouper);
362 ok = proc_commit (ds) && ok;
371 return ok ? CMD_SUCCESS : CMD_FAILURE;