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