e10d90145bce413ce0913607382a662271840796
[pspp-builds.git] / src / language / stats / means.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/hash.h>
31 #include <libpspp/magic.h>
32 #include <libpspp/message.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* (headers) */
38
39 /* (specification)
40    means (mns_):
41      *tables=custom;
42      +format=lab:!labels/nolabels/nocatlabs,
43             name:!names/nonames,
44             val:!values/novalues,
45             fmt:!table/tree;
46      missing=miss:!table/include/dependent;
47      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
48      +statistics[st_]=anova,linearity,all,none.
49 */
50 /* (declarations) */
51 /* (functions) */
52
53 /* TABLES: Variable lists for each dimension. */
54 static int n_dim;               /* Number of dimensions. */
55 static size_t *nv_dim;          /* Number of variables in each dimension. */
56 static const struct variable ***v_dim;  /* Variables in each dimension.  */
57
58 /* VARIABLES: List of variables. */
59 static struct variable **v_var;
60
61 /* Parses and executes the T-TEST procedure. */
62 int
63 cmd_means (struct lexer *lexer, struct dataset *ds)
64 {
65   struct cmd_means cmd;
66   int success = CMD_FAILURE;
67
68   n_dim = 0;
69   nv_dim = NULL;
70   v_dim = NULL;
71   v_var = NULL;
72
73   if (!parse_means (lexer, ds, &cmd, NULL))
74     goto free;
75
76   if (cmd.sbc_cells)
77     {
78       int i;
79       for (i = 0; i < MNS_CL_count; i++)
80         if (cmd.a_cells[i])
81           break;
82       if (i >= MNS_CL_count)
83         cmd.a_cells[MNS_CL_ALL] = 1;
84     }
85   else
86     cmd.a_cells[MNS_CL_DEFAULT] = 1;
87   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
88     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
89   if (cmd.a_cells[MNS_CL_ALL])
90     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
91
92   if (cmd.sbc_statistics)
93     {
94       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
95         cmd.a_statistics[MNS_ST_ANOVA] = 1;
96       if (cmd.a_statistics[MNS_ST_ALL])
97         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
98     }
99
100   if (!cmd.sbc_tables)
101     {
102       msg (SE, _("Missing required subcommand TABLES."));
103       goto free;
104     }
105
106   success = CMD_SUCCESS;
107
108 free:
109   {
110     int i;
111
112     for (i = 0; i < n_dim; i++)
113       free (v_dim[i]);
114     free (nv_dim);
115     free (v_dim);
116     free (v_var);
117   }
118
119   return success;
120 }
121
122 /* Parses the TABLES subcommand. */
123 static int
124 mns_custom_tables (struct lexer *lexer, struct dataset *ds, struct cmd_means *cmd, void *aux UNUSED)
125 {
126   struct const_var_set *var_set;
127
128   if (!lex_match_id (lexer, "TABLES")
129       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL)
130       && lex_token (lexer) != T_ALL)
131     return 2;
132   lex_match (lexer, '=');
133
134   if (cmd->sbc_tables)
135     {
136       msg (SE, _("TABLES subcommand may not appear more "
137                  "than once."));
138       return 0;
139     }
140
141   var_set = const_var_set_create_from_dict (dataset_dict (ds));
142   assert (var_set != NULL);
143
144   do
145     {
146       size_t nvl;
147       const struct variable **vl;
148
149       if (!parse_const_var_set_vars (lexer, var_set, &vl, &nvl,
150                                PV_NO_DUPLICATE | PV_NO_SCRATCH))
151         goto lossage;
152
153       n_dim++;
154       nv_dim = xnrealloc (nv_dim, n_dim, sizeof *nv_dim);
155       v_dim = xnrealloc (v_dim, n_dim, sizeof *v_dim);
156
157       nv_dim[n_dim - 1] = nvl;
158       v_dim[n_dim - 1] = vl;
159     }
160   while (lex_match (lexer, T_BY));
161
162   const_var_set_destroy (var_set);
163   return 1;
164
165  lossage:
166   const_var_set_destroy (var_set);
167   return 0;
168 }
169
170 /*
171    Local Variables:
172    mode: c
173    End:
174 */