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