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