output: Introduce pivot tables.
[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             goto error;
555
556           ll_push_tail (&oneway.contrast_list, &cl->ll);
557         }
558       else if (lex_match_id (lexer, "MISSING"))
559         {
560           lex_match (lexer, T_EQUALS);
561           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
562             {
563               if (lex_match_id (lexer, "INCLUDE"))
564                 {
565                   oneway.exclude = MV_SYSTEM;
566                 }
567               else if (lex_match_id (lexer, "EXCLUDE"))
568                 {
569                   oneway.exclude = MV_ANY;
570                 }
571               else if (lex_match_id (lexer, "LISTWISE"))
572                 {
573                   oneway.missing_type = MISS_LISTWISE;
574                 }
575               else if (lex_match_id (lexer, "ANALYSIS"))
576                 {
577                   oneway.missing_type = MISS_ANALYSIS;
578                 }
579               else
580                 {
581                   lex_error (lexer, NULL);
582                   goto error;
583                 }
584             }
585         }
586       else
587         {
588           lex_error (lexer, NULL);
589           goto error;
590         }
591     }
592
593
594   {
595     struct casegrouper *grouper;
596     struct casereader *group;
597     bool ok;
598
599     grouper = casegrouper_create_splits (proc_open (ds), dict);
600     while (casegrouper_get_next_group (grouper, &group))
601       run_oneway (&oneway, group, ds);
602     ok = casegrouper_destroy (grouper);
603     ok = proc_commit (ds) && ok;
604   }
605
606   oneway_cleanup (&oneway);
607   free (oneway.vars);
608   return CMD_SUCCESS;
609
610  error:
611   oneway_cleanup (&oneway);
612   free (oneway.vars);
613   return CMD_FAILURE;
614 }
615
616
617 \f
618
619
620 static struct descriptive_data *
621 dd_create (const struct variable *var)
622 {
623   struct descriptive_data *dd = xmalloc (sizeof *dd);
624
625   dd->mom = moments1_create (MOMENT_VARIANCE);
626   dd->minimum = DBL_MAX;
627   dd->maximum = -DBL_MAX;
628   dd->var = var;
629
630   return dd;
631 }
632
633 static void
634 dd_destroy (struct descriptive_data *dd)
635 {
636   moments1_destroy (dd->mom);
637   free (dd);
638 }
639
640 static void *
641 makeit (const void *aux1, void *aux2 UNUSED)
642 {
643   const struct variable *var = aux1;
644
645   struct descriptive_data *dd = dd_create (var);
646
647   return dd;
648 }
649
650 static void
651 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
652 {
653   struct descriptive_data *dd = user_data;
654
655   dd_destroy (dd);
656 }
657
658
659 static void
660 updateit (const void *aux1, void *aux2, void *user_data,
661           const struct ccase *c, double weight)
662 {
663   struct descriptive_data *dd = user_data;
664
665   const struct variable *varp = aux1;
666
667   const union value *valx = case_data (c, varp);
668
669   struct descriptive_data *dd_total = aux2;
670
671   moments1_add (dd->mom, valx->f, weight);
672   if (valx->f < dd->minimum)
673     dd->minimum = valx->f;
674
675   if (valx->f > dd->maximum)
676     dd->maximum = valx->f;
677
678   {
679     const struct variable *var = dd_total->var;
680     const union value *val = case_data (c, var);
681
682     moments1_add (dd_total->mom,
683                   val->f,
684                   weight);
685
686     if (val->f < dd_total->minimum)
687       dd_total->minimum = val->f;
688
689     if (val->f > dd_total->maximum)
690       dd_total->maximum = val->f;
691   }
692 }
693
694 static void
695 run_oneway (const struct oneway_spec *cmd,
696             struct casereader *input,
697             const struct dataset *ds)
698 {
699   int v;
700   struct taint *taint;
701   struct dictionary *dict = dataset_dict (ds);
702   struct casereader *reader;
703   struct ccase *c;
704
705   struct oneway_workspace ws;
706
707   ws.actual_number_of_groups = 0;
708   ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
709   ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
710
711   for (v = 0 ; v < cmd->n_vars; ++v)
712     ws.dd_total[v] = dd_create (cmd->vars[v]);
713
714   for (v = 0; v < cmd->n_vars; ++v)
715     {
716       static const struct payload payload =
717         {
718           .create = makeit,
719           .update = updateit,
720           .calculate = NULL,
721           .destroy = killit
722         };
723
724       ws.vws[v].iact = interaction_create (cmd->indep_var);
725       ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
726                                            cmd->exclude);
727
728       categoricals_set_payload (ws.vws[v].cat, &payload,
729                                 CONST_CAST (struct variable *, cmd->vars[v]),
730                                 ws.dd_total[v]);
731
732
733       ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
734                                                ws.vws[v].cat,
735                                                cmd->wv, cmd->exclude, true);
736       ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
737     }
738
739   c = casereader_peek (input, 0);
740   if (c == NULL)
741     {
742       casereader_destroy (input);
743       goto finish;
744     }
745   output_split_file_values (ds, c);
746   case_unref (c);
747
748   taint = taint_clone (casereader_get_taint (input));
749
750   input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
751                                             cmd->exclude, NULL, NULL);
752   if (cmd->missing_type == MISS_LISTWISE)
753     input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
754                                               cmd->exclude, NULL, NULL);
755   input = casereader_create_filter_weight (input, dict, NULL, NULL);
756
757   reader = casereader_clone (input);
758   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
759     {
760       int i;
761       double w = dict_get_case_weight (dict, c, NULL);
762
763       for (i = 0; i < cmd->n_vars; ++i)
764         {
765           struct per_var_ws *pvw = &ws.vws[i];
766           const struct variable *v = cmd->vars[i];
767           const union value *val = case_data (c, v);
768
769           if ( MISS_ANALYSIS == cmd->missing_type)
770             {
771               if ( var_is_value_missing (v, val, cmd->exclude))
772                 continue;
773             }
774
775           covariance_accumulate_pass1 (pvw->cov, c);
776           levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
777         }
778     }
779   casereader_destroy (reader);
780
781   reader = casereader_clone (input);
782   for ( ; (c = casereader_read (reader) ); case_unref (c))
783     {
784       int i;
785       double w = dict_get_case_weight (dict, c, NULL);
786       for (i = 0; i < cmd->n_vars; ++i)
787         {
788           struct per_var_ws *pvw = &ws.vws[i];
789           const struct variable *v = cmd->vars[i];
790           const union value *val = case_data (c, v);
791
792           if ( MISS_ANALYSIS == cmd->missing_type)
793             {
794               if ( var_is_value_missing (v, val, cmd->exclude))
795                 continue;
796             }
797
798           covariance_accumulate_pass2 (pvw->cov, c);
799           levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
800         }
801     }
802   casereader_destroy (reader);
803
804   reader = casereader_clone (input);
805   for ( ; (c = casereader_read (reader) ); case_unref (c))
806     {
807       int i;
808       double w = dict_get_case_weight (dict, c, NULL);
809
810       for (i = 0; i < cmd->n_vars; ++i)
811         {
812           struct per_var_ws *pvw = &ws.vws[i];
813           const struct variable *v = cmd->vars[i];
814           const union value *val = case_data (c, v);
815
816           if ( MISS_ANALYSIS == cmd->missing_type)
817             {
818               if ( var_is_value_missing (v, val, cmd->exclude))
819                 continue;
820             }
821
822           levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
823         }
824     }
825   casereader_destroy (reader);
826
827
828   for (v = 0; v < cmd->n_vars; ++v)
829     {
830       const gsl_matrix *ucm;
831       gsl_matrix *cm;
832       struct per_var_ws *pvw = &ws.vws[v];
833       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
834       const bool ok = categoricals_sane (cats);
835
836       if ( ! ok)
837         {
838           msg (MW,
839                _("Dependent variable %s has no non-missing values.  No analysis for this variable will be done."),
840                var_get_name (cmd->vars[v]));
841           continue;
842         }
843
844       ucm = covariance_calculate_unnormalized (pvw->cov);
845
846       cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
847       gsl_matrix_memcpy (cm, ucm);
848
849       moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
850
851       pvw->sst = gsl_matrix_get (cm, 0, 0);
852
853       reg_sweep (cm, 0);
854
855       pvw->sse = gsl_matrix_get (cm, 0, 0);
856       gsl_matrix_free (cm);
857
858       pvw->ssa = pvw->sst - pvw->sse;
859
860       pvw->n_groups = categoricals_n_total (cats);
861
862       pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
863     }
864
865   for (v = 0; v < cmd->n_vars; ++v)
866     {
867       const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
868
869       if ( ! categoricals_is_complete (cats))
870         {
871           continue;
872         }
873
874       if (categoricals_n_total (cats) > ws.actual_number_of_groups)
875         ws.actual_number_of_groups = categoricals_n_total (cats);
876     }
877
878   casereader_destroy (input);
879
880   if (!taint_has_tainted_successor (taint))
881     output_oneway (cmd, &ws);
882
883   taint_destroy (taint);
884
885  finish:
886
887   for (v = 0; v < cmd->n_vars; ++v)
888     {
889       covariance_destroy (ws.vws[v].cov);
890       levene_destroy (ws.vws[v].nl);
891       dd_destroy (ws.dd_total[v]);
892       interaction_destroy (ws.vws[v].iact);
893     }
894
895   free (ws.vws);
896   free (ws.dd_total);
897 }
898
899 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
900 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
901 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
902
903 static void
904 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
905 {
906   size_t i = 0;
907
908   /* Check the sanity of the given contrast values */
909   struct contrasts_node *coeff_list  = NULL;
910   struct contrasts_node *coeff_next  = NULL;
911   ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
912     {
913       struct coeff_node *cn = NULL;
914       double sum = 0;
915       struct ll_list *cl = &coeff_list->coefficient_list;
916       ++i;
917
918       if (ll_count (cl) != ws->actual_number_of_groups)
919         {
920           msg (SW,
921                _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
922                i, ll_count (cl), ws->actual_number_of_groups);
923
924           ll_remove (&coeff_list->ll);
925           destroy_coeff_list (coeff_list);
926           continue;
927         }
928
929       ll_for_each (cn, struct coeff_node, ll, cl)
930         sum += cn->coeff;
931
932       if ( sum != 0.0 )
933         msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
934     }
935
936   if (cmd->stats & STATS_DESCRIPTIVES)
937     show_descriptives (cmd, ws);
938
939   if (cmd->stats & STATS_HOMOGENEITY)
940     show_homogeneity (cmd, ws);
941
942   show_anova_table (cmd, ws);
943
944   if (ll_count (&cmd->contrast_list) > 0)
945     {
946       show_contrast_coeffs (cmd, ws);
947       show_contrast_tests (cmd, ws);
948     }
949
950   if ( cmd->posthoc )
951     {
952       int v;
953       for (v = 0 ; v < cmd->n_vars; ++v)
954         {
955           const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
956
957           if ( categoricals_is_complete (cats))
958             show_comparisons (cmd, ws, v);
959         }
960     }
961 }
962
963
964 /* Show the ANOVA table */
965 static void
966 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
967 {
968   struct pivot_table *table = pivot_table_create (N_("ANOVA"));
969
970   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
971                           N_("Sum of Squares"), PIVOT_RC_OTHER,
972                           N_("df"), PIVOT_RC_INTEGER,
973                           N_("Mean Square"), PIVOT_RC_OTHER,
974                           N_("F"), PIVOT_RC_OTHER,
975                           N_("Sig."), PIVOT_RC_SIGNIFICANCE);
976
977   pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
978                           N_("Between Groups"), N_("Within Groups"),
979                           N_("Total"));
980
981   struct pivot_dimension *variables = pivot_dimension_create (
982     table, PIVOT_AXIS_ROW, N_("Variables"));
983
984   for (size_t i = 0; i < cmd->n_vars; ++i)
985     {
986       int var_idx = pivot_category_create_leaf (
987         variables->root, pivot_value_new_variable (cmd->vars[i]));
988
989       const struct per_var_ws *pvw = &ws->vws[i];
990
991       double n;
992       moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
993
994       double df1 = pvw->n_groups - 1;
995       double df2 = n - pvw->n_groups;
996       double msa = pvw->ssa / df1;
997       double F = msa / pvw->mse ;
998
999       struct entry
1000         {
1001           int stat_idx;
1002           int type_idx;
1003           double x;
1004         }
1005       entries[] = {
1006         /* Sums of Squares. */
1007         { 0, 0, pvw->ssa },
1008         { 0, 1, pvw->sse },
1009         { 0, 2, pvw->sst },
1010         /* Degrees of Freedom. */
1011         { 1, 0, df1 },
1012         { 1, 1, df2 },
1013         { 1, 2, n - 1 },
1014         /* Mean Squares. */
1015         { 2, 0, msa },
1016         { 2, 1, pvw->mse },
1017         /* F. */
1018         { 3, 0, F },
1019         /* Significance. */
1020         { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
1021       };
1022       for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1023         {
1024           const struct entry *e = &entries[j];
1025           pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
1026                             pivot_value_new_number (e->x));
1027         }
1028     }
1029
1030   pivot_table_submit (table);
1031 }
1032
1033 /* Show the descriptives table */
1034 static void
1035 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1036 {
1037   if (!cmd->n_vars)
1038     return;
1039   const struct categoricals *cats = covariance_get_categoricals (
1040     ws->vws[0].cov);
1041
1042   struct pivot_table *table = pivot_table_create (N_("Descriptives"));
1043   pivot_table_set_weight_format (table, cmd->wfmt);
1044
1045   const double confidence = 0.95;
1046
1047   struct pivot_dimension *statistics = pivot_dimension_create (
1048     table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1049     N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
1050     N_("Std. Error"));
1051   struct pivot_category *interval = pivot_category_create_group__ (
1052     statistics->root,
1053     pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
1054                                  confidence * 100.0));
1055   pivot_category_create_leaves (interval, N_("Lower Bound"),
1056                                 N_("Upper Bound"));
1057   pivot_category_create_leaves (statistics->root,
1058                                 N_("Minimum"), N_("Maximum"));
1059
1060   struct pivot_dimension *indep_var = pivot_dimension_create__ (
1061     table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
1062   indep_var->root->show_label = true;
1063   size_t n;
1064   union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
1065   for (size_t j = 0; j < n; j++)
1066     pivot_category_create_leaf (
1067       indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1068   pivot_category_create_leaf (
1069     indep_var->root, pivot_value_new_text_format (N_("Total")));
1070
1071   struct pivot_dimension *dep_var = pivot_dimension_create (
1072     table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1073
1074   const double q = (1.0 - confidence) / 2.0;
1075   for (int v = 0; v < cmd->n_vars; ++v)
1076     {
1077       int dep_var_idx = pivot_category_create_leaf (
1078         dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1079
1080       struct per_var_ws *pvw = &ws->vws[v];
1081       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1082
1083       int count;
1084       for (count = 0; count < categoricals_n_total (cats); ++count)
1085         {
1086           const struct descriptive_data *dd
1087             = categoricals_get_user_data_by_category (cats, count);
1088
1089           double n, mean, variance;
1090           moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1091
1092           double std_dev = sqrt (variance);
1093           double std_error = std_dev / sqrt (n) ;
1094           double T = gsl_cdf_tdist_Qinv (q, n - 1);
1095
1096           double entries[] = {
1097             n,
1098             mean,
1099             std_dev,
1100             std_error,
1101             mean - T * std_error,
1102             mean + T * std_error,
1103             dd->minimum,
1104             dd->maximum,
1105           };
1106           for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1107             pivot_table_put3 (table, i, count, dep_var_idx,
1108                               pivot_value_new_number (entries[i]));
1109         }
1110
1111       if (categoricals_is_complete (cats))
1112         {
1113           double n, mean, variance;
1114           moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1115                               NULL, NULL);
1116
1117           double std_dev = sqrt (variance);
1118           double std_error = std_dev / sqrt (n) ;
1119           double T = gsl_cdf_tdist_Qinv (q, n - 1);
1120
1121           double entries[] = {
1122             n,
1123             mean,
1124             std_dev,
1125             std_error,
1126             mean - T * std_error,
1127             mean + T * std_error,
1128             ws->dd_total[v]->minimum,
1129             ws->dd_total[v]->maximum,
1130           };
1131           for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1132             pivot_table_put3 (table, i, count, dep_var_idx,
1133                               pivot_value_new_number (entries[i]));
1134         }
1135     }
1136
1137   pivot_table_submit (table);
1138 }
1139
1140 /* Show the homogeneity table */
1141 static void
1142 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1143 {
1144   struct pivot_table *table = pivot_table_create (
1145     N_("Test of Homogeneity of Variances"));
1146
1147   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1148                           N_("Levene Statistic"), PIVOT_RC_OTHER,
1149                           N_("df1"), PIVOT_RC_INTEGER,
1150                           N_("df2"), PIVOT_RC_INTEGER,
1151                           N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1152
1153   struct pivot_dimension *variables = pivot_dimension_create (
1154     table, PIVOT_AXIS_ROW, N_("Variables"));
1155
1156   for (int v = 0; v < cmd->n_vars; ++v)
1157     {
1158       int var_idx = pivot_category_create_leaf (
1159         variables->root, pivot_value_new_variable (cmd->vars[v]));
1160
1161       double n;
1162       moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1163
1164       const struct per_var_ws *pvw = &ws->vws[v];
1165       double df1 = pvw->n_groups - 1;
1166       double df2 = n - pvw->n_groups;
1167       double F = levene_calculate (pvw->nl);
1168
1169       double entries[] =
1170         {
1171           F,
1172           df1,
1173           df2,
1174           gsl_cdf_fdist_Q (F, df1, df2),
1175         };
1176       for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1177         pivot_table_put2 (table, i, var_idx,
1178                           pivot_value_new_number (entries[i]));
1179     }
1180
1181   pivot_table_submit (table);
1182 }
1183
1184
1185 /* Show the contrast coefficients table */
1186 static void
1187 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1188 {
1189   struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1190
1191   struct pivot_dimension *indep_var = pivot_dimension_create__ (
1192     table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1193   indep_var->root->show_label = true;
1194
1195   struct pivot_dimension *contrast = pivot_dimension_create (
1196     table, PIVOT_AXIS_ROW, N_("Contrast"));
1197   contrast->root->show_label = true;
1198
1199   const struct covariance *cov = ws->vws[0].cov;
1200
1201   const struct contrasts_node *cn;
1202   int c_num = 1;
1203   ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1204     {
1205       int contrast_idx = pivot_category_create_leaf (
1206         contrast->root, pivot_value_new_integer (c_num++));
1207
1208       const struct coeff_node *coeffn;
1209       int indep_idx = 0;
1210       ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1211         {
1212           const struct categoricals *cats = covariance_get_categoricals (cov);
1213           const struct ccase *gcc = categoricals_get_case_by_category (
1214             cats, indep_idx);
1215
1216           if (!contrast_idx)
1217             pivot_category_create_leaf (
1218               indep_var->root, pivot_value_new_var_value (
1219                 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1220
1221           pivot_table_put2 (table, indep_idx++, contrast_idx,
1222                             pivot_value_new_integer (coeffn->coeff));
1223         }
1224     }
1225
1226   pivot_table_submit (table);
1227 }
1228
1229 /* Show the results of the contrast tests */
1230 static void
1231 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1232 {
1233   struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1234
1235   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1236                           N_("Value of Contrast"), PIVOT_RC_OTHER,
1237                           N_("Std. Error"), PIVOT_RC_OTHER,
1238                           N_("t"), PIVOT_RC_OTHER,
1239                           N_("df"), PIVOT_RC_OTHER,
1240                           N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1241
1242   struct pivot_dimension *contrasts = pivot_dimension_create (
1243     table, PIVOT_AXIS_ROW, N_("Contrast"));
1244   contrasts->root->show_label = true;
1245   int n_contrasts = ll_count (&cmd->contrast_list);
1246   for (int i = 1; i <= n_contrasts; i++)
1247     pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1248
1249   pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1250                           N_("Assume equal variances"),
1251                           N_("Does not assume equal variances"));
1252
1253   struct pivot_dimension *variables = pivot_dimension_create (
1254     table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1255
1256   for (int v = 0; v < cmd->n_vars; ++v)
1257     {
1258       const struct per_var_ws *pvw = &ws->vws[v];
1259       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1260       if (!categoricals_is_complete (cats))
1261         continue;
1262
1263       int var_idx = pivot_category_create_leaf (
1264         variables->root, pivot_value_new_variable (cmd->vars[v]));
1265
1266       struct contrasts_node *cn;
1267       int contrast_idx = 0;
1268       ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1269         {
1270
1271           /* Note: The calculation of the degrees of freedom in the
1272              "variances not equal" case is painfull!!
1273              The following formula may help to understand it:
1274              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1275              {
1276              \sum_{i=1}^k\left (
1277              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1278              \right)
1279              }
1280           */
1281
1282           double grand_n;
1283           moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1284                               NULL, NULL);
1285           double df = grand_n - pvw->n_groups;
1286
1287           double contrast_value = 0.0;
1288           double coef_msq = 0.0;
1289           double sec_vneq = 0.0;
1290           double df_denominator = 0.0;
1291           double df_numerator = 0.0;
1292           struct coeff_node *coeffn;
1293           int ci = 0;
1294           ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1295             {
1296               const struct descriptive_data *dd
1297                 = categoricals_get_user_data_by_category (cats, ci);
1298               const double coef = coeffn->coeff;
1299
1300               double n, mean, variance;
1301               moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1302
1303               double winv = variance / n;
1304               contrast_value += coef * mean;
1305               coef_msq += pow2 (coef) / n;
1306               sec_vneq += pow2 (coef) * variance / n;
1307               df_numerator += pow2 (coef) * winv;
1308               df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1309
1310               ci++;
1311             }
1312           sec_vneq = sqrt (sec_vneq);
1313           df_numerator = pow2 (df_numerator);
1314
1315           double std_error_contrast = sqrt (pvw->mse * coef_msq);
1316           double T = fabs (contrast_value / std_error_contrast);
1317           double T_ne = contrast_value / sec_vneq;
1318           double df_ne = df_numerator / df_denominator;
1319           double p_ne = gsl_cdf_tdist_P (T_ne, df_ne);
1320           double q_ne = gsl_cdf_tdist_Q (T_ne, df_ne);
1321
1322           struct entry
1323             {
1324               int stat_idx;
1325               int assumption_idx;
1326               double x;
1327             }
1328           entries[] =
1329             {
1330               /* Assume equal. */
1331               { 0, 0, contrast_value },
1332               { 1, 0, std_error_contrast },
1333               { 2, 0, T },
1334               { 3, 0, df },
1335               { 4, 0, 2 * gsl_cdf_tdist_Q (T, df) },
1336               /* Do not assume equal. */
1337               { 0, 1, contrast_value },
1338               { 1, 1, sec_vneq },
1339               { 2, 1, T_ne },
1340               { 3, 1, df_ne },
1341               { 4, 1, 2 * (T > 0 ? q_ne : p_ne) },
1342             };
1343
1344           for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1345             {
1346               const struct entry *e = &entries[i];
1347               pivot_table_put4 (
1348                 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1349                 pivot_value_new_number (e->x));
1350             }
1351
1352           contrast_idx++;
1353         }
1354     }
1355
1356   pivot_table_submit (table);
1357 }
1358
1359 static void
1360 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1361 {
1362   struct pivot_table *table = pivot_table_create__ (
1363     pivot_value_new_user_text_nocopy (xasprintf (
1364                                         _("Multiple Comparisons (%s)"),
1365                                         var_to_string (cmd->vars[v]))));
1366   table->omit_empty = true;
1367
1368   struct pivot_dimension *statistics = pivot_dimension_create (
1369     table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1370     N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1371     N_("Std. Error"), PIVOT_RC_OTHER,
1372     N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1373   struct pivot_category *interval = pivot_category_create_group__ (
1374     statistics->root,
1375     pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1376                                  (1 - cmd->alpha) * 100.0));
1377   pivot_category_create_leaves (interval,
1378                                 N_("Lower Bound"), PIVOT_RC_OTHER,
1379                                 N_("Upper Bound"), PIVOT_RC_OTHER);
1380
1381   struct pivot_dimension *j_family = pivot_dimension_create (
1382     table, PIVOT_AXIS_ROW, N_("(J) Family"));
1383   j_family->root->show_label = true;
1384
1385   struct pivot_dimension *i_family = pivot_dimension_create (
1386     table, PIVOT_AXIS_ROW, N_("(J) Family"));
1387   i_family->root->show_label = true;
1388
1389   const struct per_var_ws *pvw = &ws->vws[v];
1390   const struct categoricals *cat = pvw->cat;
1391   for (int i = 0; i < pvw->n_groups; i++)
1392     {
1393       const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1394       for (int j = 0; j < 2; j++)
1395         pivot_category_create_leaf (
1396           j ? j_family->root : i_family->root,
1397           pivot_value_new_var_value (cmd->indep_var,
1398                                      case_data (gcc, cmd->indep_var)));
1399     }
1400
1401   struct pivot_dimension *test = pivot_dimension_create (
1402     table, PIVOT_AXIS_ROW, N_("Test"));
1403
1404   for (int p = 0; p < cmd->n_posthoc; ++p)
1405     {
1406       const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1407
1408       int test_idx = pivot_category_create_leaf (
1409         test->root, pivot_value_new_text (ph->label));
1410
1411       for (int i = 0; i < pvw->n_groups ; ++i)
1412         {
1413           struct descriptive_data *dd_i
1414             = categoricals_get_user_data_by_category (cat, i);
1415           double weight_i, mean_i, var_i;
1416           moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1417
1418           for (int j = 0 ; j < pvw->n_groups; ++j)
1419             {
1420               if (j == i)
1421                 continue;
1422
1423               struct descriptive_data *dd_j
1424                 = categoricals_get_user_data_by_category (cat, j);
1425               double weight_j, mean_j, var_j;
1426               moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1427
1428               double std_err = pvw->mse;
1429               std_err *= weight_i + weight_j;
1430               std_err /= weight_i * weight_j;
1431               std_err = sqrt (std_err);
1432
1433               double sig = 2 * multiple_comparison_sig (std_err, pvw,
1434                                                         dd_i, dd_j, ph);
1435               double half_range = mc_half_range (cmd, pvw, std_err,
1436                                                  dd_i, dd_j, ph);
1437               double entries[] = {
1438                 mean_i - mean_j,
1439                 std_err,
1440                 sig,
1441                 (mean_i - mean_j) - half_range,
1442                 (mean_i - mean_j) + half_range,
1443               };
1444               for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1445                 pivot_table_put4 (table, k, j, i, test_idx,
1446                                   pivot_value_new_number (entries[k]));
1447             }
1448         }
1449     }
1450
1451   pivot_table_submit (table);
1452 }