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