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 <math/covariance.h>
24 #include <math/categoricals.h>
25 #include <gsl/gsl_matrix.h>
26 #include <linreg/sweep.h>
28 #include <libpspp/ll.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <language/lexer/value-parser.h>
33 #include <language/command.h>
35 #include <data/procedure.h>
36 #include <data/dictionary.h>
39 #include <language/dictionary/split-file.h>
40 #include <libpspp/hash.h>
41 #include <libpspp/taint.h>
42 #include <math/group-proc.h>
43 #include <math/levene.h>
44 #include <libpspp/misc.h>
46 #include <output/tab.h>
48 #include <gsl/gsl_cdf.h>
50 #include <data/format.h>
52 #include <libpspp/message.h>
55 #define _(msgid) gettext (msgid)
65 STATS_DESCRIPTIVES = 0x0001,
66 STATS_HOMOGENEITY = 0x0002
79 struct ll_list coefficient_list;
81 bool bad_count; /* True if the number of coefficients does not equal the number of groups */
87 const struct variable **vars;
89 const struct variable *indep_var;
91 enum statistics stats;
93 enum missing_type missing_type;
94 enum mv_class exclude;
96 /* List of contrasts */
97 struct ll_list contrast_list;
99 /* The weight variable */
100 const struct variable *wv;
104 /* Workspace variable for each dependent variable */
107 struct covariance *cov;
118 struct oneway_workspace
120 /* The number of distinct values of the independent variable, when all
121 missing values are disregarded */
122 int actual_number_of_groups;
124 /* A hash table containing all the distinct values of the independent
126 struct hsh_table *group_hash;
128 struct per_var_ws *vws;
131 /* Routines to show the output tables */
132 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
133 static void show_descriptives (const struct oneway_spec *);
134 static void show_homogeneity (const struct oneway_spec *);
136 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
137 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
140 cmd_oneway (struct lexer *lexer, struct dataset *ds)
142 const struct dictionary *dict = dataset_dict (ds);
143 struct oneway_spec oneway ;
146 oneway.indep_var = NULL;
148 oneway.missing_type = MISS_ANALYSIS;
149 oneway.exclude = MV_ANY;
150 oneway.wv = dict_get_weight (dict);
152 ll_init (&oneway.contrast_list);
155 if ( lex_match (lexer, '/'))
157 if (!lex_force_match_id (lexer, "VARIABLES"))
161 lex_match (lexer, '=');
164 if (!parse_variables_const (lexer, dict,
165 &oneway.vars, &oneway.n_vars,
166 PV_NO_DUPLICATE | PV_NUMERIC))
169 lex_force_match (lexer, T_BY);
171 oneway.indep_var = parse_variable_const (lexer, dict);
173 while (lex_token (lexer) != '.')
175 lex_match (lexer, '/');
177 if (lex_match_id (lexer, "STATISTICS"))
179 lex_match (lexer, '=');
180 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
182 if (lex_match_id (lexer, "DESCRIPTIVES"))
184 oneway.stats |= STATS_DESCRIPTIVES;
186 else if (lex_match_id (lexer, "HOMOGENEITY"))
188 oneway.stats |= STATS_HOMOGENEITY;
192 lex_error (lexer, NULL);
197 else if (lex_match_id (lexer, "CONTRAST"))
199 struct contrasts_node *cl = xzalloc (sizeof *cl);
201 struct ll_list *coefficient_list = &cl->coefficient_list;
202 lex_match (lexer, '=');
204 ll_init (coefficient_list);
206 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
208 if ( lex_is_number (lexer))
210 struct coeff_node *cc = xmalloc (sizeof *cc);
211 cc->coeff = lex_number (lexer);
213 ll_push_tail (coefficient_list, &cc->ll);
218 lex_error (lexer, NULL);
223 ll_push_tail (&oneway.contrast_list, &cl->ll);
225 else if (lex_match_id (lexer, "MISSING"))
227 lex_match (lexer, '=');
228 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
230 if (lex_match_id (lexer, "INCLUDE"))
232 oneway.exclude = MV_SYSTEM;
234 else if (lex_match_id (lexer, "EXCLUDE"))
236 oneway.exclude = MV_ANY;
238 else if (lex_match_id (lexer, "LISTWISE"))
240 oneway.missing_type = MISS_LISTWISE;
242 else if (lex_match_id (lexer, "ANALYSIS"))
244 oneway.missing_type = MISS_ANALYSIS;
248 lex_error (lexer, NULL);
255 lex_error (lexer, NULL);
262 struct casegrouper *grouper;
263 struct casereader *group;
268 grouper = casegrouper_create_splits (proc_open (ds), dict);
269 while (casegrouper_get_next_group (grouper, &group))
270 run_oneway (&oneway, group, ds);
271 ok = casegrouper_destroy (grouper);
272 ok = proc_commit (ds) && ok;
287 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
289 const double *a = a_;
290 const double *b = b_;
291 return *a < *b ? -1 : *a > *b;
295 do_hash_double (const void *value_, const void *aux UNUSED)
297 const double *value = value_;
298 return hash_double (*value, 0);
302 free_double (void *value_, const void *aux UNUSED)
304 double *value = value_;
308 static void postcalc (const struct oneway_spec *cmd);
309 static void precalc (const struct oneway_spec *cmd);
313 run_oneway (const struct oneway_spec *cmd,
314 struct casereader *input,
315 const struct dataset *ds)
319 struct dictionary *dict = dataset_dict (ds);
320 struct casereader *reader;
324 struct oneway_workspace ws;
326 ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
328 for (v = 0; v < cmd->n_vars; ++v)
330 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
332 cmd->wv, cmd->exclude);
336 c = casereader_peek (input, 0);
339 casereader_destroy (input);
342 output_split_file_values (ds, c);
345 taint = taint_clone (casereader_get_taint (input));
347 ws.group_hash = hsh_create (4,
355 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
356 cmd->exclude, NULL, NULL);
357 if (cmd->missing_type == MISS_LISTWISE)
358 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
359 cmd->exclude, NULL, NULL);
360 input = casereader_create_filter_weight (input, dict, NULL, NULL);
362 reader = casereader_clone (input);
364 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
368 const double weight = dict_get_case_weight (dict, c, NULL);
370 const union value *indep_val = case_data (c, cmd->indep_var);
371 void **p = hsh_probe (ws.group_hash, &indep_val->f);
374 double *value = *p = xmalloc (sizeof *value);
375 *value = indep_val->f;
378 for (i = 0; i < cmd->n_vars; ++i)
381 struct per_var_ws *pvw = &ws.vws[i];
384 covariance_accumulate_pass1 (pvw->cov, c);
387 const struct variable *v = cmd->vars[i];
389 const union value *val = case_data (c, v);
391 struct group_proc *gp = group_proc_get (cmd->vars[i]);
392 struct hsh_table *group_hash = gp->group_hash;
394 struct group_statistics *gs;
396 gs = hsh_find (group_hash, indep_val );
400 gs = xmalloc (sizeof *gs);
406 gs->minimum = DBL_MAX;
407 gs->maximum = -DBL_MAX;
409 hsh_insert ( group_hash, gs );
412 if (!var_is_value_missing (v, val, cmd->exclude))
414 struct group_statistics *totals = &gp->ugs;
417 totals->sum += weight * val->f;
418 totals->ssq += weight * pow2 (val->f);
420 if ( val->f * weight < totals->minimum )
421 totals->minimum = val->f * weight;
423 if ( val->f * weight > totals->maximum )
424 totals->maximum = val->f * weight;
427 gs->sum += weight * val->f;
428 gs->ssq += weight * pow2 (val->f);
430 if ( val->f * weight < gs->minimum )
431 gs->minimum = val->f * weight;
433 if ( val->f * weight > gs->maximum )
434 gs->maximum = val->f * weight;
437 gp->n_groups = hsh_count (group_hash );
441 casereader_destroy (reader);
442 reader = casereader_clone (input);
443 for ( ; (c = casereader_read (reader) ); case_unref (c))
446 for (i = 0; i < cmd->n_vars; ++i)
448 struct per_var_ws *pvw = &ws.vws[i];
449 covariance_accumulate_pass2 (pvw->cov, c);
452 casereader_destroy (reader);
454 for (v = 0; v < cmd->n_vars; ++v)
456 struct per_var_ws *pvw = &ws.vws[v];
457 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
458 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
460 pvw->sst = gsl_matrix_get (cm, 0, 0);
464 pvw->sse = gsl_matrix_get (cm, 0, 0);
466 pvw->ssa = pvw->sst - pvw->sse;
468 pvw->n_groups = categoricals_total (cats);
473 if ( cmd->stats & STATS_HOMOGENEITY )
474 levene (dict, casereader_clone (input), cmd->indep_var,
475 cmd->n_vars, cmd->vars, cmd->exclude);
477 casereader_destroy (input);
479 ws.actual_number_of_groups = hsh_count (ws.group_hash);
481 if (!taint_has_tainted_successor (taint))
482 output_oneway (cmd, &ws);
484 taint_destroy (taint);
487 /* Pre calculations */
489 precalc (const struct oneway_spec *cmd)
493 for (i = 0; i < cmd->n_vars; ++i)
495 struct group_proc *gp = group_proc_get (cmd->vars[i]);
496 struct group_statistics *totals = &gp->ugs;
498 /* Create a hash for each of the dependent variables.
499 The hash contains a group_statistics structure,
500 and is keyed by value of the independent variable */
502 gp->group_hash = hsh_create (4, compare_group, hash_group,
503 (hsh_free_func *) free_group,
509 totals->sum_diff = 0;
510 totals->maximum = -DBL_MAX;
511 totals->minimum = DBL_MAX;
515 /* Post calculations for the ONEWAY command */
517 postcalc (const struct oneway_spec *cmd)
521 for (i = 0; i < cmd->n_vars; ++i)
523 struct group_proc *gp = group_proc_get (cmd->vars[i]);
524 struct hsh_table *group_hash = gp->group_hash;
525 struct group_statistics *totals = &gp->ugs;
527 struct hsh_iterator g;
528 struct group_statistics *gs;
530 for (gs = hsh_first (group_hash, &g);
532 gs = hsh_next (group_hash, &g))
534 gs->mean = gs->sum / gs->n;
535 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
538 gs->n / (gs->n - 1) *
539 ( gs->ssq / gs->n - pow2 (gs->mean))
542 gs->se_mean = gs->std_dev / sqrt (gs->n);
543 gs->mean_diff = gs->sum_diff / gs->n;
546 totals->mean = totals->sum / totals->n;
547 totals->std_dev = sqrt (
548 totals->n / (totals->n - 1) *
549 (totals->ssq / totals->n - pow2 (totals->mean))
552 totals->se_mean = totals->std_dev / sqrt (totals->n);
556 static void show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws);
557 static void show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws);
560 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
564 /* Check the sanity of the given contrast values */
565 struct contrasts_node *coeff_list = NULL;
566 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
568 struct coeff_node *cn = NULL;
570 struct ll_list *cl = &coeff_list->coefficient_list;
573 if (ll_count (cl) != ws->actual_number_of_groups)
576 _("Number of contrast coefficients must equal the number of groups"));
577 coeff_list->bad_count = true;
581 ll_for_each (cn, struct coeff_node, ll, cl)
585 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
588 if (cmd->stats & STATS_DESCRIPTIVES)
589 show_descriptives (cmd);
591 if (cmd->stats & STATS_HOMOGENEITY)
592 show_homogeneity (cmd);
594 show_anova_table (cmd, ws);
597 if (ll_count (&cmd->contrast_list) > 0)
599 show_contrast_coeffs (cmd, ws);
600 show_contrast_tests (cmd, ws);
605 for (i = 0; i < cmd->n_vars; ++i )
607 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
609 hsh_destroy (group_hash);
612 hsh_destroy (ws->group_hash);
616 /* Show the ANOVA table */
618 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
622 size_t n_rows = cmd->n_vars * 3 + 1;
624 struct tab_table *t = tab_create (n_cols, n_rows);
626 tab_headers (t, 2, 0, 1, 0);
632 n_cols - 1, n_rows - 1);
634 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
635 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
636 tab_vline (t, TAL_0, 1, 0, 0);
638 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
639 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
640 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
641 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
642 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
645 for (i = 0; i < cmd->n_vars; ++i)
647 const struct per_var_ws *pvw = &ws->vws[i];
648 struct group_proc *gp = group_proc_get (cmd->vars[i]);
649 const double df1 = pvw->n_groups - 1;
650 const double df2 = pvw->cc - pvw->n_groups;
651 const double msa = pvw->ssa / df1;
653 const char *s = var_to_string (cmd->vars[i]);
655 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
656 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
657 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
658 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
661 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
664 gp->mse = (pvw->sst - pvw->ssa) / df2;
666 /* Sums of Squares */
667 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
668 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
669 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
672 /* Degrees of freedom */
673 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
674 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
675 tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
678 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
679 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
682 const double F = msa / gp->mse ;
685 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
687 /* The significance */
688 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
692 tab_title (t, _("ANOVA"));
697 /* Show the descriptives table */
699 show_descriptives (const struct oneway_spec *cmd)
706 const double confidence = 0.95;
707 const double q = (1.0 - confidence) / 2.0;
709 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : & F_8_0;
713 for ( v = 0; v < cmd->n_vars; ++v )
714 n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
716 t = tab_create (n_cols, n_rows);
717 tab_headers (t, 2, 0, 2, 0);
720 /* Put a frame around the entire box, and vertical lines inside */
725 n_cols - 1, n_rows - 1);
727 /* Underline headers */
728 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
729 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
731 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
732 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
733 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
734 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
737 tab_vline (t, TAL_0, 7, 0, 0);
738 tab_hline (t, TAL_1, 6, 7, 1);
739 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
740 _("%g%% Confidence Interval for Mean"),
743 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
744 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
746 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
747 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
750 tab_title (t, _("Descriptives"));
754 for (v = 0; v < cmd->n_vars; ++v)
759 struct group_proc *gp = group_proc_get (cmd->vars[v]);
761 struct group_statistics *gs;
762 struct group_statistics *totals = &gp->ugs;
764 const char *s = var_to_string (cmd->vars[v]);
765 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
767 struct group_statistics *const *gs_array =
768 (struct group_statistics *const *) hsh_sort (gp->group_hash);
771 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
773 tab_hline (t, TAL_1, 0, n_cols - 1, row);
775 for (count = 0; count < hsh_count (gp->group_hash); ++count)
778 ds_init_empty (&vstr);
779 gs = gs_array[count];
781 var_append_value_name (cmd->indep_var, &gs->id, &vstr);
783 tab_text (t, 1, row + count,
784 TAB_LEFT | TAT_TITLE,
789 /* Now fill in the numbers ... */
791 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
793 tab_double (t, 3, row + count, 0, gs->mean, NULL);
795 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
797 std_error = gs->std_dev / sqrt (gs->n) ;
798 tab_double (t, 5, row + count, 0,
801 /* Now the confidence interval */
803 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
805 tab_double (t, 6, row + count, 0,
806 gs->mean - T * std_error, NULL);
808 tab_double (t, 7, row + count, 0,
809 gs->mean + T * std_error, NULL);
813 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
814 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
817 tab_text (t, 1, row + count,
818 TAB_LEFT | TAT_TITLE, _("Total"));
820 tab_double (t, 2, row + count, 0, totals->n, wfmt);
822 tab_double (t, 3, row + count, 0, totals->mean, NULL);
824 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
826 std_error = totals->std_dev / sqrt (totals->n) ;
828 tab_double (t, 5, row + count, 0, std_error, NULL);
830 /* Now the confidence interval */
832 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
834 tab_double (t, 6, row + count, 0,
835 totals->mean - T * std_error, NULL);
837 tab_double (t, 7, row + count, 0,
838 totals->mean + T * std_error, NULL);
842 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
843 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
845 row += gp->n_groups + 1;
851 /* Show the homogeneity table */
853 show_homogeneity (const struct oneway_spec *cmd)
857 size_t n_rows = cmd->n_vars + 1;
862 t = tab_create (n_cols, n_rows);
863 tab_headers (t, 1, 0, 1, 0);
866 /* Put a frame around the entire box, and vertical lines inside */
871 n_cols - 1, n_rows - 1);
874 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
875 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
878 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
879 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
880 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
881 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
883 tab_title (t, _("Test of Homogeneity of Variances"));
885 for (v = 0; v < cmd->n_vars; ++v)
888 const struct variable *var = cmd->vars[v];
889 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
890 const char *s = var_to_string (var);
891 const struct group_statistics *totals = &gp->ugs;
893 const double df1 = gp->n_groups - 1;
894 const double df2 = totals->n - gp->n_groups;
896 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
899 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
900 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
901 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
903 /* Now the significance */
904 tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
911 /* Show the contrast coefficients table */
913 show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
918 int n_contrasts = ll_count (&cmd->contrast_list);
919 int n_cols = 2 + ws->actual_number_of_groups;
920 int n_rows = 2 + n_contrasts;
922 void *const *group_values;
926 t = tab_create (n_cols, n_rows);
927 tab_headers (t, 2, 0, 2, 0);
929 /* Put a frame around the entire box, and vertical lines inside */
934 n_cols - 1, n_rows - 1);
948 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
949 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
951 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
953 tab_title (t, _("Contrast Coefficients"));
955 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
958 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
959 var_to_string (cmd->indep_var));
961 group_values = hsh_sort (ws->group_hash);
963 for ( cli = ll_head (&cmd->contrast_list);
964 cli != ll_null (&cmd->contrast_list);
968 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
969 struct ll *coeffi = ll_head (&cn->coefficient_list);
971 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
974 count < hsh_count (ws->group_hash) && coeffi != ll_null (&cn->coefficient_list);
977 double *group_value_p;
978 union value group_value;
981 ds_init_empty (&vstr);
983 group_value_p = group_values[count];
984 group_value.f = *group_value_p;
985 var_append_value_name (cmd->indep_var, &group_value, &vstr);
987 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
992 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
995 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
997 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1000 coeffi = ll_next (coeffi);
1009 /* Show the results of the contrast tests */
1011 show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
1013 int n_contrasts = ll_count (&cmd->contrast_list);
1016 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1018 struct tab_table *t;
1020 t = tab_create (n_cols, n_rows);
1021 tab_headers (t, 3, 0, 1, 0);
1023 /* Put a frame around the entire box, and vertical lines inside */
1028 n_cols - 1, n_rows - 1);
1036 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1037 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1040 tab_title (t, _("Contrast Tests"));
1042 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1043 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1044 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1045 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1046 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1047 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1049 for (v = 0; v < cmd->n_vars; ++v)
1053 int lines_per_variable = 2 * n_contrasts;
1055 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1056 var_to_string (cmd->vars[v]));
1058 for ( cli = ll_head (&cmd->contrast_list);
1059 cli != ll_null (&cmd->contrast_list);
1060 ++i, cli = ll_next (cli))
1062 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1063 struct ll *coeffi = ll_head (&cn->coefficient_list);
1065 double contrast_value = 0.0;
1066 double coef_msq = 0.0;
1067 struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1068 struct hsh_table *group_hash = grp_data->group_hash;
1070 void *const *group_stat_array;
1073 double std_error_contrast;
1075 double sec_vneq = 0.0;
1077 /* Note: The calculation of the degrees of freedom in the
1078 "variances not equal" case is painfull!!
1079 The following formula may help to understand it:
1080 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1083 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1088 double df_denominator = 0.0;
1089 double df_numerator = 0.0;
1092 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1093 TAB_LEFT | TAT_TITLE,
1094 _("Assume equal variances"));
1096 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1097 TAB_LEFT | TAT_TITLE,
1098 _("Does not assume equal"));
1101 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1102 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1105 tab_text_format (t, 2,
1106 (v * lines_per_variable) + i + 1 + n_contrasts,
1107 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1112 group_stat_array = hsh_sort (group_hash);
1115 coeffi != ll_null (&cn->coefficient_list) &&
1116 ci < hsh_count (group_hash);
1117 ++ci, coeffi = ll_next (coeffi))
1119 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1120 const double coef = cn->coeff;
1121 struct group_statistics *gs = group_stat_array[ci];
1123 const double winv = pow2 (gs->std_dev) / gs->n;
1125 contrast_value += coef * gs->mean;
1127 coef_msq += (coef * coef) / gs->n;
1129 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1131 df_numerator += (coef * coef) * winv;
1132 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1135 sec_vneq = sqrt (sec_vneq);
1137 df_numerator = pow2 (df_numerator);
1139 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1140 TAB_RIGHT, contrast_value, NULL);
1142 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1144 TAB_RIGHT, contrast_value, NULL);
1146 std_error_contrast = sqrt (grp_data->mse * coef_msq);
1149 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1150 TAB_RIGHT, std_error_contrast,
1153 T = fabs (contrast_value / std_error_contrast);
1157 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1161 df = grp_data->ugs.n - grp_data->n_groups;
1163 /* Degrees of Freedom */
1164 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1169 /* Significance TWO TAILED !!*/
1170 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1171 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1174 /* Now for the Variances NOT Equal case */
1178 (v * lines_per_variable) + i + 1 + n_contrasts,
1179 TAB_RIGHT, sec_vneq,
1182 T = contrast_value / sec_vneq;
1184 (v * lines_per_variable) + i + 1 + n_contrasts,
1188 df = df_numerator / df_denominator;
1191 (v * lines_per_variable) + i + 1 + n_contrasts,
1195 /* The Significance */
1196 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1197 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1202 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);