Oneway: Remove group_values struct from show_contrast_coeffs
[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
329   for (v = 0; v < cmd->n_vars; ++v)
330     {
331       struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
332                                                    cmd->wv, cmd->exclude);
333
334       ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
335                                                cats, 
336                                                cmd->wv, cmd->exclude);
337       ws.vws[v].cc = 0;
338     }
339
340   c = casereader_peek (input, 0);
341   if (c == NULL)
342     {
343       casereader_destroy (input);
344       return;
345     }
346   output_split_file_values (ds, c);
347   case_unref (c);
348
349   taint = taint_clone (casereader_get_taint (input));
350
351   ws.group_hash = hsh_create (4,
352                                   compare_double_3way,
353                                   do_hash_double,
354                                   free_double,
355                                   cmd->indep_var);
356
357   precalc (cmd);
358
359   input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
360                                             cmd->exclude, NULL, NULL);
361   if (cmd->missing_type == MISS_LISTWISE)
362     input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
363                                               cmd->exclude, NULL, NULL);
364   input = casereader_create_filter_weight (input, dict, NULL, NULL);
365
366   reader = casereader_clone (input);
367
368   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
369     {
370       size_t i;
371
372       const double weight = dict_get_case_weight (dict, c, NULL);
373
374       const union value *indep_val = case_data (c, cmd->indep_var);
375       void **p = hsh_probe (ws.group_hash, &indep_val->f);
376       if (*p == NULL)
377         {
378           double *value = *p = xmalloc (sizeof *value);
379           *value = indep_val->f;
380         }
381
382       for (i = 0; i < cmd->n_vars; ++i)
383         {
384           {
385             struct per_var_ws *pvw = &ws.vws[i];
386
387             pvw->cc += weight;
388             covariance_accumulate_pass1 (pvw->cov, c);
389           }
390
391           const struct variable *v = cmd->vars[i];
392
393           const union value *val = case_data (c, v);
394
395           struct group_proc *gp = group_proc_get (cmd->vars[i]);
396           struct hsh_table *group_hash = gp->group_hash;
397
398           struct group_statistics *gs;
399
400           gs = hsh_find (group_hash, indep_val );
401
402           if ( ! gs )
403             {
404               gs = xmalloc (sizeof *gs);
405               gs->id = *indep_val;
406               gs->sum = 0;
407               gs->n = 0;
408               gs->ssq = 0;
409               gs->sum_diff = 0;
410               gs->minimum = DBL_MAX;
411               gs->maximum = -DBL_MAX;
412
413               hsh_insert ( group_hash, gs );
414             }
415
416           if (!var_is_value_missing (v, val, cmd->exclude))
417             {
418               struct group_statistics *totals = &gp->ugs;
419
420               totals->n += weight;
421               totals->sum += weight * val->f;
422               totals->ssq += weight * pow2 (val->f);
423
424               if ( val->f * weight  < totals->minimum )
425                 totals->minimum = val->f * weight;
426
427               if ( val->f * weight  > totals->maximum )
428                 totals->maximum = val->f * weight;
429
430               gs->n += weight;
431               gs->sum += weight * val->f;
432               gs->ssq += weight * pow2 (val->f);
433
434               if ( val->f * weight  < gs->minimum )
435                 gs->minimum = val->f * weight;
436
437               if ( val->f * weight  > gs->maximum )
438                 gs->maximum = val->f * weight;
439             }
440
441           gp->n_groups = hsh_count (group_hash );
442         }
443
444     }
445   casereader_destroy (reader);
446   reader = casereader_clone (input);
447   for ( ; (c = casereader_read (reader) ); case_unref (c))
448     {
449       int i;
450       for (i = 0; i < cmd->n_vars; ++i)
451         {
452           struct per_var_ws *pvw = &ws.vws[i];
453           covariance_accumulate_pass2 (pvw->cov, c);
454         }
455     }
456   casereader_destroy (reader);
457
458   for (v = 0; v < cmd->n_vars; ++v)
459     {
460       struct per_var_ws *pvw = &ws.vws[v];
461       gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
462       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
463
464       pvw->sst = gsl_matrix_get (cm, 0, 0);
465
466       reg_sweep (cm, 0);
467
468       pvw->sse = gsl_matrix_get (cm, 0, 0);
469
470       pvw->ssa = pvw->sst - pvw->sse;
471
472       pvw->n_groups = categoricals_total (cats);
473     }
474
475   postcalc (cmd);
476
477   if ( cmd->stats & STATS_HOMOGENEITY )
478     levene (dict, casereader_clone (input), cmd->indep_var,
479             cmd->n_vars, cmd->vars, cmd->exclude);
480
481   casereader_destroy (input);
482
483   ws.actual_number_of_groups = hsh_count (ws.group_hash);
484
485   if (!taint_has_tainted_successor (taint))
486     output_oneway (cmd, &ws);
487
488   taint_destroy (taint);
489 }
490
491 /* Pre calculations */
492 static void
493 precalc (const struct oneway_spec *cmd)
494 {
495   size_t i = 0;
496
497   for (i = 0; i < cmd->n_vars; ++i)
498     {
499       struct group_proc *gp = group_proc_get (cmd->vars[i]);
500       struct group_statistics *totals = &gp->ugs;
501
502       /* Create a hash for each of the dependent variables.
503          The hash contains a group_statistics structure,
504          and is keyed by value of the independent variable */
505
506       gp->group_hash = hsh_create (4, compare_group, hash_group,
507                                    (hsh_free_func *) free_group,
508                                    cmd->indep_var);
509
510       totals->sum = 0;
511       totals->n = 0;
512       totals->ssq = 0;
513       totals->sum_diff = 0;
514       totals->maximum = -DBL_MAX;
515       totals->minimum = DBL_MAX;
516     }
517 }
518
519 /* Post calculations for the ONEWAY command */
520 static void
521 postcalc (const struct oneway_spec *cmd)
522 {
523   size_t i = 0;
524
525   for (i = 0; i < cmd->n_vars; ++i)
526     {
527       struct group_proc *gp = group_proc_get (cmd->vars[i]);
528       struct hsh_table *group_hash = gp->group_hash;
529       struct group_statistics *totals = &gp->ugs;
530
531       struct hsh_iterator g;
532       struct group_statistics *gs;
533
534       for (gs =  hsh_first (group_hash, &g);
535            gs != 0;
536            gs = hsh_next (group_hash, &g))
537         {
538           gs->mean = gs->sum / gs->n;
539           gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
540
541           gs->std_dev = sqrt (
542                               gs->n / (gs->n - 1) *
543                               ( gs->ssq / gs->n - pow2 (gs->mean))
544                               );
545
546           gs->se_mean = gs->std_dev / sqrt (gs->n);
547           gs->mean_diff = gs->sum_diff / gs->n;
548         }
549
550       totals->mean = totals->sum / totals->n;
551       totals->std_dev = sqrt (
552                               totals->n / (totals->n - 1) *
553                               (totals->ssq / totals->n - pow2 (totals->mean))
554                               );
555
556       totals->se_mean = totals->std_dev / sqrt (totals->n);
557     }
558 }
559
560 static void show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws);
561 static void show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws);
562
563 static void
564 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
565 {
566   size_t i = 0;
567
568   /* Check the sanity of the given contrast values */
569   struct contrasts_node *coeff_list  = NULL;
570   ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
571     {
572       struct coeff_node *cn = NULL;
573       double sum = 0;
574       struct ll_list *cl = &coeff_list->coefficient_list;
575       ++i;
576
577       if (ll_count (cl) != ws->actual_number_of_groups)
578         {
579           msg (SW,
580                _("Number of contrast coefficients must equal the number of groups"));
581           coeff_list->bad_count = true;
582           continue;
583         }
584
585       ll_for_each (cn, struct coeff_node, ll, cl)
586         sum += cn->coeff;
587
588       if ( sum != 0.0 )
589         msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
590     }
591
592   if (cmd->stats & STATS_DESCRIPTIVES)
593     show_descriptives (cmd);
594
595   if (cmd->stats & STATS_HOMOGENEITY)
596     show_homogeneity (cmd);
597
598   show_anova_table (cmd, ws);
599
600
601   if (ll_count (&cmd->contrast_list) > 0)
602     {
603       show_contrast_coeffs (cmd, ws);
604       show_contrast_tests (cmd, ws);
605     }
606
607
608   /* Clean up */
609   for (i = 0; i < cmd->n_vars; ++i )
610     {
611       struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
612
613       hsh_destroy (group_hash);
614     }
615
616   hsh_destroy (ws->group_hash);
617 }
618
619
620 /* Show the ANOVA table */
621 static void
622 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
623 {
624   size_t i;
625   int n_cols =7;
626   size_t n_rows = cmd->n_vars * 3 + 1;
627
628   struct tab_table *t = tab_create (n_cols, n_rows);
629
630   tab_headers (t, 2, 0, 1, 0);
631
632   tab_box (t,
633            TAL_2, TAL_2,
634            -1, TAL_1,
635            0, 0,
636            n_cols - 1, n_rows - 1);
637
638   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
639   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
640   tab_vline (t, TAL_0, 1, 0, 0);
641
642   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
643   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
644   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
645   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
646   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
647
648
649   for (i = 0; i < cmd->n_vars; ++i)
650     {
651       const struct per_var_ws *pvw = &ws->vws[i];
652       struct group_proc *gp = group_proc_get (cmd->vars[i]);
653       const double df1 = pvw->n_groups - 1;
654       const double df2 = pvw->cc - pvw->n_groups;
655       const double msa = pvw->ssa / df1;
656
657       const char *s = var_to_string (cmd->vars[i]);
658
659       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
660       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
661       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
662       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
663
664       if (i > 0)
665         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
666
667
668       gp->mse  = (pvw->sst - pvw->ssa) / df2;
669
670       /* Sums of Squares */
671       tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
672       tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
673       tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
674
675
676       /* Degrees of freedom */
677       tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
678       tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
679       tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
680
681       /* Mean Squares */
682       tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
683       tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
684
685       {
686         const double F = msa / gp->mse ;
687
688         /* The F value */
689         tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
690
691         /* The significance */
692         tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
693       }
694     }
695
696   tab_title (t, _("ANOVA"));
697   tab_submit (t);
698 }
699
700
701 /* Show the descriptives table */
702 static void
703 show_descriptives (const struct oneway_spec *cmd)
704 {
705   size_t v;
706   int n_cols = 10;
707   struct tab_table *t;
708   int row;
709
710   const double confidence = 0.95;
711   const double q = (1.0 - confidence) / 2.0;
712
713   const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : & F_8_0;
714
715   int n_rows = 2;
716
717   for ( v = 0; v < cmd->n_vars; ++v )
718     n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
719
720   t = tab_create (n_cols, n_rows);
721   tab_headers (t, 2, 0, 2, 0);
722
723
724   /* Put a frame around the entire box, and vertical lines inside */
725   tab_box (t,
726            TAL_2, TAL_2,
727            -1, TAL_1,
728            0, 0,
729            n_cols - 1, n_rows - 1);
730
731   /* Underline headers */
732   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
733   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
734
735   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
736   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
737   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
738   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
739
740
741   tab_vline (t, TAL_0, 7, 0, 0);
742   tab_hline (t, TAL_1, 6, 7, 1);
743   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
744                          _("%g%% Confidence Interval for Mean"),
745                          confidence*100.0);
746
747   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
748   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
749
750   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
751   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
752
753
754   tab_title (t, _("Descriptives"));
755
756
757   row = 2;
758   for (v = 0; v < cmd->n_vars; ++v)
759     {
760       double T;
761       double std_error;
762
763       struct group_proc *gp = group_proc_get (cmd->vars[v]);
764
765       struct group_statistics *gs;
766       struct group_statistics *totals = &gp->ugs;
767
768       const char *s = var_to_string (cmd->vars[v]);
769       const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
770
771       struct group_statistics *const *gs_array =
772         (struct group_statistics *const *) hsh_sort (gp->group_hash);
773       int count = 0;
774
775       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
776       if ( v > 0)
777         tab_hline (t, TAL_1, 0, n_cols - 1, row);
778
779       for (count = 0; count < hsh_count (gp->group_hash); ++count)
780         {
781           struct string vstr;
782           ds_init_empty (&vstr);
783           gs = gs_array[count];
784
785           var_append_value_name (cmd->indep_var, &gs->id, &vstr);
786
787           tab_text (t, 1, row + count,
788                     TAB_LEFT | TAT_TITLE,
789                     ds_cstr (&vstr));
790
791           ds_destroy (&vstr);
792
793           /* Now fill in the numbers ... */
794
795           tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
796
797           tab_double (t, 3, row + count, 0, gs->mean, NULL);
798
799           tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
800
801           std_error = gs->std_dev / sqrt (gs->n) ;
802           tab_double (t, 5, row + count, 0,
803                       std_error, NULL);
804
805           /* Now the confidence interval */
806
807           T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
808
809           tab_double (t, 6, row + count, 0,
810                       gs->mean - T * std_error, NULL);
811
812           tab_double (t, 7, row + count, 0,
813                       gs->mean + T * std_error, NULL);
814
815           /* Min and Max */
816
817           tab_double (t, 8, row + count, 0,  gs->minimum, fmt);
818           tab_double (t, 9, row + count, 0,  gs->maximum, fmt);
819         }
820
821       tab_text (t, 1, row + count,
822                 TAB_LEFT | TAT_TITLE, _("Total"));
823
824       tab_double (t, 2, row + count, 0, totals->n, wfmt);
825
826       tab_double (t, 3, row + count, 0, totals->mean, NULL);
827
828       tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
829
830       std_error = totals->std_dev / sqrt (totals->n) ;
831
832       tab_double (t, 5, row + count, 0, std_error, NULL);
833
834       /* Now the confidence interval */
835
836       T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
837
838       tab_double (t, 6, row + count, 0,
839                   totals->mean - T * std_error, NULL);
840
841       tab_double (t, 7, row + count, 0,
842                   totals->mean + T * std_error, NULL);
843
844       /* Min and Max */
845
846       tab_double (t, 8, row + count, 0,  totals->minimum, fmt);
847       tab_double (t, 9, row + count, 0,  totals->maximum, fmt);
848
849       row += gp->n_groups + 1;
850     }
851
852   tab_submit (t);
853 }
854
855 /* Show the homogeneity table */
856 static void
857 show_homogeneity (const struct oneway_spec *cmd)
858 {
859   size_t v;
860   int n_cols = 5;
861   size_t n_rows = cmd->n_vars + 1;
862
863   struct tab_table *t;
864
865
866   t = tab_create (n_cols, n_rows);
867   tab_headers (t, 1, 0, 1, 0);
868
869
870   /* Put a frame around the entire box, and vertical lines inside */
871   tab_box (t,
872            TAL_2, TAL_2,
873            -1, TAL_1,
874            0, 0,
875            n_cols - 1, n_rows - 1);
876
877
878   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
879   tab_vline (t, TAL_2, 1, 0, n_rows - 1);
880
881
882   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
883   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
884   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
885   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
886
887   tab_title (t, _("Test of Homogeneity of Variances"));
888
889   for (v = 0; v < cmd->n_vars; ++v)
890     {
891       double F;
892       const struct variable *var = cmd->vars[v];
893       const struct group_proc *gp = group_proc_get (cmd->vars[v]);
894       const char *s = var_to_string (var);
895       const struct group_statistics *totals = &gp->ugs;
896
897       const double df1 = gp->n_groups - 1;
898       const double df2 = totals->n - gp->n_groups;
899
900       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
901
902       F = gp->levene;
903       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
904       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
905       tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
906
907       /* Now the significance */
908       tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
909     }
910
911   tab_submit (t);
912 }
913
914
915 /* Show the contrast coefficients table */
916 static void
917 show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
918 {
919   int c_num = 0;
920   struct ll *cli;
921
922   int n_contrasts = ll_count (&cmd->contrast_list);
923   int n_cols = 2 + ws->actual_number_of_groups;
924   int n_rows = 2 + n_contrasts;
925
926   struct tab_table *t;
927
928   const struct covariance *cov = ws->vws[0].cov ;
929
930   t = tab_create (n_cols, n_rows);
931   tab_headers (t, 2, 0, 2, 0);
932
933   /* Put a frame around the entire box, and vertical lines inside */
934   tab_box (t,
935            TAL_2, TAL_2,
936            -1, TAL_1,
937            0, 0,
938            n_cols - 1, n_rows - 1);
939
940   tab_box (t,
941            -1, -1,
942            TAL_0, TAL_0,
943            2, 0,
944            n_cols - 1, 0);
945
946   tab_box (t,
947            -1, -1,
948            TAL_0, TAL_0,
949            0, 0,
950            1, 1);
951
952   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
953   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
954
955   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
956
957   tab_title (t, _("Contrast Coefficients"));
958
959   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
960
961
962   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
963                   var_to_string (cmd->indep_var));
964
965   for ( cli = ll_head (&cmd->contrast_list);
966         cli != ll_null (&cmd->contrast_list);
967         cli = ll_next (cli))
968     {
969       int count = 0;
970       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
971       struct ll *coeffi ;
972
973       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
974
975       for (coeffi = ll_head (&cn->coefficient_list);
976            coeffi != ll_null (&cn->coefficient_list);
977            ++count, coeffi = ll_next (coeffi))
978         {
979           const struct categoricals *cats = covariance_get_categoricals (cov);
980           const union value *val = categoricals_get_value_by_subscript (cats, count);
981           struct string vstr;
982
983           ds_init_empty (&vstr);
984
985           var_append_value_name (cmd->indep_var, val, &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       ++c_num;
1001     }
1002
1003   tab_submit (t);
1004 }
1005
1006
1007 /* Show the results of the contrast tests */
1008 static void
1009 show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
1010 {
1011   int n_contrasts = ll_count (&cmd->contrast_list);
1012   size_t v;
1013   int n_cols = 8;
1014   size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1015
1016   struct tab_table *t;
1017
1018   t = tab_create (n_cols, n_rows);
1019   tab_headers (t, 3, 0, 1, 0);
1020
1021   /* Put a frame around the entire box, and vertical lines inside */
1022   tab_box (t,
1023            TAL_2, TAL_2,
1024            -1, TAL_1,
1025            0, 0,
1026            n_cols - 1, n_rows - 1);
1027
1028   tab_box (t,
1029            -1, -1,
1030            TAL_0, TAL_0,
1031            0, 0,
1032            2, 0);
1033
1034   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1035   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1036
1037
1038   tab_title (t, _("Contrast Tests"));
1039
1040   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1041   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1042   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1043   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1044   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1045   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1046
1047   for (v = 0; v < cmd->n_vars; ++v)
1048     {
1049       struct ll *cli;
1050       int i = 0;
1051       int lines_per_variable = 2 * n_contrasts;
1052
1053       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1054                 var_to_string (cmd->vars[v]));
1055
1056       for ( cli = ll_head (&cmd->contrast_list);
1057             cli != ll_null (&cmd->contrast_list);
1058             ++i, cli = ll_next (cli))
1059         {
1060           struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1061           struct ll *coeffi = ll_head (&cn->coefficient_list);
1062           int ci;
1063           double contrast_value = 0.0;
1064           double coef_msq = 0.0;
1065           struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1066           struct hsh_table *group_hash = grp_data->group_hash;
1067
1068           void *const *group_stat_array;
1069
1070           double T;
1071           double std_error_contrast;
1072           double df;
1073           double sec_vneq = 0.0;
1074
1075           /* Note: The calculation of the degrees of freedom in the
1076              "variances not equal" case is painfull!!
1077              The following formula may help to understand it:
1078              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1079              {
1080              \sum_{i=1}^k\left (
1081              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1082              \right)
1083              }
1084           */
1085
1086           double df_denominator = 0.0;
1087           double df_numerator = 0.0;
1088           if ( i == 0 )
1089             {
1090               tab_text (t,  1, (v * lines_per_variable) + i + 1,
1091                         TAB_LEFT | TAT_TITLE,
1092                         _("Assume equal variances"));
1093
1094               tab_text (t,  1, (v * lines_per_variable) + i + 1 + n_contrasts,
1095                         TAB_LEFT | TAT_TITLE,
1096                         _("Does not assume equal"));
1097             }
1098
1099           tab_text_format (t,  2, (v * lines_per_variable) + i + 1,
1100                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1101
1102
1103           tab_text_format (t,  2,
1104                            (v * lines_per_variable) + i + 1 + n_contrasts,
1105                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1106
1107           if (cn->bad_count)
1108             continue;
1109
1110           group_stat_array = hsh_sort (group_hash);
1111
1112           for (ci = 0;
1113                coeffi != ll_null (&cn->coefficient_list) && 
1114                  ci < hsh_count (group_hash);
1115                ++ci, coeffi = ll_next (coeffi))
1116             {
1117               struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1118               const double coef = cn->coeff; 
1119               struct group_statistics *gs = group_stat_array[ci];
1120
1121               const double winv = pow2 (gs->std_dev) / gs->n;
1122
1123               contrast_value += coef * gs->mean;
1124
1125               coef_msq += (coef * coef) / gs->n;
1126
1127               sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1128
1129               df_numerator += (coef * coef) * winv;
1130               df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1131             }
1132
1133           sec_vneq = sqrt (sec_vneq);
1134
1135           df_numerator = pow2 (df_numerator);
1136
1137           tab_double (t,  3, (v * lines_per_variable) + i + 1,
1138                       TAB_RIGHT, contrast_value, NULL);
1139
1140           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
1141                       n_contrasts,
1142                       TAB_RIGHT, contrast_value, NULL);
1143
1144           std_error_contrast = sqrt (grp_data->mse * coef_msq);
1145
1146           /* Std. Error */
1147           tab_double (t,  4, (v * lines_per_variable) + i + 1,
1148                       TAB_RIGHT, std_error_contrast,
1149                       NULL);
1150
1151           T = fabs (contrast_value / std_error_contrast);
1152
1153           /* T Statistic */
1154
1155           tab_double (t,  5, (v * lines_per_variable) + i + 1,
1156                       TAB_RIGHT, T,
1157                       NULL);
1158
1159           df = grp_data->ugs.n - grp_data->n_groups;
1160
1161           /* Degrees of Freedom */
1162           tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
1163                      TAB_RIGHT,  df,
1164                      8, 0);
1165
1166
1167           /* Significance TWO TAILED !!*/
1168           tab_double (t,  7, (v * lines_per_variable) + i + 1,
1169                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
1170                       NULL);
1171
1172           /* Now for the Variances NOT Equal case */
1173
1174           /* Std. Error */
1175           tab_double (t,  4,
1176                       (v * lines_per_variable) + i + 1 + n_contrasts,
1177                       TAB_RIGHT, sec_vneq,
1178                       NULL);
1179
1180           T = contrast_value / sec_vneq;
1181           tab_double (t,  5,
1182                       (v * lines_per_variable) + i + 1 + n_contrasts,
1183                       TAB_RIGHT, T,
1184                       NULL);
1185
1186           df = df_numerator / df_denominator;
1187
1188           tab_double (t,  6,
1189                       (v * lines_per_variable) + i + 1 + n_contrasts,
1190                       TAB_RIGHT, df,
1191                       NULL);
1192
1193           /* The Significance */
1194           tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1195                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
1196                       NULL);
1197         }
1198
1199       if ( v > 0 )
1200         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1201     }
1202
1203   tab_submit (t);
1204 }
1205
1206