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 /* In the current implementation, design_vars will
58 normally be the same as factor_vars.
59 This will change once interactions, nested variables
60 and repeated measures become involved.
63 const struct variable **design_vars;
65 size_t n_interactions;
66 const struct interaction **interactions;
68 enum mv_class exclude;
70 /* The weight variable */
71 const struct variable *wv;
73 const struct dictionary *dict;
83 struct moments *totals;
85 struct categoricals *cats;
88 Sums of squares due to different variables. Element 0 is the SSE
89 for the entire model. For i > 0, element i is the SS due to
95 static void output_glm (const struct glm_spec *,
96 const struct glm_workspace *ws);
97 static void run_glm (struct glm_spec *cmd, struct casereader *input,
98 const struct dataset *ds);
101 static bool parse_design_spec (struct lexer *lexer, struct glm_spec *glm);
105 cmd_glm (struct lexer *lexer, struct dataset *ds)
107 struct const_var_set *factors = NULL;
110 glm.dict = dataset_dict (ds);
112 glm.n_factor_vars = 0;
113 glm.n_design_vars = 0;
114 glm.n_interactions = 0;
115 glm.interactions = NULL;
117 glm.factor_vars = NULL;
118 glm.design_vars = NULL;
119 glm.exclude = MV_ANY;
120 glm.intercept = true;
121 glm.wv = dict_get_weight (glm.dict);
124 if (!parse_variables_const (lexer, glm.dict,
125 &glm.dep_vars, &glm.n_dep_vars,
126 PV_NO_DUPLICATE | PV_NUMERIC))
129 lex_force_match (lexer, T_BY);
131 if (!parse_variables_const (lexer, glm.dict,
132 &glm.factor_vars, &glm.n_factor_vars,
133 PV_NO_DUPLICATE | PV_NUMERIC))
136 if (glm.n_dep_vars > 1)
138 msg (ME, _("Multivariate analysis is not yet implemented"));
143 const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
145 while (lex_token (lexer) != T_ENDCMD)
147 lex_match (lexer, T_SLASH);
149 if (lex_match_id (lexer, "MISSING"))
151 lex_match (lexer, T_EQUALS);
152 while (lex_token (lexer) != T_ENDCMD
153 && lex_token (lexer) != T_SLASH)
155 if (lex_match_id (lexer, "INCLUDE"))
157 glm.exclude = MV_SYSTEM;
159 else if (lex_match_id (lexer, "EXCLUDE"))
161 glm.exclude = MV_ANY;
165 lex_error (lexer, NULL);
170 else if (lex_match_id (lexer, "INTERCEPT"))
172 lex_match (lexer, T_EQUALS);
173 while (lex_token (lexer) != T_ENDCMD
174 && lex_token (lexer) != T_SLASH)
176 if (lex_match_id (lexer, "INCLUDE"))
178 glm.intercept = true;
180 else if (lex_match_id (lexer, "EXCLUDE"))
182 glm.intercept = false;
186 lex_error (lexer, NULL);
191 else if (lex_match_id (lexer, "CRITERIA"))
193 lex_match (lexer, T_EQUALS);
194 if (lex_match_id (lexer, "ALPHA"))
196 if (lex_force_match (lexer, T_LPAREN))
198 if (! lex_force_num (lexer))
200 lex_error (lexer, NULL);
204 glm.alpha = lex_number (lexer);
206 if ( ! lex_force_match (lexer, T_RPAREN))
208 lex_error (lexer, NULL);
215 lex_error (lexer, NULL);
219 else if (lex_match_id (lexer, "METHOD"))
221 lex_match (lexer, T_EQUALS);
222 if ( !lex_force_match_id (lexer, "SSTYPE"))
224 lex_error (lexer, NULL);
228 if ( ! lex_force_match (lexer, T_LPAREN))
230 lex_error (lexer, NULL);
234 if ( ! lex_force_int (lexer))
236 lex_error (lexer, NULL);
240 if (3 != lex_integer (lexer))
242 msg (ME, _("Only type 3 sum of squares are currently implemented"));
248 if ( ! lex_force_match (lexer, T_RPAREN))
250 lex_error (lexer, NULL);
254 else if (lex_match_id (lexer, "DESIGN"))
256 lex_match (lexer, T_EQUALS);
258 if (! parse_design_spec (lexer, &glm))
261 if ( glm.n_interactions == 0)
263 msg (ME, _("One or more design variables must be given"));
271 lex_error (lexer, NULL);
278 lex_error (lexer, _("/DESIGN is mandatory in GLM"));
283 struct casegrouper *grouper;
284 struct casereader *group;
287 grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
288 while (casegrouper_get_next_group (grouper, &group))
289 run_glm (&glm, group, ds);
290 ok = casegrouper_destroy (grouper);
291 ok = proc_commit (ds) && ok;
294 const_var_set_destroy (factors);
295 free (glm.factor_vars);
296 free (glm.interactions);
304 const_var_set_destroy (factors);
305 free (glm.factor_vars);
306 free (glm.interactions);
312 static void get_ssq (struct covariance *, gsl_vector *,
313 const struct glm_spec *);
316 not_dropped (size_t j, size_t * dropped, size_t n_dropped)
320 for (i = 0; i < n_dropped; i++)
329 get_ssq (struct covariance *cov, gsl_vector * ssq, const struct glm_spec *cmd)
331 const struct variable **vars;
332 gsl_matrix *small_cov = NULL;
333 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
342 dropped = xcalloc (covariance_dim (cov), sizeof (*dropped));
343 vars = xcalloc (covariance_dim (cov), sizeof (*vars));
344 covariance_get_var_indices (cov, vars);
346 for (k = 0; k < cmd->n_design_vars; k++)
349 for (i = 1; i < covariance_dim (cov); i++)
351 if (vars[i] == cmd->design_vars[k])
353 dropped[n_dropped++] = i;
357 gsl_matrix_alloc (cm->size1 - n_dropped, cm->size2 - n_dropped);
358 gsl_matrix_set (small_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
361 for (i = 0; i < cm->size1; i++)
363 if (not_dropped (i, dropped, n_dropped))
366 for (j = 0; j < cm->size2; j++)
368 if (not_dropped (j, dropped, n_dropped))
370 gsl_matrix_set (small_cov, n, m,
371 gsl_matrix_get (cm, i, j));
378 reg_sweep (small_cov, 0);
379 gsl_vector_set (ssq, k + 1,
380 gsl_matrix_get (small_cov, 0, 0)
381 - gsl_vector_get (ssq, 0));
382 gsl_matrix_free (small_cov);
387 gsl_matrix_free (cm);
390 //static void dump_matrix (const gsl_matrix *m);
393 run_glm (struct glm_spec *cmd, struct casereader *input,
394 const struct dataset *ds)
396 bool warn_bad_weight = true;
399 struct dictionary *dict = dataset_dict (ds);
400 struct casereader *reader;
403 struct glm_workspace ws;
404 struct covariance *cov;
405 ws.cats = categoricals_create (cmd->design_vars, cmd->n_design_vars,
406 cmd->wv, cmd->exclude,
407 NULL, NULL, NULL, NULL);
409 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
410 ws.cats, cmd->wv, cmd->exclude);
413 c = casereader_peek (input, 0);
416 casereader_destroy (input);
419 output_split_file_values (ds, c);
422 taint = taint_clone (casereader_get_taint (input));
424 ws.totals = moments_create (MOMENT_VARIANCE);
426 for (reader = casereader_clone (input);
427 (c = casereader_read (reader)) != NULL; case_unref (c))
429 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
431 for (v = 0; v < cmd->n_dep_vars; ++v)
432 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
435 covariance_accumulate_pass1 (cov, c);
437 casereader_destroy (reader);
439 categoricals_done (ws.cats);
442 (c = casereader_read (reader)) != NULL; case_unref (c))
444 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
446 for (v = 0; v < cmd->n_dep_vars; ++v)
447 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
450 covariance_accumulate_pass2 (cov, c);
452 casereader_destroy (reader);
455 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
459 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
464 Store the overall SSE.
466 ws.ssq = gsl_vector_alloc (cm->size1);
467 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
468 get_ssq (cov, ws.ssq, cmd);
471 gsl_matrix_free (cm);
474 if (!taint_has_tainted_successor (taint))
475 output_glm (cmd, &ws);
477 gsl_vector_free (ws.ssq);
479 covariance_destroy (cov);
480 moments_destroy (ws.totals);
482 taint_destroy (taint);
486 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
488 const struct fmt_spec *wfmt =
489 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
491 double n_total, mean;
492 double df_corr = 0.0;
497 const int heading_columns = 1;
498 const int heading_rows = 1;
502 int nr = heading_rows + 4 + cmd->n_interactions;
506 t = tab_create (nc, nr);
507 tab_title (t, _("Tests of Between-Subjects Effects"));
509 tab_headers (t, heading_columns, 0, heading_rows, 0);
511 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
513 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
514 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
516 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
518 /* TRANSLATORS: The parameter is a roman numeral */
519 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
520 _("Type %s Sum of Squares"), "III");
521 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
522 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
523 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
524 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
526 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
531 for (f = 0; f < cmd->n_interactions; ++f)
532 df_corr += categoricals_n_count (ws->cats, f) - 1.0;
534 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
537 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
543 const double intercept = pow2 (mean * n_total) / n_total;
544 const double df = 1.0;
545 const double F = intercept / df / mse;
546 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
547 tab_double (t, 1, r, 0, intercept, NULL);
548 tab_double (t, 2, r, 0, 1.00, wfmt);
549 tab_double (t, 3, r, 0, intercept / df, NULL);
550 tab_double (t, 4, r, 0, F, NULL);
551 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
556 for (f = 0; f < cmd->n_interactions; ++f)
558 struct string str = DS_EMPTY_INITIALIZER;
559 const double df = categoricals_n_count (ws->cats, f) - 1.0;
560 const double ssq = gsl_vector_get (ws->ssq, f + 1);
561 const double F = ssq / df / mse;
562 interaction_to_string (cmd->interactions[f], &str);
563 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
566 tab_double (t, 1, r, 0, ssq, NULL);
567 tab_double (t, 2, r, 0, df, wfmt);
568 tab_double (t, 3, r, 0, ssq / df, NULL);
569 tab_double (t, 4, r, 0, F, NULL);
571 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
577 /* Corrected Model */
578 const double df = df_corr - 1.0;
579 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
580 const double F = ssq / df / mse;
581 tab_double (t, 1, heading_rows, 0, ssq, NULL);
582 tab_double (t, 2, heading_rows, 0, df, wfmt);
583 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
584 tab_double (t, 4, heading_rows, 0, F, NULL);
586 tab_double (t, 5, heading_rows, 0,
587 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
591 const double df = n_total - df_corr;
592 const double ssq = gsl_vector_get (ws->ssq, 0);
593 const double mse = ssq / df;
594 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
595 tab_double (t, 1, r, 0, ssq, NULL);
596 tab_double (t, 2, r, 0, df, wfmt);
597 tab_double (t, 3, r++, 0, mse, NULL);
602 const double intercept = pow2 (mean * n_total) / n_total;
603 const double ssq = intercept + ws->total_ssq;
605 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
606 tab_double (t, 1, r, 0, ssq, NULL);
607 tab_double (t, 2, r, 0, n_total, wfmt);
612 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
615 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
616 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
623 dump_matrix (const gsl_matrix * m)
626 for (i = 0; i < m->size1; ++i)
628 for (j = 0; j < m->size2; ++j)
630 double x = gsl_matrix_get (m, i, j);
643 If the match succeeds, the variable will be placed in VAR.
644 Returns true if successful */
646 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
648 if (lex_token (lexer) != T_ID)
651 *var = parse_variable_const (lexer, glm->dict);
658 /* An interaction is a variable followed by {*, BY} followed by an interaction */
660 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
662 const struct variable *v = NULL;
665 switch (lex_next_token (lexer, 1))
679 if (! lex_match_variable (lexer, glm, &v))
681 interaction_destroy (*iact);
689 *iact = interaction_create (v);
691 interaction_add_variable (*iact, v);
693 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
695 lex_error (lexer, "Interactions are not yet implemented"); return false;
696 return parse_design_interaction (lexer, glm, iact);
699 glm->n_design_vars++;
700 glm->design_vars = xrealloc (glm->design_vars, sizeof (*glm->design_vars) * glm->n_design_vars);
701 glm->design_vars[glm->n_design_vars - 1] = v;
707 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
709 const struct variable *v = NULL;
710 if ( ! lex_match_variable (lexer, glm, &v))
713 if (lex_match (lexer, T_LPAREN))
715 if ( ! parse_nested_variable (lexer, glm))
718 if ( ! lex_force_match (lexer, T_RPAREN))
722 lex_error (lexer, "Nested variables are not yet implemented"); return false;
726 /* A design term is an interaction OR a nested variable */
728 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
730 struct interaction *iact = NULL;
731 if (parse_design_interaction (lexer, glm, &iact))
733 /* Interaction parsing successful. Add to list of interactions */
734 glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
735 glm->interactions[glm->n_interactions - 1] = iact;
739 if ( parse_nested_variable (lexer, glm))
747 /* Parse a complete DESIGN specification.
748 A design spec is a design term, optionally followed by a comma,
749 and another design spec.
752 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
754 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
757 if ( ! parse_design_term (lexer, glm))
760 lex_match (lexer, T_COMMA);
762 return parse_design_spec (lexer, glm);