Separate table functions that format their arguments from those that don't.
[pspp-builds.git] / src / language / stats / oneway.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009 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 #include <data/format.h>
47
48 #include "xalloc.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52
53 /* (headers) */
54
55 /* (specification)
56    "ONEWAY" (oneway_):
57    *^variables=custom;
58    missing=miss:!analysis/listwise,
59    incl:include/!exclude;
60    +contrast= double list;
61    +statistics[st_]=descriptives,homogeneity.
62 */
63 /* (declarations) */
64 /* (functions) */
65
66 static struct cmd_oneway cmd;
67
68 /* The independent variable */
69 static const struct variable *indep_var;
70
71 /* Number of dependent variables */
72 static size_t n_vars;
73
74 /* The dependent variables */
75 static const struct variable **vars;
76
77
78 /* A  hash table containing all the distinct values of the independent
79    variables */
80 static struct hsh_table *global_group_hash;
81
82 /* The number of distinct values of the independent variable, when all
83    missing values are disregarded */
84 static int ostensible_number_of_groups = -1;
85
86
87 static void run_oneway (struct cmd_oneway *, struct casereader *,
88                         const struct dataset *);
89
90
91 /* Routines to show the output tables */
92 static void show_anova_table(void);
93 static void show_descriptives (const struct dictionary *dict);
94 static void show_homogeneity(void);
95
96 static void show_contrast_coeffs (short *);
97 static void show_contrast_tests (short *);
98
99
100 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
101
102 static enum stat_table_t stat_tables;
103
104 static void output_oneway (const struct dictionary *dict);
105
106
107 int
108 cmd_oneway (struct lexer *lexer, struct dataset *ds)
109 {
110   struct casegrouper *grouper;
111   struct casereader *group;
112   int i;
113   bool ok;
114
115   if ( !parse_oneway (lexer, ds, &cmd, NULL))
116     return CMD_FAILURE;
117
118   /* What statistics were requested */
119   if ( cmd.sbc_statistics)
120     {
121
122       for (i = 0; i < ONEWAY_ST_count; ++i)
123         {
124           if (! cmd.a_statistics[i]) continue;
125
126           switch (i) 
127             {
128             case ONEWAY_ST_DESCRIPTIVES:
129               stat_tables |= STAT_DESC;
130               break;
131             case ONEWAY_ST_HOMOGENEITY:
132               stat_tables |= STAT_HOMO;
133               break;
134             }
135         }
136     }
137
138   /* Data pass.  FIXME: error handling. */
139   grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
140   while (casegrouper_get_next_group (grouper, &group))
141     run_oneway (&cmd, group, ds);
142   ok = casegrouper_destroy (grouper);
143   ok = proc_commit (ds) && ok;
144
145   free (vars);
146   free_oneway (&cmd);
147
148   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
149 }
150
151
152 static void
153 output_oneway (const struct dictionary *dict)
154 {
155   size_t i;
156   short *bad_contrast;
157
158   bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
159
160   /* Check the sanity of the given contrast values */
161   for (i = 0; i < cmd.sbc_contrast; ++i)
162     {
163       int j;
164       double sum = 0;
165
166       bad_contrast[i] = 0;
167       if (subc_list_double_count (&cmd.dl_contrast[i]) !=
168           ostensible_number_of_groups)
169         {
170           msg (SW,
171                _("Number of contrast coefficients must equal the number of groups"));
172           bad_contrast[i] = 1;
173           continue;
174         }
175
176       for (j = 0; j < ostensible_number_of_groups; ++j)
177         sum += subc_list_double_at (&cmd.dl_contrast[i], j);
178
179       if ( sum != 0.0 )
180         msg (SW, _("Coefficients for contrast %zu do not total zero"), i + 1);
181     }
182
183   if ( stat_tables & STAT_DESC )
184     show_descriptives (dict);
185
186   if ( stat_tables & STAT_HOMO )
187     show_homogeneity ();
188
189   show_anova_table ();
190
191   if (cmd.sbc_contrast )
192     {
193       show_contrast_coeffs (bad_contrast);
194       show_contrast_tests (bad_contrast);
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 /* Parser for the variables sub command */
212 static int
213 oneway_custom_variables (struct lexer *lexer,
214                          struct dataset *ds, struct cmd_oneway *cmd UNUSED,
215                          void *aux UNUSED)
216 {
217   struct dictionary *dict = dataset_dict (ds);
218
219   lex_match (lexer, '=');
220
221   if ((lex_token (lexer) != T_ID ||
222        dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
223       && lex_token (lexer) != T_ALL)
224     return 2;
225
226   if (!parse_variables_const (lexer, 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 (lexer, T_BY))
237     return 2;
238
239   indep_var = parse_variable (lexer, dict);
240
241   if ( !indep_var )
242     {
243       msg (SE, _("`%s' is not a variable name"), lex_tokid (lexer));
244       return 0;
245     }
246
247   return 1;
248 }
249
250
251 /* Show the ANOVA table */
252 static void
253 show_anova_table (void)
254 {
255   size_t i;
256   int n_cols =7;
257   size_t n_rows = n_vars * 3 + 1;
258
259   struct tab_table *t;
260
261
262   t = tab_create (n_cols, n_rows, 0);
263   tab_headers (t, 2, 0, 1, 0);
264   tab_dim (t, tab_natural_dimensions, NULL);
265
266
267   tab_box (t,
268            TAL_2, TAL_2,
269            -1, TAL_1,
270            0, 0,
271            n_cols - 1, n_rows - 1);
272
273   tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
274   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
275   tab_vline (t, TAL_0, 1, 0, 0);
276
277   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
278   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
279   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
280   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
281   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
282
283
284   for (i = 0; i < n_vars; ++i)
285     {
286       struct group_statistics *totals = &group_proc_get (vars[i])->ugs;
287       struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
288       struct hsh_iterator g;
289       struct group_statistics *gs;
290       double ssa = 0;
291       const char *s = var_to_string (vars[i]);
292
293       for (gs =  hsh_first (group_hash, &g);
294            gs != 0;
295            gs = hsh_next (group_hash, &g))
296         {
297           ssa += pow2 (gs->sum) / gs->n;
298         }
299
300       ssa -= pow2 (totals->sum) / totals->n;
301
302       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
303       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
304       tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
305       tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
306
307       if (i > 0)
308         tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
309
310       {
311         struct group_proc *gp = group_proc_get (vars[i]);
312         const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
313         const double df1 = gp->n_groups - 1;
314         const double df2 = totals->n - gp->n_groups;
315         const double msa = ssa / df1;
316
317         gp->mse  = (sst - ssa) / df2;
318
319
320         /* Sums of Squares */
321         tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
322         tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
323         tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
324
325
326         /* Degrees of freedom */
327         tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
328         tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
329         tab_fixed (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
330
331         /* Mean Squares */
332         tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
333         tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
334
335         {
336           const double F = msa / gp->mse ;
337
338           /* The F value */
339           tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
340
341           /* The significance */
342           tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
343         }
344       }
345     }
346
347
348   tab_title (t, _("ANOVA"));
349   tab_submit (t);
350 }
351
352
353 /* Show the descriptives table */
354 static void
355 show_descriptives (const struct dictionary *dict)
356 {
357   size_t v;
358   int n_cols = 10;
359   struct tab_table *t;
360   int row;
361
362   const double confidence = 0.95;
363   const double q = (1.0 - confidence) / 2.0;
364
365   const struct variable *wv = dict_get_weight (dict);
366   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
367
368   int n_rows = 2;
369
370   for ( v = 0; v < n_vars; ++v )
371     n_rows += group_proc_get (vars[v])->n_groups + 1;
372
373   t = tab_create (n_cols, n_rows, 0);
374   tab_headers (t, 2, 0, 2, 0);
375   tab_dim (t, tab_natural_dimensions, NULL);
376
377
378   /* Put a frame around the entire box, and vertical lines inside */
379   tab_box (t,
380            TAL_2, TAL_2,
381            -1, TAL_1,
382            0, 0,
383            n_cols - 1, n_rows - 1);
384
385   /* Underline headers */
386   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
387   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
388
389   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
390   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
391   tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
392   tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
393
394
395   tab_vline (t, TAL_0, 7, 0, 0);
396   tab_hline (t, TAL_1, 6, 7, 1);
397   tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
398                          _("%g%% Confidence Interval for Mean"),
399                          confidence*100.0);
400
401   tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
402   tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
403
404   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
405   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
406
407
408   tab_title (t, _("Descriptives"));
409
410
411   row = 2;
412   for (v = 0; v < n_vars; ++v)
413     {
414       double T;
415       double std_error;
416
417       struct group_proc *gp = group_proc_get (vars[v]);
418
419       struct group_statistics *gs;
420       struct group_statistics *totals = &gp->ugs;
421
422       const char *s = var_to_string (vars[v]);
423       const struct fmt_spec *fmt = var_get_print_format (vars[v]);
424
425       struct group_statistics *const *gs_array =
426         (struct group_statistics *const *) hsh_sort (gp->group_hash);
427       int count = 0;
428
429       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
430       if ( v > 0)
431         tab_hline (t, TAL_1, 0, n_cols - 1, row);
432
433       for (count = 0; count < hsh_count (gp->group_hash); ++count)
434         {
435           struct string vstr;
436           ds_init_empty (&vstr);
437           gs = gs_array[count];
438
439           var_append_value_name (indep_var, &gs->id, &vstr);
440
441           tab_text (t, 1, row + count,
442                     TAB_LEFT | TAT_TITLE,
443                     ds_cstr (&vstr));
444
445           ds_destroy (&vstr);
446
447           /* Now fill in the numbers ... */
448
449           tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
450
451           tab_double (t, 3, row + count, 0, gs->mean, NULL);
452
453           tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
454
455           std_error = gs->std_dev / sqrt (gs->n) ;
456           tab_double (t, 5, row + count, 0,
457                      std_error, NULL);
458
459           /* Now the confidence interval */
460
461           T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
462
463           tab_double (t, 6, row + count, 0,
464                     gs->mean - T * std_error, NULL);
465
466           tab_double (t, 7, row + count, 0,
467                     gs->mean + T * std_error, NULL);
468
469           /* Min and Max */
470
471           tab_double (t, 8, row + count, 0,  gs->minimum, fmt);
472           tab_double (t, 9, row + count, 0,  gs->maximum, fmt);
473         }
474
475       tab_text (t, 1, row + count,
476                 TAB_LEFT | TAT_TITLE, _("Total"));
477
478       tab_double (t, 2, row + count, 0, totals->n, wfmt);
479
480       tab_double (t, 3, row + count, 0, totals->mean, NULL);
481
482       tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
483
484       std_error = totals->std_dev / sqrt (totals->n) ;
485
486       tab_double (t, 5, row + count, 0, std_error, NULL);
487
488       /* Now the confidence interval */
489
490       T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
491
492       tab_double (t, 6, row + count, 0,
493                   totals->mean - T * std_error, NULL);
494
495       tab_double (t, 7, row + count, 0,
496                   totals->mean + T * std_error, NULL);
497
498       /* Min and Max */
499
500       tab_double (t, 8, row + count, 0,  totals->minimum, fmt);
501       tab_double (t, 9, row + count, 0,  totals->maximum, fmt);
502
503       row += gp->n_groups + 1;
504     }
505
506   tab_submit (t);
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, NULL);
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   tab_title (t, _("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_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
558       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
559       tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
560
561       /* Now the significance */
562       tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
563     }
564
565   tab_submit (t);
566 }
567
568
569 /* Show the contrast coefficients table */
570 static void
571 show_contrast_coeffs (short *bad_contrast)
572 {
573   int n_cols = 2 + ostensible_number_of_groups;
574   int n_rows = 2 + cmd.sbc_contrast;
575   int count = 0;
576   void *const *group_values;
577
578   struct tab_table *t;
579
580   t = tab_create (n_cols, n_rows, 0);
581   tab_headers (t, 2, 0, 2, 0);
582   tab_dim (t, tab_natural_dimensions, NULL);
583
584   /* Put a frame around the entire box, and vertical lines inside */
585   tab_box (t,
586            TAL_2, TAL_2,
587            -1, TAL_1,
588            0, 0,
589            n_cols - 1, n_rows - 1);
590
591   tab_box (t,
592            -1, -1,
593            TAL_0, TAL_0,
594            2, 0,
595            n_cols - 1, 0);
596
597   tab_box (t,
598            -1, -1,
599            TAL_0, TAL_0,
600            0, 0,
601            1, 1);
602
603   tab_hline (t, TAL_1, 2, n_cols - 1, 1);
604   tab_hline (t, TAL_2, 0, n_cols - 1, 2);
605
606   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
607
608   tab_title (t, _("Contrast Coefficients"));
609
610   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
611
612
613   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
614                   var_to_string (indep_var));
615
616   group_values = hsh_sort (global_group_hash);
617   for (count = 0;
618        count < hsh_count (global_group_hash);
619        ++count)
620     {
621       double *group_value_p;
622       union value group_value;
623       int i;
624       struct string vstr;
625
626       ds_init_empty (&vstr);
627
628       group_value_p = group_values[count];
629       group_value.f = *group_value_p;
630       var_append_value_name (indep_var, &group_value, &vstr);
631
632       tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
633                 ds_cstr (&vstr));
634
635       ds_destroy (&vstr);
636
637
638       for (i = 0; i < cmd.sbc_contrast; ++i )
639         {
640           tab_text_format (t, 1, i + 2, TAB_CENTER, "%d", i + 1);
641
642           if ( bad_contrast[i] )
643             tab_text (t, count + 2, i + 2, TAB_RIGHT, "?" );
644           else
645             tab_text_format (t, count + 2, i + 2, TAB_RIGHT, "%g",
646                              subc_list_double_at (&cmd.dl_contrast[i], count));
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, NULL);
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_format (t,  2, (v * lines_per_variable) + i + 1,
744                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
745
746
747           tab_text_format (t,  2,
748                            (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
749                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
750
751
752           if ( bad_contrast[i])
753             continue;
754
755           group_stat_array = hsh_sort (group_hash);
756
757           for (ci = 0; ci < hsh_count (group_hash);  ++ci)
758             {
759               const double coef = subc_list_double_at (&cmd.dl_contrast[i], ci);
760               struct group_statistics *gs = group_stat_array[ci];
761
762               const double winv = pow2 (gs->std_dev) / gs->n;
763
764               contrast_value += coef * gs->mean;
765
766               coef_msq += (coef * coef) / gs->n;
767
768               sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
769
770               df_numerator += (coef * coef) * winv;
771               df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
772             }
773           sec_vneq = sqrt (sec_vneq);
774
775           df_numerator = pow2 (df_numerator);
776
777           tab_double (t,  3, (v * lines_per_variable) + i + 1,
778                      TAB_RIGHT, contrast_value, NULL);
779
780           tab_double (t,  3, (v * lines_per_variable) + i + 1 +
781                      cmd.sbc_contrast,
782                      TAB_RIGHT, contrast_value, NULL);
783
784           std_error_contrast = sqrt (grp_data->mse * coef_msq);
785
786           /* Std. Error */
787           tab_double (t,  4, (v * lines_per_variable) + i + 1,
788                      TAB_RIGHT, std_error_contrast,
789                      NULL);
790
791           T = fabs (contrast_value / std_error_contrast);
792
793           /* T Statistic */
794
795           tab_double (t,  5, (v * lines_per_variable) + i + 1,
796                      TAB_RIGHT, T,
797                      NULL);
798
799           df = grp_data->ugs.n - grp_data->n_groups;
800
801           /* Degrees of Freedom */
802           tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
803                      TAB_RIGHT,  df,
804                      8, 0);
805
806
807           /* Significance TWO TAILED !!*/
808           tab_double (t,  7, (v * lines_per_variable) + i + 1,
809                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
810                      NULL);
811
812           /* Now for the Variances NOT Equal case */
813
814           /* Std. Error */
815           tab_double (t,  4,
816                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
817                      TAB_RIGHT, sec_vneq,
818                      NULL);
819
820           T = contrast_value / sec_vneq;
821           tab_double (t,  5,
822                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
823                      TAB_RIGHT, T,
824                      NULL);
825
826           df = df_numerator / df_denominator;
827
828           tab_double (t,  6,
829                      (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
830                      TAB_RIGHT, df,
831                      NULL);
832
833           /* The Significance */
834
835           tab_double (t, 7, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
836                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
837                      NULL);
838         }
839
840       if ( v > 0 )
841         tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
842     }
843
844   tab_submit (t);
845 }
846
847
848 /* ONEWAY ANOVA Calculations */
849
850 static void  postcalc (struct cmd_oneway *cmd UNUSED);
851
852 static void  precalc (struct cmd_oneway *cmd UNUSED);
853
854
855
856 /* Pre calculations */
857 static void
858 precalc (struct cmd_oneway *cmd UNUSED)
859 {
860   size_t i = 0;
861
862   for (i = 0; i < n_vars; ++i)
863     {
864       struct group_proc *gp = group_proc_get (vars[i]);
865       struct group_statistics *totals = &gp->ugs;
866
867       /* Create a hash for each of the dependent variables.
868          The hash contains a group_statistics structure,
869          and is keyed by value of the independent variable */
870
871       gp->group_hash = hsh_create (4, compare_group, hash_group,
872                                    (hsh_free_func *) free_group,
873                                    indep_var);
874
875       totals->sum = 0;
876       totals->n = 0;
877       totals->ssq = 0;
878       totals->sum_diff = 0;
879       totals->maximum = -DBL_MAX;
880       totals->minimum = DBL_MAX;
881     }
882 }
883
884 static int
885 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
886 {
887   const double *a = a_;
888   const double *b = b_;
889   return *a < *b ? -1 : *a > *b;
890 }
891
892 static unsigned
893 do_hash_double (const void *value_, const void *aux UNUSED)
894 {
895   const double *value = value_;
896   return hash_double (*value, 0);
897 }
898
899 static void
900 free_double (void *value_, const void *aux UNUSED)
901 {
902   double *value = value_;
903   free (value);
904 }
905
906 static void
907 run_oneway (struct cmd_oneway *cmd,
908             struct casereader *input,
909             const struct dataset *ds)
910 {
911   struct taint *taint;
912   struct dictionary *dict = dataset_dict (ds);
913   enum mv_class exclude;
914   struct casereader *reader;
915   struct ccase *c;
916
917   c = casereader_peek (input, 0);
918   if (c == NULL)
919     {
920       casereader_destroy (input);
921       return;
922     }
923   output_split_file_values (ds, c);
924   case_unref (c);
925
926   taint = taint_clone (casereader_get_taint (input));
927
928   global_group_hash = hsh_create (4,
929                                   compare_double_3way,
930                                   do_hash_double,
931                                   free_double,
932                                   indep_var);
933
934   precalc (cmd);
935
936   exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
937   input = casereader_create_filter_missing (input, &indep_var, 1,
938                                             exclude, NULL, NULL);
939   if (cmd->miss == ONEWAY_LISTWISE)
940     input = casereader_create_filter_missing (input, vars, n_vars,
941                                               exclude, NULL, NULL);
942   input = casereader_create_filter_weight (input, dict, NULL, NULL);
943
944   reader = casereader_clone (input);
945   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
946     {
947       size_t i;
948
949       const double weight = dict_get_case_weight (dict, c, NULL);
950
951       const union value *indep_val = case_data (c, indep_var);
952       void **p = hsh_probe (global_group_hash, &indep_val->f);
953       if (*p == NULL)
954         {
955           double *value = *p = xmalloc (sizeof *value);
956           *value = indep_val->f;
957         }
958
959       for (i = 0; i < n_vars; ++i)
960         {
961           const struct variable *v = vars[i];
962
963           const union value *val = case_data (c, v);
964
965           struct group_proc *gp = group_proc_get (vars[i]);
966           struct hsh_table *group_hash = gp->group_hash;
967
968           struct group_statistics *gs;
969
970           gs = hsh_find (group_hash, indep_val );
971
972           if ( ! gs )
973             {
974               gs = xmalloc (sizeof *gs);
975               gs->id = *indep_val;
976               gs->sum = 0;
977               gs->n = 0;
978               gs->ssq = 0;
979               gs->sum_diff = 0;
980               gs->minimum = DBL_MAX;
981               gs->maximum = -DBL_MAX;
982
983               hsh_insert ( group_hash, gs );
984             }
985
986           if (!var_is_value_missing (v, val, exclude))
987             {
988               struct group_statistics *totals = &gp->ugs;
989
990               totals->n += weight;
991               totals->sum += weight * val->f;
992               totals->ssq += weight * pow2 (val->f);
993
994               if ( val->f * weight  < totals->minimum )
995                 totals->minimum = val->f * weight;
996
997               if ( val->f * weight  > totals->maximum )
998                 totals->maximum = val->f * weight;
999
1000               gs->n += weight;
1001               gs->sum += weight * val->f;
1002               gs->ssq += weight * pow2 (val->f);
1003
1004               if ( val->f * weight  < gs->minimum )
1005                 gs->minimum = val->f * weight;
1006
1007               if ( val->f * weight  > gs->maximum )
1008                 gs->maximum = val->f * weight;
1009             }
1010
1011           gp->n_groups = hsh_count ( group_hash );
1012         }
1013
1014     }
1015   casereader_destroy (reader);
1016
1017   postcalc (cmd);
1018
1019
1020   if ( stat_tables & STAT_HOMO )
1021     levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
1022
1023   casereader_destroy (input);
1024
1025   ostensible_number_of_groups = hsh_count (global_group_hash);
1026
1027   if (!taint_has_tainted_successor (taint))
1028     output_oneway (dict);
1029
1030   taint_destroy (taint);
1031 }
1032
1033
1034 /* Post calculations for the ONEWAY command */
1035 void
1036 postcalc (  struct cmd_oneway *cmd UNUSED )
1037 {
1038   size_t i = 0;
1039
1040   for (i = 0; i < n_vars; ++i)
1041     {
1042       struct group_proc *gp = group_proc_get (vars[i]);
1043       struct hsh_table *group_hash = gp->group_hash;
1044       struct group_statistics *totals = &gp->ugs;
1045
1046       struct hsh_iterator g;
1047       struct group_statistics *gs;
1048
1049       for (gs =  hsh_first (group_hash, &g);
1050            gs != 0;
1051            gs = hsh_next (group_hash, &g))
1052         {
1053           gs->mean = gs->sum / gs->n;
1054           gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
1055
1056           gs->std_dev = sqrt (
1057                               gs->n / (gs->n - 1) *
1058                               ( gs->ssq / gs->n - pow2 (gs->mean))
1059                               );
1060
1061           gs->se_mean = gs->std_dev / sqrt (gs->n);
1062           gs->mean_diff = gs->sum_diff / gs->n;
1063         }
1064
1065       totals->mean = totals->sum / totals->n;
1066       totals->std_dev = sqrt (
1067                               totals->n / (totals->n - 1) *
1068                               (totals->ssq / totals->n - pow2 (totals->mean))
1069                               );
1070
1071       totals->se_mean = totals->std_dev / sqrt (totals->n);
1072     }
1073 }
1074
1075 /*
1076   Local Variables:
1077   mode: c
1078   End:
1079 */