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