b7a2da2065041fb5e2dd4d528391a6431a9e021d
[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
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include <data/dictionary.h>
26 #include <data/procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/hash.h>
32 #include <libpspp/magic.h>
33 #include <libpspp/message.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* (headers) */
39
40 /* (specification)
41    means (mns_):
42      *tables=custom;
43      +format=lab:!labels/nolabels/nocatlabs,
44             name:!names/nonames,
45             val:!values/novalues,
46             fmt:!table/tree;
47      missing=miss:!table/include/dependent;
48      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
49      +statistics[st_]=anova,linearity,all,none.
50 */
51 /* (declarations) */
52 /* (functions) */
53
54 /* TABLES: Variable lists for each dimension. */
55 static int n_dim;               /* Number of dimensions. */
56 static size_t *nv_dim;          /* Number of variables in each dimension. */
57 static struct variable ***v_dim;        /* Variables in each dimension.  */
58
59 /* VARIABLES: List of variables. */
60 static struct variable **v_var;
61
62 /* Parses and executes the T-TEST procedure. */
63 int
64 cmd_means (struct lexer *lexer, struct dataset *ds)
65 {
66   struct cmd_means cmd;
67   int success = CMD_FAILURE;
68   
69   n_dim = 0;
70   nv_dim = NULL;
71   v_dim = NULL;
72   v_var = NULL;
73
74   if (!parse_means (lexer, ds, &cmd, NULL))
75     goto free;
76
77   if (cmd.sbc_cells)
78     {
79       int i;
80       for (i = 0; i < MNS_CL_count; i++)
81         if (cmd.a_cells[i])
82           break;
83       if (i >= MNS_CL_count)
84         cmd.a_cells[MNS_CL_ALL] = 1;
85     }
86   else
87     cmd.a_cells[MNS_CL_DEFAULT] = 1;
88   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
89     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
90   if (cmd.a_cells[MNS_CL_ALL])
91     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
92
93   if (cmd.sbc_statistics)
94     {
95       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
96         cmd.a_statistics[MNS_ST_ANOVA] = 1;
97       if (cmd.a_statistics[MNS_ST_ALL])
98         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
99     }
100
101   if (!cmd.sbc_tables)
102     {
103       msg (SE, _("Missing required subcommand TABLES."));
104       goto free;
105     }
106
107   success = CMD_SUCCESS;
108
109 free:
110   {
111     int i;
112     
113     for (i = 0; i < n_dim; i++)
114       free (v_dim[i]);
115     free (nv_dim);
116     free (v_dim);
117     free (v_var);
118   }
119   
120   return success;
121 }
122
123 /* Parses the TABLES subcommand. */
124 static int
125 mns_custom_tables (struct lexer *lexer, struct dataset *ds, struct cmd_means *cmd, void *aux UNUSED)
126 {
127   struct var_set *var_set;
128   
129   if (!lex_match_id (lexer, "TABLES")
130       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL)
131       && lex_token (lexer) != T_ALL)
132     return 2;
133   lex_match (lexer, '=');
134
135   if (cmd->sbc_tables)
136     {
137       msg (SE, _("TABLES subcommand may not appear more "
138                  "than once."));
139       return 0;
140     }
141
142   var_set = var_set_create_from_dict (dataset_dict (ds));
143   assert (var_set != NULL);
144
145   do
146     {
147       size_t nvl;
148       struct variable **vl;
149
150       if (!parse_var_set_vars (lexer, var_set, &vl, &nvl,
151                                PV_NO_DUPLICATE | PV_NO_SCRATCH)) 
152         goto lossage;
153       
154       n_dim++;
155       nv_dim = xnrealloc (nv_dim, n_dim, sizeof *nv_dim);
156       v_dim = xnrealloc (v_dim, n_dim, sizeof *v_dim);
157
158       nv_dim[n_dim - 1] = nvl;
159       v_dim[n_dim - 1] = vl;
160     }
161   while (lex_match (lexer, T_BY));
162
163   var_set_destroy (var_set);
164   return 1;
165
166  lossage:
167   var_set_destroy (var_set);
168   return 0;
169 }
170
171 /* 
172    Local Variables:
173    mode: c
174    End:
175 */