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