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