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