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