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 dictionary *dict;
86 const struct variable *indep_var;
88 enum statistics stats;
90 enum missing_type missing_type;
91 enum mv_class exclude;
93 /* List of contrasts */
94 struct ll_list contrast_list;
97 struct oneway_workspace
99 /* The number of distinct values of the independent variable, when all
100 missing values are disregarded */
101 int actual_number_of_groups;
103 /* A hash table containing all the distinct values of the independent
105 struct hsh_table *group_hash;
108 /* Routines to show the output tables */
109 static void show_anova_table (const struct oneway_spec *);
110 static void show_descriptives (const struct oneway_spec *, const struct dictionary *dict);
111 static void show_homogeneity (const struct oneway_spec *);
113 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
114 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
117 cmd_oneway (struct lexer *lexer, struct dataset *ds)
119 struct oneway_spec oneway ;
122 oneway.indep_var = NULL;
124 oneway.missing_type = MISS_ANALYSIS;
125 oneway.exclude = MV_ANY;
126 oneway.dict = dataset_dict (ds);
128 ll_init (&oneway.contrast_list);
131 if ( lex_match (lexer, '/'))
133 if (!lex_force_match_id (lexer, "VARIABLES"))
137 lex_match (lexer, '=');
140 if (!parse_variables_const (lexer, oneway.dict,
141 &oneway.vars, &oneway.n_vars,
142 PV_NO_DUPLICATE | PV_NUMERIC))
145 lex_force_match (lexer, T_BY);
147 oneway.indep_var = parse_variable_const (lexer, oneway.dict);
149 while (lex_token (lexer) != '.')
151 lex_match (lexer, '/');
153 if (lex_match_id (lexer, "STATISTICS"))
155 lex_match (lexer, '=');
156 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
158 if (lex_match_id (lexer, "DESCRIPTIVES"))
160 oneway.stats |= STATS_DESCRIPTIVES;
162 else if (lex_match_id (lexer, "HOMOGENEITY"))
164 oneway.stats |= STATS_HOMOGENEITY;
168 lex_error (lexer, NULL);
173 else if (lex_match_id (lexer, "CONTRAST"))
175 struct contrasts_node *cl = xzalloc (sizeof *cl);
177 struct ll_list *coefficient_list = &cl->coefficient_list;
178 lex_match (lexer, '=');
180 ll_init (coefficient_list);
182 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
185 if ( parse_value (lexer, &val, 0))
187 struct coeff_node *cc = xmalloc (sizeof *cc);
190 ll_push_tail (coefficient_list, &cc->ll);
194 lex_error (lexer, NULL);
199 ll_push_tail (&oneway.contrast_list, &cl->ll);
201 else if (lex_match_id (lexer, "MISSING"))
203 lex_match (lexer, '=');
204 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
206 if (lex_match_id (lexer, "INCLUDE"))
208 oneway.exclude = MV_SYSTEM;
210 else if (lex_match_id (lexer, "EXCLUDE"))
212 oneway.exclude = MV_ANY;
214 else if (lex_match_id (lexer, "LISTWISE"))
216 oneway.missing_type = MISS_LISTWISE;
218 else if (lex_match_id (lexer, "ANALYSIS"))
220 oneway.missing_type = MISS_ANALYSIS;
224 lex_error (lexer, NULL);
231 lex_error (lexer, NULL);
238 struct casegrouper *grouper;
239 struct casereader *group;
242 grouper = casegrouper_create_splits (proc_open (ds), oneway.dict);
243 while (casegrouper_get_next_group (grouper, &group))
244 run_oneway (&oneway, group, ds);
245 ok = casegrouper_destroy (grouper);
246 ok = proc_commit (ds) && ok;
261 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
263 const double *a = a_;
264 const double *b = b_;
265 return *a < *b ? -1 : *a > *b;
269 do_hash_double (const void *value_, const void *aux UNUSED)
271 const double *value = value_;
272 return hash_double (*value, 0);
276 free_double (void *value_, const void *aux UNUSED)
278 double *value = value_;
282 static void postcalc (const struct oneway_spec *cmd);
283 static void precalc (const struct oneway_spec *cmd);
287 run_oneway (const struct oneway_spec *cmd,
288 struct casereader *input,
289 const struct dataset *ds)
292 struct dictionary *dict = dataset_dict (ds);
293 struct casereader *reader;
296 struct oneway_workspace ws;
298 c = casereader_peek (input, 0);
301 casereader_destroy (input);
304 output_split_file_values (ds, c);
307 taint = taint_clone (casereader_get_taint (input));
309 ws.group_hash = hsh_create (4,
317 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
318 cmd->exclude, NULL, NULL);
319 if (cmd->missing_type == MISS_LISTWISE)
320 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
321 cmd->exclude, NULL, NULL);
322 input = casereader_create_filter_weight (input, dict, NULL, NULL);
324 reader = casereader_clone (input);
325 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
329 const double weight = dict_get_case_weight (dict, c, NULL);
331 const union value *indep_val = case_data (c, cmd->indep_var);
332 void **p = hsh_probe (ws.group_hash, &indep_val->f);
335 double *value = *p = xmalloc (sizeof *value);
336 *value = indep_val->f;
339 for (i = 0; i < cmd->n_vars; ++i)
341 const struct variable *v = cmd->vars[i];
343 const union value *val = case_data (c, v);
345 struct group_proc *gp = group_proc_get (cmd->vars[i]);
346 struct hsh_table *group_hash = gp->group_hash;
348 struct group_statistics *gs;
350 gs = hsh_find (group_hash, indep_val );
354 gs = xmalloc (sizeof *gs);
360 gs->minimum = DBL_MAX;
361 gs->maximum = -DBL_MAX;
363 hsh_insert ( group_hash, gs );
366 if (!var_is_value_missing (v, val, cmd->exclude))
368 struct group_statistics *totals = &gp->ugs;
371 totals->sum += weight * val->f;
372 totals->ssq += weight * pow2 (val->f);
374 if ( val->f * weight < totals->minimum )
375 totals->minimum = val->f * weight;
377 if ( val->f * weight > totals->maximum )
378 totals->maximum = val->f * weight;
381 gs->sum += weight * val->f;
382 gs->ssq += weight * pow2 (val->f);
384 if ( val->f * weight < gs->minimum )
385 gs->minimum = val->f * weight;
387 if ( val->f * weight > gs->maximum )
388 gs->maximum = val->f * weight;
391 gp->n_groups = hsh_count (group_hash );
395 casereader_destroy (reader);
399 if ( cmd->stats & STATS_HOMOGENEITY )
400 levene (dict, casereader_clone (input), cmd->indep_var,
401 cmd->n_vars, cmd->vars, cmd->exclude);
403 casereader_destroy (input);
405 ws.actual_number_of_groups = hsh_count (ws.group_hash);
407 if (!taint_has_tainted_successor (taint))
408 output_oneway (cmd, &ws);
410 taint_destroy (taint);
413 /* Pre calculations */
415 precalc (const struct oneway_spec *cmd)
419 for (i = 0; i < cmd->n_vars; ++i)
421 struct group_proc *gp = group_proc_get (cmd->vars[i]);
422 struct group_statistics *totals = &gp->ugs;
424 /* Create a hash for each of the dependent variables.
425 The hash contains a group_statistics structure,
426 and is keyed by value of the independent variable */
428 gp->group_hash = hsh_create (4, compare_group, hash_group,
429 (hsh_free_func *) free_group,
435 totals->sum_diff = 0;
436 totals->maximum = -DBL_MAX;
437 totals->minimum = DBL_MAX;
441 /* Post calculations for the ONEWAY command */
443 postcalc (const struct oneway_spec *cmd)
447 for (i = 0; i < cmd->n_vars; ++i)
449 struct group_proc *gp = group_proc_get (cmd->vars[i]);
450 struct hsh_table *group_hash = gp->group_hash;
451 struct group_statistics *totals = &gp->ugs;
453 struct hsh_iterator g;
454 struct group_statistics *gs;
456 for (gs = hsh_first (group_hash, &g);
458 gs = hsh_next (group_hash, &g))
460 gs->mean = gs->sum / gs->n;
461 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
464 gs->n / (gs->n - 1) *
465 ( gs->ssq / gs->n - pow2 (gs->mean))
468 gs->se_mean = gs->std_dev / sqrt (gs->n);
469 gs->mean_diff = gs->sum_diff / gs->n;
472 totals->mean = totals->sum / totals->n;
473 totals->std_dev = sqrt (
474 totals->n / (totals->n - 1) *
475 (totals->ssq / totals->n - pow2 (totals->mean))
478 totals->se_mean = totals->std_dev / sqrt (totals->n);
482 static void show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws);
483 static void show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws);
486 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
490 /* Check the sanity of the given contrast values */
491 struct contrasts_node *coeff_list = NULL;
492 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
494 struct coeff_node *cn = NULL;
496 struct ll_list *cl = &coeff_list->coefficient_list;
499 if (ll_count (cl) != ws->actual_number_of_groups)
502 _("Number of contrast coefficients must equal the number of groups"));
503 coeff_list->bad_count = true;
507 ll_for_each (cn, struct coeff_node, ll, cl)
511 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
514 if (cmd->stats & STATS_DESCRIPTIVES)
515 show_descriptives (cmd, cmd->dict);
517 if (cmd->stats & STATS_HOMOGENEITY)
518 show_homogeneity (cmd);
520 show_anova_table (cmd);
523 if (ll_count (&cmd->contrast_list) > 0)
525 show_contrast_coeffs (cmd, ws);
526 show_contrast_tests (cmd, ws);
531 for (i = 0; i < cmd->n_vars; ++i )
533 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
535 hsh_destroy (group_hash);
538 hsh_destroy (ws->group_hash);
542 /* Show the ANOVA table */
544 show_anova_table (const struct oneway_spec *cmd)
548 size_t n_rows = cmd->n_vars * 3 + 1;
550 struct tab_table *t = tab_create (n_cols, n_rows);
552 tab_headers (t, 2, 0, 1, 0);
558 n_cols - 1, n_rows - 1);
560 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
561 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
562 tab_vline (t, TAL_0, 1, 0, 0);
564 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
565 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
566 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
567 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
568 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
571 for (i = 0; i < cmd->n_vars; ++i)
573 struct group_statistics *totals = &group_proc_get (cmd->vars[i])->ugs;
574 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
575 struct hsh_iterator g;
576 struct group_statistics *gs;
578 const char *s = var_to_string (cmd->vars[i]);
580 for (gs = hsh_first (group_hash, &g);
582 gs = hsh_next (group_hash, &g))
584 ssa += pow2 (gs->sum) / gs->n;
587 ssa -= pow2 (totals->sum) / totals->n;
589 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
590 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
591 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
592 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
595 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
598 struct group_proc *gp = group_proc_get (cmd->vars[i]);
599 const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
600 const double df1 = gp->n_groups - 1;
601 const double df2 = totals->n - gp->n_groups;
602 const double msa = ssa / df1;
604 gp->mse = (sst - ssa) / df2;
607 /* Sums of Squares */
608 tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
609 tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
610 tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
613 /* Degrees of freedom */
614 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
615 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
616 tab_fixed (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
619 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
620 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
623 const double F = msa / gp->mse ;
626 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
628 /* The significance */
629 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
635 tab_title (t, _("ANOVA"));
640 /* Show the descriptives table */
642 show_descriptives (const struct oneway_spec *cmd, const struct dictionary *dict)
649 const double confidence = 0.95;
650 const double q = (1.0 - confidence) / 2.0;
652 const struct variable *wv = dict_get_weight (dict);
653 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
657 for ( v = 0; v < cmd->n_vars; ++v )
658 n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
660 t = tab_create (n_cols, n_rows);
661 tab_headers (t, 2, 0, 2, 0);
664 /* Put a frame around the entire box, and vertical lines inside */
669 n_cols - 1, n_rows - 1);
671 /* Underline headers */
672 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
673 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
675 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
676 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
677 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
678 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
681 tab_vline (t, TAL_0, 7, 0, 0);
682 tab_hline (t, TAL_1, 6, 7, 1);
683 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
684 _("%g%% Confidence Interval for Mean"),
687 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
688 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
690 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
691 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
694 tab_title (t, _("Descriptives"));
698 for (v = 0; v < cmd->n_vars; ++v)
703 struct group_proc *gp = group_proc_get (cmd->vars[v]);
705 struct group_statistics *gs;
706 struct group_statistics *totals = &gp->ugs;
708 const char *s = var_to_string (cmd->vars[v]);
709 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
711 struct group_statistics *const *gs_array =
712 (struct group_statistics *const *) hsh_sort (gp->group_hash);
715 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
717 tab_hline (t, TAL_1, 0, n_cols - 1, row);
719 for (count = 0; count < hsh_count (gp->group_hash); ++count)
722 ds_init_empty (&vstr);
723 gs = gs_array[count];
725 var_append_value_name (cmd->indep_var, &gs->id, &vstr);
727 tab_text (t, 1, row + count,
728 TAB_LEFT | TAT_TITLE,
733 /* Now fill in the numbers ... */
735 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
737 tab_double (t, 3, row + count, 0, gs->mean, NULL);
739 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
741 std_error = gs->std_dev / sqrt (gs->n) ;
742 tab_double (t, 5, row + count, 0,
745 /* Now the confidence interval */
747 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
749 tab_double (t, 6, row + count, 0,
750 gs->mean - T * std_error, NULL);
752 tab_double (t, 7, row + count, 0,
753 gs->mean + T * std_error, NULL);
757 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
758 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
761 tab_text (t, 1, row + count,
762 TAB_LEFT | TAT_TITLE, _("Total"));
764 tab_double (t, 2, row + count, 0, totals->n, wfmt);
766 tab_double (t, 3, row + count, 0, totals->mean, NULL);
768 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
770 std_error = totals->std_dev / sqrt (totals->n) ;
772 tab_double (t, 5, row + count, 0, std_error, NULL);
774 /* Now the confidence interval */
776 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
778 tab_double (t, 6, row + count, 0,
779 totals->mean - T * std_error, NULL);
781 tab_double (t, 7, row + count, 0,
782 totals->mean + T * std_error, NULL);
786 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
787 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
789 row += gp->n_groups + 1;
795 /* Show the homogeneity table */
797 show_homogeneity (const struct oneway_spec *cmd)
801 size_t n_rows = cmd->n_vars + 1;
806 t = tab_create (n_cols, n_rows);
807 tab_headers (t, 1, 0, 1, 0);
810 /* Put a frame around the entire box, and vertical lines inside */
815 n_cols - 1, n_rows - 1);
818 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
819 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
822 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
823 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
824 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
825 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
827 tab_title (t, _("Test of Homogeneity of Variances"));
829 for (v = 0; v < cmd->n_vars; ++v)
832 const struct variable *var = cmd->vars[v];
833 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
834 const char *s = var_to_string (var);
835 const struct group_statistics *totals = &gp->ugs;
837 const double df1 = gp->n_groups - 1;
838 const double df2 = totals->n - gp->n_groups;
840 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
843 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
844 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
845 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
847 /* Now the significance */
848 tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
855 /* Show the contrast coefficients table */
857 show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
862 int n_contrasts = ll_count (&cmd->contrast_list);
863 int n_cols = 2 + ws->actual_number_of_groups;
864 int n_rows = 2 + n_contrasts;
866 void *const *group_values;
870 t = tab_create (n_cols, n_rows);
871 tab_headers (t, 2, 0, 2, 0);
873 /* Put a frame around the entire box, and vertical lines inside */
878 n_cols - 1, n_rows - 1);
892 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
893 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
895 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
897 tab_title (t, _("Contrast Coefficients"));
899 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
902 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
903 var_to_string (cmd->indep_var));
905 group_values = hsh_sort (ws->group_hash);
907 for ( cli = ll_head (&cmd->contrast_list);
908 cli != ll_null (&cmd->contrast_list);
912 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
913 struct ll *coeffi = ll_head (&cn->coefficient_list);
915 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
918 count < hsh_count (ws->group_hash) && coeffi != ll_null (&cn->coefficient_list);
921 double *group_value_p;
922 union value group_value;
925 ds_init_empty (&vstr);
927 group_value_p = group_values[count];
928 group_value.f = *group_value_p;
929 var_append_value_name (cmd->indep_var, &group_value, &vstr);
931 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
937 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
940 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
942 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
945 coeffi = ll_next (coeffi);
954 /* Show the results of the contrast tests */
956 show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
958 int n_contrasts = ll_count (&cmd->contrast_list);
961 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
965 t = tab_create (n_cols, n_rows);
966 tab_headers (t, 3, 0, 1, 0);
968 /* Put a frame around the entire box, and vertical lines inside */
973 n_cols - 1, n_rows - 1);
981 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
982 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
985 tab_title (t, _("Contrast Tests"));
987 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
988 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
989 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
990 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
991 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
992 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
994 for (v = 0; v < cmd->n_vars; ++v)
998 int lines_per_variable = 2 * n_contrasts;
1000 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1001 var_to_string (cmd->vars[v]));
1003 for ( cli = ll_head (&cmd->contrast_list);
1004 cli != ll_null (&cmd->contrast_list);
1005 ++i, cli = ll_next (cli))
1007 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1008 struct ll *coeffi = ll_head (&cn->coefficient_list);
1010 double contrast_value = 0.0;
1011 double coef_msq = 0.0;
1012 struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1013 struct hsh_table *group_hash = grp_data->group_hash;
1015 void *const *group_stat_array;
1018 double std_error_contrast;
1020 double sec_vneq = 0.0;
1022 /* Note: The calculation of the degrees of freedom in the
1023 "variances not equal" case is painfull!!
1024 The following formula may help to understand it:
1025 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1028 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1033 double df_denominator = 0.0;
1034 double df_numerator = 0.0;
1037 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1038 TAB_LEFT | TAT_TITLE,
1039 _("Assume equal variances"));
1041 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1042 TAB_LEFT | TAT_TITLE,
1043 _("Does not assume equal"));
1046 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1047 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1050 tab_text_format (t, 2,
1051 (v * lines_per_variable) + i + 1 + n_contrasts,
1052 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1057 group_stat_array = hsh_sort (group_hash);
1060 coeffi != ll_null (&cn->coefficient_list) &&
1061 ci < hsh_count (group_hash);
1062 ++ci, coeffi = ll_next (coeffi))
1064 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1065 const double coef = cn->coeff;
1066 struct group_statistics *gs = group_stat_array[ci];
1068 const double winv = pow2 (gs->std_dev) / gs->n;
1070 contrast_value += coef * gs->mean;
1072 coef_msq += (coef * coef) / gs->n;
1074 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1076 df_numerator += (coef * coef) * winv;
1077 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1080 sec_vneq = sqrt (sec_vneq);
1082 df_numerator = pow2 (df_numerator);
1084 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1085 TAB_RIGHT, contrast_value, NULL);
1087 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1089 TAB_RIGHT, contrast_value, NULL);
1091 std_error_contrast = sqrt (grp_data->mse * coef_msq);
1094 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1095 TAB_RIGHT, std_error_contrast,
1098 T = fabs (contrast_value / std_error_contrast);
1102 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1106 df = grp_data->ugs.n - grp_data->n_groups;
1108 /* Degrees of Freedom */
1109 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1114 /* Significance TWO TAILED !!*/
1115 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1116 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1119 /* Now for the Variances NOT Equal case */
1123 (v * lines_per_variable) + i + 1 + n_contrasts,
1124 TAB_RIGHT, sec_vneq,
1127 T = contrast_value / sec_vneq;
1129 (v * lines_per_variable) + i + 1 + n_contrasts,
1133 df = df_numerator / df_denominator;
1136 (v * lines_per_variable) + i + 1 + n_contrasts,
1140 /* The Significance */
1141 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1142 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1147 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);