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