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