1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 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 <gsl/gsl_cdf.h>
20 #include <gsl/gsl_matrix.h>
23 #include "data/case.h"
24 #include "data/casegrouper.h"
25 #include "data/casereader.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/format.h"
29 #include "data/value.h"
30 #include "language/command.h"
31 #include "language/dictionary/split-file.h"
32 #include "language/lexer/lexer.h"
33 #include "language/lexer/value-parser.h"
34 #include "language/lexer/variable-parser.h"
35 #include "libpspp/ll.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "libpspp/taint.h"
39 #include "linreg/sweep.h"
40 #include "math/categoricals.h"
41 #include "math/covariance.h"
42 #include "math/moments.h"
43 #include "output/tab.h"
46 #define _(msgid) gettext (msgid)
51 const struct variable **dep_vars;
54 const struct variable **factor_vars;
56 enum mv_class exclude;
58 /* The weight variable */
59 const struct variable *wv;
67 struct moments *totals;
70 static void output_glm (const struct glm_spec *, const struct glm_workspace *ws);
71 static void run_glm (const struct glm_spec *cmd, struct casereader *input, const struct dataset *ds);
74 cmd_glm (struct lexer *lexer, struct dataset *ds)
76 const struct dictionary *dict = dataset_dict (ds);
79 glm.n_factor_vars = 0;
81 glm.factor_vars = NULL;
84 glm.wv = dict_get_weight (dict);
87 if (!parse_variables_const (lexer, dict,
88 &glm.dep_vars, &glm.n_dep_vars,
89 PV_NO_DUPLICATE | PV_NUMERIC))
92 lex_force_match (lexer, T_BY);
94 if (!parse_variables_const (lexer, dict,
95 &glm.factor_vars, &glm.n_factor_vars,
96 PV_NO_DUPLICATE | PV_NUMERIC))
99 if ( glm.n_dep_vars > 1)
101 msg (ME, _("Multivariate analysis is not yet implemented"));
105 struct const_var_set *factors = const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
108 while (lex_token (lexer) != T_ENDCMD)
110 lex_match (lexer, T_SLASH);
112 if (lex_match_id (lexer, "MISSING"))
114 lex_match (lexer, T_EQUALS);
115 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
117 if (lex_match_id (lexer, "INCLUDE"))
119 glm.exclude = MV_SYSTEM;
121 else if (lex_match_id (lexer, "EXCLUDE"))
123 glm.exclude = MV_ANY;
127 lex_error (lexer, NULL);
132 else if (lex_match_id (lexer, "INTERCEPT"))
134 lex_match (lexer, T_EQUALS);
135 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
137 if (lex_match_id (lexer, "INCLUDE"))
139 glm.intercept = true;
141 else if (lex_match_id (lexer, "EXCLUDE"))
143 glm.intercept = false;
147 lex_error (lexer, NULL);
152 else if (lex_match_id (lexer, "DESIGN"))
155 const struct variable **des;
156 lex_match (lexer, T_EQUALS);
158 parse_const_var_set_vars (lexer, factors, &des, &n_des, 0);
162 lex_error (lexer, NULL);
169 struct casegrouper *grouper;
170 struct casereader *group;
173 grouper = casegrouper_create_splits (proc_open (ds), dict);
174 while (casegrouper_get_next_group (grouper, &group))
175 run_glm (&glm, group, ds);
176 ok = casegrouper_destroy (grouper);
177 ok = proc_commit (ds) && ok;
186 static void dump_matrix (const gsl_matrix *m);
189 run_glm (const struct glm_spec *cmd, struct casereader *input, const struct dataset *ds)
193 struct dictionary *dict = dataset_dict (ds);
194 struct casereader *reader;
197 struct glm_workspace ws;
199 struct categoricals *cats = categoricals_create (cmd->factor_vars, cmd->n_factor_vars,
200 cmd->wv, cmd->exclude,
204 struct covariance *cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
206 cmd->wv, cmd->exclude);
209 c = casereader_peek (input, 0);
212 casereader_destroy (input);
215 output_split_file_values (ds, c);
218 taint = taint_clone (casereader_get_taint (input));
220 ws.totals = moments_create (MOMENT_VARIANCE);
222 bool warn_bad_weight = true;
223 for (reader = casereader_clone (input);
224 (c = casereader_read (reader)) != NULL; case_unref (c))
226 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
228 for ( v = 0; v < cmd->n_dep_vars; ++v)
229 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f, weight);
231 covariance_accumulate_pass1 (cov, c);
233 casereader_destroy (reader);
235 categoricals_done (cats);
237 for (reader = casereader_clone (input);
238 (c = casereader_read (reader)) != NULL; case_unref (c))
240 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
242 for ( v = 0; v < cmd->n_dep_vars; ++v)
243 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f, weight);
245 covariance_accumulate_pass2 (cov, c);
247 casereader_destroy (reader);
250 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
254 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
260 gsl_matrix_free (cm);
263 if (!taint_has_tainted_successor (taint))
264 output_glm (cmd, &ws);
266 taint_destroy (taint);
270 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
272 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
276 const int heading_columns = 1;
277 const int heading_rows = 1;
278 struct tab_table *t ;
281 int nr = heading_rows + 4 + cmd->n_factor_vars;
285 t = tab_create (nc, nr);
286 tab_title (t, _("Tests of Between-Subjects Effects"));
288 tab_headers (t, heading_columns, 0, heading_rows, 0);
296 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
297 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
299 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
301 /* TRANSLATORS: The parameter is a roman numeral */
302 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Type %s Sum of Squares"), "III");
303 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
304 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
305 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
306 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
309 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
311 double intercept, n_total;
315 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
316 intercept = pow2 (mean * n_total) / n_total;
318 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
319 tab_double (t, 1, r, 0, intercept, NULL);
320 tab_double (t, 2, r, 0, 1.00, wfmt);
322 tab_double (t, 3, r, 0, intercept / 1.0 , NULL);
326 for (f = 0; f < cmd->n_factor_vars; ++f)
328 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE,
329 var_to_string (cmd->factor_vars[f]));
332 tab_text (t, 0, r++, TAB_LEFT | TAT_TITLE, _("Error"));
336 double ssq = intercept + ws->total_ssq;
337 double mse = ssq / n_total;
338 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
339 tab_double (t, 1, r, 0, ssq, NULL);
340 tab_double (t, 2, r, 0, n_total, wfmt);
345 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
347 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
348 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
354 void dump_matrix (const gsl_matrix *m)
357 for (i = 0; i < m->size1; ++i)
359 for (j = 0; j < m->size2; ++j)
361 double x = gsl_matrix_get (m, i, j);