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