1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010 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 <data/case.h>
20 #include <data/casegrouper.h>
21 #include <data/casereader.h>
23 #include <math/covariance.h>
24 #include <math/categoricals.h>
25 #include <math/moments.h>
26 #include <gsl/gsl_matrix.h>
27 #include <linreg/sweep.h>
29 #include <libpspp/ll.h>
31 #include <language/lexer/lexer.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/value-parser.h>
34 #include <language/command.h>
36 #include <data/procedure.h>
37 #include <data/value.h>
38 #include <data/dictionary.h>
40 #include <language/dictionary/split-file.h>
41 #include <libpspp/taint.h>
42 #include <libpspp/misc.h>
44 #include <gsl/gsl_cdf.h>
46 #include <data/format.h>
48 #include <libpspp/message.h>
50 #include <output/tab.h>
53 #define _(msgid) gettext (msgid)
58 const struct variable **dep_vars;
61 const struct variable **factor_vars;
63 enum mv_class exclude;
65 /* The weight variable */
66 const struct variable *wv;
74 struct moments *totals;
77 static void output_glm (const struct glm_spec *, const struct glm_workspace *ws);
78 static void run_glm (const struct glm_spec *cmd, struct casereader *input, const struct dataset *ds);
81 cmd_glm (struct lexer *lexer, struct dataset *ds)
83 const struct dictionary *dict = dataset_dict (ds);
86 glm.n_factor_vars = 0;
88 glm.factor_vars = NULL;
91 glm.wv = dict_get_weight (dict);
94 if (!parse_variables_const (lexer, dict,
95 &glm.dep_vars, &glm.n_dep_vars,
96 PV_NO_DUPLICATE | PV_NUMERIC))
99 lex_force_match (lexer, T_BY);
101 if (!parse_variables_const (lexer, dict,
102 &glm.factor_vars, &glm.n_factor_vars,
103 PV_NO_DUPLICATE | PV_NUMERIC))
106 if ( glm.n_dep_vars > 1)
108 msg (ME, _("Multivariate analysis is not yet implemented"));
112 struct const_var_set *factors = const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
115 while (lex_token (lexer) != '.')
117 lex_match (lexer, '/');
119 if (lex_match_id (lexer, "MISSING"))
121 lex_match (lexer, '=');
122 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
124 if (lex_match_id (lexer, "INCLUDE"))
126 glm.exclude = MV_SYSTEM;
128 else if (lex_match_id (lexer, "EXCLUDE"))
130 glm.exclude = MV_ANY;
134 lex_error (lexer, NULL);
139 else if (lex_match_id (lexer, "INTERCEPT"))
141 lex_match (lexer, '=');
142 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
144 if (lex_match_id (lexer, "INCLUDE"))
146 glm.intercept = true;
148 else if (lex_match_id (lexer, "EXCLUDE"))
150 glm.intercept = false;
154 lex_error (lexer, NULL);
159 else if (lex_match_id (lexer, "DESIGN"))
162 const struct variable **des;
163 lex_match (lexer, '=');
165 parse_const_var_set_vars (lexer, factors, &des, &n_des, 0);
169 lex_error (lexer, NULL);
176 struct casegrouper *grouper;
177 struct casereader *group;
180 grouper = casegrouper_create_splits (proc_open (ds), dict);
181 while (casegrouper_get_next_group (grouper, &group))
182 run_glm (&glm, group, ds);
183 ok = casegrouper_destroy (grouper);
184 ok = proc_commit (ds) && ok;
193 static void dump_matrix (const gsl_matrix *m);
196 run_glm (const struct glm_spec *cmd, struct casereader *input, const struct dataset *ds)
200 struct dictionary *dict = dataset_dict (ds);
201 struct casereader *reader;
204 struct glm_workspace ws;
206 struct categoricals *cats = categoricals_create (cmd->factor_vars, cmd->n_factor_vars,
207 cmd->wv, cmd->exclude,
211 struct covariance *cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
213 cmd->wv, cmd->exclude);
216 c = casereader_peek (input, 0);
219 casereader_destroy (input);
222 output_split_file_values (ds, c);
225 taint = taint_clone (casereader_get_taint (input));
227 ws.totals = moments_create (MOMENT_VARIANCE);
229 bool warn_bad_weight = true;
230 for (reader = casereader_clone (input);
231 (c = casereader_read (reader)) != NULL; case_unref (c))
233 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
235 for ( v = 0; v < cmd->n_dep_vars; ++v)
236 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f, weight);
238 covariance_accumulate_pass1 (cov, c);
240 casereader_destroy (reader);
242 categoricals_done (cats);
244 for (reader = casereader_clone (input);
245 (c = casereader_read (reader)) != NULL; case_unref (c))
247 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
249 for ( v = 0; v < cmd->n_dep_vars; ++v)
250 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f, weight);
252 covariance_accumulate_pass2 (cov, c);
254 casereader_destroy (reader);
257 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
261 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
268 if (!taint_has_tainted_successor (taint))
269 output_glm (cmd, &ws);
271 taint_destroy (taint);
275 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
277 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
281 const int heading_columns = 1;
282 const int heading_rows = 1;
283 struct tab_table *t ;
286 int nr = heading_rows + 4 + cmd->n_factor_vars;
290 t = tab_create (nc, nr);
291 tab_title (t, _("Tests of Between-Subjects Effects"));
293 tab_headers (t, heading_columns, 0, heading_rows, 0);
301 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
302 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
304 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
306 /* TRANSLATORS: The parameter is a roman numeral */
307 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Type %s Sum of Squares"), "III");
308 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
309 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
310 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
311 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
314 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
316 double intercept, n_total;
320 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
321 intercept = pow2 (mean * n_total) / n_total;
323 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
324 tab_double (t, 1, r, 0, intercept, NULL);
325 tab_double (t, 2, r, 0, 1.00, wfmt);
327 tab_double (t, 3, r, 0, intercept / 1.0 , NULL);
331 for (f = 0; f < cmd->n_factor_vars; ++f)
333 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE,
334 var_to_string (cmd->factor_vars[f]));
337 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE, _("Error"));
341 double ssq = intercept + ws->total_ssq;
342 double mse = ssq / n_total;
343 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
344 tab_double (t, 1, r, 0, ssq, NULL);
345 tab_double (t, 2, r, 0, n_total, wfmt);
350 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
352 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
353 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
359 void dump_matrix (const gsl_matrix *m)
362 for (i = 0; i < m->size1; ++i)
364 for (j = 0; j < m->size2; ++j)
366 double x = gsl_matrix_get (m, i, j);