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