aa45fbed6572edad9967c73859ed3e8e52c61b69
[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 /* (headers) */
47
48 /* (specification)
49    "ONEWAY" (oneway_):
50    *^variables=custom;
51    +missing=miss:!analysis/listwise,
52    incl:include/!exclude;
53    contrast= double list;
54    statistics[st_]=descriptives,homogeneity.
55 */
56 /* (declarations) */
57 /* (functions) */
58
59
60
61 static int bad_weight_warn = 1;
62
63
64 static struct cmd_oneway cmd;
65
66 /* The independent variable */
67 static struct variable *indep_var;
68
69 /* Number of dependent variables */
70 static int n_vars;
71
72 /* The dependent variables */
73 static struct variable **vars;
74
75
76 /* A  hash table containing all the distinct values of the independent
77    variables */
78 static struct hsh_table *global_group_hash ;
79
80 /* The number of distinct values of the independent variable, when all 
81    missing values are disregarded */
82 static int ostensible_number_of_groups=-1;
83
84
85 /* Function to use for testing for missing values */
86 static is_missing_func value_is_missing;
87
88
89 static void run_oneway(const struct casefile *cf, void *_mode);
90
91
92 /* Routines to show the output tables */
93 static void show_anova_table(void);
94 static void show_descriptives(void);
95 static void show_homogeneity(void);
96
97 static void show_contrast_coeffs(short *);
98 static void show_contrast_tests(short *);
99
100
101 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
102
103 static enum stat_table_t stat_tables ;
104
105 void output_oneway(void);
106
107
108 int
109 cmd_oneway(void)
110 {
111   int i;
112
113   if ( !parse_oneway(&cmd) )
114     return CMD_FAILURE;
115
116   /* If /MISSING=INCLUDE is set, then user missing values are ignored */
117   if (cmd.incl == ONEWAY_INCLUDE ) 
118     value_is_missing = is_system_missing;
119   else
120     value_is_missing = is_missing;
121
122   /* What statistics were requested */
123   if ( cmd.sbc_statistics ) 
124     {
125
126       for (i = 0 ; i < ONEWAY_ST_count ; ++i ) 
127         {
128           if  ( ! cmd.a_statistics[i]  ) continue;
129
130           switch (i) {
131           case ONEWAY_ST_DESCRIPTIVES:
132             stat_tables |= STAT_DESC;
133             break;
134           case ONEWAY_ST_HOMOGENEITY:
135             stat_tables |= STAT_HOMO;
136             break;
137           }
138         }
139     }
140
141   multipass_procedure_with_splits (run_oneway, &cmd);
142
143   free (vars);
144   free_oneway (&cmd);
145
146   return CMD_SUCCESS;
147 }
148
149
150 void
151 output_oneway(void)
152 {
153
154   int i;
155   short *bad_contrast ; 
156
157   bad_contrast = xmalloc ( sizeof (short) * cmd.sbc_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 cmd_oneway *cmd UNUSED)
217 {
218
219   lex_match('=');
220
221   if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
222       && token != T_ALL)
223     return 2;
224   
225
226   if (!parse_variables (default_dict, &vars, &n_vars,
227                         PV_DUPLICATE 
228                         | PV_NUMERIC | PV_NO_SCRATCH) )
229     {
230       free (vars);
231       return 0;
232     }
233
234   assert(n_vars);
235
236   if ( ! lex_match(T_BY))
237     return 2;
238
239
240   indep_var = parse_variable();
241
242   if ( !indep_var ) 
243     {
244       msg(SE,_("`%s' is not a variable name"),tokid);
245       return 0;
246     }
247
248
249   return 1;
250 }
251
252
253 /* Show the ANOVA table */
254 static void  
255 show_anova_table(void)
256 {
257   int i;
258   int n_cols =7;
259   int 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, 0, _("ANOVA"));
354   tab_submit (t);
355
356
357 }
358
359 /* Show the descriptives table */
360 static void  
361 show_descriptives(void)
362 {
363   int v;
364   int n_cols =10;
365   struct tab_table *t;
366   int row;
367
368   const double confidence=0.95;
369   const double q = (1.0 - confidence) / 2.0;
370
371   
372   int n_rows = 2 ; 
373
374   for ( v = 0 ; v < n_vars ; ++v ) 
375     n_rows += group_proc_get (vars[v])->n_groups + 1;
376
377   t = tab_create (n_cols,n_rows,0);
378   tab_headers (t, 2, 0, 2, 0);
379   tab_dim (t, tab_natural_dimensions);
380
381
382   /* Put a frame around the entire box, and vertical lines inside */
383   tab_box (t, 
384            TAL_2, TAL_2,
385            -1, TAL_1,
386            0, 0,
387            n_cols - 1, n_rows - 1);
388
389   /* Underline headers */
390   tab_hline (t, TAL_2, 0, n_cols - 1, 2 );
391   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
392   
393   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
394   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
395   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
396   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
397
398
399   tab_vline(t, TAL_0, 7, 0, 0);
400   tab_hline(t, TAL_1, 6, 7, 1);
401   tab_joint_text (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF, _("%g%% Confidence Interval for Mean"),confidence*100.0);
402
403   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
404   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
405
406   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
407   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
408
409
410   tab_title (t, 0, _("Descriptives"));
411
412
413   row = 2;
414   for ( v=0 ; v < n_vars ; ++v ) 
415     {
416       double T;
417       double std_error;
418       
419       struct group_proc *gp = group_proc_get (vars[v]);
420
421       struct group_statistics *gs;
422       struct group_statistics *totals = &gp->ugs; 
423
424       const char *s = var_to_string(vars[v]);
425
426       struct group_statistics *const *gs_array = 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   int v;
511   int n_cols = 5;
512   int 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, 0, _("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
568
569 /* Show the contrast coefficients table */
570 static void 
571 show_contrast_coeffs(short *bad_contrast)
572 {
573   int n_cols = 2 + ostensible_number_of_groups;
574   int n_rows = 2 + cmd.sbc_contrast;
575   union value *group_value;
576   int count = 0 ;      
577   void *const *group_values ;
578
579   struct tab_table *t;
580
581   t = tab_create (n_cols,n_rows,0);
582   tab_headers (t, 2, 0, 2, 0);
583   tab_dim (t, tab_natural_dimensions);
584
585   /* Put a frame around the entire box, and vertical lines inside */
586   tab_box (t, 
587            TAL_2, TAL_2,
588            -1, TAL_1,
589            0, 0,
590            n_cols - 1, n_rows - 1);
591
592   tab_box (t, 
593            -1,-1,
594            TAL_0, TAL_0,
595            2, 0,
596            n_cols - 1, 0);
597
598   tab_box (t,
599            -1,-1,
600            TAL_0, TAL_0,
601            0,0,
602            1,1);
603
604   tab_hline(t, TAL_1, 2, n_cols - 1, 1);
605   tab_hline(t, TAL_2, 0, n_cols - 1, 2);
606
607   tab_vline(t, TAL_2, 2, 0, n_rows - 1);
608
609   tab_title (t, 0, _("Contrast Coefficients"));
610
611   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
612
613
614   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, 
615                   var_to_string(indep_var));
616
617   group_values = hsh_sort(global_group_hash);
618   for (count = 0 ; 
619        count < hsh_count(global_group_hash) ; 
620        ++count)
621     {
622       int i;
623       group_value = group_values[count];
624
625       tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, 
626                 value_to_string(group_value, indep_var));
627
628       for (i = 0 ; i < cmd.sbc_contrast ; ++i ) 
629         {
630           tab_text(t, 1, i + 2, TAB_CENTER | TAT_PRINTF, "%d", i + 1);
631
632           if ( bad_contrast[i] ) 
633             tab_text(t, count + 2, i + 2, TAB_RIGHT, "?" );
634           else
635             tab_text(t, count + 2, i + 2, TAB_RIGHT | TAT_PRINTF, "%g", 
636                      subc_list_double_at(&cmd.dl_contrast[i], count)
637                      );
638         }
639     }
640   
641   tab_submit (t);
642 }
643
644
645 /* Show the results of the contrast tests */
646 static void 
647 show_contrast_tests(short *bad_contrast)
648 {
649   int v;
650   int n_cols = 8;
651   int n_rows = 1 + n_vars * 2 * cmd.sbc_contrast;
652
653   struct tab_table *t;
654
655   t = tab_create (n_cols,n_rows,0);
656   tab_headers (t, 3, 0, 1, 0);
657   tab_dim (t, tab_natural_dimensions);
658
659   /* Put a frame around the entire box, and vertical lines inside */
660   tab_box (t, 
661            TAL_2, TAL_2,
662            -1, TAL_1,
663            0, 0,
664            n_cols - 1, n_rows - 1);
665
666   tab_box (t, 
667            -1,-1,
668            TAL_0, TAL_0,
669            0, 0,
670            2, 0);
671
672   tab_hline(t, TAL_2, 0, n_cols - 1, 1);
673   tab_vline(t, TAL_2, 3, 0, n_rows - 1);
674
675
676   tab_title (t, 0, _("Contrast Tests"));
677
678   tab_text (t,  2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
679   tab_text (t,  3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
680   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
681   tab_text (t,  5, 0, TAB_CENTER | TAT_TITLE, _("t"));
682   tab_text (t,  6, 0, TAB_CENTER | TAT_TITLE, _("df"));
683   tab_text (t,  7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
684
685   for ( v = 0 ; v < n_vars ; ++v ) 
686     {
687       int i;
688       int lines_per_variable = 2 * cmd.sbc_contrast;
689
690
691       tab_text (t,  0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
692                 var_to_string(vars[v]));
693
694       for ( i = 0 ; i < cmd.sbc_contrast ; ++i ) 
695         {
696           int ci;
697           double contrast_value = 0.0;
698           double coef_msq = 0.0;
699           struct group_proc *grp_data = group_proc_get (vars[v]);
700           struct hsh_table *group_hash = grp_data->group_hash;
701
702           void *const *group_stat_array;
703
704           double T;
705           double std_error_contrast ;
706           double df;
707           double sec_vneq=0.0;
708
709
710           /* Note: The calculation of the degrees of freedom in the 
711              "variances not equal" case is painfull!!
712              The following formula may help to understand it:
713              \frac{\left(\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
714              {
715              \sum_{i=1}^k\left(
716              \frac{\left(c_i^2\frac{s_i^2}{n_i}\right)^2}  {n_i-1}
717              \right)
718              }
719           */
720
721           double df_denominator = 0.0;
722           double df_numerator = 0.0;
723           if ( i == 0 ) 
724             {
725               tab_text (t,  1, (v * lines_per_variable) + i + 1, 
726                         TAB_LEFT | TAT_TITLE,
727                         _("Assume equal variances"));
728
729               tab_text (t,  1, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast, 
730                         TAB_LEFT | TAT_TITLE, 
731                         _("Does not assume equal"));
732             }
733
734           tab_text (t,  2, (v * lines_per_variable) + i + 1, 
735                     TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d",i+1);
736
737
738           tab_text (t,  2, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
739                     TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d",i+1);
740
741
742           if ( bad_contrast[i]) 
743             continue;
744
745           group_stat_array = hsh_sort(group_hash);
746           
747           for (ci = 0 ; ci < hsh_count(group_hash) ;  ++ci)
748             {
749               const double coef = subc_list_double_at(&cmd.dl_contrast[i], ci);
750               struct group_statistics *gs = group_stat_array[ci];
751
752               const double winv = (gs->std_dev * gs->std_dev) / gs->n;
753
754               contrast_value += coef * gs->mean;
755
756               coef_msq += (coef * coef) / gs->n ; 
757
758               sec_vneq += (coef * coef) * (gs->std_dev * gs->std_dev ) /gs->n ;
759
760               df_numerator += (coef * coef) * winv;
761               df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
762             }
763           sec_vneq = sqrt(sec_vneq);
764
765           df_numerator = pow2(df_numerator);
766
767           tab_float (t,  3, (v * lines_per_variable) + i + 1, 
768                      TAB_RIGHT, contrast_value, 8,2);
769
770           tab_float (t,  3, (v * lines_per_variable) + i + 1 + 
771                      cmd.sbc_contrast,
772                      TAB_RIGHT, contrast_value, 8,2);
773
774           std_error_contrast = sqrt(grp_data->mse * coef_msq);
775
776           /* Std. Error */
777           tab_float (t,  4, (v * lines_per_variable) + i + 1, 
778                      TAB_RIGHT, std_error_contrast,
779                      8,3);
780
781           T = fabs(contrast_value / std_error_contrast) ;
782
783           /* T Statistic */
784
785           tab_float (t,  5, (v * lines_per_variable) + i + 1, 
786                      TAB_RIGHT, T,
787                      8,3);
788
789           df = grp_data->ugs.n - grp_data->n_groups;
790
791           /* Degrees of Freedom */
792           tab_float (t,  6, (v * lines_per_variable) + i + 1, 
793                      TAB_RIGHT,  df,
794                      8,0);
795
796
797           /* Significance TWO TAILED !!*/
798           tab_float (t,  7, (v * lines_per_variable) + i + 1, 
799                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q(T,df),
800                      8,3);
801
802
803           /* Now for the Variances NOT Equal case */
804
805           /* Std. Error */
806           tab_float (t,  4, 
807                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast, 
808                      TAB_RIGHT, sec_vneq,
809                      8,3);
810
811
812           T = contrast_value / sec_vneq;
813           tab_float (t,  5, 
814                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast, 
815                      TAB_RIGHT, T,
816                      8,3);
817
818
819           df = df_numerator / df_denominator;
820
821           tab_float (t,  6, 
822                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast, 
823                      TAB_RIGHT, df,
824                      8,3);
825
826           /* The Significance */
827
828           tab_float (t, 7, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
829                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q(T,df),
830                      8,3);
831
832
833         }
834
835       if ( v > 0 ) 
836         tab_hline(t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
837     }
838
839   tab_submit (t);
840
841 }
842
843
844 /* ONEWAY ANOVA Calculations */
845
846 static void  postcalc (  struct cmd_oneway *cmd UNUSED );
847
848 static void  precalc ( struct cmd_oneway *cmd UNUSED );
849
850
851
852 /* Pre calculations */
853 static void 
854 precalc ( struct cmd_oneway *cmd UNUSED )
855 {
856   int i=0;
857
858   for(i=0; i< n_vars ; ++i) 
859     {
860       struct group_proc *gp = group_proc_get (vars[i]);
861       struct group_statistics *totals = &gp->ugs;
862       
863       /* Create a hash for each of the dependent variables.
864          The hash contains a group_statistics structure, 
865          and is keyed by value of the independent variable */
866
867       gp->group_hash = 
868         hsh_create(4, 
869                    (hsh_compare_func *) compare_group,
870                    (hsh_hash_func *) hash_group,
871                    (hsh_free_func *) free_group,
872                    (void *) indep_var->width );
873
874
875       totals->sum=0;
876       totals->n=0;
877       totals->ssq=0;
878       totals->sum_diff=0;
879       totals->maximum = - DBL_MAX;
880       totals->minimum = DBL_MAX;
881     }
882 }
883
884
885 static void 
886 run_oneway(const struct casefile *cf, void *cmd_)
887 {
888   struct casereader *r;
889   struct ccase c;
890
891   struct cmd_oneway *cmd = (struct cmd_oneway *) cmd_;
892
893   global_group_hash = hsh_create(4, 
894                                  (hsh_compare_func *) compare_values,
895                                  (hsh_hash_func *) hash_value,
896                                  0,
897                                  (void *) indep_var->width );
898   precalc(cmd);
899
900   for(r = casefile_get_reader (cf);
901       casereader_read (r, &c) ;
902       case_destroy (&c)) 
903     {
904       int i;
905
906       const double weight = 
907         dict_get_case_weight(default_dict,&c,&bad_weight_warn);
908       
909       const union value *indep_val = case_data (&c, indep_var->fv);
910
911       /* Deal with missing values */
912       if ( value_is_missing(indep_val,indep_var) )
913         continue;
914
915       /* Skip the entire case if /MISSING=LISTWISE is set */
916       if ( cmd->miss == ONEWAY_LISTWISE ) 
917         {
918           for(i = 0; i < n_vars ; ++i) 
919             {
920               const struct variable *v = vars[i];
921               const union value *val = case_data (&c, v->fv);
922
923               if (value_is_missing(val,v) )
924                 break;
925             }
926           if ( i != n_vars ) 
927             continue;
928
929         }
930       
931           
932       hsh_insert ( global_group_hash, (void *) indep_val );
933
934       for ( i = 0 ; i < n_vars ; ++i ) 
935         {
936           const struct variable *v = vars[i];
937
938           const union value *val = case_data (&c, v->fv);
939
940           struct group_proc *gp = group_proc_get (vars[i]);
941           struct hsh_table *group_hash = gp->group_hash;
942
943           struct group_statistics *gs;
944
945           gs = hsh_find(group_hash, (void *) indep_val );
946
947           if ( ! gs ) 
948             {
949               gs = (struct group_statistics *) 
950                 xmalloc (sizeof(struct group_statistics));
951
952               gs->id = *indep_val;
953               gs->sum=0;
954               gs->n=0;
955               gs->ssq=0;
956               gs->sum_diff=0;
957               gs->minimum = DBL_MAX;
958               gs->maximum = -DBL_MAX;
959
960               hsh_insert ( group_hash, (void *) gs );
961             }
962           
963           if (! value_is_missing(val,v) )
964             {
965               struct group_statistics *totals = &gp->ugs;
966
967               totals->n+=weight;
968               totals->sum+=weight * val->f;
969               totals->ssq+=weight * val->f * val->f;
970
971               if ( val->f * weight  < totals->minimum ) 
972                 totals->minimum = val->f * weight;
973
974               if ( val->f * weight  > totals->maximum ) 
975                 totals->maximum = val->f * weight;
976
977               gs->n+=weight;
978               gs->sum+=weight * val->f;
979               gs->ssq+=weight * val->f * val->f;
980
981               if ( val->f * weight  < gs->minimum ) 
982                 gs->minimum = val->f * weight;
983
984               if ( val->f * weight  > gs->maximum ) 
985                 gs->maximum = val->f * weight;
986             }
987
988           gp->n_groups = hsh_count ( group_hash );
989         }
990   
991     }
992   casereader_destroy (r);
993
994   postcalc(cmd);
995
996   
997   if ( stat_tables & STAT_HOMO ) 
998     levene(cf, indep_var, n_vars, vars, 
999            (cmd->miss == ONEWAY_LISTWISE) ? LEV_LISTWISE : LEV_ANALYSIS ,
1000            value_is_missing);
1001
1002   ostensible_number_of_groups = hsh_count (global_group_hash);
1003
1004
1005   output_oneway();
1006
1007
1008 }
1009
1010
1011 /* Post calculations for the ONEWAY command */
1012 void 
1013 postcalc (  struct cmd_oneway *cmd UNUSED )
1014 {
1015   int i=0;
1016
1017
1018   for(i = 0; i < n_vars ; ++i) 
1019     {
1020       struct group_proc *gp = group_proc_get (vars[i]);
1021       struct hsh_table *group_hash = gp->group_hash;
1022       struct group_statistics *totals = &gp->ugs;
1023
1024       struct hsh_iterator g;
1025       struct group_statistics *gs;
1026
1027       for (gs =  hsh_first (group_hash,&g); 
1028            gs != 0; 
1029            gs = hsh_next(group_hash,&g))
1030         {
1031           gs->mean=gs->sum / gs->n;
1032           gs->s_std_dev= sqrt(
1033                               ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1034                               ) ;
1035
1036           gs->std_dev= sqrt(
1037                             gs->n/(gs->n-1) *
1038                             ( (gs->ssq / gs->n ) - gs->mean * gs->mean )
1039                             ) ;
1040
1041           gs->se_mean = gs->std_dev / sqrt(gs->n);
1042           gs->mean_diff= gs->sum_diff / gs->n;
1043
1044         }
1045
1046
1047
1048       totals->mean = totals->sum / totals->n;
1049       totals->std_dev= sqrt(
1050                             totals->n/(totals->n-1) *
1051                             ( (totals->ssq / totals->n ) - totals->mean * totals->mean )
1052                             ) ;
1053
1054       totals->se_mean = totals->std_dev / sqrt(totals->n);
1055         
1056     }
1057 }