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