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