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