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 Do the variables in X->VARS constitute a proper
369 subset of the variables in Y->VARS?
372 is_subset (struct interaction *x, struct interaction *y)
378 if (x->n_vars < y->n_vars)
380 for (i = 0; i < x->n_vars; i++)
382 for (j = 0; j < y->n_vars; j++)
384 if (x->vars [i] == y->vars [j])
397 drop_from_submodel (struct interaction *x, struct interaction *y)
403 if (is_subset (x, y))
406 for (i = 0; i < x->n_vars; i++)
407 for (j = 0; j < y->n_vars; j++)
409 if (x->vars [i] == y->vars [j])
421 fill_submatrix (gsl_matrix * cov, gsl_matrix * submatrix, size_t * dropped,
429 for (i = 0; i < cov->size1; i++)
431 if (not_dropped (i, dropped, n_dropped))
434 for (j = 0; j < cov->size2; j++)
436 if (not_dropped (j, dropped, n_dropped))
438 gsl_matrix_set (submatrix, n, m,
439 gsl_matrix_get (cov, i, j));
449 get_ssq (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
451 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
454 size_t *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
455 size_t *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
456 const struct categoricals *cats = covariance_get_categoricals (cov);
458 for (k = 0; k < cmd->n_interactions; k++)
460 gsl_matrix *model_cov = NULL;
461 gsl_matrix *submodel_cov = NULL;
462 size_t n_dropped_model = 0;
463 size_t n_dropped_submodel = 0;
464 for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
466 const struct interaction * x =
467 categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
468 if (is_subset (cmd->interactions [k], x))
470 assert (n_dropped_model < covariance_dim (cov));
471 model_dropped[n_dropped_model++] = i;
473 if (drop_from_submodel (cmd->interactions [k], x))
475 assert (n_dropped_submodel < covariance_dim (cov));
476 submodel_dropped[n_dropped_submodel++] = i;
480 gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
481 gsl_matrix_set (model_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
483 gsl_matrix_calloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
484 fill_submatrix (cm, model_cov, model_dropped, n_dropped_model);
485 fill_submatrix (cm, submodel_cov, submodel_dropped, n_dropped_submodel);
487 reg_sweep (model_cov, 0);
488 reg_sweep (submodel_cov, 0);
489 gsl_vector_set (ssq, k + 1,
490 gsl_matrix_get (submodel_cov, 0, 0)
491 - gsl_matrix_get (model_cov, 0, 0));
492 gsl_matrix_free (model_cov);
493 gsl_matrix_free (submodel_cov);
496 free (model_dropped);
497 free (submodel_dropped);
498 gsl_matrix_free (cm);
501 //static void dump_matrix (const gsl_matrix *m);
504 run_glm (struct glm_spec *cmd, struct casereader *input,
505 const struct dataset *ds)
507 bool warn_bad_weight = true;
510 struct dictionary *dict = dataset_dict (ds);
511 struct casereader *reader;
514 struct glm_workspace ws;
515 struct covariance *cov;
517 ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions,
518 cmd->wv, cmd->exclude,
519 NULL, NULL, NULL, NULL);
521 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
522 ws.cats, cmd->wv, cmd->exclude);
525 c = casereader_peek (input, 0);
528 casereader_destroy (input);
531 output_split_file_values (ds, c);
534 taint = taint_clone (casereader_get_taint (input));
536 ws.totals = moments_create (MOMENT_VARIANCE);
538 for (reader = casereader_clone (input);
539 (c = casereader_read (reader)) != NULL; case_unref (c))
541 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
543 for (v = 0; v < cmd->n_dep_vars; ++v)
544 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
547 covariance_accumulate_pass1 (cov, c);
549 casereader_destroy (reader);
552 (c = casereader_read (reader)) != NULL; case_unref (c))
554 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
556 for (v = 0; v < cmd->n_dep_vars; ++v)
557 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
560 covariance_accumulate_pass2 (cov, c);
562 casereader_destroy (reader);
565 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
569 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
574 Store the overall SSE.
576 ws.ssq = gsl_vector_alloc (cm->size1);
577 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
578 get_ssq (cov, ws.ssq, cmd);
581 gsl_matrix_free (cm);
584 if (!taint_has_tainted_successor (taint))
585 output_glm (cmd, &ws);
587 gsl_vector_free (ws.ssq);
589 covariance_destroy (cov);
590 moments_destroy (ws.totals);
592 taint_destroy (taint);
596 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
598 const struct fmt_spec *wfmt =
599 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
601 double n_total, mean;
602 double df_corr = 0.0;
607 const int heading_columns = 1;
608 const int heading_rows = 1;
612 int nr = heading_rows + 4 + cmd->n_interactions;
616 t = tab_create (nc, nr);
617 tab_title (t, _("Tests of Between-Subjects Effects"));
619 tab_headers (t, heading_columns, 0, heading_rows, 0);
621 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
623 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
624 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
626 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
628 /* TRANSLATORS: The parameter is a roman numeral */
629 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
630 _("Type %s Sum of Squares"), "III");
631 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
632 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
633 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
634 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
636 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
641 df_corr += categoricals_df_total (ws->cats);
643 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
646 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
652 const double intercept = pow2 (mean * n_total) / n_total;
653 const double df = 1.0;
654 const double F = intercept / df / mse;
655 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
656 tab_double (t, 1, r, 0, intercept, NULL);
657 tab_double (t, 2, r, 0, 1.00, wfmt);
658 tab_double (t, 3, r, 0, intercept / df, NULL);
659 tab_double (t, 4, r, 0, F, NULL);
660 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
665 for (f = 0; f < cmd->n_interactions; ++f)
667 struct string str = DS_EMPTY_INITIALIZER;
668 const double df = categoricals_df (ws->cats, f);
669 const double ssq = gsl_vector_get (ws->ssq, f + 1);
670 const double F = ssq / df / mse;
671 interaction_to_string (cmd->interactions[f], &str);
672 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
675 tab_double (t, 1, r, 0, ssq, NULL);
676 tab_double (t, 2, r, 0, df, wfmt);
677 tab_double (t, 3, r, 0, ssq / df, NULL);
678 tab_double (t, 4, r, 0, F, NULL);
680 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
686 /* Corrected Model */
687 const double df = df_corr - 1.0;
688 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
689 const double F = ssq / df / mse;
690 tab_double (t, 1, heading_rows, 0, ssq, NULL);
691 tab_double (t, 2, heading_rows, 0, df, wfmt);
692 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
693 tab_double (t, 4, heading_rows, 0, F, NULL);
695 tab_double (t, 5, heading_rows, 0,
696 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
700 const double df = n_total - df_corr;
701 const double ssq = gsl_vector_get (ws->ssq, 0);
702 const double mse = ssq / df;
703 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
704 tab_double (t, 1, r, 0, ssq, NULL);
705 tab_double (t, 2, r, 0, df, wfmt);
706 tab_double (t, 3, r++, 0, mse, NULL);
711 const double intercept = pow2 (mean * n_total) / n_total;
712 const double ssq = intercept + ws->total_ssq;
714 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
715 tab_double (t, 1, r, 0, ssq, NULL);
716 tab_double (t, 2, r, 0, n_total, wfmt);
721 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
724 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
725 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
732 dump_matrix (const gsl_matrix * m)
735 for (i = 0; i < m->size1; ++i)
737 for (j = 0; j < m->size2; ++j)
739 double x = gsl_matrix_get (m, i, j);
752 If the match succeeds, the variable will be placed in VAR.
753 Returns true if successful */
755 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
757 if (lex_token (lexer) != T_ID)
760 *var = parse_variable_const (lexer, glm->dict);
767 /* An interaction is a variable followed by {*, BY} followed by an interaction */
769 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
771 const struct variable *v = NULL;
774 switch (lex_next_token (lexer, 1))
788 if (! lex_match_variable (lexer, glm, &v))
790 interaction_destroy (*iact);
798 *iact = interaction_create (v);
800 interaction_add_variable (*iact, v);
802 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
805 lex_error (lexer, "Interactions are not yet implemented"); return false;
807 return parse_design_interaction (lexer, glm, iact);
814 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
816 const struct variable *v = NULL;
817 if ( ! lex_match_variable (lexer, glm, &v))
820 if (lex_match (lexer, T_LPAREN))
822 if ( ! parse_nested_variable (lexer, glm))
825 if ( ! lex_force_match (lexer, T_RPAREN))
829 lex_error (lexer, "Nested variables are not yet implemented"); return false;
833 /* A design term is an interaction OR a nested variable */
835 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
837 struct interaction *iact = NULL;
838 if (parse_design_interaction (lexer, glm, &iact))
840 /* Interaction parsing successful. Add to list of interactions */
841 glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
842 glm->interactions[glm->n_interactions - 1] = iact;
846 if ( parse_nested_variable (lexer, glm))
854 /* Parse a complete DESIGN specification.
855 A design spec is a design term, optionally followed by a comma,
856 and another design spec.
859 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
861 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
864 if ( ! parse_design_term (lexer, glm))
867 lex_match (lexer, T_COMMA);
869 return parse_design_spec (lexer, glm);