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