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/interaction.h"
43 #include "math/moments.h"
44 #include "output/tab.h"
47 #define _(msgid) gettext (msgid)
52 const struct variable **dep_vars;
55 const struct variable **factor_vars;
57 size_t n_interactions;
58 struct interaction **interactions;
60 enum mv_class exclude;
62 /* The weight variable */
63 const struct variable *wv;
65 const struct dictionary *dict;
75 struct moments *totals;
77 struct categoricals *cats;
80 Sums of squares due to different variables. Element 0 is the SSE
81 for the entire model. For i > 0, element i is the SS due to
87 static void output_glm (const struct glm_spec *,
88 const struct glm_workspace *ws);
89 static void run_glm (struct glm_spec *cmd, struct casereader *input,
90 const struct dataset *ds);
93 static bool parse_design_spec (struct lexer *lexer, struct glm_spec *glm);
97 cmd_glm (struct lexer *lexer, struct dataset *ds)
100 struct const_var_set *factors = NULL;
103 glm.dict = dataset_dict (ds);
105 glm.n_factor_vars = 0;
106 glm.n_interactions = 0;
107 glm.interactions = NULL;
109 glm.factor_vars = NULL;
110 glm.exclude = MV_ANY;
111 glm.intercept = true;
112 glm.wv = dict_get_weight (glm.dict);
115 if (!parse_variables_const (lexer, glm.dict,
116 &glm.dep_vars, &glm.n_dep_vars,
117 PV_NO_DUPLICATE | PV_NUMERIC))
120 lex_force_match (lexer, T_BY);
122 if (!parse_variables_const (lexer, glm.dict,
123 &glm.factor_vars, &glm.n_factor_vars,
124 PV_NO_DUPLICATE | PV_NUMERIC))
127 if (glm.n_dep_vars > 1)
129 msg (ME, _("Multivariate analysis is not yet implemented"));
134 const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
136 while (lex_token (lexer) != T_ENDCMD)
138 lex_match (lexer, T_SLASH);
140 if (lex_match_id (lexer, "MISSING"))
142 lex_match (lexer, T_EQUALS);
143 while (lex_token (lexer) != T_ENDCMD
144 && lex_token (lexer) != T_SLASH)
146 if (lex_match_id (lexer, "INCLUDE"))
148 glm.exclude = MV_SYSTEM;
150 else if (lex_match_id (lexer, "EXCLUDE"))
152 glm.exclude = MV_ANY;
156 lex_error (lexer, NULL);
161 else if (lex_match_id (lexer, "INTERCEPT"))
163 lex_match (lexer, T_EQUALS);
164 while (lex_token (lexer) != T_ENDCMD
165 && lex_token (lexer) != T_SLASH)
167 if (lex_match_id (lexer, "INCLUDE"))
169 glm.intercept = true;
171 else if (lex_match_id (lexer, "EXCLUDE"))
173 glm.intercept = false;
177 lex_error (lexer, NULL);
182 else if (lex_match_id (lexer, "CRITERIA"))
184 lex_match (lexer, T_EQUALS);
185 if (lex_match_id (lexer, "ALPHA"))
187 if (lex_force_match (lexer, T_LPAREN))
189 if (! lex_force_num (lexer))
191 lex_error (lexer, NULL);
195 glm.alpha = lex_number (lexer);
197 if ( ! lex_force_match (lexer, T_RPAREN))
199 lex_error (lexer, NULL);
206 lex_error (lexer, NULL);
210 else if (lex_match_id (lexer, "METHOD"))
212 lex_match (lexer, T_EQUALS);
213 if ( !lex_force_match_id (lexer, "SSTYPE"))
215 lex_error (lexer, NULL);
219 if ( ! lex_force_match (lexer, T_LPAREN))
221 lex_error (lexer, NULL);
225 if ( ! lex_force_int (lexer))
227 lex_error (lexer, NULL);
231 if (3 != lex_integer (lexer))
233 msg (ME, _("Only type 3 sum of squares are currently implemented"));
239 if ( ! lex_force_match (lexer, T_RPAREN))
241 lex_error (lexer, NULL);
245 else if (lex_match_id (lexer, "DESIGN"))
247 lex_match (lexer, T_EQUALS);
249 if (! parse_design_spec (lexer, &glm))
252 if ( glm.n_interactions == 0)
254 msg (ME, _("One or more design variables must be given"));
262 lex_error (lexer, NULL);
269 lex_error (lexer, _("/DESIGN is mandatory in GLM"));
274 struct casegrouper *grouper;
275 struct casereader *group;
278 grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
279 while (casegrouper_get_next_group (grouper, &group))
280 run_glm (&glm, group, ds);
281 ok = casegrouper_destroy (grouper);
282 ok = proc_commit (ds) && ok;
285 const_var_set_destroy (factors);
286 free (glm.factor_vars);
287 for (i = 0 ; i < glm.n_interactions; ++i)
288 interaction_destroy (glm.interactions[i]);
289 free (glm.interactions);
297 const_var_set_destroy (factors);
298 free (glm.factor_vars);
299 for (i = 0 ; i < glm.n_interactions; ++i)
300 interaction_destroy (glm.interactions[i]);
302 free (glm.interactions);
308 static void get_ssq (struct covariance *, gsl_vector *,
309 const struct glm_spec *);
312 not_dropped (size_t j, size_t * dropped, size_t n_dropped)
316 for (i = 0; i < n_dropped; i++)
325 get_ssq (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
327 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
331 size_t *dropped = xcalloc (covariance_dim (cov), sizeof (*dropped));
332 const struct categoricals *cats = covariance_get_categoricals (cov);
334 for (k = 0; k < cmd->n_interactions; k++)
338 gsl_matrix *small_cov = NULL;
339 size_t n_dropped = 0;
340 for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
342 if (categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars)
343 == cmd->interactions[k])
345 assert (n_dropped < covariance_dim (cov));
346 dropped[n_dropped++] = i;
350 gsl_matrix_alloc (cm->size1 - n_dropped, cm->size2 - n_dropped);
351 gsl_matrix_set (small_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
352 for (i = 0; i < cm->size1; i++)
354 if (not_dropped (i, dropped, n_dropped))
357 for (j = 0; j < cm->size2; j++)
359 if (not_dropped (j, dropped, n_dropped))
361 gsl_matrix_set (small_cov, n, m,
362 gsl_matrix_get (cm, i, j));
369 reg_sweep (small_cov, 0);
370 gsl_vector_set (ssq, k + 1,
371 gsl_matrix_get (small_cov, 0, 0)
372 - gsl_vector_get (ssq, 0));
373 gsl_matrix_free (small_cov);
377 gsl_matrix_free (cm);
380 //static void dump_matrix (const gsl_matrix *m);
383 run_glm (struct glm_spec *cmd, struct casereader *input,
384 const struct dataset *ds)
386 bool warn_bad_weight = true;
389 struct dictionary *dict = dataset_dict (ds);
390 struct casereader *reader;
393 struct glm_workspace ws;
394 struct covariance *cov;
396 ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions,
397 cmd->wv, cmd->exclude,
398 NULL, NULL, NULL, NULL);
400 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
401 ws.cats, cmd->wv, cmd->exclude);
404 c = casereader_peek (input, 0);
407 casereader_destroy (input);
410 output_split_file_values (ds, c);
413 taint = taint_clone (casereader_get_taint (input));
415 ws.totals = moments_create (MOMENT_VARIANCE);
417 for (reader = casereader_clone (input);
418 (c = casereader_read (reader)) != NULL; case_unref (c))
420 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
422 for (v = 0; v < cmd->n_dep_vars; ++v)
423 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
426 covariance_accumulate_pass1 (cov, c);
428 casereader_destroy (reader);
430 categoricals_done (ws.cats);
433 (c = casereader_read (reader)) != NULL; case_unref (c))
435 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
437 for (v = 0; v < cmd->n_dep_vars; ++v)
438 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
441 covariance_accumulate_pass2 (cov, c);
443 casereader_destroy (reader);
446 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
450 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
455 Store the overall SSE.
457 ws.ssq = gsl_vector_alloc (cm->size1);
458 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
459 get_ssq (cov, ws.ssq, cmd);
462 gsl_matrix_free (cm);
465 if (!taint_has_tainted_successor (taint))
466 output_glm (cmd, &ws);
468 gsl_vector_free (ws.ssq);
470 covariance_destroy (cov);
471 moments_destroy (ws.totals);
473 taint_destroy (taint);
477 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
479 const struct fmt_spec *wfmt =
480 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
482 double n_total, mean;
483 double df_corr = 0.0;
488 const int heading_columns = 1;
489 const int heading_rows = 1;
493 int nr = heading_rows + 4 + cmd->n_interactions;
497 t = tab_create (nc, nr);
498 tab_title (t, _("Tests of Between-Subjects Effects"));
500 tab_headers (t, heading_columns, 0, heading_rows, 0);
502 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
504 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
505 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
507 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
509 /* TRANSLATORS: The parameter is a roman numeral */
510 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
511 _("Type %s Sum of Squares"), "III");
512 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
513 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
514 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
515 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
517 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
522 df_corr += categoricals_df_total (ws->cats);
524 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
527 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
533 const double intercept = pow2 (mean * n_total) / n_total;
534 const double df = 1.0;
535 const double F = intercept / df / mse;
536 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
537 tab_double (t, 1, r, 0, intercept, NULL);
538 tab_double (t, 2, r, 0, 1.00, wfmt);
539 tab_double (t, 3, r, 0, intercept / df, NULL);
540 tab_double (t, 4, r, 0, F, NULL);
541 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
546 for (f = 0; f < cmd->n_interactions; ++f)
548 struct string str = DS_EMPTY_INITIALIZER;
549 const double df = categoricals_df (ws->cats, f);
550 const double ssq = gsl_vector_get (ws->ssq, f + 1);
551 const double F = ssq / df / mse;
552 interaction_to_string (cmd->interactions[f], &str);
553 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
556 tab_double (t, 1, r, 0, ssq, NULL);
557 tab_double (t, 2, r, 0, df, wfmt);
558 tab_double (t, 3, r, 0, ssq / df, NULL);
559 tab_double (t, 4, r, 0, F, NULL);
561 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
567 /* Corrected Model */
568 const double df = df_corr - 1.0;
569 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
570 const double F = ssq / df / mse;
571 tab_double (t, 1, heading_rows, 0, ssq, NULL);
572 tab_double (t, 2, heading_rows, 0, df, wfmt);
573 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
574 tab_double (t, 4, heading_rows, 0, F, NULL);
576 tab_double (t, 5, heading_rows, 0,
577 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
581 const double df = n_total - df_corr;
582 const double ssq = gsl_vector_get (ws->ssq, 0);
583 const double mse = ssq / df;
584 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
585 tab_double (t, 1, r, 0, ssq, NULL);
586 tab_double (t, 2, r, 0, df, wfmt);
587 tab_double (t, 3, r++, 0, mse, NULL);
592 const double intercept = pow2 (mean * n_total) / n_total;
593 const double ssq = intercept + ws->total_ssq;
595 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
596 tab_double (t, 1, r, 0, ssq, NULL);
597 tab_double (t, 2, r, 0, n_total, wfmt);
602 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
605 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
606 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
613 dump_matrix (const gsl_matrix * m)
616 for (i = 0; i < m->size1; ++i)
618 for (j = 0; j < m->size2; ++j)
620 double x = gsl_matrix_get (m, i, j);
633 If the match succeeds, the variable will be placed in VAR.
634 Returns true if successful */
636 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
638 if (lex_token (lexer) != T_ID)
641 *var = parse_variable_const (lexer, glm->dict);
648 /* An interaction is a variable followed by {*, BY} followed by an interaction */
650 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
652 const struct variable *v = NULL;
655 switch (lex_next_token (lexer, 1))
669 if (! lex_match_variable (lexer, glm, &v))
671 interaction_destroy (*iact);
679 *iact = interaction_create (v);
681 interaction_add_variable (*iact, v);
683 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
685 // lex_error (lexer, "Interactions are not yet implemented"); return false;
686 return parse_design_interaction (lexer, glm, iact);
693 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
695 const struct variable *v = NULL;
696 if ( ! lex_match_variable (lexer, glm, &v))
699 if (lex_match (lexer, T_LPAREN))
701 if ( ! parse_nested_variable (lexer, glm))
704 if ( ! lex_force_match (lexer, T_RPAREN))
708 lex_error (lexer, "Nested variables are not yet implemented"); return false;
712 /* A design term is an interaction OR a nested variable */
714 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
716 struct interaction *iact = NULL;
717 if (parse_design_interaction (lexer, glm, &iact))
719 /* Interaction parsing successful. Add to list of interactions */
720 glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
721 glm->interactions[glm->n_interactions - 1] = iact;
725 if ( parse_nested_variable (lexer, glm))
733 /* Parse a complete DESIGN specification.
734 A design spec is a design term, optionally followed by a comma,
735 and another design spec.
738 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
740 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
743 if ( ! parse_design_term (lexer, glm))
746 lex_match (lexer, T_COMMA);
748 return parse_design_spec (lexer, glm);