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