Eliminated global variable current_dataset.
[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/dictionary/split-file.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/hash.h>
40 #include <libpspp/magic.h>
41 #include <libpspp/message.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/str.h>
45 #include <math/group-proc.h>
46 #include <math/group.h>
47 #include <math/levene.h>
48 #include <output/manager.h>
49 #include <output/table.h>
50 #include "sort-criteria.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54
55 /* (headers) */
56
57 /* (specification)
58    "ONEWAY" (oneway_):
59    *^variables=custom;
60    missing=miss:!analysis/listwise,
61            incl:include/!exclude;
62    +contrast= double list;
63    +statistics[st_]=descriptives,homogeneity.
64 */
65 /* (declarations) */
66 /* (functions) */
67
68
69
70 static bool bad_weight_warn = true;
71
72
73 static struct cmd_oneway cmd;
74
75 /* The independent variable */
76 static struct variable *indep_var;
77
78 /* Number of dependent variables */
79 static size_t n_vars;
80
81 /* The dependent variables */
82 static struct variable **vars;
83
84
85 /* A  hash table containing all the distinct values of the independent
86    variables */
87 static struct hsh_table *global_group_hash ;
88
89 /* The number of distinct values of the independent variable, when all 
90    missing values are disregarded */
91 static int ostensible_number_of_groups = -1;
92
93
94 /* Function to use for testing for missing values */
95 static is_missing_func *value_is_missing;
96
97
98 static bool run_oneway(const struct ccase *first,
99                        const struct casefile *cf, void *_mode, const struct dataset *);
100
101
102 /* Routines to show the output tables */
103 static void show_anova_table(void);
104 static void show_descriptives(void);
105 static void show_homogeneity(void);
106
107 static void show_contrast_coeffs(short *);
108 static void show_contrast_tests(short *);
109
110
111 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
112
113 static enum stat_table_t stat_tables ;
114
115 void output_oneway(void);
116
117
118 int
119 cmd_oneway (struct dataset *ds)
120 {
121   int i;
122   bool ok;
123
124   if ( !parse_oneway (ds, &cmd, NULL) )
125     return CMD_FAILURE;
126
127   /* If /MISSING=INCLUDE is set, then user missing values are ignored */
128   if (cmd.incl == ONEWAY_INCLUDE ) 
129     value_is_missing = mv_is_value_system_missing;
130   else
131     value_is_missing = mv_is_value_missing;
132
133   /* What statistics were requested */
134   if ( cmd.sbc_statistics ) 
135     {
136
137       for (i = 0 ; i < ONEWAY_ST_count ; ++i ) 
138         {
139           if  ( ! cmd.a_statistics[i]  ) continue;
140
141           switch (i) {
142           case ONEWAY_ST_DESCRIPTIVES:
143             stat_tables |= STAT_DESC;
144             break;
145           case ONEWAY_ST_HOMOGENEITY:
146             stat_tables |= STAT_HOMO;
147             break;
148           }
149         }
150     }
151
152   ok = multipass_procedure_with_splits (ds, run_oneway, &cmd);
153
154   free (vars);
155   free_oneway (&cmd);
156
157   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
158 }
159
160
161 void
162 output_oneway(void)
163 {
164   size_t i;
165   short *bad_contrast ; 
166
167   bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
168
169   /* Check the sanity of the given contrast values */
170   for (i = 0 ; i < cmd.sbc_contrast ; ++i ) 
171     {
172       int j;
173       double sum = 0;
174
175       bad_contrast[i] = 0;
176       if ( subc_list_double_count(&cmd.dl_contrast[i]) != 
177            ostensible_number_of_groups )
178         {
179           msg(SW, 
180               _("Number of contrast coefficients must equal the number of groups"));
181           bad_contrast[i] = 1;
182           continue;
183         }
184
185       for (j=0; j < ostensible_number_of_groups ; ++j )
186         sum += subc_list_double_at(&cmd.dl_contrast[i],j);
187
188       if ( sum != 0.0 ) 
189         msg(SW,_("Coefficients for contrast %d do not total zero"),i + 1);
190     }
191
192   if ( stat_tables & STAT_DESC ) 
193     show_descriptives();
194
195   if ( stat_tables & STAT_HOMO )
196     show_homogeneity();
197
198   show_anova_table();
199      
200   if (cmd.sbc_contrast )
201     {
202       show_contrast_coeffs(bad_contrast);
203       show_contrast_tests(bad_contrast);
204     }
205
206
207   free(bad_contrast);
208
209   /* Clean up */
210   for (i = 0 ; i < n_vars ; ++i ) 
211     {
212       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
213
214       hsh_destroy(group_hash);
215     }
216
217   hsh_destroy(global_group_hash);
218
219 }
220
221
222
223
224 /* Parser for the variables sub command */
225 static int
226 oneway_custom_variables(struct dataset *ds, struct cmd_oneway *cmd UNUSED, void *aux UNUSED)
227 {
228   struct dictionary *dict = dataset_dict (ds);
229
230   lex_match('=');
231
232   if ((token != T_ID || dict_lookup_var (dict, tokid) == NULL)
233       && token != T_ALL)
234     return 2;
235
236   if (!parse_variables (dict, &vars, &n_vars,
237                         PV_DUPLICATE 
238                         | PV_NUMERIC | PV_NO_SCRATCH) )
239     {
240       free (vars);
241       return 0;
242     }
243
244   assert(n_vars);
245
246   if ( ! lex_match(T_BY))
247     return 2;
248
249   indep_var = parse_variable (dict);
250
251   if ( !indep_var ) 
252     {
253       msg(SE,_("`%s' is not a variable name"),tokid);
254       return 0;
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 ccase *first, const struct casefile *cf, void *cmd_, const struct dataset *ds)
896 {
897   struct casereader *r;
898   struct ccase c;
899
900   struct cmd_oneway *cmd = (struct cmd_oneway *) cmd_;
901
902   output_split_file_values (ds, first);
903
904   global_group_hash = hsh_create(4, 
905                                  (hsh_compare_func *) compare_values,
906                                  (hsh_hash_func *) hash_value,
907                                  0,
908                                  (void *) indep_var->width );
909   precalc(cmd);
910
911   for(r = casefile_get_reader (cf);
912       casereader_read (r, &c) ;
913       case_destroy (&c)) 
914     {
915       size_t i;
916
917       const double weight = 
918         dict_get_case_weight (dataset_dict (ds), &c, &bad_weight_warn);
919       
920       const union value *indep_val = case_data (&c, indep_var->fv);
921
922       /* Deal with missing values */
923       if ( value_is_missing(&indep_var->miss, indep_val) )
924         continue;
925
926       /* Skip the entire case if /MISSING=LISTWISE is set */
927       if ( cmd->miss == ONEWAY_LISTWISE ) 
928         {
929           for(i = 0; i < n_vars ; ++i) 
930             {
931               const struct variable *v = vars[i];
932               const union value *val = case_data (&c, v->fv);
933
934               if (value_is_missing(&v->miss, val) )
935                 break;
936             }
937           if ( i != n_vars ) 
938             continue;
939
940         }
941       
942           
943       hsh_insert ( global_group_hash, (void *) indep_val );
944
945       for ( i = 0 ; i < n_vars ; ++i ) 
946         {
947           const struct variable *v = vars[i];
948
949           const union value *val = case_data (&c, v->fv);
950
951           struct group_proc *gp = group_proc_get (vars[i]);
952           struct hsh_table *group_hash = gp->group_hash;
953
954           struct group_statistics *gs;
955
956           gs = hsh_find(group_hash, (void *) indep_val );
957
958           if ( ! gs ) 
959             {
960               gs = xmalloc (sizeof *gs);
961               gs->id = *indep_val;
962               gs->sum=0;
963               gs->n=0;
964               gs->ssq=0;
965               gs->sum_diff=0;
966               gs->minimum = DBL_MAX;
967               gs->maximum = -DBL_MAX;
968
969               hsh_insert ( group_hash, (void *) gs );
970             }
971           
972           if (! value_is_missing(&v->miss, val) )
973             {
974               struct group_statistics *totals = &gp->ugs;
975
976               totals->n+=weight;
977               totals->sum+=weight * val->f;
978               totals->ssq+=weight * val->f * val->f;
979
980               if ( val->f * weight  < totals->minimum ) 
981                 totals->minimum = val->f * weight;
982
983               if ( val->f * weight  > totals->maximum ) 
984                 totals->maximum = val->f * weight;
985
986               gs->n+=weight;
987               gs->sum+=weight * val->f;
988               gs->ssq+=weight * val->f * val->f;
989
990               if ( val->f * weight  < gs->minimum ) 
991                 gs->minimum = val->f * weight;
992
993               if ( val->f * weight  > gs->maximum ) 
994                 gs->maximum = val->f * weight;
995             }
996
997           gp->n_groups = hsh_count ( group_hash );
998         }
999   
1000     }
1001   casereader_destroy (r);
1002
1003   postcalc(cmd);
1004
1005   
1006   if ( stat_tables & STAT_HOMO ) 
1007     levene (dataset_dict (ds), cf, indep_var, n_vars, vars, 
1008            (cmd->miss == ONEWAY_LISTWISE) ? LEV_LISTWISE : LEV_ANALYSIS ,
1009            value_is_missing);
1010
1011   ostensible_number_of_groups = hsh_count (global_group_hash);
1012
1013
1014   output_oneway();
1015
1016   return true;
1017 }
1018
1019
1020 /* Post calculations for the ONEWAY command */
1021 void 
1022 postcalc (  struct cmd_oneway *cmd UNUSED )
1023 {
1024   size_t i=0;
1025
1026
1027   for(i = 0; i < n_vars ; ++i) 
1028     {
1029       struct group_proc *gp = group_proc_get (vars[i]);
1030       struct hsh_table *group_hash = gp->group_hash;
1031       struct group_statistics *totals = &gp->ugs;
1032
1033       struct hsh_iterator g;
1034       struct group_statistics *gs;
1035
1036       for (gs =  hsh_first (group_hash,&g); 
1037            gs != 0; 
1038            gs = hsh_next(group_hash,&g))
1039         {
1040           gs->mean=gs->sum / gs->n;
1041           gs->s_std_dev= sqrt(
1042                               ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1043                               ) ;
1044
1045           gs->std_dev= sqrt(
1046                             gs->n/(gs->n-1) *
1047                             ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1048                             ) ;
1049
1050           gs->se_mean = gs->std_dev / sqrt(gs->n);
1051           gs->mean_diff= gs->sum_diff / gs->n;
1052
1053         }
1054
1055
1056
1057       totals->mean = totals->sum / totals->n;
1058       totals->std_dev= sqrt(
1059                             totals->n/(totals->n-1) *
1060                             ( (totals->ssq / totals->n ) - totals->mean * totals->mean )
1061                             ) ;
1062
1063       totals->se_mean = totals->std_dev / sqrt(totals->n);
1064         
1065     }
1066 }