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/moments.h"
43 #include "output/tab.h"
46 #define _(msgid) gettext (msgid)
51 const struct variable **dep_vars;
54 const struct variable **factor_vars;
56 /* In the current implementation, design_vars will
57 normally be the same as factor_vars.
58 This will change once interactions, nested variables
59 and repeated measures become involved.
62 const struct variable **design_vars;
64 enum mv_class exclude;
66 /* The weight variable */
67 const struct variable *wv;
69 const struct dictionary *dict;
79 struct moments *totals;
81 struct categoricals *cats;
84 Sums of squares due to different variables. Element 0 is the SSE
85 for the entire model. For i > 0, element i is the SS due to
91 static void output_glm (const struct glm_spec *,
92 const struct glm_workspace *ws);
93 static void run_glm (struct glm_spec *cmd, struct casereader *input,
94 const struct dataset *ds);
97 static bool parse_design_spec (struct lexer *lexer, struct glm_spec *glm);
101 cmd_glm (struct lexer *lexer, struct dataset *ds)
103 struct const_var_set *factors = NULL;
106 glm.dict = dataset_dict (ds);
108 glm.n_factor_vars = 0;
109 glm.n_design_vars = 0;
111 glm.factor_vars = NULL;
112 glm.design_vars = NULL;
113 glm.exclude = MV_ANY;
114 glm.intercept = true;
115 glm.wv = dict_get_weight (glm.dict);
118 if (!parse_variables_const (lexer, glm.dict,
119 &glm.dep_vars, &glm.n_dep_vars,
120 PV_NO_DUPLICATE | PV_NUMERIC))
123 lex_force_match (lexer, T_BY);
125 if (!parse_variables_const (lexer, glm.dict,
126 &glm.factor_vars, &glm.n_factor_vars,
127 PV_NO_DUPLICATE | PV_NUMERIC))
130 if (glm.n_dep_vars > 1)
132 msg (ME, _("Multivariate analysis is not yet implemented"));
137 const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
139 while (lex_token (lexer) != T_ENDCMD)
141 lex_match (lexer, T_SLASH);
143 if (lex_match_id (lexer, "MISSING"))
145 lex_match (lexer, T_EQUALS);
146 while (lex_token (lexer) != T_ENDCMD
147 && lex_token (lexer) != T_SLASH)
149 if (lex_match_id (lexer, "INCLUDE"))
151 glm.exclude = MV_SYSTEM;
153 else if (lex_match_id (lexer, "EXCLUDE"))
155 glm.exclude = MV_ANY;
159 lex_error (lexer, NULL);
164 else if (lex_match_id (lexer, "INTERCEPT"))
166 lex_match (lexer, T_EQUALS);
167 while (lex_token (lexer) != T_ENDCMD
168 && lex_token (lexer) != T_SLASH)
170 if (lex_match_id (lexer, "INCLUDE"))
172 glm.intercept = true;
174 else if (lex_match_id (lexer, "EXCLUDE"))
176 glm.intercept = false;
180 lex_error (lexer, NULL);
185 else if (lex_match_id (lexer, "CRITERIA"))
187 lex_match (lexer, T_EQUALS);
188 if (lex_match_id (lexer, "ALPHA"))
190 if (lex_force_match (lexer, T_LPAREN))
192 if (! lex_force_num (lexer))
194 lex_error (lexer, NULL);
198 glm.alpha = lex_number (lexer);
200 if ( ! lex_force_match (lexer, T_RPAREN))
202 lex_error (lexer, NULL);
209 lex_error (lexer, NULL);
213 else if (lex_match_id (lexer, "METHOD"))
215 lex_match (lexer, T_EQUALS);
216 if ( !lex_force_match_id (lexer, "SSTYPE"))
218 lex_error (lexer, NULL);
222 if ( ! lex_force_match (lexer, T_LPAREN))
224 lex_error (lexer, NULL);
228 if ( ! lex_force_int (lexer))
230 lex_error (lexer, NULL);
234 if (3 != lex_integer (lexer))
236 msg (ME, _("Only type 3 sum of squares are currently implemented"));
242 if ( ! lex_force_match (lexer, T_RPAREN))
244 lex_error (lexer, NULL);
248 else if (lex_match_id (lexer, "DESIGN"))
250 lex_match (lexer, T_EQUALS);
252 if (! parse_design_spec (lexer, &glm))
255 if ( glm.n_design_vars == 0)
257 msg (ME, _("One or more design variables must be given"));
265 lex_error (lexer, NULL);
272 lex_error (lexer, _("/DESIGN is mandatory in GLM"));
277 struct casegrouper *grouper;
278 struct casereader *group;
281 grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
282 while (casegrouper_get_next_group (grouper, &group))
283 run_glm (&glm, group, ds);
284 ok = casegrouper_destroy (grouper);
285 ok = proc_commit (ds) && ok;
288 const_var_set_destroy (factors);
289 free (glm.factor_vars);
290 free (glm.design_vars);
298 const_var_set_destroy (factors);
299 free (glm.factor_vars);
300 free (glm.design_vars);
306 static void get_ssq (struct covariance *, gsl_vector *,
307 const struct glm_spec *);
310 not_dropped (size_t j, size_t * dropped, size_t n_dropped)
314 for (i = 0; i < n_dropped; i++)
323 get_ssq (struct covariance *cov, gsl_vector * ssq, const struct glm_spec *cmd)
325 const struct variable **vars;
326 gsl_matrix *small_cov = NULL;
327 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
336 dropped = xcalloc (covariance_dim (cov), sizeof (*dropped));
337 vars = xcalloc (covariance_dim (cov), sizeof (*vars));
338 covariance_get_var_indices (cov, vars);
340 for (k = 0; k < cmd->n_design_vars; k++)
343 for (i = 1; i < covariance_dim (cov); i++)
345 if (vars[i] == cmd->design_vars[k])
347 dropped[n_dropped++] = i;
351 gsl_matrix_alloc (cm->size1 - n_dropped, cm->size2 - n_dropped);
352 gsl_matrix_set (small_cov, 0, 0, gsl_matrix_get (cm, 0, 0));
355 for (i = 0; i < cm->size1; i++)
357 if (not_dropped (i, dropped, n_dropped))
360 for (j = 0; j < cm->size2; j++)
362 if (not_dropped (j, dropped, n_dropped))
364 gsl_matrix_set (small_cov, n, m,
365 gsl_matrix_get (cm, i, j));
372 reg_sweep (small_cov, 0);
373 gsl_vector_set (ssq, k + 1,
374 gsl_matrix_get (small_cov, 0, 0)
375 - gsl_vector_get (ssq, 0));
376 gsl_matrix_free (small_cov);
381 gsl_matrix_free (cm);
384 //static void dump_matrix (const gsl_matrix *m);
387 run_glm (struct glm_spec *cmd, struct casereader *input,
388 const struct dataset *ds)
390 bool warn_bad_weight = true;
393 struct dictionary *dict = dataset_dict (ds);
394 struct casereader *reader;
397 struct glm_workspace ws;
398 struct covariance *cov;
399 ws.cats = categoricals_create (cmd->design_vars, cmd->n_design_vars,
400 cmd->wv, cmd->exclude,
401 NULL, NULL, NULL, NULL);
403 cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
404 ws.cats, cmd->wv, cmd->exclude);
407 c = casereader_peek (input, 0);
410 casereader_destroy (input);
413 output_split_file_values (ds, c);
416 taint = taint_clone (casereader_get_taint (input));
418 ws.totals = moments_create (MOMENT_VARIANCE);
420 for (reader = casereader_clone (input);
421 (c = casereader_read (reader)) != NULL; case_unref (c))
423 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
425 for (v = 0; v < cmd->n_dep_vars; ++v)
426 moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
429 covariance_accumulate_pass1 (cov, c);
431 casereader_destroy (reader);
433 categoricals_done (ws.cats);
436 (c = casereader_read (reader)) != NULL; case_unref (c))
438 double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
440 for (v = 0; v < cmd->n_dep_vars; ++v)
441 moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
444 covariance_accumulate_pass2 (cov, c);
446 casereader_destroy (reader);
449 gsl_matrix *cm = covariance_calculate_unnormalized (cov);
453 ws.total_ssq = gsl_matrix_get (cm, 0, 0);
458 Store the overall SSE.
460 ws.ssq = gsl_vector_alloc (cm->size1);
461 gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
462 get_ssq (cov, ws.ssq, cmd);
465 gsl_matrix_free (cm);
468 if (!taint_has_tainted_successor (taint))
469 output_glm (cmd, &ws);
471 gsl_vector_free (ws.ssq);
473 covariance_destroy (cov);
474 moments_destroy (ws.totals);
476 taint_destroy (taint);
480 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
482 const struct fmt_spec *wfmt =
483 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
485 double n_total, mean;
486 double df_corr = 0.0;
491 const int heading_columns = 1;
492 const int heading_rows = 1;
496 int nr = heading_rows + 4 + cmd->n_design_vars;
500 t = tab_create (nc, nr);
501 tab_title (t, _("Tests of Between-Subjects Effects"));
503 tab_headers (t, heading_columns, 0, heading_rows, 0);
505 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
507 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
508 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
510 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
512 /* TRANSLATORS: The parameter is a roman numeral */
513 tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
514 _("Type %s Sum of Squares"), "III");
515 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
516 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
517 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
518 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
520 moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
525 for (f = 0; f < cmd->n_design_vars; ++f)
526 df_corr += categoricals_n_count (ws->cats, f) - 1.0;
528 mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
531 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
537 const double intercept = pow2 (mean * n_total) / n_total;
538 const double df = 1.0;
539 const double F = intercept / df / mse;
540 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
541 tab_double (t, 1, r, 0, intercept, NULL);
542 tab_double (t, 2, r, 0, 1.00, wfmt);
543 tab_double (t, 3, r, 0, intercept / df, NULL);
544 tab_double (t, 4, r, 0, F, NULL);
545 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
550 for (f = 0; f < cmd->n_design_vars; ++f)
552 const double df = categoricals_n_count (ws->cats, f) - 1.0;
553 const double ssq = gsl_vector_get (ws->ssq, f + 1);
554 const double F = ssq / df / mse;
555 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
556 var_to_string (cmd->design_vars[f]));
558 tab_double (t, 1, r, 0, ssq, NULL);
559 tab_double (t, 2, r, 0, df, wfmt);
560 tab_double (t, 3, r, 0, ssq / df, NULL);
561 tab_double (t, 4, r, 0, F, NULL);
563 tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
571 /* Corrected Model */
572 const double df = df_corr - 1.0;
573 const double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
574 const double F = ssq / df / mse;
575 tab_double (t, 1, heading_rows, 0, ssq, NULL);
576 tab_double (t, 2, heading_rows, 0, df, wfmt);
577 tab_double (t, 3, heading_rows, 0, ssq / df, NULL);
578 tab_double (t, 4, heading_rows, 0, F, NULL);
580 tab_double (t, 5, heading_rows, 0,
581 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL);
585 const double df = n_total - df_corr;
586 const double ssq = gsl_vector_get (ws->ssq, 0);
587 const double mse = ssq / df;
588 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
589 tab_double (t, 1, r, 0, ssq, NULL);
590 tab_double (t, 2, r, 0, df, wfmt);
591 tab_double (t, 3, r++, 0, mse, NULL);
596 const double intercept = pow2 (mean * n_total) / n_total;
597 const double ssq = intercept + ws->total_ssq;
599 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
600 tab_double (t, 1, r, 0, ssq, NULL);
601 tab_double (t, 2, r, 0, n_total, wfmt);
606 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
609 tab_double (t, 1, r, 0, ws->total_ssq, NULL);
610 tab_double (t, 2, r, 0, n_total - 1.0, wfmt);
617 dump_matrix (const gsl_matrix * m)
620 for (i = 0; i < m->size1; ++i)
622 for (j = 0; j < m->size2; ++j)
624 double x = gsl_matrix_get (m, i, j);
637 If the match succeeds, the variable will be placed in VAR.
638 Returns true if successful */
640 lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
642 if (lex_token (lexer) != T_ID)
645 *var = parse_variable_const (lexer, glm->dict);
652 /* An interaction is a variable followed by {*, BY} followed by an interaction */
654 parse_design_interaction (struct lexer *lexer, struct glm_spec *glm)
656 const struct variable *v = NULL;
657 if (! lex_match_variable (lexer, glm, &v))
660 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
662 lex_error (lexer, "Interactions are not yet implemented"); return false;
663 return parse_design_interaction (lexer, glm);
666 glm->n_design_vars++;
667 glm->design_vars = xrealloc (glm->design_vars, sizeof (*glm->design_vars) * glm->n_design_vars);
668 glm->design_vars[glm->n_design_vars - 1] = v;
673 /* A design term is a varible OR an interaction */
675 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
677 const struct variable *v = NULL;
678 if (parse_design_interaction (lexer, glm))
681 /* FIXME: This should accept nexted variables */
682 if ( lex_match_variable (lexer, glm, &v))
692 /* Parse a complete DESIGN specification.
693 A design spec is a design term, optionally followed by a comma,
694 and another design spec.
697 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
699 /* Kludge: Return success if end of design spec */
700 if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
703 if ( ! parse_design_term (lexer, glm))
706 lex_match (lexer, T_COMMA);
708 return parse_design_spec (lexer, glm);