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