0a2b8e719d03064d83bcc16cf47b72f5cf63de83
[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       gsl_matrix_free (cm);
500     }
501
502   for (v = 0; v < cmd->n_vars; ++v)
503     {
504       const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
505
506       categoricals_done (cats);
507       
508       if (categoricals_total (cats) > ws.actual_number_of_groups)
509         ws.actual_number_of_groups = categoricals_total (cats);
510     }
511
512   casereader_destroy (input);
513
514   if (!taint_has_tainted_successor (taint))
515     output_oneway (cmd, &ws);
516
517   taint_destroy (taint);
518
519  finish:
520   for (v = 0; v < cmd->n_vars; ++v)
521     {
522       covariance_destroy (ws.vws[v].cov);
523       dd_destroy (ws.dd_total[v]);
524     }
525   free (ws.vws);
526   free (ws.dd_total);
527
528 }
529
530 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
531 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
532
533 static void
534 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
535 {
536   size_t i = 0;
537
538   /* Check the sanity of the given contrast values */
539   struct contrasts_node *coeff_list  = NULL;
540   ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
541     {
542       struct coeff_node *cn = NULL;
543       double sum = 0;
544       struct ll_list *cl = &coeff_list->coefficient_list;
545       ++i;
546
547       if (ll_count (cl) != ws->actual_number_of_groups)
548         {
549           msg (SW,
550                _("Number of contrast coefficients must equal the number of groups"));
551           coeff_list->bad_count = true;
552           continue;
553         }
554
555       ll_for_each (cn, struct coeff_node, ll, cl)
556         sum += cn->coeff;
557
558       if ( sum != 0.0 )
559         msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
560     }
561
562   if (cmd->stats & STATS_DESCRIPTIVES)
563     show_descriptives (cmd, ws);
564
565   if (cmd->stats & STATS_HOMOGENEITY)
566     show_homogeneity (cmd, ws);
567
568   show_anova_table (cmd, ws);
569
570   if (ll_count (&cmd->contrast_list) > 0)
571     {
572       show_contrast_coeffs (cmd, ws);
573       show_contrast_tests (cmd, ws);
574     }
575 }
576
577
578 /* Show the ANOVA table */
579 static void
580 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
581 {
582   size_t i;
583   int n_cols =7;
584   size_t n_rows = cmd->n_vars * 3 + 1;
585
586   struct tab_table *t = tab_create (n_cols, n_rows);
587
588   tab_headers (t, 2, 0, 1, 0);
589
590   tab_box (t,
591            TAL_2, TAL_2,
592            -1, TAL_1,
593            0, 0,
594            n_cols - 1, n_rows - 1);
595
596   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
597   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
598   tab_vline (t, TAL_0, 1, 0, 0);
599
600   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
601   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
602   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
603   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
604   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
605
606
607   for (i = 0; i < cmd->n_vars; ++i)
608     {
609       double n;
610       double df1, df2;
611       double msa;
612       const char *s = var_to_string (cmd->vars[i]);
613       const struct per_var_ws *pvw = &ws->vws[i];
614
615       moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
616
617       df1 = pvw->n_groups - 1;
618       df2 = n - pvw->n_groups;
619       msa = pvw->ssa / df1;
620
621       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
622       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
623       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
624       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
625
626       if (i > 0)
627         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
628
629
630       /* Sums of Squares */
631       tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
632       tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
633       tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
634
635
636       /* Degrees of freedom */
637       tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
638       tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
639       tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
640
641       /* Mean Squares */
642       tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
643       tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
644
645       {
646         const double F = msa / pvw->mse ;
647
648         /* The F value */
649         tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
650
651         /* The significance */
652         tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
653       }
654     }
655
656   tab_title (t, _("ANOVA"));
657   tab_submit (t);
658 }
659
660
661 /* Show the descriptives table */
662 static void
663 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
664 {
665   size_t v;
666   int n_cols = 10;
667   struct tab_table *t;
668   int row;
669
670   const double confidence = 0.95;
671   const double q = (1.0 - confidence) / 2.0;
672
673   const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
674
675   int n_rows = 2;
676
677   for (v = 0; v < cmd->n_vars; ++v)
678     n_rows += ws->actual_number_of_groups + 1;
679
680   t = tab_create (n_cols, n_rows);
681   tab_headers (t, 2, 0, 2, 0);
682
683   /* Put a frame around the entire box, and vertical lines inside */
684   tab_box (t,
685            TAL_2, TAL_2,
686            -1, TAL_1,
687            0, 0,
688            n_cols - 1, n_rows - 1);
689
690   /* Underline headers */
691   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
692   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
693
694   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
695   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
696   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
697   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
698
699
700   tab_vline (t, TAL_0, 7, 0, 0);
701   tab_hline (t, TAL_1, 6, 7, 1);
702   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
703                          _("%g%% Confidence Interval for Mean"),
704                          confidence*100.0);
705
706   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
707   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
708
709   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
710   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
711
712   tab_title (t, _("Descriptives"));
713
714   row = 2;
715   for (v = 0; v < cmd->n_vars; ++v)
716     {
717       const char *s = var_to_string (cmd->vars[v]);
718       const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
719
720       int count = 0;
721
722       struct per_var_ws *pvw = &ws->vws[v];
723       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
724
725       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
726       if ( v > 0)
727         tab_hline (t, TAL_1, 0, n_cols - 1, row);
728
729       for (count = 0; count < categoricals_total (cats); ++count)
730         {
731           double T;
732           double n, mean, variance;
733           double std_dev, std_error ;
734
735           struct string vstr;
736
737           const union value *gval = categoricals_get_value_by_subscript (cats, count);
738           const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
739
740           moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
741
742           std_dev = sqrt (variance);
743           std_error = std_dev / sqrt (n) ;
744
745           ds_init_empty (&vstr);
746
747           var_append_value_name (cmd->indep_var, gval, &vstr);
748
749           tab_text (t, 1, row + count,
750                     TAB_LEFT | TAT_TITLE,
751                     ds_cstr (&vstr));
752
753           ds_destroy (&vstr);
754
755           /* Now fill in the numbers ... */
756
757           tab_double (t, 2, row + count, 0, n, wfmt);
758
759           tab_double (t, 3, row + count, 0, mean, NULL);
760
761           tab_double (t, 4, row + count, 0, std_dev, NULL);
762
763
764           tab_double (t, 5, row + count, 0, std_error, NULL);
765
766           /* Now the confidence interval */
767
768           T = gsl_cdf_tdist_Qinv (q, n - 1);
769
770           tab_double (t, 6, row + count, 0,
771                       mean - T * std_error, NULL);
772
773           tab_double (t, 7, row + count, 0,
774                       mean + T * std_error, NULL);
775
776           /* Min and Max */
777
778           tab_double (t, 8, row + count, 0,  dd->minimum, fmt);
779           tab_double (t, 9, row + count, 0,  dd->maximum, fmt);
780         }
781
782       {
783         double T;
784         double n, mean, variance;
785         double std_dev;
786         double std_error;
787
788         moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
789
790         std_dev = sqrt (variance);
791         std_error = std_dev / sqrt (n) ;
792
793         tab_text (t, 1, row + count,
794                   TAB_LEFT | TAT_TITLE, _("Total"));
795
796         tab_double (t, 2, row + count, 0, n, wfmt);
797
798         tab_double (t, 3, row + count, 0, mean, NULL);
799
800         tab_double (t, 4, row + count, 0, std_dev, NULL);
801
802         tab_double (t, 5, row + count, 0, std_error, NULL);
803
804         /* Now the confidence interval */
805         T = gsl_cdf_tdist_Qinv (q, n - 1);
806
807         tab_double (t, 6, row + count, 0,
808                     mean - T * std_error, NULL);
809
810         tab_double (t, 7, row + count, 0,
811                     mean + T * std_error, NULL);
812
813         /* Min and Max */
814         tab_double (t, 8, row + count, 0,  ws->dd_total[v]->minimum, fmt);
815         tab_double (t, 9, row + count, 0,  ws->dd_total[v]->maximum, fmt);
816       }
817
818       row += categoricals_total (cats) + 1;
819     }
820
821   tab_submit (t);
822 }
823
824 /* Show the homogeneity table */
825 static void
826 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
827 {
828   size_t v;
829   int n_cols = 5;
830   size_t n_rows = cmd->n_vars + 1;
831
832   struct tab_table *t = tab_create (n_cols, n_rows);
833   tab_headers (t, 1, 0, 1, 0);
834
835   /* Put a frame around the entire box, and vertical lines inside */
836   tab_box (t,
837            TAL_2, TAL_2,
838            -1, TAL_1,
839            0, 0,
840            n_cols - 1, n_rows - 1);
841
842
843   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
844   tab_vline (t, TAL_2, 1, 0, n_rows - 1);
845
846   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
847   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
848   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
849   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
850
851   tab_title (t, _("Test of Homogeneity of Variances"));
852
853   for (v = 0; v < cmd->n_vars; ++v)
854     {
855       double n;
856       const struct per_var_ws *pvw = &ws->vws[v];
857       double F = pvw->levene_w;
858
859       const struct variable *var = cmd->vars[v];
860       const char *s = var_to_string (var);
861       double df1, df2;
862
863       moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
864
865       df1 = pvw->n_groups - 1;
866       df2 = n - pvw->n_groups;
867
868       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
869
870       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
871       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
872       tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
873
874       /* Now the significance */
875       tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
876     }
877
878   tab_submit (t);
879 }
880
881
882 /* Show the contrast coefficients table */
883 static void
884 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
885 {
886   int c_num = 0;
887   struct ll *cli;
888
889   int n_contrasts = ll_count (&cmd->contrast_list);
890   int n_cols = 2 + ws->actual_number_of_groups;
891   int n_rows = 2 + n_contrasts;
892
893   struct tab_table *t;
894
895   const struct covariance *cov = ws->vws[0].cov ;
896
897   t = tab_create (n_cols, n_rows);
898   tab_headers (t, 2, 0, 2, 0);
899
900   /* Put a frame around the entire box, and vertical lines inside */
901   tab_box (t,
902            TAL_2, TAL_2,
903            -1, TAL_1,
904            0, 0,
905            n_cols - 1, n_rows - 1);
906
907   tab_box (t,
908            -1, -1,
909            TAL_0, TAL_0,
910            2, 0,
911            n_cols - 1, 0);
912
913   tab_box (t,
914            -1, -1,
915            TAL_0, TAL_0,
916            0, 0,
917            1, 1);
918
919   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
920   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
921
922   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
923
924   tab_title (t, _("Contrast Coefficients"));
925
926   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
927
928
929   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
930                   var_to_string (cmd->indep_var));
931
932   for ( cli = ll_head (&cmd->contrast_list);
933         cli != ll_null (&cmd->contrast_list);
934         cli = ll_next (cli))
935     {
936       int count = 0;
937       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
938       struct ll *coeffi ;
939
940       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
941
942       for (coeffi = ll_head (&cn->coefficient_list);
943            coeffi != ll_null (&cn->coefficient_list);
944            ++count, coeffi = ll_next (coeffi))
945         {
946           const struct categoricals *cats = covariance_get_categoricals (cov);
947           const union value *val = categoricals_get_value_by_subscript (cats, count);
948           struct string vstr;
949
950           ds_init_empty (&vstr);
951
952           var_append_value_name (cmd->indep_var, val, &vstr);
953
954           tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
955
956           ds_destroy (&vstr);
957
958           if (cn->bad_count)
959             tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
960           else
961             {
962               struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
963
964               tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
965             }
966         }
967       ++c_num;
968     }
969
970   tab_submit (t);
971 }
972
973
974 /* Show the results of the contrast tests */
975 static void
976 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
977 {
978   int n_contrasts = ll_count (&cmd->contrast_list);
979   size_t v;
980   int n_cols = 8;
981   size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
982
983   struct tab_table *t;
984
985   t = tab_create (n_cols, n_rows);
986   tab_headers (t, 3, 0, 1, 0);
987
988   /* Put a frame around the entire box, and vertical lines inside */
989   tab_box (t,
990            TAL_2, TAL_2,
991            -1, TAL_1,
992            0, 0,
993            n_cols - 1, n_rows - 1);
994
995   tab_box (t,
996            -1, -1,
997            TAL_0, TAL_0,
998            0, 0,
999            2, 0);
1000
1001   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1002   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1003
1004   tab_title (t, _("Contrast Tests"));
1005
1006   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1007   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1008   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1009   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1010   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1011   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1012
1013   for (v = 0; v < cmd->n_vars; ++v)
1014     {
1015       const struct per_var_ws *pvw = &ws->vws[v];
1016       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1017       struct ll *cli;
1018       int i = 0;
1019       int lines_per_variable = 2 * n_contrasts;
1020
1021       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1022                 var_to_string (cmd->vars[v]));
1023
1024       for ( cli = ll_head (&cmd->contrast_list);
1025             cli != ll_null (&cmd->contrast_list);
1026             ++i, cli = ll_next (cli))
1027         {
1028           struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1029           struct ll *coeffi ;
1030           int ci = 0;
1031           double contrast_value = 0.0;
1032           double coef_msq = 0.0;
1033
1034           double T;
1035           double std_error_contrast;
1036           double df;
1037           double sec_vneq = 0.0;
1038
1039           /* Note: The calculation of the degrees of freedom in the
1040              "variances not equal" case is painfull!!
1041              The following formula may help to understand it:
1042              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1043              {
1044              \sum_{i=1}^k\left (
1045              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1046              \right)
1047              }
1048           */
1049
1050           double df_denominator = 0.0;
1051           double df_numerator = 0.0;
1052
1053           double grand_n;
1054           moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1055           df = grand_n - pvw->n_groups;
1056
1057           if ( i == 0 )
1058             {
1059               tab_text (t,  1, (v * lines_per_variable) + i + 1,
1060                         TAB_LEFT | TAT_TITLE,
1061                         _("Assume equal variances"));
1062
1063               tab_text (t,  1, (v * lines_per_variable) + i + 1 + n_contrasts,
1064                         TAB_LEFT | TAT_TITLE,
1065                         _("Does not assume equal"));
1066             }
1067
1068           tab_text_format (t,  2, (v * lines_per_variable) + i + 1,
1069                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1070
1071
1072           tab_text_format (t,  2,
1073                            (v * lines_per_variable) + i + 1 + n_contrasts,
1074                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1075
1076           if (cn->bad_count)
1077             continue;
1078
1079           for (coeffi = ll_head (&cn->coefficient_list);
1080                coeffi != ll_null (&cn->coefficient_list);
1081                ++ci, coeffi = ll_next (coeffi))
1082             {
1083               double n, mean, variance;
1084               const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1085               struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1086               const double coef = cn->coeff; 
1087               double winv ;
1088
1089               moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1090
1091               winv = variance / n;
1092
1093               contrast_value += coef * mean;
1094
1095               coef_msq += (pow2 (coef)) / n;
1096
1097               sec_vneq += (pow2 (coef)) * variance / n;
1098
1099               df_numerator += (pow2 (coef)) * winv;
1100               df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1101             }
1102
1103           sec_vneq = sqrt (sec_vneq);
1104
1105           df_numerator = pow2 (df_numerator);
1106
1107           tab_double (t,  3, (v * lines_per_variable) + i + 1,
1108                       TAB_RIGHT, contrast_value, NULL);
1109
1110           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
1111                       n_contrasts,
1112                       TAB_RIGHT, contrast_value, NULL);
1113
1114           std_error_contrast = sqrt (pvw->mse * coef_msq);
1115
1116           /* Std. Error */
1117           tab_double (t,  4, (v * lines_per_variable) + i + 1,
1118                       TAB_RIGHT, std_error_contrast,
1119                       NULL);
1120
1121           T = fabs (contrast_value / std_error_contrast);
1122
1123           /* T Statistic */
1124
1125           tab_double (t,  5, (v * lines_per_variable) + i + 1,
1126                       TAB_RIGHT, T,
1127                       NULL);
1128
1129
1130           /* Degrees of Freedom */
1131           tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
1132                      TAB_RIGHT,  df,
1133                      8, 0);
1134
1135
1136           /* Significance TWO TAILED !!*/
1137           tab_double (t,  7, (v * lines_per_variable) + i + 1,
1138                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
1139                       NULL);
1140
1141           /* Now for the Variances NOT Equal case */
1142
1143           /* Std. Error */
1144           tab_double (t,  4,
1145                       (v * lines_per_variable) + i + 1 + n_contrasts,
1146                       TAB_RIGHT, sec_vneq,
1147                       NULL);
1148
1149           T = contrast_value / sec_vneq;
1150           tab_double (t,  5,
1151                       (v * lines_per_variable) + i + 1 + n_contrasts,
1152                       TAB_RIGHT, T,
1153                       NULL);
1154
1155           df = df_numerator / df_denominator;
1156
1157           tab_double (t,  6,
1158                       (v * lines_per_variable) + i + 1 + n_contrasts,
1159                       TAB_RIGHT, df,
1160                       NULL);
1161
1162           /* The Significance */
1163           tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1164                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
1165                       NULL);
1166         }
1167
1168       if ( v > 0 )
1169         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1170     }
1171
1172   tab_submit (t);
1173 }
1174
1175