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>
21 #include <gsl/gsl_combination.h>
24 #include "data/case.h"
25 #include "data/casegrouper.h"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/format.h"
30 #include "data/value.h"
31 #include "language/command.h"
32 #include "language/dictionary/split-file.h"
33 #include "language/lexer/lexer.h"
34 #include "language/lexer/value-parser.h"
35 #include "language/lexer/variable-parser.h"
36 #include "libpspp/ll.h"
37 #include "libpspp/message.h"
38 #include "libpspp/misc.h"
39 #include "libpspp/taint.h"
40 #include "linreg/sweep.h"
41 #include "math/categoricals.h"
42 #include "math/covariance.h"
43 #include "math/interaction.h"
44 #include "math/moments.h"
45 #include "output/tab.h"
48 #define _(msgid) gettext (msgid)
53 const struct variable **dep_vars;
56 const struct variable **factor_vars;
58 size_t n_interactions;
59 struct interaction **interactions;
61 enum mv_class exclude;
63 /* The weight variable */
64 const struct variable *wv;
66 const struct dictionary *dict;
76 struct moments *totals;
78 struct categoricals *cats;
81 Sums of squares due to different variables. Element 0 is the SSE
82 for the entire model. For i > 0, element i is the SS due to
89 /* Default design: all possible interactions */
91 design_full (struct glm_spec *glm)
95 glm->n_interactions = (1 << glm->n_factor_vars) - 1;
97 glm->interactions = xcalloc (glm->n_interactions, sizeof *glm->interactions);
99 /* All subsets, with exception of the empty set, of [0, glm->n_factor_vars) */
100 for (sz = 1; sz <= glm->n_factor_vars; ++sz)
102 gsl_combination *c = gsl_combination_calloc (glm->n_factor_vars, sz);
106 struct interaction *iact = interaction_create (NULL);
108 for (e = 0 ; e < gsl_combination_k (c); ++e)
109 interaction_add_variable (iact, glm->factor_vars [gsl_combination_get (c, e)]);
111 glm->interactions[i++] = iact;
113 while (gsl_combination_next (c) == GSL_SUCCESS);
115 gsl_combination_free (c);
119 static void output_glm (const struct glm_spec *,
120 const struct glm_workspace *ws);
121 static void run_glm (struct glm_spec *cmd, struct casereader *input,
122 const struct dataset *ds);
125 static bool parse_design_spec (struct lexer *lexer, struct glm_spec *glm);
127 /* Define to 1 if the /DESIGN subcommand should not be optional */
128 #define DESIGN_MANDATORY 1
131 cmd_glm (struct lexer *lexer, struct dataset *ds)
134 struct const_var_set *factors = NULL;
137 glm.dict = dataset_dict (ds);
139 glm.n_factor_vars = 0;
140 glm.n_interactions = 0;
141 glm.interactions = NULL;
143 glm.factor_vars = NULL;
144 glm.exclude = MV_ANY;
145 glm.intercept = true;
146 glm.wv = dict_get_weight (glm.dict);
149 if (!parse_variables_const (lexer, glm.dict,
150 &glm.dep_vars, &glm.n_dep_vars,
151 PV_NO_DUPLICATE | PV_NUMERIC))
154 lex_force_match (lexer, T_BY);
156 if (!parse_variables_const (lexer, glm.dict,
157 &glm.factor_vars, &glm.n_factor_vars,
158 PV_NO_DUPLICATE | PV_NUMERIC))
161 if (glm.n_dep_vars > 1)
163 msg (ME, _("Multivariate analysis is not yet implemented"));
168 const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
170 while (lex_token (lexer) != T_ENDCMD)
172 lex_match (lexer, T_SLASH);
174 if (lex_match_id (lexer, "MISSING"))
176 lex_match (lexer, T_EQUALS);
177 while (lex_token (lexer) != T_ENDCMD
178 && lex_token (lexer) != T_SLASH)
180 if (lex_match_id (lexer, "INCLUDE"))
182 glm.exclude = MV_SYSTEM;
184 else if (lex_match_id (lexer, "EXCLUDE"))
186 glm.exclude = MV_ANY;
190 lex_error (lexer, NULL);
195 else if (lex_match_id (lexer, "INTERCEPT"))
197 lex_match (lexer, T_EQUALS);
198 while (lex_token (lexer) != T_ENDCMD
199 && lex_token (lexer) != T_SLASH)
201 if (lex_match_id (lexer, "INCLUDE"))
203 glm.intercept = true;
205 else if (lex_match_id (lexer, "EXCLUDE"))
207 glm.intercept = false;
211 lex_error (lexer, NULL);
216 else if (lex_match_id (lexer, "CRITERIA"))
218 lex_match (lexer, T_EQUALS);
219 if (lex_match_id (lexer, "ALPHA"))
221 if (lex_force_match (lexer, T_LPAREN))
223 if (! lex_force_num (lexer))
225 lex_error (lexer, NULL);
229 glm.alpha = lex_number (lexer);
231 if ( ! lex_force_match (lexer, T_RPAREN))
233 lex_error (lexer, NULL);
240 lex_error (lexer, NULL);
244 else if (lex_match_id (lexer, "METHOD"))
246 lex_match (lexer, T_EQUALS);
247 if ( !lex_force_match_id (lexer, "SSTYPE"))
249 lex_error (lexer, NULL);
253 if ( ! lex_force_match (lexer, T_LPAREN))
255 lex_error (lexer, NULL);
259 if ( ! lex_force_int (lexer))
261 lex_error (lexer, NULL);
265 if (3 != lex_integer (lexer))
267 msg (ME, _("Only type 3 sum of squares are currently implemented"));
273 if ( ! lex_force_match (lexer, T_RPAREN))
275 lex_error (lexer, NULL);
279 else if (lex_match_id (lexer, "DESIGN"))
281 lex_match (lexer, T_EQUALS);
283 if (! parse_design_spec (lexer, &glm))
287 if ( glm.n_interactions == 0)
289 msg (ME, _("One or more design variables must be given"));
295 if (glm.n_interactions > 0)
301 lex_error (lexer, NULL);
309 lex_error (lexer, _("/DESIGN is mandatory in GLM"));
317 struct casegrouper *grouper;
318 struct casereader *group;
321 grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
322 while (casegrouper_get_next_group (grouper, &group))
323 run_glm (&glm, group, ds);
324 ok = casegrouper_destroy (grouper);
325 ok = proc_commit (ds) && ok;
328 const_var_set_destroy (factors);
329 free (glm.factor_vars);
330 for (i = 0 ; i < glm.n_interactions; ++i)
331 interaction_destroy (glm.interactions[i]);
332 free (glm.interactions);
340 const_var_set_destroy (factors);
341 free (glm.factor_vars);
342 for (i = 0 ; i < glm.n_interactions; ++i)
343 interaction_destroy (glm.interactions[i]);
345 free (glm.interactions);
351 static void get_ssq (struct covariance *, gsl_vector *,
352 const struct glm_spec *);
355 not_dropped (size_t j, const size_t *dropped, size_t n_dropped)
359 for (i = 0; i < n_dropped; i++)
368 get_ssq (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
370 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
374 size_t *dropped = xcalloc (covariance_dim (cov), sizeof (*dropped));
375 const struct categoricals *cats = covariance_get_categoricals (cov);
377 for (k = 0; k < cmd->n_interactions; k++)
381 gsl_matrix *small_cov = NULL;
382 size_t n_dropped = 0;
383 for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
385 if (categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars)
386 == cmd->interactions[k])
388 assert (n_dropped < covariance_dim (cov));
389 dropped[n_dropped++] = i;
393 gsl_matrix_alloc (cm->size1 - n_dropped, cm->size2 - n_dropped);
394 gsl_matrix_set (small_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
395 for (i = 0; i < cm->size1; i++)
397 if (not_dropped (i, dropped, n_dropped))
400 for (j = 0; j < cm->size2; j++)
402 if (not_dropped (j, dropped, n_dropped))
404 gsl_matrix_set (small_cov, n, m,
405 gsl_matrix_get (cm, i, j));
412 reg_sweep (small_cov, 0);
413 gsl_vector_set (ssq, k + 1,
414 gsl_matrix_get (small_cov, 0, 0)
415 - gsl_vector_get (ssq, 0));
416 gsl_matrix_free (small_cov);
420 gsl_matrix_free (cm);
423 //static void dump_matrix (const gsl_matrix *m);
426 run_glm (struct glm_spec *cmd, struct casereader *input,
427 const struct dataset *ds)
429 bool warn_bad_weight = true;
432 struct dictionary *dict = dataset_dict (ds);
433 struct casereader *reader;
436 struct glm_workspace ws;
437 struct covariance *cov;
439 ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions,
440 cmd->wv, cmd->exclude,
441 NULL, NULL, NULL, NULL);
443 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
444 ws.cats, cmd->wv, cmd->exclude);
447 c = casereader_peek (input, 0);
450 casereader_destroy (input);
453 output_split_file_values (ds, c);
456 taint = taint_clone (casereader_get_taint (input));
458 ws.totals = moments_create (MOMENT_VARIANCE);
460 for (reader = casereader_clone (input);
461 (c = casereader_read (reader)) != NULL; case_unref (c))
463 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
465 for (v = 0; v < cmd->n_dep_vars; ++v)
466 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
469 covariance_accumulate_pass1 (cov, c);
471 casereader_destroy (reader);
474 (c = casereader_read (reader)) != NULL; case_unref (c))
476 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
478 for (v = 0; v < cmd->n_dep_vars; ++v)
479 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
482 covariance_accumulate_pass2 (cov, c);
484 casereader_destroy (reader);
487 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
491 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
496 Store the overall SSE.
498 ws.ssq = gsl_vector_alloc (cm->size1);
499 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
500 get_ssq (cov, ws.ssq, cmd);
503 gsl_matrix_free (cm);
506 if (!taint_has_tainted_successor (taint))
507 output_glm (cmd, &ws);
509 gsl_vector_free (ws.ssq);
511 covariance_destroy (cov);
512 moments_destroy (ws.totals);
514 taint_destroy (taint);
518 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
520 const struct fmt_spec *wfmt =
521 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
523 double n_total, mean;
524 double df_corr = 0.0;
529 const int heading_columns = 1;
530 const int heading_rows = 1;
534 int nr = heading_rows + 4 + cmd->n_interactions;
538 t = tab_create (nc, nr);
539 tab_title (t, _("Tests of Between-Subjects Effects"));
541 tab_headers (t, heading_columns, 0, heading_rows, 0);
543 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
545 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
546 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
548 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
550 /* TRANSLATORS: The parameter is a roman numeral */
551 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
552 _("Type %s Sum of Squares"), "III");
553 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
554 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
555 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
556 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
558 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
563 df_corr += categoricals_df_total (ws->cats);
565 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
568 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
574 const double intercept = pow2 (mean * n_total) / n_total;
575 const double df = 1.0;
576 const double F = intercept / df / mse;
577 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
578 tab_double (t, 1, r, 0, intercept, NULL);
579 tab_double (t, 2, r, 0, 1.00, wfmt);
580 tab_double (t, 3, r, 0, intercept / df, NULL);
581 tab_double (t, 4, r, 0, F, NULL);
582 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
587 for (f = 0; f < cmd->n_interactions; ++f)
589 struct string str = DS_EMPTY_INITIALIZER;
590 const double df = categoricals_df (ws->cats, f);
591 const double ssq = gsl_vector_get (ws->ssq, f + 1);
592 const double F = ssq / df / mse;
593 interaction_to_string (cmd->interactions[f], &str);
594 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
597 tab_double (t, 1, r, 0, ssq, NULL);
598 tab_double (t, 2, r, 0, df, wfmt);
599 tab_double (t, 3, r, 0, ssq / df, NULL);
600 tab_double (t, 4, r, 0, F, NULL);
602 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
608 /* Corrected Model */
609 const double df = df_corr - 1.0;
610 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
611 const double F = ssq / df / mse;
612 tab_double (t, 1, heading_rows, 0, ssq, NULL);
613 tab_double (t, 2, heading_rows, 0, df, wfmt);
614 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
615 tab_double (t, 4, heading_rows, 0, F, NULL);
617 tab_double (t, 5, heading_rows, 0,
618 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
622 const double df = n_total - df_corr;
623 const double ssq = gsl_vector_get (ws->ssq, 0);
624 const double mse = ssq / df;
625 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
626 tab_double (t, 1, r, 0, ssq, NULL);
627 tab_double (t, 2, r, 0, df, wfmt);
628 tab_double (t, 3, r++, 0, mse, NULL);
633 const double intercept = pow2 (mean * n_total) / n_total;
634 const double ssq = intercept + ws->total_ssq;
636 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
637 tab_double (t, 1, r, 0, ssq, NULL);
638 tab_double (t, 2, r, 0, n_total, wfmt);
643 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
646 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
647 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
654 dump_matrix (const gsl_matrix * m)
657 for (i = 0; i < m->size1; ++i)
659 for (j = 0; j < m->size2; ++j)
661 double x = gsl_matrix_get (m, i, j);
674 If the match succeeds, the variable will be placed in VAR.
675 Returns true if successful */
677 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
679 if (lex_token (lexer) != T_ID)
682 *var = parse_variable_const (lexer, glm->dict);
689 /* An interaction is a variable followed by {*, BY} followed by an interaction */
691 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
693 const struct variable *v = NULL;
696 switch (lex_next_token (lexer, 1))
710 if (! lex_match_variable (lexer, glm, &v))
712 interaction_destroy (*iact);
720 *iact = interaction_create (v);
722 interaction_add_variable (*iact, v);
724 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
726 lex_error (lexer, "Interactions are not yet implemented"); return false;
727 return parse_design_interaction (lexer, glm, iact);
734 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
736 const struct variable *v = NULL;
737 if ( ! lex_match_variable (lexer, glm, &v))
740 if (lex_match (lexer, T_LPAREN))
742 if ( ! parse_nested_variable (lexer, glm))
745 if ( ! lex_force_match (lexer, T_RPAREN))
749 lex_error (lexer, "Nested variables are not yet implemented"); return false;
753 /* A design term is an interaction OR a nested variable */
755 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
757 struct interaction *iact = NULL;
758 if (parse_design_interaction (lexer, glm, &iact))
760 /* Interaction parsing successful. Add to list of interactions */
761 glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
762 glm->interactions[glm->n_interactions - 1] = iact;
766 if ( parse_nested_variable (lexer, glm))
774 /* Parse a complete DESIGN specification.
775 A design spec is a design term, optionally followed by a comma,
776 and another design spec.
779 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
781 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
784 if ( ! parse_design_term (lexer, glm))
787 lex_match (lexer, T_COMMA);
789 return parse_design_spec (lexer, glm);