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