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