97088ebf961861780646a035617df276b9945fba
[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 *, const struct oneway_workspace *);
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, ws);
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, const struct oneway_workspace *ws)
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   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
882   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
883   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
884   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
885
886   tab_title (t, _("Test of Homogeneity of Variances"));
887
888   for (v = 0; v < cmd->n_vars; ++v)
889     {
890       struct per_var_ws *pvw = &ws->vws[v];
891       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
892
893       const struct variable *var = cmd->vars[v];
894       const struct group_proc *gp = group_proc_get (cmd->vars[v]);
895       const char *s = var_to_string (var);
896
897       const double df1 = pvw->n_groups - 1;
898       const double df2 = pvw->cc - pvw->n_groups;
899       double F = gp->levene;
900
901       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
902
903
904       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
905       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
906       tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
907
908       /* Now the significance */
909       tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
910     }
911
912   tab_submit (t);
913 }
914
915
916 /* Show the contrast coefficients table */
917 static void
918 show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
919 {
920   int c_num = 0;
921   struct ll *cli;
922
923   int n_contrasts = ll_count (&cmd->contrast_list);
924   int n_cols = 2 + ws->actual_number_of_groups;
925   int n_rows = 2 + n_contrasts;
926
927   struct tab_table *t;
928
929   const struct covariance *cov = ws->vws[0].cov ;
930
931   t = tab_create (n_cols, n_rows);
932   tab_headers (t, 2, 0, 2, 0);
933
934   /* Put a frame around the entire box, and vertical lines inside */
935   tab_box (t,
936            TAL_2, TAL_2,
937            -1, TAL_1,
938            0, 0,
939            n_cols - 1, n_rows - 1);
940
941   tab_box (t,
942            -1, -1,
943            TAL_0, TAL_0,
944            2, 0,
945            n_cols - 1, 0);
946
947   tab_box (t,
948            -1, -1,
949            TAL_0, TAL_0,
950            0, 0,
951            1, 1);
952
953   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
954   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
955
956   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
957
958   tab_title (t, _("Contrast Coefficients"));
959
960   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
961
962
963   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
964                   var_to_string (cmd->indep_var));
965
966   for ( cli = ll_head (&cmd->contrast_list);
967         cli != ll_null (&cmd->contrast_list);
968         cli = ll_next (cli))
969     {
970       int count = 0;
971       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
972       struct ll *coeffi ;
973
974       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
975
976       for (coeffi = ll_head (&cn->coefficient_list);
977            coeffi != ll_null (&cn->coefficient_list);
978            ++count, coeffi = ll_next (coeffi))
979         {
980           const struct categoricals *cats = covariance_get_categoricals (cov);
981           const union value *val = categoricals_get_value_by_subscript (cats, count);
982           struct string vstr;
983
984           ds_init_empty (&vstr);
985
986           var_append_value_name (cmd->indep_var, val, &vstr);
987
988           tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
989
990           ds_destroy (&vstr);
991
992           if (cn->bad_count)
993             tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
994           else
995             {
996               struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
997
998               tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
999             }
1000         }
1001       ++c_num;
1002     }
1003
1004   tab_submit (t);
1005 }
1006
1007
1008 /* Show the results of the contrast tests */
1009 static void
1010 show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
1011 {
1012   int n_contrasts = ll_count (&cmd->contrast_list);
1013   size_t v;
1014   int n_cols = 8;
1015   size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1016
1017   struct tab_table *t;
1018
1019   t = tab_create (n_cols, n_rows);
1020   tab_headers (t, 3, 0, 1, 0);
1021
1022   /* Put a frame around the entire box, and vertical lines inside */
1023   tab_box (t,
1024            TAL_2, TAL_2,
1025            -1, TAL_1,
1026            0, 0,
1027            n_cols - 1, n_rows - 1);
1028
1029   tab_box (t,
1030            -1, -1,
1031            TAL_0, TAL_0,
1032            0, 0,
1033            2, 0);
1034
1035   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1036   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1037
1038
1039   tab_title (t, _("Contrast Tests"));
1040
1041   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1042   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1043   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1044   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1045   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1046   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1047
1048   for (v = 0; v < cmd->n_vars; ++v)
1049     {
1050       struct ll *cli;
1051       int i = 0;
1052       int lines_per_variable = 2 * n_contrasts;
1053
1054       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1055                 var_to_string (cmd->vars[v]));
1056
1057       for ( cli = ll_head (&cmd->contrast_list);
1058             cli != ll_null (&cmd->contrast_list);
1059             ++i, cli = ll_next (cli))
1060         {
1061           struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1062           struct ll *coeffi = ll_head (&cn->coefficient_list);
1063           int ci;
1064           double contrast_value = 0.0;
1065           double coef_msq = 0.0;
1066           struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1067           struct hsh_table *group_hash = grp_data->group_hash;
1068
1069           void *const *group_stat_array;
1070
1071           double T;
1072           double std_error_contrast;
1073           double df;
1074           double sec_vneq = 0.0;
1075
1076           /* Note: The calculation of the degrees of freedom in the
1077              "variances not equal" case is painfull!!
1078              The following formula may help to understand it:
1079              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1080              {
1081              \sum_{i=1}^k\left (
1082              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
1083              \right)
1084              }
1085           */
1086
1087           double df_denominator = 0.0;
1088           double df_numerator = 0.0;
1089           if ( i == 0 )
1090             {
1091               tab_text (t,  1, (v * lines_per_variable) + i + 1,
1092                         TAB_LEFT | TAT_TITLE,
1093                         _("Assume equal variances"));
1094
1095               tab_text (t,  1, (v * lines_per_variable) + i + 1 + n_contrasts,
1096                         TAB_LEFT | TAT_TITLE,
1097                         _("Does not assume equal"));
1098             }
1099
1100           tab_text_format (t,  2, (v * lines_per_variable) + i + 1,
1101                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1102
1103
1104           tab_text_format (t,  2,
1105                            (v * lines_per_variable) + i + 1 + n_contrasts,
1106                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
1107
1108           if (cn->bad_count)
1109             continue;
1110
1111           group_stat_array = hsh_sort (group_hash);
1112
1113           for (ci = 0;
1114                coeffi != ll_null (&cn->coefficient_list) && 
1115                  ci < hsh_count (group_hash);
1116                ++ci, coeffi = ll_next (coeffi))
1117             {
1118               struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1119               const double coef = cn->coeff; 
1120               struct group_statistics *gs = group_stat_array[ci];
1121
1122               const double winv = pow2 (gs->std_dev) / gs->n;
1123
1124               contrast_value += coef * gs->mean;
1125
1126               coef_msq += (coef * coef) / gs->n;
1127
1128               sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1129
1130               df_numerator += (coef * coef) * winv;
1131               df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1132             }
1133
1134           sec_vneq = sqrt (sec_vneq);
1135
1136           df_numerator = pow2 (df_numerator);
1137
1138           tab_double (t,  3, (v * lines_per_variable) + i + 1,
1139                       TAB_RIGHT, contrast_value, NULL);
1140
1141           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
1142                       n_contrasts,
1143                       TAB_RIGHT, contrast_value, NULL);
1144
1145           std_error_contrast = sqrt (grp_data->mse * coef_msq);
1146
1147           /* Std. Error */
1148           tab_double (t,  4, (v * lines_per_variable) + i + 1,
1149                       TAB_RIGHT, std_error_contrast,
1150                       NULL);
1151
1152           T = fabs (contrast_value / std_error_contrast);
1153
1154           /* T Statistic */
1155
1156           tab_double (t,  5, (v * lines_per_variable) + i + 1,
1157                       TAB_RIGHT, T,
1158                       NULL);
1159
1160           df = grp_data->ugs.n - grp_data->n_groups;
1161
1162           /* Degrees of Freedom */
1163           tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
1164                      TAB_RIGHT,  df,
1165                      8, 0);
1166
1167
1168           /* Significance TWO TAILED !!*/
1169           tab_double (t,  7, (v * lines_per_variable) + i + 1,
1170                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
1171                       NULL);
1172
1173           /* Now for the Variances NOT Equal case */
1174
1175           /* Std. Error */
1176           tab_double (t,  4,
1177                       (v * lines_per_variable) + i + 1 + n_contrasts,
1178                       TAB_RIGHT, sec_vneq,
1179                       NULL);
1180
1181           T = contrast_value / sec_vneq;
1182           tab_double (t,  5,
1183                       (v * lines_per_variable) + i + 1 + n_contrasts,
1184                       TAB_RIGHT, T,
1185                       NULL);
1186
1187           df = df_numerator / df_denominator;
1188
1189           tab_double (t,  6,
1190                       (v * lines_per_variable) + i + 1 + n_contrasts,
1191                       TAB_RIGHT, df,
1192                       NULL);
1193
1194           /* The Significance */
1195           tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1196                       TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
1197                       NULL);
1198         }
1199
1200       if ( v > 0 )
1201         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1202     }
1203
1204   tab_submit (t);
1205 }
1206
1207