Added result_class parameter to tab_double and updated all callers. Removed tab_fixed
[pspp] / src / language / stats / glm.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gsl/gsl_cdf.h>
20 #include <gsl/gsl_matrix.h>
21 #include <gsl/gsl_combination.h>
22 #include <math.h>
23
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/assertion.h"
37 #include "libpspp/ll.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/taint.h"
41 #include "linreg/sweep.h"
42 #include "math/categoricals.h"
43 #include "math/covariance.h"
44 #include "math/interaction.h"
45 #include "math/moments.h"
46 #include "output/tab.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext (msgid)
50
51 struct glm_spec
52 {
53   size_t n_dep_vars;
54   const struct variable **dep_vars;
55
56   size_t n_factor_vars;
57   const struct variable **factor_vars;
58
59   size_t n_interactions;
60   struct interaction **interactions;
61
62   enum mv_class exclude;
63
64   /* The weight variable */
65   const struct variable *wv;
66
67   const struct dictionary *dict;
68
69   int ss_type;
70   bool intercept;
71
72   double alpha;
73
74   bool dump_coding;
75 };
76
77 struct glm_workspace
78 {
79   double total_ssq;
80   struct moments *totals;
81
82   struct categoricals *cats;
83
84   /* 
85      Sums of squares due to different variables. Element 0 is the SSE
86      for the entire model. For i > 0, element i is the SS due to
87      variable i.
88    */
89   gsl_vector *ssq;
90 };
91
92
93 /* Default design: all possible interactions */
94 static void
95 design_full (struct glm_spec *glm)
96 {
97   int sz;
98   int i = 0;
99   glm->n_interactions = (1 << glm->n_factor_vars) - 1;
100
101   glm->interactions = xcalloc (glm->n_interactions, sizeof *glm->interactions);
102
103   /* All subsets, with exception of the empty set, of [0, glm->n_factor_vars) */
104   for (sz = 1; sz <= glm->n_factor_vars; ++sz)
105     {
106       gsl_combination *c = gsl_combination_calloc (glm->n_factor_vars, sz);
107
108       do
109         {
110           struct interaction *iact = interaction_create (NULL);
111           int e;
112           for (e = 0 ; e < gsl_combination_k (c); ++e)
113             interaction_add_variable (iact, glm->factor_vars [gsl_combination_get (c, e)]);
114
115           glm->interactions[i++] = iact;
116         }
117       while (gsl_combination_next (c) == GSL_SUCCESS);
118
119       gsl_combination_free (c);
120     }
121 }
122
123 static void output_glm (const struct glm_spec *,
124                         const struct glm_workspace *ws);
125 static void run_glm (struct glm_spec *cmd, struct casereader *input,
126                      const struct dataset *ds);
127
128
129 static bool parse_design_spec (struct lexer *lexer, struct glm_spec *glm);
130
131
132 int
133 cmd_glm (struct lexer *lexer, struct dataset *ds)
134 {
135   int i;
136   struct const_var_set *factors = NULL;
137   struct glm_spec glm;
138   bool design = false;
139   glm.dict = dataset_dict (ds);
140   glm.n_dep_vars = 0;
141   glm.n_factor_vars = 0;
142   glm.n_interactions = 0;
143   glm.interactions = NULL;
144   glm.dep_vars = NULL;
145   glm.factor_vars = NULL;
146   glm.exclude = MV_ANY;
147   glm.intercept = true;
148   glm.wv = dict_get_weight (glm.dict);
149   glm.alpha = 0.05;
150   glm.dump_coding = false;
151   glm.ss_type = 3;
152
153   if (!parse_variables_const (lexer, glm.dict,
154                               &glm.dep_vars, &glm.n_dep_vars,
155                               PV_NO_DUPLICATE | PV_NUMERIC))
156     goto error;
157
158   lex_force_match (lexer, T_BY);
159
160   if (!parse_variables_const (lexer, glm.dict,
161                               &glm.factor_vars, &glm.n_factor_vars,
162                               PV_NO_DUPLICATE | PV_NUMERIC))
163     goto error;
164
165   if (glm.n_dep_vars > 1)
166     {
167       msg (ME, _("Multivariate analysis is not yet implemented"));
168       return CMD_FAILURE;
169     }
170
171   factors =
172     const_var_set_create_from_array (glm.factor_vars, glm.n_factor_vars);
173
174   while (lex_token (lexer) != T_ENDCMD)
175     {
176       lex_match (lexer, T_SLASH);
177
178       if (lex_match_id (lexer, "MISSING"))
179         {
180           lex_match (lexer, T_EQUALS);
181           while (lex_token (lexer) != T_ENDCMD
182                  && lex_token (lexer) != T_SLASH)
183             {
184               if (lex_match_id (lexer, "INCLUDE"))
185                 {
186                   glm.exclude = MV_SYSTEM;
187                 }
188               else if (lex_match_id (lexer, "EXCLUDE"))
189                 {
190                   glm.exclude = MV_ANY;
191                 }
192               else
193                 {
194                   lex_error (lexer, NULL);
195                   goto error;
196                 }
197             }
198         }
199       else if (lex_match_id (lexer, "INTERCEPT"))
200         {
201           lex_match (lexer, T_EQUALS);
202           while (lex_token (lexer) != T_ENDCMD
203                  && lex_token (lexer) != T_SLASH)
204             {
205               if (lex_match_id (lexer, "INCLUDE"))
206                 {
207                   glm.intercept = true;
208                 }
209               else if (lex_match_id (lexer, "EXCLUDE"))
210                 {
211                   glm.intercept = false;
212                 }
213               else
214                 {
215                   lex_error (lexer, NULL);
216                   goto error;
217                 }
218             }
219         }
220       else if (lex_match_id (lexer, "CRITERIA"))
221         {
222           lex_match (lexer, T_EQUALS);
223           if (lex_match_id (lexer, "ALPHA"))
224             {
225               if (lex_force_match (lexer, T_LPAREN))
226                 {
227                   if (! lex_force_num (lexer))
228                     {
229                       lex_error (lexer, NULL);
230                       goto error;
231                     }
232                   
233                   glm.alpha = lex_number (lexer);
234                   lex_get (lexer);
235                   if ( ! lex_force_match (lexer, T_RPAREN))
236                     {
237                       lex_error (lexer, NULL);
238                       goto error;
239                     }
240                 }
241             }
242           else
243             {
244               lex_error (lexer, NULL);
245               goto error;
246             }
247         }
248       else if (lex_match_id (lexer, "METHOD"))
249         {
250           lex_match (lexer, T_EQUALS);
251           if ( !lex_force_match_id (lexer, "SSTYPE"))
252             {
253               lex_error (lexer, NULL);
254               goto error;
255             }
256
257           if ( ! lex_force_match (lexer, T_LPAREN))
258             {
259               lex_error (lexer, NULL);
260               goto error;
261             }
262
263           if ( ! lex_force_int (lexer))
264             {
265               lex_error (lexer, NULL);
266               goto error;
267             }
268
269           glm.ss_type = lex_integer (lexer);
270           if (1 > glm.ss_type  ||  3 < glm.ss_type )
271             {
272               msg (ME, _("Only types 1, 2 & 3 sums of squares are currently implemented"));
273               goto error;
274             }
275
276           lex_get (lexer);
277
278           if ( ! lex_force_match (lexer, T_RPAREN))
279             {
280               lex_error (lexer, NULL);
281               goto error;
282             }
283         }
284       else if (lex_match_id (lexer, "DESIGN"))
285         {
286           lex_match (lexer, T_EQUALS);
287
288           if (! parse_design_spec (lexer, &glm))
289             goto error;
290
291           if (glm.n_interactions > 0)
292             design = true;
293         }
294       else if (lex_match_id (lexer, "SHOWCODES"))
295         /* Undocumented debug option */
296         {
297           lex_match (lexer, T_EQUALS);
298
299           glm.dump_coding = true;
300         }
301       else
302         {
303           lex_error (lexer, NULL);
304           goto error;
305         }
306     }
307
308   if ( ! design )
309     {
310       design_full (&glm);
311     }
312
313   {
314     struct casegrouper *grouper;
315     struct casereader *group;
316     bool ok;
317
318     grouper = casegrouper_create_splits (proc_open (ds), glm.dict);
319     while (casegrouper_get_next_group (grouper, &group))
320       run_glm (&glm, group, ds);
321     ok = casegrouper_destroy (grouper);
322     ok = proc_commit (ds) && ok;
323   }
324
325   const_var_set_destroy (factors);
326   free (glm.factor_vars);
327   for (i = 0 ; i < glm.n_interactions; ++i)
328     interaction_destroy (glm.interactions[i]);
329
330   free (glm.interactions);
331   free (glm.dep_vars);
332
333
334   return CMD_SUCCESS;
335
336 error:
337
338   const_var_set_destroy (factors);
339   free (glm.factor_vars);
340   for (i = 0 ; i < glm.n_interactions; ++i)
341     interaction_destroy (glm.interactions[i]);
342
343   free (glm.interactions);
344   free (glm.dep_vars);
345
346   return CMD_FAILURE;
347 }
348
349 static inline bool
350 not_dropped (size_t j, const bool *ff)
351 {
352   return ! ff[j];
353 }
354
355 static void
356 fill_submatrix (const gsl_matrix * cov, gsl_matrix * submatrix, bool *dropped_f)
357 {
358   size_t i;
359   size_t j;
360   size_t n = 0;
361   size_t m = 0;
362   
363   for (i = 0; i < cov->size1; i++)
364     {
365       if (not_dropped (i, dropped_f))
366         {         
367           m = 0;
368           for (j = 0; j < cov->size2; j++)
369             {
370               if (not_dropped (j, dropped_f))
371                 {
372                   gsl_matrix_set (submatrix, n, m,
373                                   gsl_matrix_get (cov, i, j));
374                   m++;
375                 }       
376             }
377           n++;
378         }
379     }
380 }
381
382
383 /* 
384    Type 1 sums of squares.
385    Populate SSQ with the Type 1 sums of squares according to COV
386  */
387 static void
388 ssq_type1 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
389 {
390   const gsl_matrix *cm = covariance_calculate_unnormalized (cov);
391   size_t i;
392   size_t k;
393   bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
394   bool *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
395   const struct categoricals *cats = covariance_get_categoricals (cov);
396
397   size_t n_dropped_model = 0;
398   size_t n_dropped_submodel = 0;
399
400   for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
401     {
402       n_dropped_model++;
403       n_dropped_submodel++;
404       model_dropped[i] = true;
405       submodel_dropped[i] = true;
406     }
407
408   for (k = 0; k < cmd->n_interactions; k++)
409     {
410       gsl_matrix *model_cov = NULL;
411       gsl_matrix *submodel_cov = NULL;
412       
413       n_dropped_submodel = n_dropped_model;
414       for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
415         {
416           submodel_dropped[i] = model_dropped[i];
417         }
418
419       for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
420         {
421           const struct interaction * x = 
422             categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
423
424           if ( x == cmd->interactions [k])
425             {
426               model_dropped[i] = false;
427               n_dropped_model--;
428             }
429         }
430
431       model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
432       submodel_cov = gsl_matrix_alloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
433
434       fill_submatrix (cm, model_cov,    model_dropped);
435       fill_submatrix (cm, submodel_cov, submodel_dropped);
436
437       reg_sweep (model_cov, 0);
438       reg_sweep (submodel_cov, 0);
439
440       gsl_vector_set (ssq, k + 1,
441                       gsl_matrix_get (submodel_cov, 0, 0) - gsl_matrix_get (model_cov, 0, 0)
442                       );
443
444       gsl_matrix_free (model_cov);
445       gsl_matrix_free (submodel_cov);
446     }
447
448   free (model_dropped);
449   free (submodel_dropped);
450 }
451
452 /* 
453    Type 2 sums of squares.
454    Populate SSQ with the Type 2 sums of squares according to COV
455  */
456 static void
457 ssq_type2 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
458 {
459   const gsl_matrix *cm = covariance_calculate_unnormalized (cov);
460   size_t i;
461   size_t k;
462   bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
463   bool *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
464   const struct categoricals *cats = covariance_get_categoricals (cov);
465
466   for (k = 0; k < cmd->n_interactions; k++)
467     {
468       gsl_matrix *model_cov = NULL;
469       gsl_matrix *submodel_cov = NULL;
470       size_t n_dropped_model = 0;
471       size_t n_dropped_submodel = 0;
472       for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
473         {
474           const struct interaction * x = 
475             categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
476
477           model_dropped[i] = false;
478           submodel_dropped[i] = false;
479           if (interaction_is_subset (cmd->interactions [k], x))
480             {
481               assert (n_dropped_submodel < covariance_dim (cov));
482               n_dropped_submodel++;
483               submodel_dropped[i] = true;
484
485               if ( cmd->interactions [k]->n_vars < x->n_vars)
486                 {
487                   assert (n_dropped_model < covariance_dim (cov));
488                   n_dropped_model++;
489                   model_dropped[i] = true;
490                 }
491             }
492         }
493
494       model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
495       submodel_cov = gsl_matrix_alloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
496
497       fill_submatrix (cm, model_cov,    model_dropped);
498       fill_submatrix (cm, submodel_cov, submodel_dropped);
499
500       reg_sweep (model_cov, 0);
501       reg_sweep (submodel_cov, 0);
502
503       gsl_vector_set (ssq, k + 1,
504                       gsl_matrix_get (submodel_cov, 0, 0) - gsl_matrix_get (model_cov, 0, 0)
505                       );
506
507       gsl_matrix_free (model_cov);
508       gsl_matrix_free (submodel_cov);
509     }
510
511   free (model_dropped);
512   free (submodel_dropped);
513 }
514
515 /* 
516    Type 3 sums of squares.
517    Populate SSQ with the Type 2 sums of squares according to COV
518  */
519 static void
520 ssq_type3 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
521 {
522   const gsl_matrix *cm = covariance_calculate_unnormalized (cov);
523   size_t i;
524   size_t k;
525   bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped));
526   bool *submodel_dropped = xcalloc (covariance_dim (cov), sizeof (*submodel_dropped));
527   const struct categoricals *cats = covariance_get_categoricals (cov);
528
529   double ss0;
530   gsl_matrix *submodel_cov = gsl_matrix_alloc (cm->size1, cm->size2);
531   fill_submatrix (cm, submodel_cov, submodel_dropped);
532   reg_sweep (submodel_cov, 0);
533   ss0 = gsl_matrix_get (submodel_cov, 0, 0);
534   gsl_matrix_free (submodel_cov);
535   free (submodel_dropped);
536
537   for (k = 0; k < cmd->n_interactions; k++)
538     {
539       gsl_matrix *model_cov = NULL;
540       size_t n_dropped_model = 0;
541
542       for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
543         {
544           const struct interaction * x = 
545             categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
546
547           model_dropped[i] = false;
548
549           if ( cmd->interactions [k] == x)
550             {
551               assert (n_dropped_model < covariance_dim (cov));
552               n_dropped_model++;
553               model_dropped[i] = true;
554             }
555         }
556
557       model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
558
559       fill_submatrix (cm, model_cov,    model_dropped);
560
561       reg_sweep (model_cov, 0);
562
563       gsl_vector_set (ssq, k + 1,
564                       gsl_matrix_get (model_cov, 0, 0) - ss0);
565
566       gsl_matrix_free (model_cov);
567     }
568   free (model_dropped);
569 }
570
571
572
573 //static  void dump_matrix (const gsl_matrix *m);
574
575 static void
576 run_glm (struct glm_spec *cmd, struct casereader *input,
577          const struct dataset *ds)
578 {
579   bool warn_bad_weight = true;
580   int v;
581   struct taint *taint;
582   struct dictionary *dict = dataset_dict (ds);
583   struct casereader *reader;
584   struct ccase *c;
585
586   struct glm_workspace ws;
587   struct covariance *cov;
588
589   ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions,
590                                  cmd->wv, cmd->exclude, MV_ANY);
591
592   cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars,
593                                  ws.cats, cmd->wv, cmd->exclude);
594
595
596   c = casereader_peek (input, 0);
597   if (c == NULL)
598     {
599       casereader_destroy (input);
600       return;
601     }
602   output_split_file_values (ds, c);
603   case_unref (c);
604
605   taint = taint_clone (casereader_get_taint (input));
606
607   ws.totals = moments_create (MOMENT_VARIANCE);
608
609   for (reader = casereader_clone (input);
610        (c = casereader_read (reader)) != NULL; case_unref (c))
611     {
612       double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
613
614       for (v = 0; v < cmd->n_dep_vars; ++v)
615         moments_pass_one (ws.totals, case_data (c, cmd->dep_vars[v])->f,
616                           weight);
617
618       covariance_accumulate_pass1 (cov, c);
619     }
620   casereader_destroy (reader);
621
622   if (cmd->dump_coding)
623     reader = casereader_clone (input);
624   else
625     reader = input;
626
627   for (;
628        (c = casereader_read (reader)) != NULL; case_unref (c))
629     {
630       double weight = dict_get_case_weight (dict, c, &warn_bad_weight);
631
632       for (v = 0; v < cmd->n_dep_vars; ++v)
633         moments_pass_two (ws.totals, case_data (c, cmd->dep_vars[v])->f,
634                           weight);
635
636       covariance_accumulate_pass2 (cov, c);
637     }
638   casereader_destroy (reader);
639
640
641   if (cmd->dump_coding)
642     {
643       struct tab_table *t =
644         covariance_dump_enc_header (cov,
645                                     1 + casereader_count_cases (input));
646       for (reader = input;
647            (c = casereader_read (reader)) != NULL; case_unref (c))
648         {
649           covariance_dump_enc (cov, c, t);
650         }
651       casereader_destroy (reader);
652       tab_submit (t);
653     }
654
655   {
656     const gsl_matrix *ucm = covariance_calculate_unnormalized (cov);
657     gsl_matrix *cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
658     gsl_matrix_memcpy (cm, ucm);
659
660     //    dump_matrix (cm);
661
662     ws.total_ssq = gsl_matrix_get (cm, 0, 0);
663
664     reg_sweep (cm, 0);
665
666     /*
667       Store the overall SSE.
668     */
669     ws.ssq = gsl_vector_alloc (cm->size1);
670     gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0));
671     switch (cmd->ss_type)
672       {
673       case 1:
674         ssq_type1 (cov, ws.ssq, cmd);
675         break;
676       case 2:
677         ssq_type2 (cov, ws.ssq, cmd);
678         break;
679       case 3:
680         ssq_type3 (cov, ws.ssq, cmd);
681         break;
682       default:
683         NOT_REACHED ();
684         break;
685       }
686     //    dump_matrix (cm);
687     gsl_matrix_free (cm);
688   }
689
690   if (!taint_has_tainted_successor (taint))
691     output_glm (cmd, &ws);
692
693   gsl_vector_free (ws.ssq);
694
695   covariance_destroy (cov);
696   moments_destroy (ws.totals);
697
698   taint_destroy (taint);
699 }
700
701 static const char *roman[] = 
702   {
703     "", /* The Romans had no concept of zero */
704     "I",
705     "II",
706     "III",
707     "IV"
708   };
709
710 static void
711 output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws)
712 {
713   const struct fmt_spec *wfmt =
714     cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
715
716   double intercept_ssq;
717   double ssq_effects;
718   double n_total, mean;
719   double df_corr = 1.0;
720   double mse = 0;
721
722   int f;
723   int r;
724   const int heading_columns = 1;
725   const int heading_rows = 1;
726   struct tab_table *t;
727
728   const int nc = 6;
729   int nr = heading_rows + 3 + cmd->n_interactions;
730   if (cmd->intercept)
731     nr += 2;
732
733   msg (MW, "GLM is experimental.  Do not rely on these results.");
734   t = tab_create (nc, nr);
735   tab_set_format (t, RC_WEIGHT, wfmt);
736   tab_title (t, _("Tests of Between-Subjects Effects"));
737
738   tab_headers (t, heading_columns, 0, heading_rows, 0);
739
740   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
741
742   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
743   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
744
745   tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
746
747   /* TRANSLATORS: The parameter is a roman numeral */
748   tab_text_format (t, 1, 0, TAB_CENTER | TAT_TITLE,
749                    _("Type %s Sum of Squares"), 
750                    roman[cmd->ss_type]);
751   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df"));
752   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
753   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("F"));
754   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
755
756   moments_calculate (ws->totals, &n_total, &mean, NULL, NULL, NULL);
757
758   df_corr += categoricals_df_total (ws->cats);
759
760   r = heading_rows;
761   if (cmd->intercept)
762     tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Model"));
763   else
764     tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Model"));
765
766   r++;
767
768   mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr);
769
770   intercept_ssq = pow2 (mean * n_total) / n_total;
771
772   ssq_effects = 0.0;
773   if (cmd->intercept)
774     {
775       const double df = 1.0;
776       const double F = intercept_ssq / df / mse;
777       tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept"));
778       tab_double (t, 1, r, 0, intercept_ssq, NULL, RC_OTHER);
779       tab_double (t, 2, r, 0, 1.00, NULL, RC_WEIGHT);
780       tab_double (t, 3, r, 0, intercept_ssq / df, NULL, RC_OTHER);
781       tab_double (t, 4, r, 0, F, NULL, RC_OTHER);
782       tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
783                   NULL, RC_PVALUE);
784       r++;
785     }
786
787   for (f = 0; f < cmd->n_interactions; ++f)
788     {
789       struct string str = DS_EMPTY_INITIALIZER;
790       double df = categoricals_df (ws->cats, f);
791
792       double ssq = gsl_vector_get (ws->ssq, f + 1);
793       double F;
794
795       ssq_effects += ssq;
796
797       if (! cmd->intercept) 
798         {
799           df++;
800           ssq += intercept_ssq;
801         }
802
803       F = ssq / df / mse;
804       interaction_to_string (cmd->interactions[f], &str);
805       tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
806       ds_destroy (&str);
807
808       tab_double (t, 1, r, 0, ssq, NULL, RC_OTHER);
809       tab_double (t, 2, r, 0, df, NULL, RC_WEIGHT);
810       tab_double (t, 3, r, 0, ssq / df, NULL, RC_OTHER);
811       tab_double (t, 4, r, 0, F, NULL, RC_OTHER);
812
813       tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr),
814                   NULL, RC_PVALUE);
815       r++;
816     }
817
818   {
819     /* Model / Corrected Model */
820     double df = df_corr;
821     double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0);
822     double F;
823
824     if ( cmd->intercept )
825       df --;
826     else
827       ssq += intercept_ssq;
828
829     F = ssq / df / mse;
830     tab_double (t, 1, heading_rows, 0, ssq, NULL, RC_OTHER);
831     tab_double (t, 2, heading_rows, 0, df, NULL, RC_WEIGHT);
832     tab_double (t, 3, heading_rows, 0, ssq / df, NULL, RC_OTHER);
833     tab_double (t, 4, heading_rows, 0, F, NULL, RC_OTHER);
834
835     tab_double (t, 5, heading_rows, 0,
836                 gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL, RC_PVALUE);
837   }
838
839   {
840     const double df = n_total - df_corr;
841     const double ssq = gsl_vector_get (ws->ssq, 0);
842     const double mse = ssq / df;
843     tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error"));
844     tab_double (t, 1, r, 0, ssq, NULL, RC_OTHER);
845     tab_double (t, 2, r, 0, df, NULL, RC_WEIGHT);
846     tab_double (t, 3, r++, 0, mse, NULL, RC_OTHER);
847   }
848
849   {
850     tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total"));
851     tab_double (t, 1, r, 0, ws->total_ssq + intercept_ssq, NULL, RC_OTHER);
852     tab_double (t, 2, r, 0, n_total, NULL, RC_WEIGHT);
853     
854     r++;
855   }
856
857   if (cmd->intercept)
858     {
859       tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total"));
860       tab_double (t, 1, r, 0, ws->total_ssq, NULL, RC_OTHER);
861       tab_double (t, 2, r, 0, n_total - 1.0, NULL, RC_WEIGHT);
862     }
863
864   tab_submit (t);
865 }
866
867 #if 0
868 static void
869 dump_matrix (const gsl_matrix * m)
870 {
871   size_t i, j;
872   for (i = 0; i < m->size1; ++i)
873     {
874       for (j = 0; j < m->size2; ++j)
875         {
876           double x = gsl_matrix_get (m, i, j);
877           printf ("%.3f ", x);
878         }
879       printf ("\n");
880     }
881   printf ("\n");
882 }
883 #endif
884
885
886 \f
887 static bool
888 parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
889 {
890   const struct variable *v = NULL;
891   if ( ! lex_match_variable (lexer, glm->dict, &v))
892     return false;
893
894   if (lex_match (lexer, T_LPAREN))
895     {
896       if ( ! parse_nested_variable (lexer, glm))
897         return false;
898
899       if ( ! lex_force_match (lexer, T_RPAREN))
900         return false;
901     }
902
903   lex_error (lexer, "Nested variables are not yet implemented"); return false;  
904   return true;
905 }
906
907 /* A design term is an interaction OR a nested variable */
908 static bool
909 parse_design_term (struct lexer *lexer, struct glm_spec *glm)
910 {
911   struct interaction *iact = NULL;
912   if (parse_design_interaction (lexer, glm->dict, &iact))
913     {
914       /* Interaction parsing successful.  Add to list of interactions */
915       glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
916       glm->interactions[glm->n_interactions - 1] = iact;
917       return true;
918     }
919
920   if ( parse_nested_variable (lexer, glm))
921     return true;
922
923   return false;
924 }
925
926
927
928 /* Parse a complete DESIGN specification.
929    A design spec is a design term, optionally followed by a comma,
930    and another design spec.
931 */
932 static bool
933 parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
934 {
935   if  (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
936     return true;
937
938   if ( ! parse_design_term (lexer, glm))
939     return false;
940
941   lex_match (lexer, T_COMMA);
942
943   return parse_design_spec (lexer, glm);
944 }
945