Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / stats / oneway.q
1 /* PSPP - One way ANOVA. -*-c-*-
2
3 Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <gsl/gsl_cdf.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <data/case.h>
28 #include <data/casegrouper.h>
29 #include <data/casereader.h>
30 #include <data/dictionary.h>
31 #include <data/procedure.h>
32 #include <data/value-labels.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/dictionary/split-file.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/hash.h>
40 #include <libpspp/magic.h>
41 #include <libpspp/message.h>
42 #include <libpspp/misc.h>
43 #include <libpspp/str.h>
44 #include <libpspp/taint.h>
45 #include <math/group-proc.h>
46 #include <math/group.h>
47 #include <math/levene.h>
48 #include <output/manager.h>
49 #include <output/table.h>
50 #include "sort-criteria.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54
55 /* (headers) */
56
57 /* (specification)
58    "ONEWAY" (oneway_):
59    *^variables=custom;
60    missing=miss:!analysis/listwise,
61            incl:include/!exclude;
62    +contrast= double list;
63    +statistics[st_]=descriptives,homogeneity.
64 */
65 /* (declarations) */
66 /* (functions) */
67
68 static struct cmd_oneway cmd;
69
70 /* The independent variable */
71 static const struct variable *indep_var;
72
73 /* Number of dependent variables */
74 static size_t n_vars;
75
76 /* The dependent variables */
77 static const struct variable **vars;
78
79
80 /* A  hash table containing all the distinct values of the independent
81    variables */
82 static struct hsh_table *global_group_hash ;
83
84 /* The number of distinct values of the independent variable, when all 
85    missing values are disregarded */
86 static int ostensible_number_of_groups = -1;
87
88
89 static void run_oneway (struct cmd_oneway *, struct casereader *, 
90                         const struct dataset *);
91
92
93 /* Routines to show the output tables */
94 static void show_anova_table(void);
95 static void show_descriptives(void);
96 static void show_homogeneity(void);
97
98 static void show_contrast_coeffs(short *);
99 static void show_contrast_tests(short *);
100
101
102 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
103
104 static enum stat_table_t stat_tables ;
105
106 void output_oneway(void);
107
108
109 int
110 cmd_oneway (struct lexer *lexer, struct dataset *ds)
111 {
112   struct casegrouper *grouper;
113   struct casereader *group;
114   int i;
115   bool ok;
116
117   if ( !parse_oneway (lexer, ds, &cmd, NULL) )
118     return CMD_FAILURE;
119
120   /* What statistics were requested */
121   if ( cmd.sbc_statistics ) 
122     {
123
124       for (i = 0 ; i < ONEWAY_ST_count ; ++i ) 
125         {
126           if  ( ! cmd.a_statistics[i]  ) continue;
127
128           switch (i) {
129           case ONEWAY_ST_DESCRIPTIVES:
130             stat_tables |= STAT_DESC;
131             break;
132           case ONEWAY_ST_HOMOGENEITY:
133             stat_tables |= STAT_HOMO;
134             break;
135           }
136         }
137     }
138
139   /* Data pass.  FIXME: error handling. */
140   grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
141   while (casegrouper_get_next_group (grouper, &group)) 
142     run_oneway (&cmd, group, ds);
143   ok = casegrouper_destroy (grouper);
144   ok = proc_commit (ds) && ok;
145
146   free (vars);
147   free_oneway (&cmd);
148
149   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
150 }
151
152
153 void
154 output_oneway(void)
155 {
156   size_t i;
157   short *bad_contrast ; 
158
159   bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
160
161   /* Check the sanity of the given contrast values */
162   for (i = 0 ; i < cmd.sbc_contrast ; ++i ) 
163     {
164       int j;
165       double sum = 0;
166
167       bad_contrast[i] = 0;
168       if ( subc_list_double_count(&cmd.dl_contrast[i]) != 
169            ostensible_number_of_groups )
170         {
171           msg(SW, 
172               _("Number of contrast coefficients must equal the number of groups"));
173           bad_contrast[i] = 1;
174           continue;
175         }
176
177       for (j=0; j < ostensible_number_of_groups ; ++j )
178         sum += subc_list_double_at(&cmd.dl_contrast[i],j);
179
180       if ( sum != 0.0 ) 
181         msg(SW,_("Coefficients for contrast %d do not total zero"),
182             (int) i + 1);
183     }
184
185   if ( stat_tables & STAT_DESC ) 
186     show_descriptives();
187
188   if ( stat_tables & STAT_HOMO )
189     show_homogeneity();
190
191   show_anova_table();
192      
193   if (cmd.sbc_contrast )
194     {
195       show_contrast_coeffs(bad_contrast);
196       show_contrast_tests(bad_contrast);
197     }
198
199
200   free(bad_contrast);
201
202   /* Clean up */
203   for (i = 0 ; i < n_vars ; ++i ) 
204     {
205       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
206
207       hsh_destroy(group_hash);
208     }
209
210   hsh_destroy(global_group_hash);
211
212 }
213
214
215
216
217 /* Parser for the variables sub command */
218 static int
219 oneway_custom_variables (struct lexer *lexer, 
220                         struct dataset *ds, struct cmd_oneway *cmd UNUSED, 
221                         void *aux UNUSED)
222 {
223   struct dictionary *dict = dataset_dict (ds);
224
225   lex_match (lexer, '=');
226
227   if ((lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
228       && lex_token (lexer) != T_ALL)
229     return 2;
230
231   if (!parse_variables_const (lexer, 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 (lexer, T_BY))
242     return 2;
243
244   indep_var = parse_variable (lexer, dict);
245
246   if ( !indep_var ) 
247     {
248       msg(SE,_("`%s' is not a variable name"),lex_tokid (lexer));
249       return 0;
250     }
251
252   return 1;
253 }
254
255
256 /* Show the ANOVA table */
257 static void  
258 show_anova_table(void)
259 {
260   size_t i;
261   int n_cols =7;
262   size_t n_rows = n_vars * 3 + 1;
263
264   struct tab_table *t;
265
266
267   t = tab_create (n_cols,n_rows,0);
268   tab_headers (t, 2, 0, 1, 0);
269   tab_dim (t, tab_natural_dimensions);
270
271
272   tab_box (t, 
273            TAL_2, TAL_2,
274            -1, TAL_1,
275            0, 0,
276            n_cols - 1, n_rows - 1);
277
278   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
279   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
280   tab_vline (t, TAL_0, 1, 0, 0);
281   
282   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
283   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
284   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
285   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
286   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
287
288
289   for ( i=0 ; i < n_vars ; ++i ) 
290     {
291       struct group_statistics *totals = &group_proc_get (vars[i])->ugs;
292       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
293       struct hsh_iterator g;
294       struct group_statistics *gs;
295       double ssa=0;
296       const char *s = var_to_string(vars[i]);
297
298       for (gs =  hsh_first (group_hash,&g); 
299            gs != 0; 
300            gs = hsh_next(group_hash,&g))
301         {
302           ssa += (gs->sum * gs->sum)/gs->n;
303         }
304       
305       ssa -= ( totals->sum * totals->sum ) / totals->n ;
306
307       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
308       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
309       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
310       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
311       
312       if (i > 0)
313         tab_hline(t, TAL_1, 0, n_cols - 1 , i * 3 + 1);
314
315       {
316         struct group_proc *gp = group_proc_get (vars[i]);
317         const double sst = totals->ssq - ( totals->sum * totals->sum) / totals->n ;
318         const double df1 = gp->n_groups - 1;
319         const double df2 = totals->n - gp->n_groups ;
320         const double msa = ssa / df1;
321         
322         gp->mse  = (sst - ssa) / df2;
323         
324         
325         /* Sums of Squares */
326         tab_float (t, 2, i * 3 + 1, 0, ssa, 10, 2);
327         tab_float (t, 2, i * 3 + 3, 0, sst, 10, 2);
328         tab_float (t, 2, i * 3 + 2, 0, sst - ssa, 10, 2);
329
330
331         /* Degrees of freedom */
332         tab_float (t, 3, i * 3 + 1, 0, df1, 4, 0);
333         tab_float (t, 3, i * 3 + 2, 0, df2, 4, 0);
334         tab_float (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
335
336         /* Mean Squares */
337         tab_float (t, 4, i * 3 + 1, TAB_RIGHT, msa, 8, 3);
338         tab_float (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, 8, 3);
339         
340
341         { 
342           const double F = msa/gp->mse ;
343
344           /* The F value */
345           tab_float (t, 5, i * 3 + 1, 0,  F, 8, 3);
346         
347           /* The significance */
348           tab_float (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q(F,df1,df2), 8, 3);
349         }
350
351       }
352
353     }
354
355
356   tab_title (t, _("ANOVA"));
357   tab_submit (t);
358 }
359
360
361 /* Show the descriptives table */
362 static void  
363 show_descriptives(void)
364 {
365   size_t 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, _("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 *const *gs_array =
429         (struct group_statistics *const *) hsh_sort(gp->group_hash);
430       int count = 0;
431
432       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
433       if ( v > 0) 
434         tab_hline(t, TAL_1, 0, n_cols - 1 , row);
435
436       for (count = 0 ; count < hsh_count(gp->group_hash) ; ++count)
437         {
438           gs = gs_array[count];
439
440           tab_text (t, 1, row + count, 
441                     TAB_LEFT | TAT_TITLE, var_get_value_name(indep_var,
442                                                              &gs->id));
443
444           /* Now fill in the numbers ... */
445
446           tab_float (t, 2, row + count, 0, gs->n, 8,0);
447
448           tab_float (t, 3, row + count, 0, gs->mean,8,2);
449           
450           tab_float (t, 4, row + count, 0, gs->std_dev,8,2);
451
452           std_error = gs->std_dev/sqrt(gs->n) ;
453           tab_float (t, 5, row + count, 0, 
454                      std_error, 8,2);
455
456           /* Now the confidence interval */
457       
458           T = gsl_cdf_tdist_Qinv(q,gs->n - 1);
459
460           tab_float(t, 6, row + count, 0,
461                     gs->mean - T * std_error, 8, 2); 
462
463           tab_float(t, 7, row + count, 0,
464                     gs->mean + T * std_error, 8, 2); 
465
466           /* Min and Max */
467
468           tab_float(t, 8, row + count, 0,  gs->minimum, 8, 2); 
469           tab_float(t, 9, row + count, 0,  gs->maximum, 8, 2); 
470
471         }
472
473       tab_text (t, 1, row + count, 
474                 TAB_LEFT | TAT_TITLE ,_("Total"));
475
476       tab_float (t, 2, row + count, 0, totals->n, 8,0);
477
478       tab_float (t, 3, row + count, 0, totals->mean, 8,2);
479
480       tab_float (t, 4, row + count, 0, totals->std_dev,8,2);
481
482       std_error = totals->std_dev/sqrt(totals->n) ;
483
484       tab_float (t, 5, row + count, 0, std_error, 8,2);
485
486       /* Now the confidence interval */
487       
488       T = gsl_cdf_tdist_Qinv(q,totals->n - 1);
489
490       tab_float(t, 6, row + count, 0,
491                 totals->mean - T * std_error, 8, 2); 
492
493       tab_float(t, 7, row + count, 0,
494                 totals->mean + T * std_error, 8, 2); 
495
496       /* Min and Max */
497
498       tab_float(t, 8, row + count, 0,  totals->minimum, 8, 2); 
499       tab_float(t, 9, row + count, 0,  totals->maximum, 8, 2); 
500
501       row += gp->n_groups + 1;
502     }
503
504
505   tab_submit (t);
506
507
508 }
509
510 /* Show the homogeneity table */
511 static void 
512 show_homogeneity(void)
513 {
514   size_t v;
515   int n_cols = 5;
516   size_t n_rows = n_vars + 1;
517
518   struct tab_table *t;
519
520
521   t = tab_create (n_cols,n_rows,0);
522   tab_headers (t, 1, 0, 1, 0);
523   tab_dim (t, tab_natural_dimensions);
524
525   /* Put a frame around the entire box, and vertical lines inside */
526   tab_box (t, 
527            TAL_2, TAL_2,
528            -1, TAL_1,
529            0, 0,
530            n_cols - 1, n_rows - 1);
531
532
533   tab_hline(t, TAL_2, 0, n_cols - 1, 1);
534   tab_vline(t, TAL_2, 1, 0, n_rows - 1);
535
536
537   tab_text (t,  1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
538   tab_text (t,  2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
539   tab_text (t,  3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
540   tab_text (t,  4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
541   
542
543   tab_title (t, _("Test of Homogeneity of Variances"));
544
545   for ( v=0 ; v < n_vars ; ++v ) 
546     {
547       double F;
548       const struct variable *var = vars[v];
549       const struct group_proc *gp = group_proc_get (vars[v]);
550       const char *s = var_to_string(var);
551       const struct group_statistics *totals = &gp->ugs;
552
553       const double df1 = gp->n_groups - 1;
554       const double df2 = totals->n - gp->n_groups ;
555
556       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
557
558       F = gp->levene;
559       tab_float (t, 1, v + 1, TAB_RIGHT, F, 8,3);
560       tab_float (t, 2, v + 1, TAB_RIGHT, df1 ,8,0);
561       tab_float (t, 3, v + 1, TAB_RIGHT, df2 ,8,0);
562
563       /* Now the significance */
564       tab_float (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q(F,df1,df2), 8, 3);
565     }
566
567   tab_submit (t);
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 *const *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, _("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                 var_get_value_name (indep_var, group_value));
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   size_t v;
652   int n_cols = 8;
653   size_t 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, _("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
704           void *const *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               const double coef = subc_list_double_at(&cmd.dl_contrast[i], ci);
752               struct group_statistics *gs = group_stat_array[ci];
753
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   size_t 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 *) var_get_width (indep_var) );
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 static void
887 free_value (void *value_, const void *aux UNUSED) 
888 {
889   union value *value = value_;
890   free (value);
891 }
892
893 static void
894 run_oneway (struct cmd_oneway *cmd,
895             struct casereader *input, 
896             const struct dataset *ds)
897 {
898   struct taint *taint;
899   struct dictionary *dict = dataset_dict (ds);
900   enum mv_class exclude;
901   struct casereader *reader;
902   struct ccase c;
903
904   if (!casereader_peek (input, 0, &c))
905     return;
906   output_split_file_values (ds, &c);
907   case_destroy (&c);
908
909   taint = taint_clone (casereader_get_taint (input));
910
911   global_group_hash = hsh_create(4, 
912                                  (hsh_compare_func *) compare_values,
913                                  (hsh_hash_func *) hash_value,
914                                  free_value,
915                                  (void *) var_get_width (indep_var) );
916
917   precalc(cmd);
918
919   exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
920   input = casereader_create_filter_missing (input, &indep_var, 1,
921                                             exclude, NULL);
922   if (cmd->miss == ONEWAY_LISTWISE)
923     input = casereader_create_filter_missing (input, vars, n_vars,
924                                               exclude, NULL);
925   input = casereader_create_filter_weight (input, dict, NULL, NULL);
926
927   reader = casereader_clone (input);
928   for (; casereader_read (reader, &c); case_destroy (&c)) 
929     {
930       size_t i;
931
932       const double weight = dict_get_case_weight (dict, &c, NULL);
933       
934       const union value *indep_val = case_data (&c, indep_var);
935       void **p = hsh_probe (global_group_hash, indep_val);
936       if (*p == NULL)
937         *p = value_dup (indep_val, var_get_width (indep_var));
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);
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 (!var_is_value_missing (v, val, exclude))
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 (reader);
996
997   postcalc(cmd);
998
999   
1000   if ( stat_tables & STAT_HOMO ) 
1001     levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
1002
1003   casereader_destroy (input);
1004
1005   ostensible_number_of_groups = hsh_count (global_group_hash);
1006
1007   if (!taint_has_tainted_successor (taint))
1008     output_oneway();
1009   taint_destroy (taint);
1010 }
1011
1012
1013 /* Post calculations for the ONEWAY command */
1014 void 
1015 postcalc (  struct cmd_oneway *cmd UNUSED )
1016 {
1017   size_t 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 }