bda3eb8568ac1bd55fb3b8b9f2881f8a65d9604b
[pspp-builds.git] / src / means.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include "alloc.h"
25 #include "command.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "error.h"
29 #include "magic.h"
30 #include "var.h"
31 /* (headers) */
32
33 #include "debug-print.h"
34
35 /* (specification)
36    means (mns_):
37      *tables=custom;
38      +variables=custom;
39      +crossbreak=custom;
40      +format=lab:!labels/nolabels/nocatlabs,
41             name:!names/nonames,
42             val:!values/novalues,
43             fmt:!table/tree;
44      +missing=miss:!table/include/dependent;
45      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
46      +statistics[st_]=anova,linearity,all,none.
47 */
48 /* (declarations) */
49 /* (functions) */
50
51 #if DEBUGGING
52 static void debug_print (struct cmd_means *cmd);
53 #endif
54
55 /* TABLES: Variable lists for each dimension. */
56 int n_dim;              /* Number of dimensions. */
57 int *nv_dim;            /* Number of variables in each dimension. */
58 struct variable ***v_dim;       /* Variables in each dimension.  */
59
60 /* VARIABLES: List of variables. */
61 int n_var;
62 struct variable **v_var;
63
64 /* Parses and executes the T-TEST procedure. */
65 int
66 cmd_means (void)
67 {
68   struct cmd_means cmd;
69   int success = CMD_FAILURE;
70   
71   n_dim = 0;
72   nv_dim = NULL;
73   v_dim = NULL;
74   v_var = NULL;
75
76   lex_match_id ("MEANS");
77   if (!parse_means (&cmd))
78     goto free;
79
80   if (cmd.sbc_cells)
81     {
82       int i;
83       for (i = 0; i < MNS_CL_count; i++)
84         if (cmd.a_cells[i])
85           break;
86       if (i >= MNS_CL_count)
87         cmd.a_cells[MNS_CL_ALL] = 1;
88     }
89   else
90     cmd.a_cells[MNS_CL_DEFAULT] = 1;
91   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
92     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
93   if (cmd.a_cells[MNS_CL_ALL])
94     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
95
96   if (cmd.sbc_statistics)
97     {
98       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
99         cmd.a_statistics[MNS_ST_ANOVA] = 1;
100       if (cmd.a_statistics[MNS_ST_ALL])
101         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
102     }
103
104   if (!cmd.sbc_tables)
105     {
106       msg (SE, _("Missing required subcommand TABLES."));
107       goto free;
108     }
109
110 #if DEBUGGING
111   debug_print (&cmd);
112 #endif
113   
114   success = CMD_SUCCESS;
115
116 free:
117   {
118     int i;
119     
120     for (i = 0; i < n_dim; i++)
121       free (v_dim[i]);
122     free (nv_dim);
123     free (v_dim);
124     free (v_var);
125   }
126   
127   return success;
128 }
129
130 /* Returns nonzero only if value V is valid as an endpoint for a
131    dependent variable in integer mode. */
132 int
133 validate_dependent_endpoint (double V)
134 {
135   return V == (int) V && V != LOWEST && V != HIGHEST;
136 }
137
138 /* Parses the TABLES subcommand. */
139 static int
140 mns_custom_tables (struct cmd_means *cmd)
141 {
142   struct dictionary *dict;
143   struct dictionary temp_dict;
144   
145   if (!lex_match_id ("TABLES")
146       && (token != T_ID || !is_varname (tokid))
147       && token != T_ALL)
148     return 2;
149   lex_match ('=');
150
151   if (cmd->sbc_tables || cmd->sbc_crossbreak)
152     {
153       msg (SE, _("TABLES or CROSSBREAK subcommand may not appear more "
154                  "than once."));
155       return 0;
156     }
157
158   if (cmd->sbc_variables)
159     {
160       dict = &temp_dict;
161       temp_dict.var = v_var;
162       temp_dict.nvar = n_var;
163       
164       {
165         int i;
166       
167         temp_dict.name_tab = hsh_create (8, compare_variables, hash_variable,
168                                          NULL, NULL);
169         for (i = 0; i < temp_dict.nvar; i++)
170           hsh_force_insert (temp_dict.name_tab, temp_dict.var[i]);
171       }
172     }
173   else
174     dict = &default_dict;
175
176   do
177     {
178       int nvl;
179       struct variable **vl;
180         
181       if (!parse_variables (dict, &vl, &nvl, PV_NO_DUPLICATE | PV_NO_SCRATCH))
182         return 0;
183       
184       n_dim++;
185       nv_dim = xrealloc (nv_dim, n_dim * sizeof (int));
186       v_dim = xrealloc (v_dim, n_dim * sizeof (struct variable **));
187
188       nv_dim[n_dim - 1] = nvl;
189       v_dim[n_dim - 1] = vl;
190
191       if (cmd->sbc_variables)
192         {
193           int i;
194
195           for (i = 0; i < nv_dim[0]; i++)
196             {
197               struct means_proc *v_inf = &v_dim[0][i]->p.mns;
198
199               if (v_inf->min == SYSMIS)
200                 {
201                   msg (SE, _("Variable %s specified on TABLES or "
202                              "CROSSBREAK, but not specified on "
203                              "VARIABLES."),
204                        v_dim[0][i]->name);
205                   return 0;
206                 }
207               
208               if (n_dim == 1)
209                 {
210                   v_inf->min = (int) v_inf->min;
211                   v_inf->max = (int) v_inf->max;
212                 } else {
213                   if (v_inf->min == LOWEST || v_inf->max == HIGHEST)
214                     {
215                       msg (SE, _("LOWEST and HIGHEST may not be used "
216                                  "for independent variables (%s)."),
217                            v_dim[0][i]->name);
218                       return 0;
219                     }
220                   if (v_inf->min != (int) v_inf->min
221                       || v_inf->max != (int) v_inf->max)
222                     {
223                       msg (SE, _("Independent variables (%s) may not "
224                                  "have noninteger endpoints in their "
225                                  "ranges."),
226                            v_dim[0][i]->name);
227                       return 0;
228                     }
229                 }
230             }
231         }
232     }
233   while (lex_match (T_BY));
234
235   /* Check for duplicates. */
236   {
237     int i;
238     
239     for (i = 0; i < default_dict.nvar; i++)
240       default_dict.var[i]->foo = 0;
241     for (i = 0; i < dict->nvar; i++)
242       if (dict->var[i]->foo++)
243         {
244           msg (SE, _("Variable %s is multiply specified on TABLES "
245                      "or CROSSBREAK."),
246                dict->var[i]->name);
247           return 0;
248         }
249   }
250   
251   if (cmd->sbc_variables)
252     hsh_destroy (temp_dict.name_tab);
253
254   return 1;
255 }
256
257 /* Parse CROSSBREAK subcommand. */
258 static int
259 mns_custom_crossbreak (struct cmd_means *cmd)
260 {
261   return mns_custom_tables (cmd);
262 }
263
264 /* Parses the VARIABLES subcommand. */
265 static int
266 mns_custom_variables (struct cmd_means *cmd)
267 {
268   if (cmd->sbc_tables)
269     {
270       msg (SE, _("VARIABLES must precede TABLES."));
271       return 0;
272     }
273
274   if (cmd->sbc_variables == 1)
275     {
276       int i;
277       
278       for (i = 0; i < default_dict.nvar; i++)
279         default_dict.var[i]->p.mns.min = SYSMIS;
280     }
281   
282   do
283     {
284       int orig_n = n_var;
285       
286       double min, max;
287       
288       if (!parse_variables (&default_dict, &v_var, &n_var,
289                             PV_APPEND | PV_NO_DUPLICATE | PV_NO_SCRATCH))
290         return 0;
291
292       if (!lex_force_match ('('))
293         return 0;
294
295       /* Lower value. */
296       if (token == T_ID
297           && (!strcmp (tokid, "LO") || lex_id_match ("LOWEST", tokid)))
298         min = LOWEST;
299       else
300         {
301           if (!lex_force_num ())
302             return 0;
303           min = tokval;
304         }
305       lex_get ();
306
307       lex_match (',');
308
309       /* Higher value. */
310       if (token == T_ID
311           && (!strcmp (tokid, "HI") || lex_id_match ("HIGHEST", tokid)))
312         max = HIGHEST;
313       else
314         {
315           if (!lex_force_num ())
316             return 0;
317           max = tokval;
318         }
319       lex_get ();
320
321       if (!lex_force_match (')'))
322         return 0;
323
324       /* Range check. */
325       if (max < min)
326         {
327           msg (SE, _("Upper value (%g) is less than lower value "
328                      "(%g) on VARIABLES subcommand."), max, min);
329           return 0;
330         }
331       
332       {
333         int i;
334
335         for (i = orig_n; i < n_var; i++)
336           {
337             struct means_proc *v_inf = &v_var[i]->p.mns;
338
339             v_inf->min = min;
340             v_inf->max = max;
341           }
342       }
343     }
344   while (token != '/' && token != '.');
345   
346   return 1;
347 }
348
349 #if DEBUGGING
350 static void
351 debug_print (struct cmd_means *cmd)
352 {
353   int i;
354   
355   printf ("MEANS");
356
357   if (cmd->sbc_variables)
358     {
359       int j = 0;
360       
361       printf (" VARIABLES=");
362       for (i = 0; i < default_dict.nvar; i++)
363         {
364           struct variable *v = default_dict.var[i];
365           
366           if (v->p.mns.min == SYSMIS)
367             continue;
368           if (j++)
369             printf (" ");
370           printf ("%s(", v->name);
371           if (v->p.mns.min == LOWEST)
372             printf ("LO");
373           else
374             printf ("%g", v->p.mns.min);
375           printf (",");
376           if (v->p.mns.max == HIGHEST)
377             printf ("HI");
378           else
379             printf ("%g", v->p.mns.max);
380           printf (")");
381         }
382       printf ("\n");
383     }
384
385   printf (" TABLES=");
386   for (i = 0; i < n_dim; i++)
387     {
388       int j;
389
390       if (i)
391         printf (" BY");
392
393       for (j = 0; j < nv_dim[i]; j++)
394         {
395           if (i || j)
396             printf (" ");
397           printf (v_dim[i][j]->name);
398         }
399     }
400   printf ("\n");
401 }
402 #endif /* DEBUGGING */
403
404 /* 
405    Local Variables:
406    mode: c
407    End:
408 */