Added a cell-padding parameter to the sheet.
[pspp-builds.git] / src / language / stats / oneway.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009 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 <gsl/gsl_cdf.h>
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <data/case.h>
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/dictionary/split-file.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/str.h>
39 #include <libpspp/taint.h>
40 #include <math/group-proc.h>
41 #include <math/group.h>
42 #include <math/levene.h>
43 #include <output/manager.h>
44 #include <output/table.h>
45 #include "sort-criteria.h"
46
47 #include "xalloc.h"
48
49 #include "gettext.h"
50 #define _(msgid) gettext (msgid)
51
52 /* (headers) */
53
54 /* (specification)
55    "ONEWAY" (oneway_):
56    *^variables=custom;
57    missing=miss:!analysis/listwise,
58    incl:include/!exclude;
59    +contrast= double list;
60    +statistics[st_]=descriptives,homogeneity.
61 */
62 /* (declarations) */
63 /* (functions) */
64
65 static struct cmd_oneway cmd;
66
67 /* The independent variable */
68 static const struct variable *indep_var;
69
70 /* Number of dependent variables */
71 static size_t n_vars;
72
73 /* The dependent variables */
74 static const struct variable **vars;
75
76
77 /* A  hash table containing all the distinct values of the independent
78    variables */
79 static struct hsh_table *global_group_hash;
80
81 /* The number of distinct values of the independent variable, when all
82    missing values are disregarded */
83 static int ostensible_number_of_groups = -1;
84
85
86 static void run_oneway (struct cmd_oneway *, struct casereader *,
87                         const struct dataset *);
88
89
90 /* Routines to show the output tables */
91 static void show_anova_table (void);
92 static void show_descriptives (void);
93 static void show_homogeneity (void);
94
95 static void show_contrast_coeffs (short *);
96 static void show_contrast_tests (short *);
97
98
99 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
100
101 static enum stat_table_t stat_tables;
102
103 void output_oneway (void);
104
105
106 int
107 cmd_oneway (struct lexer *lexer, struct dataset *ds)
108 {
109   struct casegrouper *grouper;
110   struct casereader *group;
111   int i;
112   bool ok;
113
114   if ( !parse_oneway (lexer, ds, &cmd, NULL))
115     return CMD_FAILURE;
116
117   /* What statistics were requested */
118   if ( cmd.sbc_statistics)
119     {
120
121       for (i = 0; i < ONEWAY_ST_count; ++i)
122         {
123           if (! cmd.a_statistics[i]) continue;
124
125           switch (i) 
126             {
127             case ONEWAY_ST_DESCRIPTIVES:
128               stat_tables |= STAT_DESC;
129               break;
130             case ONEWAY_ST_HOMOGENEITY:
131               stat_tables |= STAT_HOMO;
132               break;
133             }
134         }
135     }
136
137   /* Data pass.  FIXME: error handling. */
138   grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
139   while (casegrouper_get_next_group (grouper, &group))
140     run_oneway (&cmd, group, ds);
141   ok = casegrouper_destroy (grouper);
142   ok = proc_commit (ds) && ok;
143
144   free (vars);
145   free_oneway (&cmd);
146
147   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
148 }
149
150
151 void
152 output_oneway (void)
153 {
154   size_t i;
155   short *bad_contrast;
156
157   bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
158
159   /* Check the sanity of the given contrast values */
160   for (i = 0; i < cmd.sbc_contrast; ++i)
161     {
162       int j;
163       double sum = 0;
164
165       bad_contrast[i] = 0;
166       if (subc_list_double_count (&cmd.dl_contrast[i]) !=
167           ostensible_number_of_groups)
168         {
169           msg (SW,
170                _("Number of contrast coefficients must equal the number of groups"));
171           bad_contrast[i] = 1;
172           continue;
173         }
174
175       for (j = 0; j < ostensible_number_of_groups; ++j)
176         sum += subc_list_double_at (&cmd.dl_contrast[i], j);
177
178       if ( sum != 0.0 )
179         msg (SW, _("Coefficients for contrast %zu do not total zero"), i + 1);
180     }
181
182   if ( stat_tables & STAT_DESC )
183     show_descriptives ();
184
185   if ( stat_tables & STAT_HOMO )
186     show_homogeneity ();
187
188   show_anova_table ();
189
190   if (cmd.sbc_contrast )
191     {
192       show_contrast_coeffs (bad_contrast);
193       show_contrast_tests (bad_contrast);
194     }
195
196   free (bad_contrast);
197
198   /* Clean up */
199   for (i = 0; i < n_vars; ++i )
200     {
201       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
202
203       hsh_destroy (group_hash);
204     }
205
206   hsh_destroy (global_group_hash);
207 }
208
209
210 /* Parser for the variables sub command */
211 static int
212 oneway_custom_variables (struct lexer *lexer,
213                          struct dataset *ds, struct cmd_oneway *cmd UNUSED,
214                          void *aux UNUSED)
215 {
216   struct dictionary *dict = dataset_dict (ds);
217
218   lex_match (lexer, '=');
219
220   if ((lex_token (lexer) != T_ID ||
221        dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
222       && lex_token (lexer) != T_ALL)
223     return 2;
224
225   if (!parse_variables_const (lexer, dict, &vars, &n_vars,
226                               PV_DUPLICATE
227                               | PV_NUMERIC | PV_NO_SCRATCH) )
228     {
229       free (vars);
230       return 0;
231     }
232
233   assert (n_vars);
234
235   if ( ! lex_match (lexer, T_BY))
236     return 2;
237
238   indep_var = parse_variable (lexer, dict);
239
240   if ( !indep_var )
241     {
242       msg (SE, _("`%s' is not a variable name"), lex_tokid (lexer));
243       return 0;
244     }
245
246   return 1;
247 }
248
249
250 /* Show the ANOVA table */
251 static void
252 show_anova_table (void)
253 {
254   size_t i;
255   int n_cols =7;
256   size_t n_rows = n_vars * 3 + 1;
257
258   struct tab_table *t;
259
260
261   t = tab_create (n_cols, n_rows, 0);
262   tab_headers (t, 2, 0, 1, 0);
263   tab_dim (t, tab_natural_dimensions);
264
265
266   tab_box (t,
267            TAL_2, TAL_2,
268            -1, TAL_1,
269            0, 0,
270            n_cols - 1, n_rows - 1);
271
272   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
273   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
274   tab_vline (t, TAL_0, 1, 0, 0);
275
276   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
277   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
278   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
279   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
280   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
281
282
283   for (i = 0; i < n_vars; ++i)
284     {
285       struct group_statistics *totals = &group_proc_get (vars[i])->ugs;
286       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
287       struct hsh_iterator g;
288       struct group_statistics *gs;
289       double ssa = 0;
290       const char *s = var_to_string (vars[i]);
291
292       for (gs =  hsh_first (group_hash, &g);
293            gs != 0;
294            gs = hsh_next (group_hash, &g))
295         {
296           ssa += pow2 (gs->sum) / gs->n;
297         }
298
299       ssa -= pow2 (totals->sum) / totals->n;
300
301       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
302       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
303       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
304       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
305
306       if (i > 0)
307         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
308
309       {
310         struct group_proc *gp = group_proc_get (vars[i]);
311         const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
312         const double df1 = gp->n_groups - 1;
313         const double df2 = totals->n - gp->n_groups;
314         const double msa = ssa / df1;
315
316         gp->mse  = (sst - ssa) / df2;
317
318
319         /* Sums of Squares */
320         tab_float (t, 2, i * 3 + 1, 0, ssa, 10, 2);
321         tab_float (t, 2, i * 3 + 3, 0, sst, 10, 2);
322         tab_float (t, 2, i * 3 + 2, 0, sst - ssa, 10, 2);
323
324
325         /* Degrees of freedom */
326         tab_float (t, 3, i * 3 + 1, 0, df1, 4, 0);
327         tab_float (t, 3, i * 3 + 2, 0, df2, 4, 0);
328         tab_float (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
329
330         /* Mean Squares */
331         tab_float (t, 4, i * 3 + 1, TAB_RIGHT, msa, 8, 3);
332         tab_float (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, 8, 3);
333
334         {
335           const double F = msa/gp->mse;
336
337           /* The F value */
338           tab_float (t, 5, i * 3 + 1, 0,  F, 8, 3);
339
340           /* The significance */
341           tab_float (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), 8, 3);
342         }
343       }
344     }
345
346
347   tab_title (t, _("ANOVA"));
348   tab_submit (t);
349 }
350
351
352 /* Show the descriptives table */
353 static void
354 show_descriptives (void)
355 {
356   size_t v;
357   int n_cols =10;
358   struct tab_table *t;
359   int row;
360
361   const double confidence = 0.95;
362   const double q = (1.0 - confidence) / 2.0;
363
364
365   int n_rows = 2;
366
367   for ( v = 0; v < n_vars; ++v )
368     n_rows += group_proc_get (vars[v])->n_groups + 1;
369
370   t = tab_create (n_cols, n_rows, 0);
371   tab_headers (t, 2, 0, 2, 0);
372   tab_dim (t, tab_natural_dimensions);
373
374
375   /* Put a frame around the entire box, and vertical lines inside */
376   tab_box (t,
377            TAL_2, TAL_2,
378            -1, TAL_1,
379            0, 0,
380            n_cols - 1, n_rows - 1);
381
382   /* Underline headers */
383   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
384   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
385
386   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
387   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
388   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
389   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
390
391
392   tab_vline (t, TAL_0, 7, 0, 0);
393   tab_hline (t, TAL_1, 6, 7, 1);
394   tab_joint_text (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
395                   _("%g%% Confidence Interval for Mean"), confidence*100.0);
396
397   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
398   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
399
400   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
401   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
402
403
404   tab_title (t, _("Descriptives"));
405
406
407   row = 2;
408   for (v = 0; v < n_vars; ++v)
409     {
410       double T;
411       double std_error;
412
413       struct group_proc *gp = group_proc_get (vars[v]);
414
415       struct group_statistics *gs;
416       struct group_statistics *totals = &gp->ugs;
417
418       const char *s = var_to_string (vars[v]);
419
420       struct group_statistics *const *gs_array =
421         (struct group_statistics *const *) hsh_sort (gp->group_hash);
422       int count = 0;
423
424       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
425       if ( v > 0)
426         tab_hline (t, TAL_1, 0, n_cols - 1, row);
427
428       for (count = 0; count < hsh_count (gp->group_hash); ++count)
429         {
430           struct string vstr;
431           ds_init_empty (&vstr);
432           gs = gs_array[count];
433
434           var_append_value_name (indep_var, &gs->id, &vstr);
435
436           tab_text (t, 1, row + count,
437                     TAB_LEFT | TAT_TITLE,
438                     ds_cstr (&vstr));
439
440           ds_destroy (&vstr);
441
442           /* Now fill in the numbers ... */
443
444           tab_float (t, 2, row + count, 0, gs->n, 8, 0);
445
446           tab_float (t, 3, row + count, 0, gs->mean, 8, 2);
447
448           tab_float (t, 4, row + count, 0, gs->std_dev, 8, 2);
449
450           std_error = gs->std_dev/sqrt (gs->n);
451           tab_float (t, 5, row + count, 0,
452                      std_error, 8, 2);
453
454           /* Now the confidence interval */
455
456           T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
457
458           tab_float (t, 6, row + count, 0,
459                      gs->mean - T * std_error, 8, 2);
460
461           tab_float (t, 7, row + count, 0,
462                      gs->mean + T * std_error, 8, 2);
463
464           /* Min and Max */
465           tab_float (t, 8, row + count, 0,  gs->minimum, 8, 2);
466           tab_float (t, 9, row + count, 0,  gs->maximum, 8, 2);
467         }
468
469       tab_text (t, 1, row + count,
470                 TAB_LEFT | TAT_TITLE, _("Total"));
471
472       tab_float (t, 2, row + count, 0, totals->n, 8, 0);
473
474       tab_float (t, 3, row + count, 0, totals->mean, 8, 2);
475
476       tab_float (t, 4, row + count, 0, totals->std_dev, 8, 2);
477
478       std_error = totals->std_dev/sqrt (totals->n);
479
480       tab_float (t, 5, row + count, 0, std_error, 8, 2);
481
482       /* Now the confidence interval */
483
484       T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
485
486       tab_float (t, 6, row + count, 0,
487                  totals->mean - T * std_error, 8, 2);
488
489       tab_float (t, 7, row + count, 0,
490                  totals->mean + T * std_error, 8, 2);
491
492       /* Min and Max */
493       tab_float (t, 8, row + count, 0,  totals->minimum, 8, 2);
494       tab_float (t, 9, row + count, 0,  totals->maximum, 8, 2);
495
496       row += gp->n_groups + 1;
497     }
498
499   tab_submit (t);
500 }
501
502 /* Show the homogeneity table */
503 static void
504 show_homogeneity (void)
505 {
506   size_t v;
507   int n_cols = 5;
508   size_t n_rows = n_vars + 1;
509
510   struct tab_table *t;
511
512
513   t = tab_create (n_cols, n_rows, 0);
514   tab_headers (t, 1, 0, 1, 0);
515   tab_dim (t, tab_natural_dimensions);
516
517   /* Put a frame around the entire box, and vertical lines inside */
518   tab_box (t,
519            TAL_2, TAL_2,
520            -1, TAL_1,
521            0, 0,
522            n_cols - 1, n_rows - 1);
523
524
525   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
526   tab_vline (t, TAL_2, 1, 0, n_rows - 1);
527
528
529   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
530   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
531   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
532   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
533
534   tab_title (t, _("Test of Homogeneity of Variances"));
535
536   for (v = 0; v < n_vars; ++v)
537     {
538       double F;
539       const struct variable *var = vars[v];
540       const struct group_proc *gp = group_proc_get (vars[v]);
541       const char *s = var_to_string (var);
542       const struct group_statistics *totals = &gp->ugs;
543
544       const double df1 = gp->n_groups - 1;
545       const double df2 = totals->n - gp->n_groups;
546
547       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
548
549       F = gp->levene;
550       tab_float (t, 1, v + 1, TAB_RIGHT, F, 8, 3);
551       tab_float (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
552       tab_float (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
553
554       /* Now the significance */
555       tab_float (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), 8, 3);
556     }
557
558   tab_submit (t);
559 }
560
561
562 /* Show the contrast coefficients table */
563 static void
564 show_contrast_coeffs (short *bad_contrast)
565 {
566   int n_cols = 2 + ostensible_number_of_groups;
567   int n_rows = 2 + cmd.sbc_contrast;
568   union value *group_value;
569   int count = 0;
570   void *const *group_values;
571
572   struct tab_table *t;
573
574   t = tab_create (n_cols, n_rows, 0);
575   tab_headers (t, 2, 0, 2, 0);
576   tab_dim (t, tab_natural_dimensions);
577
578   /* Put a frame around the entire box, and vertical lines inside */
579   tab_box (t,
580            TAL_2, TAL_2,
581            -1, TAL_1,
582            0, 0,
583            n_cols - 1, n_rows - 1);
584
585   tab_box (t,
586            -1, -1,
587            TAL_0, TAL_0,
588            2, 0,
589            n_cols - 1, 0);
590
591   tab_box (t,
592            -1, -1,
593            TAL_0, TAL_0,
594            0, 0,
595            1, 1);
596
597   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
598   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
599
600   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
601
602   tab_title (t, _("Contrast Coefficients"));
603
604   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
605
606
607   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
608                   var_to_string (indep_var));
609
610   group_values = hsh_sort (global_group_hash);
611   for (count = 0;
612        count < hsh_count (global_group_hash);
613        ++count)
614     {
615       int i;
616       struct string vstr;
617       group_value = group_values[count];
618
619       ds_init_empty (&vstr);
620
621       var_append_value_name (indep_var, group_value, &vstr);
622
623       tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
624                 ds_cstr (&vstr));
625
626       ds_destroy (&vstr);
627
628
629       for (i = 0; i < cmd.sbc_contrast; ++i )
630         {
631           tab_text (t, 1, i + 2, TAB_CENTER | TAT_PRINTF, "%d", i + 1);
632
633           if ( bad_contrast[i] )
634             tab_text (t, count + 2, i + 2, TAB_RIGHT, "?" );
635           else
636             tab_text (t, count + 2, i + 2, TAB_RIGHT | TAT_PRINTF, "%g",
637                       subc_list_double_at (&cmd.dl_contrast[i], count)
638                       );
639         }
640     }
641
642   tab_submit (t);
643 }
644
645
646 /* Show the results of the contrast tests */
647 static void
648 show_contrast_tests (short *bad_contrast)
649 {
650   size_t v;
651   int n_cols = 8;
652   size_t n_rows = 1 + n_vars * 2 * cmd.sbc_contrast;
653
654   struct tab_table *t;
655
656   t = tab_create (n_cols, n_rows, 0);
657   tab_headers (t, 3, 0, 1, 0);
658   tab_dim (t, tab_natural_dimensions);
659
660   /* Put a frame around the entire box, and vertical lines inside */
661   tab_box (t,
662            TAL_2, TAL_2,
663            -1, TAL_1,
664            0, 0,
665            n_cols - 1, n_rows - 1);
666
667   tab_box (t,
668            -1, -1,
669            TAL_0, TAL_0,
670            0, 0,
671            2, 0);
672
673   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
674   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
675
676
677   tab_title (t, _("Contrast Tests"));
678
679   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
680   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
681   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
682   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
683   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
684   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
685
686   for (v = 0; v < n_vars; ++v)
687     {
688       int i;
689       int lines_per_variable = 2 * cmd.sbc_contrast;
690
691
692       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
693                 var_to_string (vars[v]));
694
695       for (i = 0; i < cmd.sbc_contrast; ++i)
696         {
697           int ci;
698           double contrast_value = 0.0;
699           double coef_msq = 0.0;
700           struct group_proc *grp_data = group_proc_get (vars[v]);
701           struct hsh_table *group_hash = grp_data->group_hash;
702
703           void *const *group_stat_array;
704
705           double T;
706           double std_error_contrast;
707           double df;
708           double sec_vneq = 0.0;
709
710
711           /* Note: The calculation of the degrees of freedom in the
712              "variances not equal" case is painfull!!
713              The following formula may help to understand it:
714              \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
715              {
716              \sum_{i=1}^k\left (
717              \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
718              \right)
719              }
720           */
721
722           double df_denominator = 0.0;
723           double df_numerator = 0.0;
724           if ( i == 0 )
725             {
726               tab_text (t,  1, (v * lines_per_variable) + i + 1,
727                         TAB_LEFT | TAT_TITLE,
728                         _("Assume equal variances"));
729
730               tab_text (t,  1, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
731                         TAB_LEFT | TAT_TITLE,
732                         _("Does not assume equal"));
733             }
734
735           tab_text (t,  2, (v * lines_per_variable) + i + 1,
736                     TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d", i + 1);
737
738
739           tab_text (t,  2, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
740                     TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d", i + 1);
741
742
743           if ( bad_contrast[i])
744             continue;
745
746           group_stat_array = hsh_sort (group_hash);
747
748           for (ci = 0; ci < hsh_count (group_hash);  ++ci)
749             {
750               const double coef = subc_list_double_at (&cmd.dl_contrast[i], ci);
751               struct group_statistics *gs = group_stat_array[ci];
752
753               const double winv = pow2 (gs->std_dev) / gs->n;
754
755               contrast_value += coef * gs->mean;
756
757               coef_msq += (coef * coef) / gs->n;
758
759               sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
760
761               df_numerator += (coef * coef) * winv;
762               df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
763             }
764           sec_vneq = sqrt (sec_vneq);
765
766           df_numerator = pow2(df_numerator);
767
768           tab_float (t,  3, (v * lines_per_variable) + i + 1,
769                      TAB_RIGHT, contrast_value, 8, 2);
770
771           tab_float (t,  3, (v * lines_per_variable) + i + 1 +
772                      cmd.sbc_contrast,
773                      TAB_RIGHT, contrast_value, 8, 2);
774
775           std_error_contrast = sqrt (grp_data->mse * coef_msq);
776
777           /* Std. Error */
778           tab_float (t,  4, (v * lines_per_variable) + i + 1,
779                      TAB_RIGHT, std_error_contrast,
780                      8, 3);
781
782           T = fabs (contrast_value / std_error_contrast);
783
784           /* T Statistic */
785
786           tab_float (t,  5, (v * lines_per_variable) + i + 1,
787                      TAB_RIGHT, T,
788                      8, 3);
789
790           df = grp_data->ugs.n - grp_data->n_groups;
791
792           /* Degrees of Freedom */
793           tab_float (t,  6, (v * lines_per_variable) + i + 1,
794                      TAB_RIGHT,  df,
795                      8, 0);
796
797
798           /* Significance TWO TAILED !!*/
799           tab_float (t,  7, (v * lines_per_variable) + i + 1,
800                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
801                      8, 3);
802
803
804           /* Now for the Variances NOT Equal case */
805
806           /* Std. Error */
807           tab_float (t,  4,
808                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
809                      TAB_RIGHT, sec_vneq,
810                      8, 3);
811
812
813           T = contrast_value / sec_vneq;
814           tab_float (t,  5,
815                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
816                      TAB_RIGHT, T,
817                      8, 3);
818
819
820           df = df_numerator / df_denominator;
821
822           tab_float (t,  6,
823                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
824                      TAB_RIGHT, df,
825                      8, 3);
826
827           /* The Significance */
828
829           tab_float (t, 7, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
830                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
831                      8, 3);
832
833
834         }
835
836       if ( v > 0 )
837         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
838     }
839
840   tab_submit (t);
841
842 }
843
844
845 /* ONEWAY ANOVA Calculations */
846
847 static void  postcalc (struct cmd_oneway *cmd UNUSED);
848
849 static void  precalc (struct cmd_oneway *cmd UNUSED);
850
851
852
853 /* Pre calculations */
854 static void
855 precalc (struct cmd_oneway *cmd UNUSED)
856 {
857   size_t i = 0;
858
859   for (i = 0; i < n_vars; ++i)
860     {
861       struct group_proc *gp = group_proc_get (vars[i]);
862       struct group_statistics *totals = &gp->ugs;
863
864       /* Create a hash for each of the dependent variables.
865          The hash contains a group_statistics structure,
866          and is keyed by value of the independent variable */
867
868       gp->group_hash = hsh_create (4, compare_group, hash_group,
869                                    (hsh_free_func *) free_group,
870                                    indep_var);
871
872       totals->sum = 0;
873       totals->n = 0;
874       totals->ssq = 0;
875       totals->sum_diff = 0;
876       totals->maximum = -DBL_MAX;
877       totals->minimum = DBL_MAX;
878     }
879 }
880
881 static void
882 free_value (void *value_, const void *aux UNUSED)
883 {
884   union value *value = value_;
885   free (value);
886 }
887
888 static void
889 run_oneway (struct cmd_oneway *cmd,
890             struct casereader *input,
891             const struct dataset *ds)
892 {
893   struct taint *taint;
894   struct dictionary *dict = dataset_dict (ds);
895   enum mv_class exclude;
896   struct casereader *reader;
897   struct ccase *c;
898
899   c = casereader_peek (input, 0);
900   if (c == NULL)
901     {
902       casereader_destroy (input);
903       return;
904     }
905   output_split_file_values (ds, c);
906   case_unref (c);
907
908   taint = taint_clone (casereader_get_taint (input));
909
910   global_group_hash = hsh_create (4,
911                                   compare_values_short,
912                                   hash_value_short,
913                                   free_value,
914                                   indep_var);
915
916   precalc (cmd);
917
918   exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
919   input = casereader_create_filter_missing (input, &indep_var, 1,
920                                             exclude, NULL, NULL);
921   if (cmd->miss == ONEWAY_LISTWISE)
922     input = casereader_create_filter_missing (input, vars, n_vars,
923                                               exclude, NULL, NULL);
924   input = casereader_create_filter_weight (input, dict, NULL, NULL);
925
926   reader = casereader_clone (input);
927   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
928     {
929       size_t i;
930
931       const double weight = dict_get_case_weight (dict, c, NULL);
932
933       const union value *indep_val = case_data (c, indep_var);
934       void **p = hsh_probe (global_group_hash, indep_val);
935       if (*p == NULL)
936         *p = value_dup (indep_val, var_get_width (indep_var));
937
938       for (i = 0; i < n_vars; ++i)
939         {
940           const struct variable *v = vars[i];
941
942           const union value *val = case_data (c, v);
943
944           struct group_proc *gp = group_proc_get (vars[i]);
945           struct hsh_table *group_hash = gp->group_hash;
946
947           struct group_statistics *gs;
948
949           gs = hsh_find (group_hash, indep_val );
950
951           if ( ! gs )
952             {
953               gs = xmalloc (sizeof *gs);
954               gs->id = *indep_val;
955               gs->sum = 0;
956               gs->n = 0;
957               gs->ssq = 0;
958               gs->sum_diff = 0;
959               gs->minimum = DBL_MAX;
960               gs->maximum = -DBL_MAX;
961
962               hsh_insert ( group_hash, gs );
963             }
964
965           if (!var_is_value_missing (v, val, exclude))
966             {
967               struct group_statistics *totals = &gp->ugs;
968
969               totals->n += weight;
970               totals->sum += weight * val->f;
971               totals->ssq += weight * pow2 (val->f);
972
973               if ( val->f * weight  < totals->minimum )
974                 totals->minimum = val->f * weight;
975
976               if ( val->f * weight  > totals->maximum )
977                 totals->maximum = val->f * weight;
978
979               gs->n += weight;
980               gs->sum += weight * val->f;
981               gs->ssq += weight * pow2 (val->f);
982
983               if ( val->f * weight  < gs->minimum )
984                 gs->minimum = val->f * weight;
985
986               if ( val->f * weight  > gs->maximum )
987                 gs->maximum = val->f * weight;
988             }
989
990           gp->n_groups = hsh_count ( group_hash );
991         }
992
993     }
994   casereader_destroy (reader);
995
996   postcalc (cmd);
997
998
999   if ( stat_tables & STAT_HOMO )
1000     levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
1001
1002   casereader_destroy (input);
1003
1004   ostensible_number_of_groups = hsh_count (global_group_hash);
1005
1006   if (!taint_has_tainted_successor (taint))
1007     output_oneway ();
1008   taint_destroy (taint);
1009 }
1010
1011
1012 /* Post calculations for the ONEWAY command */
1013 void
1014 postcalc (  struct cmd_oneway *cmd UNUSED )
1015 {
1016   size_t i = 0;
1017
1018   for (i = 0; i < n_vars; ++i)
1019     {
1020       struct group_proc *gp = group_proc_get (vars[i]);
1021       struct hsh_table *group_hash = gp->group_hash;
1022       struct group_statistics *totals = &gp->ugs;
1023
1024       struct hsh_iterator g;
1025       struct group_statistics *gs;
1026
1027       for (gs =  hsh_first (group_hash, &g);
1028            gs != 0;
1029            gs = hsh_next (group_hash, &g))
1030         {
1031           gs->mean = gs->sum / gs->n;
1032           gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
1033
1034           gs->std_dev = sqrt (
1035                               gs->n / (gs->n - 1) *
1036                               ( gs->ssq / gs->n - pow2 (gs->mean))
1037                               );
1038
1039           gs->se_mean = gs->std_dev / sqrt (gs->n);
1040           gs->mean_diff = gs->sum_diff / gs->n;
1041         }
1042
1043       totals->mean = totals->sum / totals->n;
1044       totals->std_dev = sqrt (
1045                               totals->n / (totals->n - 1) *
1046                               (totals->ssq / totals->n - pow2 (totals->mean))
1047                               );
1048
1049       totals->se_mean = totals->std_dev / sqrt (totals->n);
1050     }
1051 }
1052
1053 /*
1054   Local Variables:
1055   mode: c
1056   End:
1057 */