96c1d703e32f2924f74e5450510e5ac22e4cbe8d
[pspp-builds.git] / src / language / stats / 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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <data/dictionary.h>
24 #include <libpspp/message.h>
25 #include <libpspp/alloc.h>
26 #include <language/command.h>
27 #include <libpspp/hash.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/message.h>
30 #include <libpspp/magic.h>
31 #include <data/variable.h>
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* (headers) */
37
38 /* (specification)
39    means (mns_):
40      *tables=custom;
41      +format=lab:!labels/nolabels/nocatlabs,
42             name:!names/nonames,
43             val:!values/novalues,
44             fmt:!table/tree;
45      +missing=miss:!table/include/dependent;
46      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
47      +statistics[st_]=anova,linearity,all,none.
48 */
49 /* (declarations) */
50 /* (functions) */
51
52 /* TABLES: Variable lists for each dimension. */
53 int n_dim;              /* Number of dimensions. */
54 size_t *nv_dim;         /* Number of variables in each dimension. */
55 struct variable ***v_dim;       /* Variables in each dimension.  */
56
57 /* VARIABLES: List of variables. */
58 int n_var;
59 struct variable **v_var;
60
61 /* Parses and executes the T-TEST procedure. */
62 int
63 cmd_means (void)
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 (&cmd))
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 cmd_means *cmd)
125 {
126   struct var_set *var_set;
127   
128   if (!lex_match_id ("TABLES")
129       && (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
130       && token != T_ALL)
131     return 2;
132   lex_match ('=');
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 = var_set_create_from_dict (default_dict);
142   assert (var_set != NULL);
143
144   do
145     {
146       size_t nvl;
147       struct variable **vl;
148
149       if (!parse_var_set_vars (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 (T_BY));
161
162   var_set_destroy (var_set);
163   return 1;
164
165  lossage:
166   var_set_destroy (var_set);
167   return 0;
168 }
169
170 /* 
171    Local Variables:
172    mode: c
173    End:
174 */