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 *, const struct oneway_workspace *);
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));
329 for (v = 0; v < cmd->n_vars; ++v)
331 struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
332 cmd->wv, cmd->exclude);
334 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
336 cmd->wv, cmd->exclude);
340 c = casereader_peek (input, 0);
343 casereader_destroy (input);
346 output_split_file_values (ds, c);
349 taint = taint_clone (casereader_get_taint (input));
351 ws.group_hash = hsh_create (4,
359 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
360 cmd->exclude, NULL, NULL);
361 if (cmd->missing_type == MISS_LISTWISE)
362 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
363 cmd->exclude, NULL, NULL);
364 input = casereader_create_filter_weight (input, dict, NULL, NULL);
366 reader = casereader_clone (input);
368 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
372 const double weight = dict_get_case_weight (dict, c, NULL);
374 const union value *indep_val = case_data (c, cmd->indep_var);
375 void **p = hsh_probe (ws.group_hash, &indep_val->f);
378 double *value = *p = xmalloc (sizeof *value);
379 *value = indep_val->f;
382 for (i = 0; i < cmd->n_vars; ++i)
385 struct per_var_ws *pvw = &ws.vws[i];
388 covariance_accumulate_pass1 (pvw->cov, c);
391 const struct variable *v = cmd->vars[i];
393 const union value *val = case_data (c, v);
395 struct group_proc *gp = group_proc_get (cmd->vars[i]);
396 struct hsh_table *group_hash = gp->group_hash;
398 struct group_statistics *gs;
400 gs = hsh_find (group_hash, indep_val );
404 gs = xmalloc (sizeof *gs);
410 gs->minimum = DBL_MAX;
411 gs->maximum = -DBL_MAX;
413 hsh_insert ( group_hash, gs );
416 if (!var_is_value_missing (v, val, cmd->exclude))
418 struct group_statistics *totals = &gp->ugs;
421 totals->sum += weight * val->f;
422 totals->ssq += weight * pow2 (val->f);
424 if ( val->f * weight < totals->minimum )
425 totals->minimum = val->f * weight;
427 if ( val->f * weight > totals->maximum )
428 totals->maximum = val->f * weight;
431 gs->sum += weight * val->f;
432 gs->ssq += weight * pow2 (val->f);
434 if ( val->f * weight < gs->minimum )
435 gs->minimum = val->f * weight;
437 if ( val->f * weight > gs->maximum )
438 gs->maximum = val->f * weight;
441 gp->n_groups = hsh_count (group_hash );
445 casereader_destroy (reader);
446 reader = casereader_clone (input);
447 for ( ; (c = casereader_read (reader) ); case_unref (c))
450 for (i = 0; i < cmd->n_vars; ++i)
452 struct per_var_ws *pvw = &ws.vws[i];
453 covariance_accumulate_pass2 (pvw->cov, c);
456 casereader_destroy (reader);
458 for (v = 0; v < cmd->n_vars; ++v)
460 struct per_var_ws *pvw = &ws.vws[v];
461 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
462 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
464 pvw->sst = gsl_matrix_get (cm, 0, 0);
468 pvw->sse = gsl_matrix_get (cm, 0, 0);
470 pvw->ssa = pvw->sst - pvw->sse;
472 pvw->n_groups = categoricals_total (cats);
477 if ( cmd->stats & STATS_HOMOGENEITY )
478 levene (dict, casereader_clone (input), cmd->indep_var,
479 cmd->n_vars, cmd->vars, cmd->exclude);
481 casereader_destroy (input);
483 ws.actual_number_of_groups = hsh_count (ws.group_hash);
485 if (!taint_has_tainted_successor (taint))
486 output_oneway (cmd, &ws);
488 taint_destroy (taint);
491 /* Pre calculations */
493 precalc (const struct oneway_spec *cmd)
497 for (i = 0; i < cmd->n_vars; ++i)
499 struct group_proc *gp = group_proc_get (cmd->vars[i]);
500 struct group_statistics *totals = &gp->ugs;
502 /* Create a hash for each of the dependent variables.
503 The hash contains a group_statistics structure,
504 and is keyed by value of the independent variable */
506 gp->group_hash = hsh_create (4, compare_group, hash_group,
507 (hsh_free_func *) free_group,
513 totals->sum_diff = 0;
514 totals->maximum = -DBL_MAX;
515 totals->minimum = DBL_MAX;
519 /* Post calculations for the ONEWAY command */
521 postcalc (const struct oneway_spec *cmd)
525 for (i = 0; i < cmd->n_vars; ++i)
527 struct group_proc *gp = group_proc_get (cmd->vars[i]);
528 struct hsh_table *group_hash = gp->group_hash;
529 struct group_statistics *totals = &gp->ugs;
531 struct hsh_iterator g;
532 struct group_statistics *gs;
534 for (gs = hsh_first (group_hash, &g);
536 gs = hsh_next (group_hash, &g))
538 gs->mean = gs->sum / gs->n;
539 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
542 gs->n / (gs->n - 1) *
543 ( gs->ssq / gs->n - pow2 (gs->mean))
546 gs->se_mean = gs->std_dev / sqrt (gs->n);
547 gs->mean_diff = gs->sum_diff / gs->n;
550 totals->mean = totals->sum / totals->n;
551 totals->std_dev = sqrt (
552 totals->n / (totals->n - 1) *
553 (totals->ssq / totals->n - pow2 (totals->mean))
556 totals->se_mean = totals->std_dev / sqrt (totals->n);
560 static void show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws);
561 static void show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws);
564 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
568 /* Check the sanity of the given contrast values */
569 struct contrasts_node *coeff_list = NULL;
570 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
572 struct coeff_node *cn = NULL;
574 struct ll_list *cl = &coeff_list->coefficient_list;
577 if (ll_count (cl) != ws->actual_number_of_groups)
580 _("Number of contrast coefficients must equal the number of groups"));
581 coeff_list->bad_count = true;
585 ll_for_each (cn, struct coeff_node, ll, cl)
589 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
592 if (cmd->stats & STATS_DESCRIPTIVES)
593 show_descriptives (cmd);
595 if (cmd->stats & STATS_HOMOGENEITY)
596 show_homogeneity (cmd, ws);
598 show_anova_table (cmd, ws);
601 if (ll_count (&cmd->contrast_list) > 0)
603 show_contrast_coeffs (cmd, ws);
604 show_contrast_tests (cmd, ws);
609 for (i = 0; i < cmd->n_vars; ++i )
611 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
613 hsh_destroy (group_hash);
616 hsh_destroy (ws->group_hash);
620 /* Show the ANOVA table */
622 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
626 size_t n_rows = cmd->n_vars * 3 + 1;
628 struct tab_table *t = tab_create (n_cols, n_rows);
630 tab_headers (t, 2, 0, 1, 0);
636 n_cols - 1, n_rows - 1);
638 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
639 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
640 tab_vline (t, TAL_0, 1, 0, 0);
642 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
643 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
644 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
645 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
646 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
649 for (i = 0; i < cmd->n_vars; ++i)
651 const struct per_var_ws *pvw = &ws->vws[i];
652 struct group_proc *gp = group_proc_get (cmd->vars[i]);
653 const double df1 = pvw->n_groups - 1;
654 const double df2 = pvw->cc - pvw->n_groups;
655 const double msa = pvw->ssa / df1;
657 const char *s = var_to_string (cmd->vars[i]);
659 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
660 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
661 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
662 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
665 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
668 gp->mse = (pvw->sst - pvw->ssa) / df2;
670 /* Sums of Squares */
671 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
672 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
673 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
676 /* Degrees of freedom */
677 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
678 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
679 tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
682 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
683 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
686 const double F = msa / gp->mse ;
689 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
691 /* The significance */
692 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
696 tab_title (t, _("ANOVA"));
701 /* Show the descriptives table */
703 show_descriptives (const struct oneway_spec *cmd)
710 const double confidence = 0.95;
711 const double q = (1.0 - confidence) / 2.0;
713 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : & F_8_0;
717 for ( v = 0; v < cmd->n_vars; ++v )
718 n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
720 t = tab_create (n_cols, n_rows);
721 tab_headers (t, 2, 0, 2, 0);
724 /* Put a frame around the entire box, and vertical lines inside */
729 n_cols - 1, n_rows - 1);
731 /* Underline headers */
732 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
733 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
735 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
736 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
737 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
738 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
741 tab_vline (t, TAL_0, 7, 0, 0);
742 tab_hline (t, TAL_1, 6, 7, 1);
743 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
744 _("%g%% Confidence Interval for Mean"),
747 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
748 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
750 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
751 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
754 tab_title (t, _("Descriptives"));
758 for (v = 0; v < cmd->n_vars; ++v)
763 struct group_proc *gp = group_proc_get (cmd->vars[v]);
765 struct group_statistics *gs;
766 struct group_statistics *totals = &gp->ugs;
768 const char *s = var_to_string (cmd->vars[v]);
769 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
771 struct group_statistics *const *gs_array =
772 (struct group_statistics *const *) hsh_sort (gp->group_hash);
775 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
777 tab_hline (t, TAL_1, 0, n_cols - 1, row);
779 for (count = 0; count < hsh_count (gp->group_hash); ++count)
782 ds_init_empty (&vstr);
783 gs = gs_array[count];
785 var_append_value_name (cmd->indep_var, &gs->id, &vstr);
787 tab_text (t, 1, row + count,
788 TAB_LEFT | TAT_TITLE,
793 /* Now fill in the numbers ... */
795 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
797 tab_double (t, 3, row + count, 0, gs->mean, NULL);
799 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
801 std_error = gs->std_dev / sqrt (gs->n) ;
802 tab_double (t, 5, row + count, 0,
805 /* Now the confidence interval */
807 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
809 tab_double (t, 6, row + count, 0,
810 gs->mean - T * std_error, NULL);
812 tab_double (t, 7, row + count, 0,
813 gs->mean + T * std_error, NULL);
817 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
818 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
821 tab_text (t, 1, row + count,
822 TAB_LEFT | TAT_TITLE, _("Total"));
824 tab_double (t, 2, row + count, 0, totals->n, wfmt);
826 tab_double (t, 3, row + count, 0, totals->mean, NULL);
828 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
830 std_error = totals->std_dev / sqrt (totals->n) ;
832 tab_double (t, 5, row + count, 0, std_error, NULL);
834 /* Now the confidence interval */
836 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
838 tab_double (t, 6, row + count, 0,
839 totals->mean - T * std_error, NULL);
841 tab_double (t, 7, row + count, 0,
842 totals->mean + T * std_error, NULL);
846 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
847 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
849 row += gp->n_groups + 1;
855 /* Show the homogeneity table */
857 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
861 size_t n_rows = cmd->n_vars + 1;
866 t = tab_create (n_cols, n_rows);
867 tab_headers (t, 1, 0, 1, 0);
870 /* Put a frame around the entire box, and vertical lines inside */
875 n_cols - 1, n_rows - 1);
878 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
879 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
881 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
882 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
883 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
884 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
886 tab_title (t, _("Test of Homogeneity of Variances"));
888 for (v = 0; v < cmd->n_vars; ++v)
890 struct per_var_ws *pvw = &ws->vws[v];
891 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
893 const struct variable *var = cmd->vars[v];
894 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
895 const char *s = var_to_string (var);
897 const double df1 = pvw->n_groups - 1;
898 const double df2 = pvw->cc - pvw->n_groups;
899 double F = gp->levene;
901 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
904 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
905 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
906 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
908 /* Now the significance */
909 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
916 /* Show the contrast coefficients table */
918 show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
923 int n_contrasts = ll_count (&cmd->contrast_list);
924 int n_cols = 2 + ws->actual_number_of_groups;
925 int n_rows = 2 + n_contrasts;
929 const struct covariance *cov = ws->vws[0].cov ;
931 t = tab_create (n_cols, n_rows);
932 tab_headers (t, 2, 0, 2, 0);
934 /* Put a frame around the entire box, and vertical lines inside */
939 n_cols - 1, n_rows - 1);
953 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
954 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
956 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
958 tab_title (t, _("Contrast Coefficients"));
960 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
963 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
964 var_to_string (cmd->indep_var));
966 for ( cli = ll_head (&cmd->contrast_list);
967 cli != ll_null (&cmd->contrast_list);
971 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
974 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
976 for (coeffi = ll_head (&cn->coefficient_list);
977 coeffi != ll_null (&cn->coefficient_list);
978 ++count, coeffi = ll_next (coeffi))
980 const struct categoricals *cats = covariance_get_categoricals (cov);
981 const union value *val = categoricals_get_value_by_subscript (cats, count);
984 ds_init_empty (&vstr);
986 var_append_value_name (cmd->indep_var, val, &vstr);
988 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
993 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
996 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
998 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1008 /* Show the results of the contrast tests */
1010 show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
1012 int n_contrasts = ll_count (&cmd->contrast_list);
1015 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1017 struct tab_table *t;
1019 t = tab_create (n_cols, n_rows);
1020 tab_headers (t, 3, 0, 1, 0);
1022 /* Put a frame around the entire box, and vertical lines inside */
1027 n_cols - 1, n_rows - 1);
1035 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1036 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1039 tab_title (t, _("Contrast Tests"));
1041 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1042 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1043 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1044 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1045 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1046 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1048 for (v = 0; v < cmd->n_vars; ++v)
1052 int lines_per_variable = 2 * n_contrasts;
1054 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1055 var_to_string (cmd->vars[v]));
1057 for ( cli = ll_head (&cmd->contrast_list);
1058 cli != ll_null (&cmd->contrast_list);
1059 ++i, cli = ll_next (cli))
1061 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1062 struct ll *coeffi = ll_head (&cn->coefficient_list);
1064 double contrast_value = 0.0;
1065 double coef_msq = 0.0;
1066 struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1067 struct hsh_table *group_hash = grp_data->group_hash;
1069 void *const *group_stat_array;
1072 double std_error_contrast;
1074 double sec_vneq = 0.0;
1076 /* Note: The calculation of the degrees of freedom in the
1077 "variances not equal" case is painfull!!
1078 The following formula may help to understand it:
1079 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1082 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1087 double df_denominator = 0.0;
1088 double df_numerator = 0.0;
1091 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1092 TAB_LEFT | TAT_TITLE,
1093 _("Assume equal variances"));
1095 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1096 TAB_LEFT | TAT_TITLE,
1097 _("Does not assume equal"));
1100 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1101 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1104 tab_text_format (t, 2,
1105 (v * lines_per_variable) + i + 1 + n_contrasts,
1106 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1111 group_stat_array = hsh_sort (group_hash);
1114 coeffi != ll_null (&cn->coefficient_list) &&
1115 ci < hsh_count (group_hash);
1116 ++ci, coeffi = ll_next (coeffi))
1118 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1119 const double coef = cn->coeff;
1120 struct group_statistics *gs = group_stat_array[ci];
1122 const double winv = pow2 (gs->std_dev) / gs->n;
1124 contrast_value += coef * gs->mean;
1126 coef_msq += (coef * coef) / gs->n;
1128 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1130 df_numerator += (coef * coef) * winv;
1131 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1134 sec_vneq = sqrt (sec_vneq);
1136 df_numerator = pow2 (df_numerator);
1138 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1139 TAB_RIGHT, contrast_value, NULL);
1141 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1143 TAB_RIGHT, contrast_value, NULL);
1145 std_error_contrast = sqrt (grp_data->mse * coef_msq);
1148 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1149 TAB_RIGHT, std_error_contrast,
1152 T = fabs (contrast_value / std_error_contrast);
1156 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1160 df = grp_data->ugs.n - grp_data->n_groups;
1162 /* Degrees of Freedom */
1163 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1168 /* Significance TWO TAILED !!*/
1169 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1170 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1173 /* Now for the Variances NOT Equal case */
1177 (v * lines_per_variable) + i + 1 + n_contrasts,
1178 TAB_RIGHT, sec_vneq,
1181 T = contrast_value / sec_vneq;
1183 (v * lines_per_variable) + i + 1 + n_contrasts,
1187 df = df_numerator / df_denominator;
1190 (v * lines_per_variable) + i + 1 + n_contrasts,
1194 /* The Significance */
1195 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1196 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1201 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);