1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <data/case.h>
20 #include <data/casegrouper.h>
21 #include <data/casereader.h>
23 #include <libpspp/ll.h>
25 #include <language/lexer/lexer.h>
26 #include <language/lexer/variable-parser.h>
27 #include <language/lexer/value-parser.h>
28 #include <language/command.h>
30 #include <data/procedure.h>
31 #include <data/dictionary.h>
34 #include <language/dictionary/split-file.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/taint.h>
37 #include <math/group-proc.h>
38 #include <math/levene.h>
39 #include <libpspp/misc.h>
41 #include <output/tab.h>
43 #include <gsl/gsl_cdf.h>
45 #include <data/format.h>
47 #include <libpspp/message.h>
50 #define _(msgid) gettext (msgid)
60 STATS_DESCRIPTIVES = 0x0001,
61 STATS_HOMOGENEITY = 0x0002
74 struct ll_list coefficient_list;
76 bool bad_count; /* True if the number of coefficients does not equal the number of groups */
82 const struct variable **vars;
84 const struct variable *indep_var;
86 enum statistics stats;
88 enum missing_type missing_type;
89 enum mv_class exclude;
91 /* The number of distinct values of the independent variable, when all
92 missing values are disregarded */
93 int actual_number_of_groups;
95 /* A hash table containing all the distinct values of the independent
97 struct hsh_table *group_hash;
99 /* List of contrasts */
100 struct ll_list contrast_list;
103 /* Routines to show the output tables */
104 static void show_anova_table (const struct oneway *);
105 static void show_descriptives (const struct oneway *, const struct dictionary *dict);
106 static void show_homogeneity (const struct oneway *);
108 static void output_oneway (const struct oneway *, const struct dictionary *dict);
109 static void run_oneway (struct oneway *cmd, struct casereader *input, const struct dataset *ds);
112 cmd_oneway (struct lexer *lexer, struct dataset *ds)
114 const struct dictionary *dict = dataset_dict (ds);
116 struct oneway oneway ;
119 oneway.indep_var = NULL;
121 oneway.missing_type = MISS_ANALYSIS;
122 oneway.exclude = MV_ANY;
123 oneway.actual_number_of_groups = 0;
124 oneway.group_hash = NULL;
126 ll_init (&oneway.contrast_list);
129 if ( lex_match (lexer, '/'))
131 if (!lex_force_match_id (lexer, "VARIABLES"))
135 lex_match (lexer, '=');
138 if (!parse_variables_const (lexer, dict,
139 &oneway.vars, &oneway.n_vars,
140 PV_NO_DUPLICATE | PV_NUMERIC))
143 lex_force_match (lexer, T_BY);
145 oneway.indep_var = parse_variable_const (lexer, dict);
147 while (lex_token (lexer) != '.')
149 lex_match (lexer, '/');
151 if (lex_match_id (lexer, "STATISTICS"))
153 lex_match (lexer, '=');
154 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
156 if (lex_match_id (lexer, "DESCRIPTIVES"))
158 oneway.stats |= STATS_DESCRIPTIVES;
160 else if (lex_match_id (lexer, "HOMOGENEITY"))
162 oneway.stats |= STATS_HOMOGENEITY;
166 lex_error (lexer, NULL);
171 else if (lex_match_id (lexer, "CONTRAST"))
173 struct contrasts_node *cl = xzalloc (sizeof *cl);
175 struct ll_list *coefficient_list = &cl->coefficient_list;
176 lex_match (lexer, '=');
178 ll_init (coefficient_list);
180 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
183 if ( parse_value (lexer, &val, 0))
185 struct coeff_node *cc = xmalloc (sizeof *cc);
188 ll_push_tail (coefficient_list, &cc->ll);
192 lex_error (lexer, NULL);
197 ll_push_tail (&oneway.contrast_list, &cl->ll);
199 else if (lex_match_id (lexer, "MISSING"))
201 lex_match (lexer, '=');
202 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
204 if (lex_match_id (lexer, "INCLUDE"))
206 oneway.exclude = MV_SYSTEM;
208 else if (lex_match_id (lexer, "EXCLUDE"))
210 oneway.exclude = MV_ANY;
212 else if (lex_match_id (lexer, "LISTWISE"))
214 oneway.missing_type = MISS_LISTWISE;
216 else if (lex_match_id (lexer, "ANALYSIS"))
218 oneway.missing_type = MISS_ANALYSIS;
222 lex_error (lexer, NULL);
229 lex_error (lexer, NULL);
236 struct casegrouper *grouper;
237 struct casereader *group;
240 grouper = casegrouper_create_splits (proc_open (ds), dict);
241 while (casegrouper_get_next_group (grouper, &group))
242 run_oneway (&oneway, group, ds);
243 ok = casegrouper_destroy (grouper);
244 ok = proc_commit (ds) && ok;
259 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
261 const double *a = a_;
262 const double *b = b_;
263 return *a < *b ? -1 : *a > *b;
267 do_hash_double (const void *value_, const void *aux UNUSED)
269 const double *value = value_;
270 return hash_double (*value, 0);
274 free_double (void *value_, const void *aux UNUSED)
276 double *value = value_;
280 static void postcalc (const struct oneway *cmd);
281 static void precalc (const struct oneway *cmd);
285 run_oneway (struct oneway *cmd,
286 struct casereader *input,
287 const struct dataset *ds)
290 struct dictionary *dict = dataset_dict (ds);
291 struct casereader *reader;
294 c = casereader_peek (input, 0);
297 casereader_destroy (input);
300 output_split_file_values (ds, c);
303 taint = taint_clone (casereader_get_taint (input));
305 cmd->group_hash = hsh_create (4,
313 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
314 cmd->exclude, NULL, NULL);
315 if (cmd->missing_type == MISS_LISTWISE)
316 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
317 cmd->exclude, NULL, NULL);
318 input = casereader_create_filter_weight (input, dict, NULL, NULL);
320 reader = casereader_clone (input);
321 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
325 const double weight = dict_get_case_weight (dict, c, NULL);
327 const union value *indep_val = case_data (c, cmd->indep_var);
328 void **p = hsh_probe (cmd->group_hash, &indep_val->f);
331 double *value = *p = xmalloc (sizeof *value);
332 *value = indep_val->f;
335 for (i = 0; i < cmd->n_vars; ++i)
337 const struct variable *v = cmd->vars[i];
339 const union value *val = case_data (c, v);
341 struct group_proc *gp = group_proc_get (cmd->vars[i]);
342 struct hsh_table *group_hash = gp->group_hash;
344 struct group_statistics *gs;
346 gs = hsh_find (group_hash, indep_val );
350 gs = xmalloc (sizeof *gs);
356 gs->minimum = DBL_MAX;
357 gs->maximum = -DBL_MAX;
359 hsh_insert ( group_hash, gs );
362 if (!var_is_value_missing (v, val, cmd->exclude))
364 struct group_statistics *totals = &gp->ugs;
367 totals->sum += weight * val->f;
368 totals->ssq += weight * pow2 (val->f);
370 if ( val->f * weight < totals->minimum )
371 totals->minimum = val->f * weight;
373 if ( val->f * weight > totals->maximum )
374 totals->maximum = val->f * weight;
377 gs->sum += weight * val->f;
378 gs->ssq += weight * pow2 (val->f);
380 if ( val->f * weight < gs->minimum )
381 gs->minimum = val->f * weight;
383 if ( val->f * weight > gs->maximum )
384 gs->maximum = val->f * weight;
387 gp->n_groups = hsh_count (group_hash );
391 casereader_destroy (reader);
395 if ( cmd->stats & STATS_HOMOGENEITY )
396 levene (dict, casereader_clone (input), cmd->indep_var,
397 cmd->n_vars, cmd->vars, cmd->exclude);
399 casereader_destroy (input);
401 cmd->actual_number_of_groups = hsh_count (cmd->group_hash);
403 if (!taint_has_tainted_successor (taint))
404 output_oneway (cmd, dict);
406 taint_destroy (taint);
409 /* Pre calculations */
411 precalc (const struct oneway *cmd)
415 for (i = 0; i < cmd->n_vars; ++i)
417 struct group_proc *gp = group_proc_get (cmd->vars[i]);
418 struct group_statistics *totals = &gp->ugs;
420 /* Create a hash for each of the dependent variables.
421 The hash contains a group_statistics structure,
422 and is keyed by value of the independent variable */
424 gp->group_hash = hsh_create (4, compare_group, hash_group,
425 (hsh_free_func *) free_group,
431 totals->sum_diff = 0;
432 totals->maximum = -DBL_MAX;
433 totals->minimum = DBL_MAX;
437 /* Post calculations for the ONEWAY command */
439 postcalc (const struct oneway *cmd)
443 for (i = 0; i < cmd->n_vars; ++i)
445 struct group_proc *gp = group_proc_get (cmd->vars[i]);
446 struct hsh_table *group_hash = gp->group_hash;
447 struct group_statistics *totals = &gp->ugs;
449 struct hsh_iterator g;
450 struct group_statistics *gs;
452 for (gs = hsh_first (group_hash, &g);
454 gs = hsh_next (group_hash, &g))
456 gs->mean = gs->sum / gs->n;
457 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
460 gs->n / (gs->n - 1) *
461 ( gs->ssq / gs->n - pow2 (gs->mean))
464 gs->se_mean = gs->std_dev / sqrt (gs->n);
465 gs->mean_diff = gs->sum_diff / gs->n;
468 totals->mean = totals->sum / totals->n;
469 totals->std_dev = sqrt (
470 totals->n / (totals->n - 1) *
471 (totals->ssq / totals->n - pow2 (totals->mean))
474 totals->se_mean = totals->std_dev / sqrt (totals->n);
478 static void show_contrast_coeffs (const struct oneway *cmd);
479 static void show_contrast_tests (const struct oneway *cmd);
482 output_oneway (const struct oneway *cmd, const struct dictionary *dict)
486 /* Check the sanity of the given contrast values */
487 struct contrasts_node *coeff_list = NULL;
488 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
490 struct coeff_node *cn = NULL;
492 struct ll_list *cl = &coeff_list->coefficient_list;
495 if (ll_count (cl) != cmd->actual_number_of_groups)
498 _("Number of contrast coefficients must equal the number of groups"));
499 coeff_list->bad_count = true;
503 ll_for_each (cn, struct coeff_node, ll, cl)
507 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
510 if (cmd->stats & STATS_DESCRIPTIVES)
511 show_descriptives (cmd, dict);
513 if (cmd->stats & STATS_HOMOGENEITY)
514 show_homogeneity (cmd);
516 show_anova_table (cmd);
519 if (ll_count (&cmd->contrast_list) > 0)
521 show_contrast_coeffs (cmd);
522 show_contrast_tests (cmd);
527 for (i = 0; i < cmd->n_vars; ++i )
529 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
531 hsh_destroy (group_hash);
534 hsh_destroy (cmd->group_hash);
538 /* Show the ANOVA table */
540 show_anova_table (const struct oneway *cmd)
544 size_t n_rows = cmd->n_vars * 3 + 1;
546 struct tab_table *t = tab_create (n_cols, n_rows);
548 tab_headers (t, 2, 0, 1, 0);
554 n_cols - 1, n_rows - 1);
556 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
557 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
558 tab_vline (t, TAL_0, 1, 0, 0);
560 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
561 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
562 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
563 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
564 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
567 for (i = 0; i < cmd->n_vars; ++i)
569 struct group_statistics *totals = &group_proc_get (cmd->vars[i])->ugs;
570 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
571 struct hsh_iterator g;
572 struct group_statistics *gs;
574 const char *s = var_to_string (cmd->vars[i]);
576 for (gs = hsh_first (group_hash, &g);
578 gs = hsh_next (group_hash, &g))
580 ssa += pow2 (gs->sum) / gs->n;
583 ssa -= pow2 (totals->sum) / totals->n;
585 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
586 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
587 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
588 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
591 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
594 struct group_proc *gp = group_proc_get (cmd->vars[i]);
595 const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
596 const double df1 = gp->n_groups - 1;
597 const double df2 = totals->n - gp->n_groups;
598 const double msa = ssa / df1;
600 gp->mse = (sst - ssa) / df2;
603 /* Sums of Squares */
604 tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
605 tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
606 tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
609 /* Degrees of freedom */
610 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
611 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
612 tab_fixed (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
615 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
616 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
619 const double F = msa / gp->mse ;
622 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
624 /* The significance */
625 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
631 tab_title (t, _("ANOVA"));
636 /* Show the descriptives table */
638 show_descriptives (const struct oneway *cmd, const struct dictionary *dict)
645 const double confidence = 0.95;
646 const double q = (1.0 - confidence) / 2.0;
648 const struct variable *wv = dict_get_weight (dict);
649 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
653 for ( v = 0; v < cmd->n_vars; ++v )
654 n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
656 t = tab_create (n_cols, n_rows);
657 tab_headers (t, 2, 0, 2, 0);
660 /* Put a frame around the entire box, and vertical lines inside */
665 n_cols - 1, n_rows - 1);
667 /* Underline headers */
668 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
669 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
671 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
672 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
673 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
674 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
677 tab_vline (t, TAL_0, 7, 0, 0);
678 tab_hline (t, TAL_1, 6, 7, 1);
679 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
680 _("%g%% Confidence Interval for Mean"),
683 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
684 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
686 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
687 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
690 tab_title (t, _("Descriptives"));
694 for (v = 0; v < cmd->n_vars; ++v)
699 struct group_proc *gp = group_proc_get (cmd->vars[v]);
701 struct group_statistics *gs;
702 struct group_statistics *totals = &gp->ugs;
704 const char *s = var_to_string (cmd->vars[v]);
705 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
707 struct group_statistics *const *gs_array =
708 (struct group_statistics *const *) hsh_sort (gp->group_hash);
711 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
713 tab_hline (t, TAL_1, 0, n_cols - 1, row);
715 for (count = 0; count < hsh_count (gp->group_hash); ++count)
718 ds_init_empty (&vstr);
719 gs = gs_array[count];
721 var_append_value_name (cmd->indep_var, &gs->id, &vstr);
723 tab_text (t, 1, row + count,
724 TAB_LEFT | TAT_TITLE,
729 /* Now fill in the numbers ... */
731 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
733 tab_double (t, 3, row + count, 0, gs->mean, NULL);
735 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
737 std_error = gs->std_dev / sqrt (gs->n) ;
738 tab_double (t, 5, row + count, 0,
741 /* Now the confidence interval */
743 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
745 tab_double (t, 6, row + count, 0,
746 gs->mean - T * std_error, NULL);
748 tab_double (t, 7, row + count, 0,
749 gs->mean + T * std_error, NULL);
753 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
754 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
757 tab_text (t, 1, row + count,
758 TAB_LEFT | TAT_TITLE, _("Total"));
760 tab_double (t, 2, row + count, 0, totals->n, wfmt);
762 tab_double (t, 3, row + count, 0, totals->mean, NULL);
764 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
766 std_error = totals->std_dev / sqrt (totals->n) ;
768 tab_double (t, 5, row + count, 0, std_error, NULL);
770 /* Now the confidence interval */
772 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
774 tab_double (t, 6, row + count, 0,
775 totals->mean - T * std_error, NULL);
777 tab_double (t, 7, row + count, 0,
778 totals->mean + T * std_error, NULL);
782 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
783 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
785 row += gp->n_groups + 1;
791 /* Show the homogeneity table */
793 show_homogeneity (const struct oneway *cmd)
797 size_t n_rows = cmd->n_vars + 1;
802 t = tab_create (n_cols, n_rows);
803 tab_headers (t, 1, 0, 1, 0);
806 /* Put a frame around the entire box, and vertical lines inside */
811 n_cols - 1, n_rows - 1);
814 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
815 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
818 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
819 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
820 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
821 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
823 tab_title (t, _("Test of Homogeneity of Variances"));
825 for (v = 0; v < cmd->n_vars; ++v)
828 const struct variable *var = cmd->vars[v];
829 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
830 const char *s = var_to_string (var);
831 const struct group_statistics *totals = &gp->ugs;
833 const double df1 = gp->n_groups - 1;
834 const double df2 = totals->n - gp->n_groups;
836 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
839 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
840 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
841 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
843 /* Now the significance */
844 tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
851 /* Show the contrast coefficients table */
853 show_contrast_coeffs (const struct oneway *cmd)
858 int n_contrasts = ll_count (&cmd->contrast_list);
859 int n_cols = 2 + cmd->actual_number_of_groups;
860 int n_rows = 2 + n_contrasts;
862 void *const *group_values;
866 t = tab_create (n_cols, n_rows);
867 tab_headers (t, 2, 0, 2, 0);
869 /* Put a frame around the entire box, and vertical lines inside */
874 n_cols - 1, n_rows - 1);
888 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
889 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
891 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
893 tab_title (t, _("Contrast Coefficients"));
895 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
898 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
899 var_to_string (cmd->indep_var));
901 group_values = hsh_sort (cmd->group_hash);
903 for ( cli = ll_head (&cmd->contrast_list);
904 cli != ll_null (&cmd->contrast_list);
908 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
909 struct ll *coeffi = ll_head (&cn->coefficient_list);
911 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
914 count < hsh_count (cmd->group_hash) && coeffi != ll_null (&cn->coefficient_list);
917 double *group_value_p;
918 union value group_value;
921 ds_init_empty (&vstr);
923 group_value_p = group_values[count];
924 group_value.f = *group_value_p;
925 var_append_value_name (cmd->indep_var, &group_value, &vstr);
927 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
933 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
936 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
938 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
941 coeffi = ll_next (coeffi);
950 /* Show the results of the contrast tests */
952 show_contrast_tests (const struct oneway *cmd)
954 int n_contrasts = ll_count (&cmd->contrast_list);
957 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
961 t = tab_create (n_cols, n_rows);
962 tab_headers (t, 3, 0, 1, 0);
964 /* Put a frame around the entire box, and vertical lines inside */
969 n_cols - 1, n_rows - 1);
977 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
978 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
981 tab_title (t, _("Contrast Tests"));
983 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
984 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
985 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
986 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
987 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
988 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
990 for (v = 0; v < cmd->n_vars; ++v)
994 int lines_per_variable = 2 * n_contrasts;
996 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
997 var_to_string (cmd->vars[v]));
999 for ( cli = ll_head (&cmd->contrast_list);
1000 cli != ll_null (&cmd->contrast_list);
1001 ++i, cli = ll_next (cli))
1003 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1004 struct ll *coeffi = ll_head (&cn->coefficient_list);
1006 double contrast_value = 0.0;
1007 double coef_msq = 0.0;
1008 struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1009 struct hsh_table *group_hash = grp_data->group_hash;
1011 void *const *group_stat_array;
1014 double std_error_contrast;
1016 double sec_vneq = 0.0;
1018 /* Note: The calculation of the degrees of freedom in the
1019 "variances not equal" case is painfull!!
1020 The following formula may help to understand it:
1021 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1024 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1029 double df_denominator = 0.0;
1030 double df_numerator = 0.0;
1033 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1034 TAB_LEFT | TAT_TITLE,
1035 _("Assume equal variances"));
1037 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1038 TAB_LEFT | TAT_TITLE,
1039 _("Does not assume equal"));
1042 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1043 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1046 tab_text_format (t, 2,
1047 (v * lines_per_variable) + i + 1 + n_contrasts,
1048 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1053 group_stat_array = hsh_sort (group_hash);
1056 coeffi != ll_null (&cn->coefficient_list) &&
1057 ci < hsh_count (group_hash);
1058 ++ci, coeffi = ll_next (coeffi))
1060 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1061 const double coef = cn->coeff;
1062 struct group_statistics *gs = group_stat_array[ci];
1064 const double winv = pow2 (gs->std_dev) / gs->n;
1066 contrast_value += coef * gs->mean;
1068 coef_msq += (coef * coef) / gs->n;
1070 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1072 df_numerator += (coef * coef) * winv;
1073 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1076 sec_vneq = sqrt (sec_vneq);
1078 df_numerator = pow2 (df_numerator);
1080 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1081 TAB_RIGHT, contrast_value, NULL);
1083 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1085 TAB_RIGHT, contrast_value, NULL);
1087 std_error_contrast = sqrt (grp_data->mse * coef_msq);
1090 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1091 TAB_RIGHT, std_error_contrast,
1094 T = fabs (contrast_value / std_error_contrast);
1098 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1102 df = grp_data->ugs.n - grp_data->n_groups;
1104 /* Degrees of Freedom */
1105 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1110 /* Significance TWO TAILED !!*/
1111 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1112 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1115 /* Now for the Variances NOT Equal case */
1119 (v * lines_per_variable) + i + 1 + n_contrasts,
1120 TAB_RIGHT, sec_vneq,
1123 T = contrast_value / sec_vneq;
1125 (v * lines_per_variable) + i + 1 + n_contrasts,
1129 df = df_numerator / df_denominator;
1132 (v * lines_per_variable) + i + 1 + n_contrasts,
1136 /* The Significance */
1137 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1138 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1143 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);