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