ONEWAY: Fix warning for passing "const" pointer as non-const parameter.
[pspp-builds.git] / src / language / stats / oneway.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010 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 <data/case.h>
20 #include <data/casegrouper.h>
21 #include <data/casereader.h>
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/value.h>
25
26
27 #include <math/covariance.h>
28 #include <math/categoricals.h>
29 #include <math/levene.h>
30 #include <math/moments.h>
31 #include <gsl/gsl_matrix.h>
32 #include <linreg/sweep.h>
33
34 #include <libpspp/ll.h>
35
36 #include <language/lexer/lexer.h>
37 #include <language/lexer/variable-parser.h>
38 #include <language/lexer/value-parser.h>
39 #include <language/command.h>
40
41
42 #include <language/dictionary/split-file.h>
43 #include <libpspp/taint.h>
44 #include <libpspp/misc.h>
45
46 #include <output/tab.h>
47
48 #include <gsl/gsl_cdf.h>
49 #include <math.h>
50 #include <data/format.h>
51
52 #include <libpspp/message.h>
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56
57
58 enum missing_type
59   {
60     MISS_LISTWISE,
61     MISS_ANALYSIS,
62   };
63
64 enum statistics
65   {
66     STATS_DESCRIPTIVES = 0x0001,
67     STATS_HOMOGENEITY = 0x0002
68   };
69
70 struct coeff_node
71 {
72   struct ll ll; 
73   double coeff; 
74 };
75
76
77 struct contrasts_node
78 {
79   struct ll ll; 
80   struct ll_list coefficient_list;
81
82   bool bad_count; /* True if the number of coefficients does not equal the number of groups */
83 };
84
85 struct oneway_spec
86 {
87   size_t n_vars;
88   const struct variable **vars;
89
90   const struct variable *indep_var;
91
92   enum statistics stats;
93
94   enum missing_type missing_type;
95   enum mv_class exclude;
96
97   /* List of contrasts */
98   struct ll_list contrast_list;
99
100   /* The weight variable */
101   const struct variable *wv;
102
103 };
104
105 /* Per category data */
106 struct descriptive_data
107 {
108   const struct variable *var;
109   struct moments1 *mom;
110
111   double minimum;
112   double maximum;
113 };
114
115 /* Workspace variable for each dependent variable */
116 struct per_var_ws
117 {
118   struct categoricals *cat;
119   struct covariance *cov;
120
121   double sst;
122   double sse;
123   double ssa;
124
125   int n_groups;
126
127   double mse;
128   double levene_w;
129 };
130
131 struct oneway_workspace
132 {
133   /* The number of distinct values of the independent variable, when all
134      missing values are disregarded */
135   int actual_number_of_groups;
136
137   struct per_var_ws *vws;
138
139   /* An array of descriptive data.  One for each dependent variable */
140   struct descriptive_data **dd_total;
141 };
142
143 /* Routines to show the output tables */
144 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
145 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
146 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
147
148 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
149 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
150
151 int
152 cmd_oneway (struct lexer *lexer, struct dataset *ds)
153 {
154   const struct dictionary *dict = dataset_dict (ds);  
155   struct oneway_spec oneway ;
156   oneway.n_vars = 0;
157   oneway.vars = NULL;
158   oneway.indep_var = NULL;
159   oneway.stats = 0;
160   oneway.missing_type = MISS_ANALYSIS;
161   oneway.exclude = MV_ANY;
162   oneway.wv = dict_get_weight (dict);
163
164   ll_init (&oneway.contrast_list);
165
166   
167   if ( lex_match (lexer, T_SLASH))
168     {
169       if (!lex_force_match_id (lexer, "VARIABLES"))
170         {
171           goto error;
172         }
173       lex_match (lexer, T_EQUALS);
174     }
175
176   if (!parse_variables_const (lexer, dict,
177                               &oneway.vars, &oneway.n_vars,
178                               PV_NO_DUPLICATE | PV_NUMERIC))
179     goto error;
180
181   lex_force_match (lexer, T_BY);
182
183   oneway.indep_var = parse_variable_const (lexer, dict);
184
185   while (lex_token (lexer) != T_ENDCMD)
186     {
187       lex_match (lexer, T_SLASH);
188
189       if (lex_match_id (lexer, "STATISTICS"))
190         {
191           lex_match (lexer, T_EQUALS);
192           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
193             {
194               if (lex_match_id (lexer, "DESCRIPTIVES"))
195                 {
196                   oneway.stats |= STATS_DESCRIPTIVES;
197                 }
198               else if (lex_match_id (lexer, "HOMOGENEITY"))
199                 {
200                   oneway.stats |= STATS_HOMOGENEITY;
201                 }
202               else
203                 {
204                   lex_error (lexer, NULL);
205                   goto error;
206                 }
207             }
208         }
209       else if (lex_match_id (lexer, "CONTRAST"))
210         {
211           struct contrasts_node *cl = xzalloc (sizeof *cl);
212
213           struct ll_list *coefficient_list = &cl->coefficient_list;
214           lex_match (lexer, T_EQUALS);
215
216           ll_init (coefficient_list);
217
218           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
219             {
220               if ( lex_is_number (lexer))
221                 {
222                   struct coeff_node *cc = xmalloc (sizeof *cc);
223                   cc->coeff = lex_number (lexer);
224
225                   ll_push_tail (coefficient_list, &cc->ll);
226                   lex_get (lexer);
227                 }
228               else
229                 {
230                   lex_error (lexer, NULL);
231                   goto error;
232                 }
233             }
234
235           ll_push_tail (&oneway.contrast_list, &cl->ll);
236         }
237       else if (lex_match_id (lexer, "MISSING"))
238         {
239           lex_match (lexer, T_EQUALS);
240           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
241             {
242               if (lex_match_id (lexer, "INCLUDE"))
243                 {
244                   oneway.exclude = MV_SYSTEM;
245                 }
246               else if (lex_match_id (lexer, "EXCLUDE"))
247                 {
248                   oneway.exclude = MV_ANY;
249                 }
250               else if (lex_match_id (lexer, "LISTWISE"))
251                 {
252                   oneway.missing_type = MISS_LISTWISE;
253                 }
254               else if (lex_match_id (lexer, "ANALYSIS"))
255                 {
256                   oneway.missing_type = MISS_ANALYSIS;
257                 }
258               else
259                 {
260                   lex_error (lexer, NULL);
261                   goto error;
262                 }
263             }
264         }
265       else
266         {
267           lex_error (lexer, NULL);
268           goto error;
269         }
270     }
271
272
273   {
274     struct casegrouper *grouper;
275     struct casereader *group;
276     bool ok;
277
278     grouper = casegrouper_create_splits (proc_open (ds), dict);
279     while (casegrouper_get_next_group (grouper, &group))
280       run_oneway (&oneway, group, ds);
281     ok = casegrouper_destroy (grouper);
282     ok = proc_commit (ds) && ok;
283   }
284
285   free (oneway.vars);
286   return CMD_SUCCESS;
287
288  error:
289   free (oneway.vars);
290   return CMD_FAILURE;
291 }
292
293
294 \f
295
296
297 static struct descriptive_data *
298 dd_create (const struct variable *var)
299 {
300   struct descriptive_data *dd = xmalloc (sizeof *dd);
301
302   dd->mom = moments1_create (MOMENT_VARIANCE);
303   dd->minimum = DBL_MAX;
304   dd->maximum = -DBL_MAX;
305   dd->var = var;
306
307   return dd;
308 }
309
310 static void
311 dd_destroy (struct descriptive_data *dd)
312 {
313   moments1_destroy (dd->mom);
314   free (dd);
315 }
316
317 static void *
318 makeit (void *aux1, void *aux2 UNUSED)
319 {
320   const struct variable *var = aux1;
321
322   struct descriptive_data *dd = dd_create (var);
323
324   return dd;
325 }
326
327 static void 
328 updateit (void *user_data, 
329           enum mv_class exclude,
330           const struct variable *wv, 
331           const struct variable *catvar UNUSED,
332           const struct ccase *c,
333           void *aux1, void *aux2)
334 {
335   struct descriptive_data *dd = user_data;
336
337   const struct variable *varp = aux1;
338
339   const union value *valx = case_data (c, varp);
340
341   struct descriptive_data *dd_total = aux2;
342
343   double weight;
344
345   if ( var_is_value_missing (varp, valx, exclude))
346     return;
347
348   weight = wv != NULL ? case_data (c, wv)->f : 1.0;
349
350   moments1_add (dd->mom, valx->f, weight);
351   if (valx->f < dd->minimum)
352     dd->minimum = valx->f;
353
354   if (valx->f > dd->maximum)
355     dd->maximum = valx->f;
356
357   {
358     const struct variable *var = dd_total->var;
359     const union value *val = case_data (c, var);
360
361     moments1_add (dd_total->mom,
362                   val->f,
363                   weight);
364
365     if (val->f < dd_total->minimum)
366       dd_total->minimum = val->f;
367
368     if (val->f > dd_total->maximum)
369       dd_total->maximum = val->f;
370   }
371 }
372
373 static void
374 run_oneway (const struct oneway_spec *cmd,
375             struct casereader *input,
376             const struct dataset *ds)
377 {
378   int v;
379   struct taint *taint;
380   struct dictionary *dict = dataset_dict (ds);
381   struct casereader *reader;
382   struct ccase *c;
383
384   struct oneway_workspace ws;
385
386   ws.actual_number_of_groups = 0;
387   ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
388   ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
389
390   for (v = 0 ; v < cmd->n_vars; ++v)
391     ws.dd_total[v] = dd_create (cmd->vars[v]);
392
393   for (v = 0; v < cmd->n_vars; ++v)
394     {
395       ws.vws[v].cat = categoricals_create (&cmd->indep_var, 1, cmd->wv,
396                                            cmd->exclude, makeit, updateit,
397                                            CONST_CAST (struct variable *,
398                                                        cmd->vars[v]),
399                                            ws.dd_total[v]);
400
401       ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
402                                                ws.vws[v].cat, 
403                                                cmd->wv, cmd->exclude);
404     }
405
406   c = casereader_peek (input, 0);
407   if (c == NULL)
408     {
409       casereader_destroy (input);
410       goto finish;
411     }
412   output_split_file_values (ds, c);
413   case_unref (c);
414
415   taint = taint_clone (casereader_get_taint (input));
416
417   input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
418                                             cmd->exclude, NULL, NULL);
419   if (cmd->missing_type == MISS_LISTWISE)
420     input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
421                                               cmd->exclude, NULL, NULL);
422   input = casereader_create_filter_weight (input, dict, NULL, NULL);
423
424
425   if (cmd->stats & STATS_HOMOGENEITY)
426     for (v = 0; v < cmd->n_vars; ++v)
427       {
428         struct per_var_ws *pvw = &ws.vws[v];
429
430         pvw->levene_w = levene (input, cmd->indep_var, cmd->vars[v], cmd->wv, cmd->exclude);
431       }
432
433   reader = casereader_clone (input);
434
435   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
436     {
437       int i;
438
439       for (i = 0; i < cmd->n_vars; ++i)
440         {
441           struct per_var_ws *pvw = &ws.vws[i];
442           const struct variable *v = cmd->vars[i];
443           const union value *val = case_data (c, v);
444
445           if ( MISS_ANALYSIS == cmd->missing_type)
446             {
447               if ( var_is_value_missing (v, val, cmd->exclude))
448                 continue;
449             }
450
451           covariance_accumulate_pass1 (pvw->cov, c);
452         }
453     }
454   casereader_destroy (reader);
455   reader = casereader_clone (input);
456   for ( ; (c = casereader_read (reader) ); case_unref (c))
457     {
458       int i;
459       for (i = 0; i < cmd->n_vars; ++i)
460         {
461           struct per_var_ws *pvw = &ws.vws[i];
462           const struct variable *v = cmd->vars[i];
463           const union value *val = case_data (c, v);
464
465           if ( MISS_ANALYSIS == cmd->missing_type)
466             {
467               if ( var_is_value_missing (v, val, cmd->exclude))
468                 continue;
469             }
470
471           covariance_accumulate_pass2 (pvw->cov, c);
472         }
473     }
474   casereader_destroy (reader);
475
476   for (v = 0; v < cmd->n_vars; ++v)
477     {
478       struct per_var_ws *pvw = &ws.vws[v];
479       gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
480       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
481
482       double n;
483       moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
484
485       pvw->sst = gsl_matrix_get (cm, 0, 0);
486
487       //      gsl_matrix_fprintf (stdout, cm, "%g ");
488
489       reg_sweep (cm, 0);
490
491       pvw->sse = gsl_matrix_get (cm, 0, 0);
492
493       pvw->ssa = pvw->sst - pvw->sse;
494
495       pvw->n_groups = categoricals_total (cats);
496
497       pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
498     }
499
500   for (v = 0; v < cmd->n_vars; ++v)
501     {
502       struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
503
504       categoricals_done (cats);
505       
506       if (categoricals_total (cats) > ws.actual_number_of_groups)
507         ws.actual_number_of_groups = categoricals_total (cats);
508     }
509
510   casereader_destroy (input);
511
512   if (!taint_has_tainted_successor (taint))
513     output_oneway (cmd, &ws);
514
515   taint_destroy (taint);
516
517  finish:
518   for (v = 0; v < cmd->n_vars; ++v)
519     {
520       covariance_destroy (ws.vws[v].cov);
521       dd_destroy (ws.dd_total[v]);
522     }
523   free (ws.vws);
524   free (ws.dd_total);
525
526 }
527
528 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
529 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
530
531 static void
532 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
533 {
534   size_t i = 0;
535
536   /* Check the sanity of the given contrast values */
537   struct contrasts_node *coeff_list  = NULL;
538   ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
539     {
540       struct coeff_node *cn = NULL;
541       double sum = 0;
542       struct ll_list *cl = &coeff_list->coefficient_list;
543       ++i;
544
545       if (ll_count (cl) != ws->actual_number_of_groups)
546         {
547           msg (SW,
548                _("Number of contrast coefficients must equal the number of groups"));
549           coeff_list->bad_count = true;
550           continue;
551         }
552
553       ll_for_each (cn, struct coeff_node, ll, cl)
554         sum += cn->coeff;
555
556       if ( sum != 0.0 )
557         msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
558     }
559
560   if (cmd->stats & STATS_DESCRIPTIVES)
561     show_descriptives (cmd, ws);
562
563   if (cmd->stats & STATS_HOMOGENEITY)
564     show_homogeneity (cmd, ws);
565
566   show_anova_table (cmd, ws);
567
568   if (ll_count (&cmd->contrast_list) > 0)
569     {
570       show_contrast_coeffs (cmd, ws);
571       show_contrast_tests (cmd, ws);
572     }
573 }
574
575
576 /* Show the ANOVA table */
577 static void
578 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
579 {
580   size_t i;
581   int n_cols =7;
582   size_t n_rows = cmd->n_vars * 3 + 1;
583
584   struct tab_table *t = tab_create (n_cols, n_rows);
585
586   tab_headers (t, 2, 0, 1, 0);
587
588   tab_box (t,
589            TAL_2, TAL_2,
590            -1, TAL_1,
591            0, 0,
592            n_cols - 1, n_rows - 1);
593
594   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
595   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
596   tab_vline (t, TAL_0, 1, 0, 0);
597
598   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
599   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
600   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
601   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
602   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
603
604
605   for (i = 0; i < cmd->n_vars; ++i)
606     {
607       double n;
608       double df1, df2;
609       double msa;
610       const char *s = var_to_string (cmd->vars[i]);
611       const struct per_var_ws *pvw = &ws->vws[i];
612
613       moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
614
615       df1 = pvw->n_groups - 1;
616       df2 = n - pvw->n_groups;
617       msa = pvw->ssa / df1;
618
619       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
620       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
621       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
622       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
623
624       if (i > 0)
625         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
626
627
628       /* Sums of Squares */
629       tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
630       tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
631       tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
632
633
634       /* Degrees of freedom */
635       tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
636       tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
637       tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
638
639       /* Mean Squares */
640       tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
641       tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
642
643       {
644         const double F = msa / pvw->mse ;
645
646         /* The F value */
647         tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
648
649         /* The significance */
650         tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
651       }
652     }
653
654   tab_title (t, _("ANOVA"));
655   tab_submit (t);
656 }
657
658
659 /* Show the descriptives table */
660 static void
661 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
662 {
663   size_t v;
664   int n_cols = 10;
665   struct tab_table *t;
666   int row;
667
668   const double confidence = 0.95;
669   const double q = (1.0 - confidence) / 2.0;
670
671   const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
672
673   int n_rows = 2;
674
675   for (v = 0; v < cmd->n_vars; ++v)
676     n_rows += ws->actual_number_of_groups + 1;
677
678   t = tab_create (n_cols, n_rows);
679   tab_headers (t, 2, 0, 2, 0);
680
681   /* Put a frame around the entire box, and vertical lines inside */
682   tab_box (t,
683            TAL_2, TAL_2,
684            -1, TAL_1,
685            0, 0,
686            n_cols - 1, n_rows - 1);
687
688   /* Underline headers */
689   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
690   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
691
692   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
693   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
694   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
695   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
696
697
698   tab_vline (t, TAL_0, 7, 0, 0);
699   tab_hline (t, TAL_1, 6, 7, 1);
700   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
701                          _("%g%% Confidence Interval for Mean"),
702                          confidence*100.0);
703
704   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
705   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
706
707   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
708   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
709
710   tab_title (t, _("Descriptives"));
711
712   row = 2;
713   for (v = 0; v < cmd->n_vars; ++v)
714     {
715       const char *s = var_to_string (cmd->vars[v]);
716       const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
717
718       int count = 0;
719
720       struct per_var_ws *pvw = &ws->vws[v];
721       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
722
723       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
724       if ( v > 0)
725         tab_hline (t, TAL_1, 0, n_cols - 1, row);
726
727       for (count = 0; count < categoricals_total (cats); ++count)
728         {
729           double T;
730           double n, mean, variance;
731           double std_dev, std_error ;
732
733           struct string vstr;
734
735           const union value *gval = categoricals_get_value_by_subscript (cats, count);
736           const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
737
738           moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
739
740           std_dev = sqrt (variance);
741           std_error = std_dev / sqrt (n) ;
742
743           ds_init_empty (&vstr);
744
745           var_append_value_name (cmd->indep_var, gval, &vstr);
746
747           tab_text (t, 1, row + count,
748                     TAB_LEFT | TAT_TITLE,
749                     ds_cstr (&vstr));
750
751           ds_destroy (&vstr);
752
753           /* Now fill in the numbers ... */
754
755           tab_double (t, 2, row + count, 0, n, wfmt);
756
757           tab_double (t, 3, row + count, 0, mean, NULL);
758
759           tab_double (t, 4, row + count, 0, std_dev, NULL);
760
761
762           tab_double (t, 5, row + count, 0, std_error, NULL);
763
764           /* Now the confidence interval */
765
766           T = gsl_cdf_tdist_Qinv (q, n - 1);
767
768           tab_double (t, 6, row + count, 0,
769                       mean - T * std_error, NULL);
770
771           tab_double (t, 7, row + count, 0,
772                       mean + T * std_error, NULL);
773
774           /* Min and Max */
775
776           tab_double (t, 8, row + count, 0,  dd->minimum, fmt);
777           tab_double (t, 9, row + count, 0,  dd->maximum, fmt);
778         }
779
780       {
781         double T;
782         double n, mean, variance;
783         double std_dev;
784         double std_error;
785
786         moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
787
788         std_dev = sqrt (variance);
789         std_error = std_dev / sqrt (n) ;
790
791         tab_text (t, 1, row + count,
792                   TAB_LEFT | TAT_TITLE, _("Total"));
793
794         tab_double (t, 2, row + count, 0, n, wfmt);
795
796         tab_double (t, 3, row + count, 0, mean, NULL);
797
798         tab_double (t, 4, row + count, 0, std_dev, NULL);
799
800         tab_double (t, 5, row + count, 0, std_error, NULL);
801
802         /* Now the confidence interval */
803         T = gsl_cdf_tdist_Qinv (q, n - 1);
804
805         tab_double (t, 6, row + count, 0,
806                     mean - T * std_error, NULL);
807
808         tab_double (t, 7, row + count, 0,
809                     mean + T * std_error, NULL);
810
811         /* Min and Max */
812         tab_double (t, 8, row + count, 0,  ws->dd_total[v]->minimum, fmt);
813         tab_double (t, 9, row + count, 0,  ws->dd_total[v]->maximum, fmt);
814       }
815
816       row += categoricals_total (cats) + 1;
817     }
818
819   tab_submit (t);
820 }
821
822 /* Show the homogeneity table */
823 static void
824 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
825 {
826   size_t v;
827   int n_cols = 5;
828   size_t n_rows = cmd->n_vars + 1;
829
830   struct tab_table *t = tab_create (n_cols, n_rows);
831   tab_headers (t, 1, 0, 1, 0);
832
833   /* Put a frame around the entire box, and vertical lines inside */
834   tab_box (t,
835            TAL_2, TAL_2,
836            -1, TAL_1,
837            0, 0,
838            n_cols - 1, n_rows - 1);
839
840
841   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
842   tab_vline (t, TAL_2, 1, 0, n_rows - 1);
843
844   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
845   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
846   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
847   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
848
849   tab_title (t, _("Test of Homogeneity of Variances"));
850
851   for (v = 0; v < cmd->n_vars; ++v)
852     {
853       double n;
854       const struct per_var_ws *pvw = &ws->vws[v];
855       double F = pvw->levene_w;
856
857       const struct variable *var = cmd->vars[v];
858       const char *s = var_to_string (var);
859       double df1, df2;
860
861       moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
862
863       df1 = pvw->n_groups - 1;
864       df2 = n - pvw->n_groups;
865
866       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
867
868       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
869       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
870       tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
871
872       /* Now the significance */
873       tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
874     }
875
876   tab_submit (t);
877 }
878
879
880 /* Show the contrast coefficients table */
881 static void
882 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
883 {
884   int c_num = 0;
885   struct ll *cli;
886
887   int n_contrasts = ll_count (&cmd->contrast_list);
888   int n_cols = 2 + ws->actual_number_of_groups;
889   int n_rows = 2 + n_contrasts;
890
891   struct tab_table *t;
892
893   const struct covariance *cov = ws->vws[0].cov ;
894
895   t = tab_create (n_cols, n_rows);
896   tab_headers (t, 2, 0, 2, 0);
897
898   /* Put a frame around the entire box, and vertical lines inside */
899   tab_box (t,
900            TAL_2, TAL_2,
901            -1, TAL_1,
902            0, 0,
903            n_cols - 1, n_rows - 1);
904
905   tab_box (t,
906            -1, -1,
907            TAL_0, TAL_0,
908            2, 0,
909            n_cols - 1, 0);
910
911   tab_box (t,
912            -1, -1,
913            TAL_0, TAL_0,
914            0, 0,
915            1, 1);
916
917   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
918   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
919
920   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
921
922   tab_title (t, _("Contrast Coefficients"));
923
924   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
925
926
927   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
928                   var_to_string (cmd->indep_var));
929
930   for ( cli = ll_head (&cmd->contrast_list);
931         cli != ll_null (&cmd->contrast_list);
932         cli = ll_next (cli))
933     {
934       int count = 0;
935       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
936       struct ll *coeffi ;
937
938       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
939
940       for (coeffi = ll_head (&cn->coefficient_list);
941            coeffi != ll_null (&cn->coefficient_list);
942            ++count, coeffi = ll_next (coeffi))
943         {
944           const struct categoricals *cats = covariance_get_categoricals (cov);
945           const union value *val = categoricals_get_value_by_subscript (cats, count);
946           struct string vstr;
947
948           ds_init_empty (&vstr);
949
950           var_append_value_name (cmd->indep_var, val, &vstr);
951
952           tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
953
954           ds_destroy (&vstr);
955
956           if (cn->bad_count)
957             tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
958           else
959             {
960               struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
961
962               tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
963             }
964         }
965       ++c_num;
966     }
967
968   tab_submit (t);
969 }
970
971
972 /* Show the results of the contrast tests */
973 static void
974 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
975 {
976   int n_contrasts = ll_count (&cmd->contrast_list);
977   size_t v;
978   int n_cols = 8;
979   size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
980
981   struct tab_table *t;
982
983   t = tab_create (n_cols, n_rows);
984   tab_headers (t, 3, 0, 1, 0);
985
986   /* Put a frame around the entire box, and vertical lines inside */
987   tab_box (t,
988            TAL_2, TAL_2,
989            -1, TAL_1,
990            0, 0,
991            n_cols - 1, n_rows - 1);
992
993   tab_box (t,
994            -1, -1,
995            TAL_0, TAL_0,
996            0, 0,
997            2, 0);
998
999   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1000   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1001
1002   tab_title (t, _("Contrast Tests"));
1003
1004   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1005   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1006   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1007   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1008   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1009   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1010
1011   for (v = 0; v < cmd->n_vars; ++v)
1012     {
1013       const struct per_var_ws *pvw = &ws->vws[v];
1014       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1015       struct ll *cli;
1016       int i = 0;
1017       int lines_per_variable = 2 * n_contrasts;
1018
1019       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1020                 var_to_string (cmd->vars[v]));
1021
1022       for ( cli = ll_head (&cmd->contrast_list);
1023             cli != ll_null (&cmd->contrast_list);
1024             ++i, cli = ll_next (cli))
1025         {
1026           struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1027           struct ll *coeffi ;
1028           int ci = 0;
1029           double contrast_value = 0.0;
1030           double coef_msq = 0.0;
1031
1032           double T;
1033           double std_error_contrast;
1034           double df;
1035           double sec_vneq = 0.0;
1036
1037           /* Note: The calculation of the degrees of freedom in the
1038              "variances not equal" case is painfull!!
1039              The following formula may help to understand it:
1040              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1041              {
1042              \sum_{i=1}^k\left (
1043              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1044              \right)
1045              }
1046           */
1047
1048           double df_denominator = 0.0;
1049           double df_numerator = 0.0;
1050
1051           double grand_n;
1052           moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1053           df = grand_n - pvw->n_groups;
1054
1055           if ( i == 0 )
1056             {
1057               tab_text (t,  1, (v * lines_per_variable) + i + 1,
1058                         TAB_LEFT | TAT_TITLE,
1059                         _("Assume equal variances"));
1060
1061               tab_text (t,  1, (v * lines_per_variable) + i + 1 + n_contrasts,
1062                         TAB_LEFT | TAT_TITLE,
1063                         _("Does not assume equal"));
1064             }
1065
1066           tab_text_format (t,  2, (v * lines_per_variable) + i + 1,
1067                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1068
1069
1070           tab_text_format (t,  2,
1071                            (v * lines_per_variable) + i + 1 + n_contrasts,
1072                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1073
1074           if (cn->bad_count)
1075             continue;
1076
1077           for (coeffi = ll_head (&cn->coefficient_list);
1078                coeffi != ll_null (&cn->coefficient_list);
1079                ++ci, coeffi = ll_next (coeffi))
1080             {
1081               double n, mean, variance;
1082               const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1083               struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1084               const double coef = cn->coeff; 
1085               double winv ;
1086
1087               moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1088
1089               winv = variance / n;
1090
1091               contrast_value += coef * mean;
1092
1093               coef_msq += (pow2 (coef)) / n;
1094
1095               sec_vneq += (pow2 (coef)) * variance / n;
1096
1097               df_numerator += (pow2 (coef)) * winv;
1098               df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1099             }
1100
1101           sec_vneq = sqrt (sec_vneq);
1102
1103           df_numerator = pow2 (df_numerator);
1104
1105           tab_double (t,  3, (v * lines_per_variable) + i + 1,
1106                       TAB_RIGHT, contrast_value, NULL);
1107
1108           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
1109                       n_contrasts,
1110                       TAB_RIGHT, contrast_value, NULL);
1111
1112           std_error_contrast = sqrt (pvw->mse * coef_msq);
1113
1114           /* Std. Error */
1115           tab_double (t,  4, (v * lines_per_variable) + i + 1,
1116                       TAB_RIGHT, std_error_contrast,
1117                       NULL);
1118
1119           T = fabs (contrast_value / std_error_contrast);
1120
1121           /* T Statistic */
1122
1123           tab_double (t,  5, (v * lines_per_variable) + i + 1,
1124                       TAB_RIGHT, T,
1125                       NULL);
1126
1127
1128           /* Degrees of Freedom */
1129           tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
1130                      TAB_RIGHT,  df,
1131                      8, 0);
1132
1133
1134           /* Significance TWO TAILED !!*/
1135           tab_double (t,  7, (v * lines_per_variable) + i + 1,
1136                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
1137                       NULL);
1138
1139           /* Now for the Variances NOT Equal case */
1140
1141           /* Std. Error */
1142           tab_double (t,  4,
1143                       (v * lines_per_variable) + i + 1 + n_contrasts,
1144                       TAB_RIGHT, sec_vneq,
1145                       NULL);
1146
1147           T = contrast_value / sec_vneq;
1148           tab_double (t,  5,
1149                       (v * lines_per_variable) + i + 1 + n_contrasts,
1150                       TAB_RIGHT, T,
1151                       NULL);
1152
1153           df = df_numerator / df_denominator;
1154
1155           tab_double (t,  6,
1156                       (v * lines_per_variable) + i + 1 + n_contrasts,
1157                       TAB_RIGHT, df,
1158                       NULL);
1159
1160           /* The Significance */
1161           tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1162                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
1163                       NULL);
1164         }
1165
1166       if ( v > 0 )
1167         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1168     }
1169
1170   tab_submit (t);
1171 }
1172
1173