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);
129 cmd_glm (struct lexer *lexer, struct dataset *ds)
132 struct const_var_set *factors = NULL;
135 glm.dict = dataset_dict (ds);
137 glm.n_factor_vars = 0;
138 glm.n_interactions = 0;
139 glm.interactions = NULL;
141 glm.factor_vars = NULL;
142 glm.exclude = MV_ANY;
143 glm.intercept = true;
144 glm.wv = dict_get_weight (glm.dict);
147 if (!parse_variables_const (lexer, glm.dict,
148 &glm.dep_vars, &glm.n_dep_vars,
149 PV_NO_DUPLICATE | PV_NUMERIC))
152 lex_force_match (lexer, T_BY);
154 if (!parse_variables_const (lexer, glm.dict,
155 &glm.factor_vars, &glm.n_factor_vars,
156 PV_NO_DUPLICATE | PV_NUMERIC))
159 if (glm.n_dep_vars > 1)
161 msg (ME, _("Multivariate analysis is not yet implemented"));
166 const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
168 while (lex_token (lexer) != T_ENDCMD)
170 lex_match (lexer, T_SLASH);
172 if (lex_match_id (lexer, "MISSING"))
174 lex_match (lexer, T_EQUALS);
175 while (lex_token (lexer) != T_ENDCMD
176 && lex_token (lexer) != T_SLASH)
178 if (lex_match_id (lexer, "INCLUDE"))
180 glm.exclude = MV_SYSTEM;
182 else if (lex_match_id (lexer, "EXCLUDE"))
184 glm.exclude = MV_ANY;
188 lex_error (lexer, NULL);
193 else if (lex_match_id (lexer, "INTERCEPT"))
195 lex_match (lexer, T_EQUALS);
196 while (lex_token (lexer) != T_ENDCMD
197 && lex_token (lexer) != T_SLASH)
199 if (lex_match_id (lexer, "INCLUDE"))
201 glm.intercept = true;
203 else if (lex_match_id (lexer, "EXCLUDE"))
205 glm.intercept = false;
209 lex_error (lexer, NULL);
214 else if (lex_match_id (lexer, "CRITERIA"))
216 lex_match (lexer, T_EQUALS);
217 if (lex_match_id (lexer, "ALPHA"))
219 if (lex_force_match (lexer, T_LPAREN))
221 if (! lex_force_num (lexer))
223 lex_error (lexer, NULL);
227 glm.alpha = lex_number (lexer);
229 if ( ! lex_force_match (lexer, T_RPAREN))
231 lex_error (lexer, NULL);
238 lex_error (lexer, NULL);
242 else if (lex_match_id (lexer, "METHOD"))
244 lex_match (lexer, T_EQUALS);
245 if ( !lex_force_match_id (lexer, "SSTYPE"))
247 lex_error (lexer, NULL);
251 if ( ! lex_force_match (lexer, T_LPAREN))
253 lex_error (lexer, NULL);
257 if ( ! lex_force_int (lexer))
259 lex_error (lexer, NULL);
263 if (3 != lex_integer (lexer))
265 msg (ME, _("Only type 3 sum of squares are currently implemented"));
271 if ( ! lex_force_match (lexer, T_RPAREN))
273 lex_error (lexer, NULL);
277 else if (lex_match_id (lexer, "DESIGN"))
279 lex_match (lexer, T_EQUALS);
281 if (! parse_design_spec (lexer, &glm))
284 if (glm.n_interactions > 0)
289 lex_error (lexer, NULL);
300 struct casegrouper *grouper;
301 struct casereader *group;
304 grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
305 while (casegrouper_get_next_group (grouper, &group))
306 run_glm (&glm, group, ds);
307 ok = casegrouper_destroy (grouper);
308 ok = proc_commit (ds) && ok;
311 const_var_set_destroy (factors);
312 free (glm.factor_vars);
313 for (i = 0 ; i < glm.n_interactions; ++i)
314 interaction_destroy (glm.interactions[i]);
315 free (glm.interactions);
323 const_var_set_destroy (factors);
324 free (glm.factor_vars);
325 for (i = 0 ; i < glm.n_interactions; ++i)
326 interaction_destroy (glm.interactions[i]);
328 free (glm.interactions);
334 static void get_ssq (struct covariance *, gsl_vector *,
335 const struct glm_spec *);
338 not_dropped (size_t j, const size_t *dropped, size_t n_dropped)
342 for (i = 0; i < n_dropped; i++)
351 fill_submatrix (gsl_matrix * cov, gsl_matrix * submatrix, size_t * dropped,
359 for (i = 0; i < cov->size1; i++)
361 if (not_dropped (i, dropped, n_dropped))
364 for (j = 0; j < cov->size2; j++)
366 if (not_dropped (j, dropped, n_dropped))
368 gsl_matrix_set (submatrix, n, m,
369 gsl_matrix_get (cov, i, j));
379 get_ssq (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
381 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
384 size_t *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
385 size_t *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
386 const struct categoricals *cats = covariance_get_categoricals (cov);
388 for (k = 0; k < cmd->n_interactions; k++)
390 gsl_matrix *model_cov = NULL;
391 gsl_matrix *submodel_cov = NULL;
392 size_t n_dropped_model = 0;
393 size_t n_dropped_submodel = 0;
394 for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
396 const struct interaction * x =
397 categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
398 if (interaction_is_proper_subset (cmd->interactions [k], x))
400 assert (n_dropped_model < covariance_dim (cov));
401 model_dropped[n_dropped_model++] = i;
403 if (interaction_is_subset (cmd->interactions [k], x))
405 assert (n_dropped_submodel < covariance_dim (cov));
406 submodel_dropped[n_dropped_submodel++] = i;
410 gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
411 gsl_matrix_set (model_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
413 gsl_matrix_calloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
414 fill_submatrix (cm, model_cov, model_dropped, n_dropped_model);
415 fill_submatrix (cm, submodel_cov, submodel_dropped, n_dropped_submodel);
417 reg_sweep (model_cov, 0);
418 reg_sweep (submodel_cov, 0);
419 gsl_vector_set (ssq, k + 1,
420 gsl_matrix_get (submodel_cov, 0, 0)
421 - gsl_matrix_get (model_cov, 0, 0));
422 gsl_matrix_free (model_cov);
423 gsl_matrix_free (submodel_cov);
426 free (model_dropped);
427 free (submodel_dropped);
428 gsl_matrix_free (cm);
431 //static void dump_matrix (const gsl_matrix *m);
434 run_glm (struct glm_spec *cmd, struct casereader *input,
435 const struct dataset *ds)
437 bool warn_bad_weight = true;
440 struct dictionary *dict = dataset_dict (ds);
441 struct casereader *reader;
444 struct glm_workspace ws;
445 struct covariance *cov;
447 ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions,
448 cmd->wv, cmd->exclude,
449 NULL, NULL, NULL, NULL);
451 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
452 ws.cats, cmd->wv, cmd->exclude);
455 c = casereader_peek (input, 0);
458 casereader_destroy (input);
461 output_split_file_values (ds, c);
464 taint = taint_clone (casereader_get_taint (input));
466 ws.totals = moments_create (MOMENT_VARIANCE);
468 for (reader = casereader_clone (input);
469 (c = casereader_read (reader)) != NULL; case_unref (c))
471 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
473 for (v = 0; v < cmd->n_dep_vars; ++v)
474 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
477 covariance_accumulate_pass1 (cov, c);
479 casereader_destroy (reader);
482 (c = casereader_read (reader)) != NULL; case_unref (c))
484 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
486 for (v = 0; v < cmd->n_dep_vars; ++v)
487 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
490 covariance_accumulate_pass2 (cov, c);
492 casereader_destroy (reader);
495 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
499 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
504 Store the overall SSE.
506 ws.ssq = gsl_vector_alloc (cm->size1);
507 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
508 get_ssq (cov, ws.ssq, cmd);
511 gsl_matrix_free (cm);
514 if (!taint_has_tainted_successor (taint))
515 output_glm (cmd, &ws);
517 gsl_vector_free (ws.ssq);
519 covariance_destroy (cov);
520 moments_destroy (ws.totals);
522 taint_destroy (taint);
526 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
528 const struct fmt_spec *wfmt =
529 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
531 double n_total, mean;
532 double df_corr = 0.0;
537 const int heading_columns = 1;
538 const int heading_rows = 1;
542 int nr = heading_rows + 4 + cmd->n_interactions;
546 t = tab_create (nc, nr);
547 tab_title (t, _("Tests of Between-Subjects Effects"));
549 tab_headers (t, heading_columns, 0, heading_rows, 0);
551 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
553 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
554 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
556 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
558 /* TRANSLATORS: The parameter is a roman numeral */
559 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
560 _("Type %s Sum of Squares"), "III");
561 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
562 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
563 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
564 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
566 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
571 df_corr += categoricals_df_total (ws->cats);
573 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
576 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
582 const double intercept = pow2 (mean * n_total) / n_total;
583 const double df = 1.0;
584 const double F = intercept / df / mse;
585 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
586 tab_double (t, 1, r, 0, intercept, NULL);
587 tab_double (t, 2, r, 0, 1.00, wfmt);
588 tab_double (t, 3, r, 0, intercept / df, NULL);
589 tab_double (t, 4, r, 0, F, NULL);
590 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
595 for (f = 0; f < cmd->n_interactions; ++f)
597 struct string str = DS_EMPTY_INITIALIZER;
598 const double df = categoricals_df (ws->cats, f);
599 const double ssq = gsl_vector_get (ws->ssq, f + 1);
600 const double F = ssq / df / mse;
601 interaction_to_string (cmd->interactions[f], &str);
602 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
605 tab_double (t, 1, r, 0, ssq, NULL);
606 tab_double (t, 2, r, 0, df, wfmt);
607 tab_double (t, 3, r, 0, ssq / df, NULL);
608 tab_double (t, 4, r, 0, F, NULL);
610 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
616 /* Corrected Model */
617 const double df = df_corr - 1.0;
618 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
619 const double F = ssq / df / mse;
620 tab_double (t, 1, heading_rows, 0, ssq, NULL);
621 tab_double (t, 2, heading_rows, 0, df, wfmt);
622 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
623 tab_double (t, 4, heading_rows, 0, F, NULL);
625 tab_double (t, 5, heading_rows, 0,
626 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
630 const double df = n_total - df_corr;
631 const double ssq = gsl_vector_get (ws->ssq, 0);
632 const double mse = ssq / df;
633 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
634 tab_double (t, 1, r, 0, ssq, NULL);
635 tab_double (t, 2, r, 0, df, wfmt);
636 tab_double (t, 3, r++, 0, mse, NULL);
641 const double intercept = pow2 (mean * n_total) / n_total;
642 const double ssq = intercept + ws->total_ssq;
644 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
645 tab_double (t, 1, r, 0, ssq, NULL);
646 tab_double (t, 2, r, 0, n_total, wfmt);
651 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
654 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
655 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
662 dump_matrix (const gsl_matrix * m)
665 for (i = 0; i < m->size1; ++i)
667 for (j = 0; j < m->size2; ++j)
669 double x = gsl_matrix_get (m, i, j);
682 If the match succeeds, the variable will be placed in VAR.
683 Returns true if successful */
685 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
687 if (lex_token (lexer) != T_ID)
690 *var = parse_variable_const (lexer, glm->dict);
697 /* An interaction is a variable followed by {*, BY} followed by an interaction */
699 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
701 const struct variable *v = NULL;
704 switch (lex_next_token (lexer, 1))
718 if (! lex_match_variable (lexer, glm, &v))
720 interaction_destroy (*iact);
728 *iact = interaction_create (v);
730 interaction_add_variable (*iact, v);
732 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
734 return parse_design_interaction (lexer, glm, iact);
741 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
743 const struct variable *v = NULL;
744 if ( ! lex_match_variable (lexer, glm, &v))
747 if (lex_match (lexer, T_LPAREN))
749 if ( ! parse_nested_variable (lexer, glm))
752 if ( ! lex_force_match (lexer, T_RPAREN))
756 lex_error (lexer, "Nested variables are not yet implemented"); return false;
760 /* A design term is an interaction OR a nested variable */
762 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
764 struct interaction *iact = NULL;
765 if (parse_design_interaction (lexer, glm, &iact))
767 /* Interaction parsing successful. Add to list of interactions */
768 glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
769 glm->interactions[glm->n_interactions - 1] = iact;
773 if ( parse_nested_variable (lexer, glm))
781 /* Parse a complete DESIGN specification.
782 A design spec is a design term, optionally followed by a comma,
783 and another design spec.
786 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
788 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
791 if ( ! parse_design_term (lexer, glm))
794 lex_match (lexer, T_COMMA);
796 return parse_design_spec (lexer, glm);