Fix crash when ONEWAY/CONTRASTS contained an empty list.
[pspp] / src / language / stats / oneway.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013, 2014 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 <float.h>
20 #include <gsl/gsl_cdf.h>
21 #include <gsl/gsl_matrix.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 "tukey/tukey.h"
42 #include "math/categoricals.h"
43 #include "math/interaction.h"
44 #include "math/covariance.h"
45 #include "math/levene.h"
46 #include "math/moments.h"
47 #include "output/tab.h"
48
49 #include "gettext.h"
50 #define _(msgid) gettext (msgid)
51 #define N_(msgid) msgid
52
53 /* Workspace variable for each dependent variable */
54 struct per_var_ws
55 {
56   struct interaction *iact;
57   struct categoricals *cat;
58   struct covariance *cov;
59   struct levene *nl;
60
61   double n;
62
63   double sst;
64   double sse;
65   double ssa;
66
67   int n_groups;
68
69   double mse;
70 };
71
72 /* Per category data */
73 struct descriptive_data
74 {
75   const struct variable *var;
76   struct moments1 *mom;
77
78   double minimum;
79   double maximum;
80 };
81
82 enum missing_type
83   {
84     MISS_LISTWISE,
85     MISS_ANALYSIS,
86   };
87
88 enum statistics
89   {
90     STATS_DESCRIPTIVES = 0x0001,
91     STATS_HOMOGENEITY = 0x0002
92   };
93
94 struct coeff_node
95 {
96   struct ll ll; 
97   double coeff; 
98 };
99
100
101 struct contrasts_node
102 {
103   struct ll ll; 
104   struct ll_list coefficient_list;
105 };
106
107
108 struct oneway_spec;
109
110 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
111 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
112 typedef double p1tail_func (double ts, double df1, double df2);
113
114 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
115
116
117 struct posthoc
118 {
119   const char *syntax;
120   const char *label;
121
122   df_func *dff;
123   ts_func *tsf;
124   p1tail_func *p1f;
125
126   pinv_func *pinv;
127 };
128
129 struct oneway_spec
130 {
131   size_t n_vars;
132   const struct variable **vars;
133
134   const struct variable *indep_var;
135
136   enum statistics stats;
137
138   enum missing_type missing_type;
139   enum mv_class exclude;
140
141   /* List of contrasts */
142   struct ll_list contrast_list;
143
144   /* The weight variable */
145   const struct variable *wv;
146
147   /* The confidence level for multiple comparisons */
148   double alpha;
149
150   int *posthoc;
151   int n_posthoc;
152 };
153
154 static double
155 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
156 {
157   return  pvw->n - pvw->n_groups;
158 }
159
160 static double
161 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
162 {
163   double n_i, var_i;
164   double n_j, var_j;
165   double nom,denom;
166
167   moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);  
168   moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
169   
170   if ( n_i <= 1.0 || n_j <= 1.0)
171     return SYSMIS;
172
173   nom = pow2 (var_i/n_i + var_j/n_j);
174   denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
175
176   return nom / denom;
177 }
178
179 static double lsd_pinv (double std_err, double alpha, double df, int k UNUSED, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
180 {
181   return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
182 }
183
184 static double bonferroni_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
185 {
186   const int m = k * (k - 1) / 2;
187   return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
188 }
189
190 static double sidak_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
191 {
192   const double m = k * (k - 1) / 2;
193   double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
194   return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
195 }
196
197 static double tukey_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
198 {
199   if ( k < 2 || df < 2)
200     return SYSMIS;
201
202   return std_err / sqrt (2.0)  * qtukey (1 - alpha, 1.0, k, df, 1, 0);
203 }
204
205 static double scheffe_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
206 {
207   double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
208   return std_err * sqrt (x);
209 }
210
211 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
212 {
213   double n_i, mean_i, var_i;
214   double n_j, mean_j, var_j;
215   double m;
216
217   moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
218   moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
219
220   m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
221
222   if ( k < 2 || df < 2)
223     return SYSMIS;
224
225   return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
226 }
227
228
229 static double 
230 multiple_comparison_sig (double std_err,
231                                        const struct per_var_ws *pvw,
232                                        const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
233                                        const struct posthoc *ph)
234 {
235   int k = pvw->n_groups;
236   double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
237   double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
238   if ( df == SYSMIS)
239     return SYSMIS;
240   return  ph->p1f (ts, k - 1, df);
241 }
242
243 static double 
244 mc_half_range (const struct oneway_spec *cmd, const struct per_var_ws *pvw, double std_err, const struct descriptive_data *dd_i, const struct descriptive_data *dd_j, const struct posthoc *ph)
245 {
246   int k = pvw->n_groups;
247   double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
248   if ( df == SYSMIS)
249     return SYSMIS;
250
251   return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
252 }
253
254 static double tukey_1tailsig (double ts, double df1, double df2)
255 {
256   double twotailedsig;
257
258   if (df2 < 2 || df1 < 1)
259     return SYSMIS;
260
261   twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
262
263   return twotailedsig / 2.0;
264 }
265
266 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
267 {
268   return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
269 }
270
271 static double sidak_1tailsig (double ts, double df1, double df2)
272 {
273   double ex = (df1 + 1.0) * df1 / 2.0;
274   double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
275
276   return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
277 }
278
279 static double bonferroni_1tailsig (double ts, double df1, double df2)
280 {
281   const int m = (df1 + 1) * df1 / 2;
282
283   double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
284   p *= m;
285
286   return p > 0.5 ? 0.5 : p;
287 }
288
289 static double scheffe_1tailsig (double ts, double df1, double df2)
290 {
291   return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
292 }
293
294
295 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
296 {
297   double ts;
298   double n_i, mean_i, var_i;
299   double n_j, mean_j, var_j;
300
301   moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
302   moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
303
304   ts =  (mean_i - mean_j) / std_err;
305   ts = fabs (ts) * sqrt (2.0);
306
307   return ts;
308 }
309
310 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
311 {
312   double n_i, mean_i, var_i;
313   double n_j, mean_j, var_j;
314
315   moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
316   moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
317
318   return (mean_i - mean_j) / std_err;
319 }
320
321 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
322 {
323   double t;
324   double n_i, mean_i, var_i;
325   double n_j, mean_j, var_j;
326
327   moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
328   moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
329
330   t = (mean_i - mean_j) / std_err;
331   t = pow2 (t);
332   t /= k - 1;
333
334   return t;
335 }
336
337 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
338 {
339   double ts;
340   double thing;
341   double n_i, mean_i, var_i;
342   double n_j, mean_j, var_j;
343
344   moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
345   moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
346
347   thing = var_i / n_i + var_j / n_j;
348   thing /= 2.0;
349   thing = sqrt (thing);
350
351   ts = (mean_i - mean_j) / thing;
352
353   return fabs (ts);
354 }
355
356
357
358 static const struct posthoc ph_tests [] = 
359   {
360     { "LSD",        N_("LSD"),          df_common, lsd_test_stat,     lsd_1tailsig,          lsd_pinv},
361     { "TUKEY",      N_("Tukey HSD"),    df_common, tukey_test_stat,   tukey_1tailsig,        tukey_pinv},
362     { "BONFERRONI", N_("Bonferroni"),   df_common, lsd_test_stat,     bonferroni_1tailsig,   bonferroni_pinv},
363     { "SCHEFFE",    N_("Scheffé"),      df_common, scheffe_test_stat, scheffe_1tailsig,      scheffe_pinv},
364     { "GH",         N_("Games-Howell"), df_individual, gh_test_stat,  tukey_1tailsig,        gh_pinv},
365     { "SIDAK",      N_("Å idák"),        df_common, lsd_test_stat,     sidak_1tailsig,        sidak_pinv}
366   };
367
368
369 struct oneway_workspace
370 {
371   /* The number of distinct values of the independent variable, when all
372      missing values are disregarded */
373   int actual_number_of_groups;
374
375   struct per_var_ws *vws;
376
377   /* An array of descriptive data.  One for each dependent variable */
378   struct descriptive_data **dd_total;
379 };
380
381 /* Routines to show the output tables */
382 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
383 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
384 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
385
386 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
387 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
388
389
390 static void
391 destroy_coeff_list (struct contrasts_node *coeff_list)
392 {
393   struct coeff_node *cn = NULL;
394   struct coeff_node *cnx = NULL;
395   struct ll_list *cl = &coeff_list->coefficient_list;
396   
397   ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
398     {
399       free (cn);
400     }
401   
402   free (coeff_list);
403 }
404
405 static void
406 oneway_cleanup (struct oneway_spec *cmd)
407 {
408   struct contrasts_node *coeff_list  = NULL;
409   struct contrasts_node *coeff_next  = NULL;
410   ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
411     {
412       destroy_coeff_list (coeff_list);
413     }
414
415   free (cmd->posthoc);
416 }
417
418
419
420 int
421 cmd_oneway (struct lexer *lexer, struct dataset *ds)
422 {
423   const struct dictionary *dict = dataset_dict (ds);  
424   struct oneway_spec oneway ;
425   oneway.n_vars = 0;
426   oneway.vars = NULL;
427   oneway.indep_var = NULL;
428   oneway.stats = 0;
429   oneway.missing_type = MISS_ANALYSIS;
430   oneway.exclude = MV_ANY;
431   oneway.wv = dict_get_weight (dict);
432   oneway.alpha = 0.05;
433   oneway.posthoc = NULL;
434   oneway.n_posthoc = 0;
435
436   ll_init (&oneway.contrast_list);
437
438   
439   if ( lex_match (lexer, T_SLASH))
440     {
441       if (!lex_force_match_id (lexer, "VARIABLES"))
442         {
443           goto error;
444         }
445       lex_match (lexer, T_EQUALS);
446     }
447
448   if (!parse_variables_const (lexer, dict,
449                               &oneway.vars, &oneway.n_vars,
450                               PV_NO_DUPLICATE | PV_NUMERIC))
451     goto error;
452
453   lex_force_match (lexer, T_BY);
454
455   oneway.indep_var = parse_variable_const (lexer, dict);
456   if (oneway.indep_var == NULL)
457     goto error;
458
459   while (lex_token (lexer) != T_ENDCMD)
460     {
461       lex_match (lexer, T_SLASH);
462
463       if (lex_match_id (lexer, "STATISTICS"))
464         {
465           lex_match (lexer, T_EQUALS);
466           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
467             {
468               if (lex_match_id (lexer, "DESCRIPTIVES"))
469                 {
470                   oneway.stats |= STATS_DESCRIPTIVES;
471                 }
472               else if (lex_match_id (lexer, "HOMOGENEITY"))
473                 {
474                   oneway.stats |= STATS_HOMOGENEITY;
475                 }
476               else
477                 {
478                   lex_error (lexer, NULL);
479                   goto error;
480                 }
481             }
482         }
483       else if (lex_match_id (lexer, "POSTHOC"))
484         {
485           lex_match (lexer, T_EQUALS);
486           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
487             {
488               int p;
489               bool method = false;
490               for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
491                 {
492                   if (lex_match_id (lexer, ph_tests[p].syntax))
493                     {
494                       oneway.n_posthoc++;
495                       oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
496                       oneway.posthoc[oneway.n_posthoc - 1] = p;
497                       method = true;
498                       break;
499                     }
500                 }
501               if ( method == false)
502                 {
503                   if (lex_match_id (lexer, "ALPHA"))
504                     {
505                       if ( !lex_force_match (lexer, T_LPAREN))
506                         goto error;
507                       lex_force_num (lexer);
508                       oneway.alpha = lex_number (lexer);
509                       lex_get (lexer);
510                       if ( !lex_force_match (lexer, T_RPAREN))
511                         goto error;
512                     }
513                   else
514                     {
515                       msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
516                       lex_error (lexer, NULL);
517                       goto error;
518                     }
519                 }
520             }
521         }
522       else if (lex_match_id (lexer, "CONTRAST"))
523         {
524           struct contrasts_node *cl = xzalloc (sizeof *cl);
525
526           struct ll_list *coefficient_list = &cl->coefficient_list;
527           lex_match (lexer, T_EQUALS);
528
529           ll_init (coefficient_list);
530
531           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
532             {
533               if ( lex_is_number (lexer))
534                 {
535                   struct coeff_node *cc = xmalloc (sizeof *cc);
536                   cc->coeff = lex_number (lexer);
537
538                   ll_push_tail (coefficient_list, &cc->ll);
539                   lex_get (lexer);
540                 }
541               else
542                 {
543                   destroy_coeff_list (cl);
544                   lex_error (lexer, NULL);
545                   goto error;
546                 }
547             }
548           
549           if ( ll_count (coefficient_list) <= 0)
550             goto error;
551
552           ll_push_tail (&oneway.contrast_list, &cl->ll);
553         }
554       else if (lex_match_id (lexer, "MISSING"))
555         {
556           lex_match (lexer, T_EQUALS);
557           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
558             {
559               if (lex_match_id (lexer, "INCLUDE"))
560                 {
561                   oneway.exclude = MV_SYSTEM;
562                 }
563               else if (lex_match_id (lexer, "EXCLUDE"))
564                 {
565                   oneway.exclude = MV_ANY;
566                 }
567               else if (lex_match_id (lexer, "LISTWISE"))
568                 {
569                   oneway.missing_type = MISS_LISTWISE;
570                 }
571               else if (lex_match_id (lexer, "ANALYSIS"))
572                 {
573                   oneway.missing_type = MISS_ANALYSIS;
574                 }
575               else
576                 {
577                   lex_error (lexer, NULL);
578                   goto error;
579                 }
580             }
581         }
582       else
583         {
584           lex_error (lexer, NULL);
585           goto error;
586         }
587     }
588
589
590   {
591     struct casegrouper *grouper;
592     struct casereader *group;
593     bool ok;
594
595     grouper = casegrouper_create_splits (proc_open (ds), dict);
596     while (casegrouper_get_next_group (grouper, &group))
597       run_oneway (&oneway, group, ds);
598     ok = casegrouper_destroy (grouper);
599     ok = proc_commit (ds) && ok;
600   }
601
602   oneway_cleanup (&oneway);
603   free (oneway.vars);
604   return CMD_SUCCESS;
605
606  error:
607   oneway_cleanup (&oneway);
608   free (oneway.vars);
609   return CMD_FAILURE;
610 }
611
612
613 \f
614
615
616 static struct descriptive_data *
617 dd_create (const struct variable *var)
618 {
619   struct descriptive_data *dd = xmalloc (sizeof *dd);
620
621   dd->mom = moments1_create (MOMENT_VARIANCE);
622   dd->minimum = DBL_MAX;
623   dd->maximum = -DBL_MAX;
624   dd->var = var;
625
626   return dd;
627 }
628
629 static void
630 dd_destroy (struct descriptive_data *dd)
631 {
632   moments1_destroy (dd->mom);
633   free (dd);
634 }
635
636 static void *
637 makeit (const void *aux1, void *aux2 UNUSED)
638 {
639   const struct variable *var = aux1;
640
641   struct descriptive_data *dd = dd_create (var);
642
643   return dd;
644 }
645
646 static void 
647 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
648 {
649   struct descriptive_data *dd = user_data;
650
651   dd_destroy (dd);
652 }
653
654
655 static void 
656 updateit (const void *aux1, void *aux2, void *user_data,
657           const struct ccase *c, double weight)
658 {
659   struct descriptive_data *dd = user_data;
660
661   const struct variable *varp = aux1;
662
663   const union value *valx = case_data (c, varp);
664
665   struct descriptive_data *dd_total = aux2;
666
667   moments1_add (dd->mom, valx->f, weight);
668   if (valx->f < dd->minimum)
669     dd->minimum = valx->f;
670
671   if (valx->f > dd->maximum)
672     dd->maximum = valx->f;
673
674   {
675     const struct variable *var = dd_total->var;
676     const union value *val = case_data (c, var);
677
678     moments1_add (dd_total->mom,
679                   val->f,
680                   weight);
681
682     if (val->f < dd_total->minimum)
683       dd_total->minimum = val->f;
684
685     if (val->f > dd_total->maximum)
686       dd_total->maximum = val->f;
687   }
688 }
689
690 static void
691 run_oneway (const struct oneway_spec *cmd,
692             struct casereader *input,
693             const struct dataset *ds)
694 {
695   int v;
696   struct taint *taint;
697   struct dictionary *dict = dataset_dict (ds);
698   struct casereader *reader;
699   struct ccase *c;
700
701   struct oneway_workspace ws;
702
703   ws.actual_number_of_groups = 0;
704   ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
705   ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
706
707   for (v = 0 ; v < cmd->n_vars; ++v)
708     ws.dd_total[v] = dd_create (cmd->vars[v]);
709
710   for (v = 0; v < cmd->n_vars; ++v)
711     {
712       struct payload payload;
713       payload.create = makeit;
714       payload.update = updateit;
715       payload.calculate = NULL;
716       payload.destroy = killit;
717
718       ws.vws[v].iact = interaction_create (cmd->indep_var);
719       ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
720                                            cmd->exclude, cmd->exclude);
721
722       categoricals_set_payload (ws.vws[v].cat, &payload, 
723                                 CONST_CAST (struct variable *, cmd->vars[v]),
724                                 ws.dd_total[v]);
725
726
727       ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
728                                                ws.vws[v].cat, 
729                                                cmd->wv, cmd->exclude);
730       ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
731     }
732
733   c = casereader_peek (input, 0);
734   if (c == NULL)
735     {
736       casereader_destroy (input);
737       goto finish;
738     }
739   output_split_file_values (ds, c);
740   case_unref (c);
741
742   taint = taint_clone (casereader_get_taint (input));
743
744   input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
745                                             cmd->exclude, NULL, NULL);
746   if (cmd->missing_type == MISS_LISTWISE)
747     input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
748                                               cmd->exclude, NULL, NULL);
749   input = casereader_create_filter_weight (input, dict, NULL, NULL);
750
751   reader = casereader_clone (input);
752   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
753     {
754       int i;
755       double w = dict_get_case_weight (dict, c, NULL);
756
757       for (i = 0; i < cmd->n_vars; ++i)
758         {
759           struct per_var_ws *pvw = &ws.vws[i];
760           const struct variable *v = cmd->vars[i];
761           const union value *val = case_data (c, v);
762
763           if ( MISS_ANALYSIS == cmd->missing_type)
764             {
765               if ( var_is_value_missing (v, val, cmd->exclude))
766                 continue;
767             }
768
769           covariance_accumulate_pass1 (pvw->cov, c);
770           levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
771         }
772     }
773   casereader_destroy (reader);
774
775   reader = casereader_clone (input);
776   for ( ; (c = casereader_read (reader) ); case_unref (c))
777     {
778       int i;
779       double w = dict_get_case_weight (dict, c, NULL);
780       for (i = 0; i < cmd->n_vars; ++i)
781         {
782           struct per_var_ws *pvw = &ws.vws[i];
783           const struct variable *v = cmd->vars[i];
784           const union value *val = case_data (c, v);
785
786           if ( MISS_ANALYSIS == cmd->missing_type)
787             {
788               if ( var_is_value_missing (v, val, cmd->exclude))
789                 continue;
790             }
791
792           covariance_accumulate_pass2 (pvw->cov, c);
793           levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
794         }
795     }
796   casereader_destroy (reader);
797
798   reader = casereader_clone (input);
799   for ( ; (c = casereader_read (reader) ); case_unref (c))
800     {
801       int i;
802       double w = dict_get_case_weight (dict, c, NULL);
803
804       for (i = 0; i < cmd->n_vars; ++i)
805         {
806           struct per_var_ws *pvw = &ws.vws[i];
807           const struct variable *v = cmd->vars[i];
808           const union value *val = case_data (c, v);
809
810           if ( MISS_ANALYSIS == cmd->missing_type)
811             {
812               if ( var_is_value_missing (v, val, cmd->exclude))
813                 continue;
814             }
815
816           levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
817         }
818     }
819   casereader_destroy (reader);
820
821
822   for (v = 0; v < cmd->n_vars; ++v)
823     {
824       const gsl_matrix *ucm;
825       gsl_matrix *cm;
826       struct per_var_ws *pvw = &ws.vws[v];
827       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
828       const bool ok = categoricals_sane (cats);
829
830       if ( ! ok)
831         {
832           msg (MW, 
833                _("Dependent variable %s has no non-missing values.  No analysis for this variable will be done."),
834                var_get_name (cmd->vars[v]));
835           continue;
836         }
837
838       ucm = covariance_calculate_unnormalized (pvw->cov);
839
840       cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
841       gsl_matrix_memcpy (cm, ucm);
842
843       moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
844
845       pvw->sst = gsl_matrix_get (cm, 0, 0);
846
847       reg_sweep (cm, 0);
848
849       pvw->sse = gsl_matrix_get (cm, 0, 0);
850       gsl_matrix_free (cm);
851
852       pvw->ssa = pvw->sst - pvw->sse;
853
854       pvw->n_groups = categoricals_n_total (cats);
855
856       pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
857     }
858
859   for (v = 0; v < cmd->n_vars; ++v)
860     {
861       const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
862
863       if ( ! categoricals_is_complete (cats))
864         {
865           continue;
866         }
867
868       if (categoricals_n_total (cats) > ws.actual_number_of_groups)
869         ws.actual_number_of_groups = categoricals_n_total (cats);
870     }
871
872   casereader_destroy (input);
873
874   if (!taint_has_tainted_successor (taint))
875     output_oneway (cmd, &ws);
876
877   taint_destroy (taint);
878
879  finish:
880
881   for (v = 0; v < cmd->n_vars; ++v)
882     {
883       covariance_destroy (ws.vws[v].cov);
884       levene_destroy (ws.vws[v].nl);
885       dd_destroy (ws.dd_total[v]);
886       interaction_destroy (ws.vws[v].iact);
887     }
888
889   free (ws.vws);
890   free (ws.dd_total);
891 }
892
893 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
894 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
895 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
896
897 static void
898 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
899 {
900   size_t i = 0;
901
902   /* Check the sanity of the given contrast values */
903   struct contrasts_node *coeff_list  = NULL;
904   struct contrasts_node *coeff_next  = NULL;
905   ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
906     {
907       struct coeff_node *cn = NULL;
908       double sum = 0;
909       struct ll_list *cl = &coeff_list->coefficient_list;
910       ++i;
911
912       if (ll_count (cl) != ws->actual_number_of_groups)
913         {
914           msg (SW,
915                _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
916                i, ll_count (cl), ws->actual_number_of_groups);
917
918           ll_remove (&coeff_list->ll);
919           destroy_coeff_list (coeff_list);
920           continue;
921         }
922
923       ll_for_each (cn, struct coeff_node, ll, cl)
924         sum += cn->coeff;
925
926       if ( sum != 0.0 )
927         msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
928     }
929
930   if (cmd->stats & STATS_DESCRIPTIVES)
931     show_descriptives (cmd, ws);
932
933   if (cmd->stats & STATS_HOMOGENEITY)
934     show_homogeneity (cmd, ws);
935
936   show_anova_table (cmd, ws);
937
938   if (ll_count (&cmd->contrast_list) > 0)
939     {
940       show_contrast_coeffs (cmd, ws);
941       show_contrast_tests (cmd, ws);
942     }
943
944   if ( cmd->posthoc )
945     {
946       int v;
947       for (v = 0 ; v < cmd->n_vars; ++v)
948         {
949           const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
950
951           if ( categoricals_is_complete (cats))
952             show_comparisons (cmd, ws, v);
953         }
954     }
955 }
956
957
958 /* Show the ANOVA table */
959 static void
960 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
961 {
962   size_t i;
963   int n_cols =7;
964   size_t n_rows = cmd->n_vars * 3 + 1;
965
966   struct tab_table *t = tab_create (n_cols, n_rows);
967
968   tab_headers (t, 2, 0, 1, 0);
969
970   tab_box (t,
971            TAL_2, TAL_2,
972            -1, TAL_1,
973            0, 0,
974            n_cols - 1, n_rows - 1);
975
976   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
977   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
978   tab_vline (t, TAL_0, 1, 0, 0);
979
980   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
981   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
982   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
983   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
984   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
985
986
987   for (i = 0; i < cmd->n_vars; ++i)
988     {
989       double n;
990       double df1, df2;
991       double msa;
992       const char *s = var_to_string (cmd->vars[i]);
993       const struct per_var_ws *pvw = &ws->vws[i];
994
995       moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
996
997       df1 = pvw->n_groups - 1;
998       df2 = n - pvw->n_groups;
999       msa = pvw->ssa / df1;
1000
1001       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
1002       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
1003       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
1004       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
1005
1006       if (i > 0)
1007         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
1008
1009
1010       /* Sums of Squares */
1011       tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL, RC_OTHER);
1012       tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL, RC_OTHER);
1013       tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL, RC_OTHER);
1014
1015
1016       /* Degrees of freedom */
1017       tab_double (t, 3, i * 3 + 1, 0, df1, NULL, RC_INTEGER);
1018       tab_double (t, 3, i * 3 + 2, 0, df2,  NULL, RC_INTEGER); 
1019       tab_double (t, 3, i * 3 + 3, 0, n - 1, NULL, RC_INTEGER); 
1020
1021       /* Mean Squares */
1022       tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL, RC_OTHER);
1023       tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL, RC_OTHER);
1024
1025       {
1026         const double F = msa / pvw->mse ;
1027
1028         /* The F value */
1029         tab_double (t, 5, i * 3 + 1, 0,  F, NULL, RC_OTHER);
1030
1031         /* The significance */
1032         tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL, RC_PVALUE);
1033       }
1034     }
1035
1036   tab_title (t, _("ANOVA"));
1037   tab_submit (t);
1038 }
1039
1040
1041 /* Show the descriptives table */
1042 static void
1043 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1044 {
1045   size_t v;
1046   int n_cols = 10;
1047   struct tab_table *t;
1048   int row;
1049
1050   const double confidence = 0.95;
1051   const double q = (1.0 - confidence) / 2.0;
1052
1053   const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
1054
1055   int n_rows = 2;
1056
1057   for (v = 0; v < cmd->n_vars; ++v)
1058     n_rows += ws->actual_number_of_groups + 1;
1059
1060   t = tab_create (n_cols, n_rows);
1061   tab_set_format (t, RC_WEIGHT, wfmt);
1062   tab_headers (t, 2, 0, 2, 0);
1063
1064   /* Put a frame around the entire box, and vertical lines inside */
1065   tab_box (t,
1066            TAL_2, TAL_2,
1067            -1, TAL_1,
1068            0, 0,
1069            n_cols - 1, n_rows - 1);
1070
1071   /* Underline headers */
1072   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1073   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1074
1075   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
1076   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
1077   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1078   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1079
1080
1081   tab_vline (t, TAL_0, 7, 0, 0);
1082   tab_hline (t, TAL_1, 6, 7, 1);
1083   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1084                          _("%g%% Confidence Interval for Mean"),
1085                          confidence*100.0);
1086
1087   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1088   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1089
1090   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
1091   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
1092
1093   tab_title (t, _("Descriptives"));
1094
1095   row = 2;
1096   for (v = 0; v < cmd->n_vars; ++v)
1097     {
1098       const char *s = var_to_string (cmd->vars[v]);
1099       const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
1100
1101       int count = 0;
1102
1103       struct per_var_ws *pvw = &ws->vws[v];
1104       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1105
1106       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
1107       if ( v > 0)
1108         tab_hline (t, TAL_1, 0, n_cols - 1, row);
1109
1110       for (count = 0; count < categoricals_n_total (cats); ++count)
1111         {
1112           double T;
1113           double n, mean, variance;
1114           double std_dev, std_error ;
1115
1116           struct string vstr;
1117
1118           const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1119           const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
1120
1121           moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1122
1123           std_dev = sqrt (variance);
1124           std_error = std_dev / sqrt (n) ;
1125
1126           ds_init_empty (&vstr);
1127
1128           var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1129
1130           tab_text (t, 1, row + count,
1131                     TAB_LEFT | TAT_TITLE,
1132                     ds_cstr (&vstr));
1133
1134           ds_destroy (&vstr);
1135
1136           /* Now fill in the numbers ... */
1137
1138           tab_double (t, 2, row + count, 0, n, NULL, RC_WEIGHT);
1139
1140           tab_double (t, 3, row + count, 0, mean, NULL, RC_OTHER);
1141
1142           tab_double (t, 4, row + count, 0, std_dev, NULL, RC_OTHER);
1143
1144
1145           tab_double (t, 5, row + count, 0, std_error, NULL, RC_OTHER);
1146
1147           /* Now the confidence interval */
1148
1149           T = gsl_cdf_tdist_Qinv (q, n - 1);
1150
1151           tab_double (t, 6, row + count, 0,
1152                       mean - T * std_error, NULL, RC_OTHER);
1153
1154           tab_double (t, 7, row + count, 0,
1155                       mean + T * std_error, NULL, RC_OTHER);
1156
1157           /* Min and Max */
1158
1159           tab_double (t, 8, row + count, 0,  dd->minimum, fmt, RC_OTHER);
1160           tab_double (t, 9, row + count, 0,  dd->maximum, fmt, RC_OTHER);
1161         }
1162
1163       if (categoricals_is_complete (cats))
1164       {
1165         double T;
1166         double n, mean, variance;
1167         double std_dev;
1168         double std_error;
1169
1170         moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
1171
1172         std_dev = sqrt (variance);
1173         std_error = std_dev / sqrt (n) ;
1174
1175         tab_text (t, 1, row + count,
1176                   TAB_LEFT | TAT_TITLE, _("Total"));
1177
1178         tab_double (t, 2, row + count, 0, n, NULL, RC_WEIGHT);
1179
1180         tab_double (t, 3, row + count, 0, mean, NULL, RC_OTHER);
1181
1182         tab_double (t, 4, row + count, 0, std_dev, NULL, RC_OTHER);
1183
1184         tab_double (t, 5, row + count, 0, std_error, NULL, RC_OTHER);
1185
1186         /* Now the confidence interval */
1187         T = gsl_cdf_tdist_Qinv (q, n - 1);
1188
1189         tab_double (t, 6, row + count, 0,
1190                     mean - T * std_error, NULL, RC_OTHER);
1191
1192         tab_double (t, 7, row + count, 0,
1193                     mean + T * std_error, NULL, RC_OTHER);
1194
1195
1196         /* Min and Max */
1197         tab_double (t, 8, row + count, 0,  ws->dd_total[v]->minimum, fmt, RC_OTHER);
1198         tab_double (t, 9, row + count, 0,  ws->dd_total[v]->maximum, fmt, RC_OTHER);
1199       }
1200
1201       row += categoricals_n_total (cats) + 1;
1202     }
1203
1204   tab_submit (t);
1205 }
1206
1207 /* Show the homogeneity table */
1208 static void
1209 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1210 {
1211   size_t v;
1212   int n_cols = 5;
1213   size_t n_rows = cmd->n_vars + 1;
1214
1215   struct tab_table *t = tab_create (n_cols, n_rows);
1216   tab_headers (t, 1, 0, 1, 0);
1217
1218   /* Put a frame around the entire box, and vertical lines inside */
1219   tab_box (t,
1220            TAL_2, TAL_2,
1221            -1, TAL_1,
1222            0, 0,
1223            n_cols - 1, n_rows - 1);
1224
1225
1226   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1227   tab_vline (t, TAL_2, 1, 0, n_rows - 1);
1228
1229   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
1230   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1231   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1232   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
1233
1234   tab_title (t, _("Test of Homogeneity of Variances"));
1235
1236   for (v = 0; v < cmd->n_vars; ++v)
1237     {
1238       double n;
1239       const struct per_var_ws *pvw = &ws->vws[v];
1240       double F = levene_calculate (pvw->nl);
1241
1242       const struct variable *var = cmd->vars[v];
1243       const char *s = var_to_string (var);
1244       double df1, df2;
1245
1246       moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1247
1248       df1 = pvw->n_groups - 1;
1249       df2 = n - pvw->n_groups;
1250
1251       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1252
1253       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL, RC_OTHER);
1254       tab_double (t, 2, v + 1, TAB_RIGHT, df1, NULL, RC_INTEGER);
1255       tab_double (t, 3, v + 1, TAB_RIGHT, df2, NULL, RC_INTEGER);
1256
1257       /* Now the significance */
1258       tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL, RC_PVALUE);
1259     }
1260
1261   tab_submit (t);
1262 }
1263
1264
1265 /* Show the contrast coefficients table */
1266 static void
1267 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1268 {
1269   int c_num = 0;
1270   struct ll *cli;
1271
1272   int n_contrasts = ll_count (&cmd->contrast_list);
1273   int n_cols = 2 + ws->actual_number_of_groups;
1274   int n_rows = 2 + n_contrasts;
1275
1276   struct tab_table *t;
1277
1278   const struct covariance *cov = ws->vws[0].cov ;
1279
1280   t = tab_create (n_cols, n_rows);
1281   tab_headers (t, 2, 0, 2, 0);
1282
1283   /* Put a frame around the entire box, and vertical lines inside */
1284   tab_box (t,
1285            TAL_2, TAL_2,
1286            -1, TAL_1,
1287            0, 0,
1288            n_cols - 1, n_rows - 1);
1289
1290   tab_box (t,
1291            -1, -1,
1292            TAL_0, TAL_0,
1293            2, 0,
1294            n_cols - 1, 0);
1295
1296   tab_box (t,
1297            -1, -1,
1298            TAL_0, TAL_0,
1299            0, 0,
1300            1, 1);
1301
1302   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1303   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1304
1305   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1306
1307   tab_title (t, _("Contrast Coefficients"));
1308
1309   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1310
1311
1312   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1313                   var_to_string (cmd->indep_var));
1314
1315   for ( cli = ll_head (&cmd->contrast_list);
1316         cli != ll_null (&cmd->contrast_list);
1317         cli = ll_next (cli))
1318     {
1319       int count = 0;
1320       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1321       struct ll *coeffi ;
1322
1323       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1324
1325       for (coeffi = ll_head (&cn->coefficient_list);
1326            coeffi != ll_null (&cn->coefficient_list);
1327            ++count, coeffi = ll_next (coeffi))
1328         {
1329           const struct categoricals *cats = covariance_get_categoricals (cov);
1330           const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1331           struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1332           struct string vstr;
1333
1334           ds_init_empty (&vstr);
1335
1336           var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1337
1338           tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1339
1340           ds_destroy (&vstr);
1341
1342           tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%.*g",
1343                            DBL_DIG + 1, coeffn->coeff);
1344         }
1345       ++c_num;
1346     }
1347
1348   tab_submit (t);
1349 }
1350
1351
1352 /* Show the results of the contrast tests */
1353 static void
1354 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1355 {
1356   int n_contrasts = ll_count (&cmd->contrast_list);
1357   size_t v;
1358   int n_cols = 8;
1359   size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1360
1361   struct tab_table *t;
1362
1363   t = tab_create (n_cols, n_rows);
1364   tab_headers (t, 3, 0, 1, 0);
1365
1366   /* Put a frame around the entire box, and vertical lines inside */
1367   tab_box (t,
1368            TAL_2, TAL_2,
1369            -1, TAL_1,
1370            0, 0,
1371            n_cols - 1, n_rows - 1);
1372
1373   tab_box (t,
1374            -1, -1,
1375            TAL_0, TAL_0,
1376            0, 0,
1377            2, 0);
1378
1379   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1380   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1381
1382   tab_title (t, _("Contrast Tests"));
1383
1384   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1385   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1386   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1387   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1388   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1389   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1390
1391   for (v = 0; v < cmd->n_vars; ++v)
1392     {
1393       const struct per_var_ws *pvw = &ws->vws[v];
1394       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1395       if (!categoricals_is_complete (cats))
1396         continue;
1397       struct ll *cli;
1398       int i = 0;
1399       int lines_per_variable = 2 * n_contrasts;
1400
1401       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1402                 var_to_string (cmd->vars[v]));
1403
1404       for ( cli = ll_head (&cmd->contrast_list);
1405             cli != ll_null (&cmd->contrast_list);
1406             ++i, cli = ll_next (cli))
1407         {
1408           struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1409           struct ll *coeffi ;
1410           int ci = 0;
1411           double contrast_value = 0.0;
1412           double coef_msq = 0.0;
1413
1414           double T;
1415           double std_error_contrast;
1416           double df;
1417           double sec_vneq = 0.0;
1418
1419           /* Note: The calculation of the degrees of freedom in the
1420              "variances not equal" case is painfull!!
1421              The following formula may help to understand it:
1422              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1423              {
1424              \sum_{i=1}^k\left (
1425              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1426              \right)
1427              }
1428           */
1429
1430           double df_denominator = 0.0;
1431           double df_numerator = 0.0;
1432
1433           double grand_n;
1434           moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1435           df = grand_n - pvw->n_groups;
1436
1437           if ( i == 0 )
1438             {
1439               tab_text (t,  1, (v * lines_per_variable) + i + 1,
1440                         TAB_LEFT | TAT_TITLE,
1441                         _("Assume equal variances"));
1442
1443               tab_text (t,  1, (v * lines_per_variable) + i + 1 + n_contrasts,
1444                         TAB_LEFT | TAT_TITLE,
1445                         _("Does not assume equal"));
1446             }
1447
1448           tab_text_format (t,  2, (v * lines_per_variable) + i + 1,
1449                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1450
1451
1452           tab_text_format (t,  2,
1453                            (v * lines_per_variable) + i + 1 + n_contrasts,
1454                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1455
1456           for (coeffi = ll_head (&cn->coefficient_list);
1457                coeffi != ll_null (&cn->coefficient_list);
1458                ++ci, coeffi = ll_next (coeffi))
1459             {
1460               double n, mean, variance;
1461               const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
1462               struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1463               const double coef = cn->coeff; 
1464               double winv ;
1465
1466               moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1467
1468               winv = variance / n;
1469
1470               contrast_value += coef * mean;
1471
1472               coef_msq += (pow2 (coef)) / n;
1473
1474               sec_vneq += (pow2 (coef)) * variance / n;
1475
1476               df_numerator += (pow2 (coef)) * winv;
1477               df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1478             }
1479
1480           sec_vneq = sqrt (sec_vneq);
1481
1482           df_numerator = pow2 (df_numerator);
1483
1484           tab_double (t,  3, (v * lines_per_variable) + i + 1,
1485                       TAB_RIGHT, contrast_value, NULL, RC_OTHER);
1486
1487           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
1488                       n_contrasts,
1489                       TAB_RIGHT, contrast_value, NULL, RC_OTHER);
1490
1491           std_error_contrast = sqrt (pvw->mse * coef_msq);
1492
1493           /* Std. Error */
1494           tab_double (t,  4, (v * lines_per_variable) + i + 1,
1495                       TAB_RIGHT, std_error_contrast,
1496                       NULL, RC_OTHER);
1497
1498           T = fabs (contrast_value / std_error_contrast);
1499
1500           /* T Statistic */
1501
1502           tab_double (t,  5, (v * lines_per_variable) + i + 1,
1503                       TAB_RIGHT, T,
1504                       NULL, RC_OTHER);
1505
1506
1507           /* Degrees of Freedom */
1508           tab_double (t,  6, (v * lines_per_variable) + i + 1,
1509                      TAB_RIGHT,  df, NULL, RC_INTEGER);
1510
1511
1512           /* Significance TWO TAILED !!*/
1513           tab_double (t,  7, (v * lines_per_variable) + i + 1,
1514                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
1515                       NULL, RC_PVALUE);
1516
1517           /* Now for the Variances NOT Equal case */
1518
1519           /* Std. Error */
1520           tab_double (t,  4,
1521                       (v * lines_per_variable) + i + 1 + n_contrasts,
1522                       TAB_RIGHT, sec_vneq,
1523                       NULL, RC_OTHER);
1524
1525           T = contrast_value / sec_vneq;
1526           tab_double (t,  5,
1527                       (v * lines_per_variable) + i + 1 + n_contrasts,
1528                       TAB_RIGHT, T,
1529                       NULL, RC_OTHER);
1530
1531           df = df_numerator / df_denominator;
1532
1533           tab_double (t,  6,
1534                       (v * lines_per_variable) + i + 1 + n_contrasts,
1535                       TAB_RIGHT, df,
1536                       NULL, RC_OTHER);
1537
1538           {
1539             double p = gsl_cdf_tdist_P (T, df);
1540             double q = gsl_cdf_tdist_Q (T, df);
1541
1542             /* The Significance */
1543             tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1544                         TAB_RIGHT,  2 * ((T > 0) ? q : p),
1545                         NULL, RC_PVALUE);
1546           }
1547         }
1548
1549       if ( v > 0 )
1550         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1551     }
1552
1553   tab_submit (t);
1554 }
1555
1556
1557
1558 static void
1559 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1560 {
1561   const int n_cols = 8;
1562   const int heading_rows = 2;
1563   const int heading_cols = 3;
1564
1565   int p;
1566   int r = heading_rows ;
1567
1568   const struct per_var_ws *pvw = &ws->vws[v];
1569   const struct categoricals *cat = pvw->cat;
1570   const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
1571
1572   struct tab_table *t = tab_create (n_cols, n_rows);
1573
1574   tab_headers (t, heading_cols, 0, heading_rows, 0);
1575
1576   /* Put a frame around the entire box, and vertical lines inside */
1577   tab_box (t,
1578            TAL_2, TAL_2,
1579            -1, -1,
1580            0, 0,
1581            n_cols - 1, n_rows - 1);
1582
1583   tab_box (t,
1584            -1, -1,
1585            -1, TAL_1,
1586            heading_cols, 0,
1587            n_cols - 1, n_rows - 1);
1588
1589   tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
1590
1591   tab_title (t, _("Multiple Comparisons (%s)"), var_to_string (cmd->vars[v]));
1592
1593   tab_text_format (t,  1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
1594   tab_text_format (t,  2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
1595   tab_text (t,  3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1596   tab_text (t,  3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
1597   tab_text (t,  4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1598   tab_text (t,  5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
1599
1600   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1601                          _("%g%% Confidence Interval"),
1602                          (1 - cmd->alpha) * 100.0);
1603
1604   tab_text (t,  6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1605   tab_text (t,  7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1606
1607
1608   for (p = 0; p < cmd->n_posthoc; ++p)
1609     {
1610       int i;
1611       const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1612
1613       tab_hline (t, TAL_2, 0, n_cols - 1, r);
1614
1615       tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
1616
1617       for (i = 0; i < pvw->n_groups ; ++i)
1618         {
1619           double weight_i, mean_i, var_i;
1620           int rx = 0;
1621           struct string vstr;
1622           int j;
1623           struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
1624           const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1625           
1626
1627           ds_init_empty (&vstr);
1628           var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1629
1630           if ( i != 0)
1631             tab_hline (t, TAL_1, 1, n_cols - 1, r);
1632           tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1633
1634           moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1635
1636           for (j = 0 ; j < pvw->n_groups; ++j)
1637             {
1638               double std_err;
1639               double weight_j, mean_j, var_j;
1640               double half_range;
1641               const struct ccase *cc;
1642               struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
1643               if (j == i)
1644                 continue;
1645
1646               ds_clear (&vstr);
1647               cc = categoricals_get_case_by_category (cat, j);
1648               var_append_value_name (cmd->indep_var, case_data (cc, cmd->indep_var), &vstr);
1649               tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1650
1651               moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1652
1653               tab_double  (t, 3, r + rx, 0, mean_i - mean_j, NULL, RC_OTHER);
1654
1655               std_err = pvw->mse;
1656               std_err *= weight_i + weight_j;
1657               std_err /= weight_i * weight_j;
1658               std_err = sqrt (std_err);
1659
1660               tab_double  (t, 4, r + rx, 0, std_err, NULL, RC_OTHER);
1661           
1662               tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), NULL, RC_PVALUE);
1663
1664               half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
1665
1666               tab_double (t, 6, r + rx, 0,
1667                           (mean_i - mean_j) - half_range, NULL, RC_OTHER);
1668
1669               tab_double (t, 7, r + rx, 0,
1670                           (mean_i - mean_j) + half_range, NULL, RC_OTHER);
1671
1672               rx++;
1673             }
1674           ds_destroy (&vstr);
1675           r += pvw->n_groups - 1;
1676         }
1677     }
1678
1679   tab_submit (t);
1680 }