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 <math/moments.h>
26 #include <gsl/gsl_matrix.h>
27 #include <linreg/sweep.h>
29 #include <libpspp/ll.h>
31 #include <language/lexer/lexer.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/value-parser.h>
34 #include <language/command.h>
36 #include <data/procedure.h>
37 #include <data/value.h>
38 #include <data/dictionary.h>
40 #include <language/dictionary/split-file.h>
41 #include <libpspp/hash.h>
42 #include <libpspp/taint.h>
43 #include <math/group-proc.h>
44 #include <math/levene.h>
45 #include <libpspp/misc.h>
47 #include <output/tab.h>
49 #include <gsl/gsl_cdf.h>
51 #include <data/format.h>
53 #include <libpspp/message.h>
56 #define _(msgid) gettext (msgid)
66 STATS_DESCRIPTIVES = 0x0001,
67 STATS_HOMOGENEITY = 0x0002
80 struct ll_list coefficient_list;
82 bool bad_count; /* True if the number of coefficients does not equal the number of groups */
88 const struct variable **vars;
90 const struct variable *indep_var;
92 enum statistics stats;
94 enum missing_type missing_type;
95 enum mv_class exclude;
97 /* List of contrasts */
98 struct ll_list contrast_list;
100 /* The weight variable */
101 const struct variable *wv;
105 /* Workspace variable for each dependent variable */
108 struct covariance *cov;
119 struct oneway_workspace
121 /* The number of distinct values of the independent variable, when all
122 missing values are disregarded */
123 int actual_number_of_groups;
125 /* A hash table containing all the distinct values of the independent
127 struct hsh_table *group_hash;
129 struct per_var_ws *vws;
131 struct moments1 *totals;
136 /* Routines to show the output tables */
137 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
138 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
139 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
141 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
142 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
145 cmd_oneway (struct lexer *lexer, struct dataset *ds)
147 const struct dictionary *dict = dataset_dict (ds);
148 struct oneway_spec oneway ;
151 oneway.indep_var = NULL;
153 oneway.missing_type = MISS_ANALYSIS;
154 oneway.exclude = MV_ANY;
155 oneway.wv = dict_get_weight (dict);
157 ll_init (&oneway.contrast_list);
160 if ( lex_match (lexer, '/'))
162 if (!lex_force_match_id (lexer, "VARIABLES"))
166 lex_match (lexer, '=');
169 if (!parse_variables_const (lexer, dict,
170 &oneway.vars, &oneway.n_vars,
171 PV_NO_DUPLICATE | PV_NUMERIC))
174 lex_force_match (lexer, T_BY);
176 oneway.indep_var = parse_variable_const (lexer, dict);
178 while (lex_token (lexer) != '.')
180 lex_match (lexer, '/');
182 if (lex_match_id (lexer, "STATISTICS"))
184 lex_match (lexer, '=');
185 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
187 if (lex_match_id (lexer, "DESCRIPTIVES"))
189 oneway.stats |= STATS_DESCRIPTIVES;
191 else if (lex_match_id (lexer, "HOMOGENEITY"))
193 oneway.stats |= STATS_HOMOGENEITY;
197 lex_error (lexer, NULL);
202 else if (lex_match_id (lexer, "CONTRAST"))
204 struct contrasts_node *cl = xzalloc (sizeof *cl);
206 struct ll_list *coefficient_list = &cl->coefficient_list;
207 lex_match (lexer, '=');
209 ll_init (coefficient_list);
211 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
213 if ( lex_is_number (lexer))
215 struct coeff_node *cc = xmalloc (sizeof *cc);
216 cc->coeff = lex_number (lexer);
218 ll_push_tail (coefficient_list, &cc->ll);
223 lex_error (lexer, NULL);
228 ll_push_tail (&oneway.contrast_list, &cl->ll);
230 else if (lex_match_id (lexer, "MISSING"))
232 lex_match (lexer, '=');
233 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
235 if (lex_match_id (lexer, "INCLUDE"))
237 oneway.exclude = MV_SYSTEM;
239 else if (lex_match_id (lexer, "EXCLUDE"))
241 oneway.exclude = MV_ANY;
243 else if (lex_match_id (lexer, "LISTWISE"))
245 oneway.missing_type = MISS_LISTWISE;
247 else if (lex_match_id (lexer, "ANALYSIS"))
249 oneway.missing_type = MISS_ANALYSIS;
253 lex_error (lexer, NULL);
260 lex_error (lexer, NULL);
267 struct casegrouper *grouper;
268 struct casereader *group;
273 grouper = casegrouper_create_splits (proc_open (ds), dict);
274 while (casegrouper_get_next_group (grouper, &group))
275 run_oneway (&oneway, group, ds);
276 ok = casegrouper_destroy (grouper);
277 ok = proc_commit (ds) && ok;
292 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
294 const double *a = a_;
295 const double *b = b_;
296 return *a < *b ? -1 : *a > *b;
300 do_hash_double (const void *value_, const void *aux UNUSED)
302 const double *value = value_;
303 return hash_double (*value, 0);
307 free_double (void *value_, const void *aux UNUSED)
309 double *value = value_;
315 static void postcalc (const struct oneway_spec *cmd);
316 static void precalc (const struct oneway_spec *cmd);
318 struct descriptive_data
320 struct moments1 *mom;
328 struct descriptive_data *dd = xmalloc (sizeof *dd);
329 dd->mom = moments1_create (MOMENT_VARIANCE);
330 dd->minimum = DBL_MAX;
331 dd->maximum = -DBL_MAX;
337 updateit (void *user_data, const struct variable *wv,
338 const struct variable *catvar, const struct ccase *c, void *aux)
340 const union value *val = case_data_idx (c, 0);
341 struct descriptive_data *dd = user_data;
342 struct oneway_workspace *ws = aux;
346 weight = case_data (c, wv)->f;
348 moments1_add (dd->mom, val->f, weight);
349 moments1_add (ws->totals, val->f, weight);
351 if (val->f * weight < dd->minimum)
352 dd->minimum = val->f * weight;
354 if (val->f * weight > dd->maximum)
355 dd->maximum = val->f * weight;
358 if (val->f * weight < ws->minimum)
359 ws->minimum = val->f * weight;
361 if (val->f * weight > ws->maximum)
362 ws->maximum = val->f * weight;
366 run_oneway (const struct oneway_spec *cmd,
367 struct casereader *input,
368 const struct dataset *ds)
372 struct dictionary *dict = dataset_dict (ds);
373 struct casereader *reader;
376 struct oneway_workspace ws;
378 ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
380 ws.totals = moments1_create (MOMENT_VARIANCE);
381 ws.minimum = DBL_MAX;
382 ws.maximum = -DBL_MAX;
385 for (v = 0; v < cmd->n_vars; ++v)
387 struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
388 cmd->wv, cmd->exclude,
392 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
394 cmd->wv, cmd->exclude);
398 c = casereader_peek (input, 0);
401 casereader_destroy (input);
404 output_split_file_values (ds, c);
407 taint = taint_clone (casereader_get_taint (input));
409 ws.group_hash = hsh_create (4,
417 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
418 cmd->exclude, NULL, NULL);
419 if (cmd->missing_type == MISS_LISTWISE)
420 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
421 cmd->exclude, NULL, NULL);
422 input = casereader_create_filter_weight (input, dict, NULL, NULL);
424 reader = casereader_clone (input);
426 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
430 const double weight = dict_get_case_weight (dict, c, NULL);
432 const union value *indep_val = case_data (c, cmd->indep_var);
433 void **p = hsh_probe (ws.group_hash, &indep_val->f);
436 double *value = *p = xmalloc (sizeof *value);
437 *value = indep_val->f;
440 for (i = 0; i < cmd->n_vars; ++i)
443 struct per_var_ws *pvw = &ws.vws[i];
446 covariance_accumulate_pass1 (pvw->cov, c);
449 const struct variable *v = cmd->vars[i];
451 const union value *val = case_data (c, v);
453 struct group_proc *gp = group_proc_get (cmd->vars[i]);
454 struct hsh_table *group_hash = gp->group_hash;
456 struct group_statistics *gs;
458 gs = hsh_find (group_hash, indep_val );
462 gs = xmalloc (sizeof *gs);
468 gs->minimum = DBL_MAX;
469 gs->maximum = -DBL_MAX;
471 hsh_insert ( group_hash, gs );
474 if (!var_is_value_missing (v, val, cmd->exclude))
476 struct group_statistics *totals = &gp->ugs;
479 totals->sum += weight * val->f;
480 totals->ssq += weight * pow2 (val->f);
482 if ( val->f * weight < totals->minimum )
483 totals->minimum = val->f * weight;
485 if ( val->f * weight > totals->maximum )
486 totals->maximum = val->f * weight;
489 gs->sum += weight * val->f;
490 gs->ssq += weight * pow2 (val->f);
492 if ( val->f * weight < gs->minimum )
493 gs->minimum = val->f * weight;
495 if ( val->f * weight > gs->maximum )
496 gs->maximum = val->f * weight;
499 gp->n_groups = hsh_count (group_hash );
503 casereader_destroy (reader);
504 reader = casereader_clone (input);
505 for ( ; (c = casereader_read (reader) ); case_unref (c))
508 for (i = 0; i < cmd->n_vars; ++i)
510 struct per_var_ws *pvw = &ws.vws[i];
511 covariance_accumulate_pass2 (pvw->cov, c);
514 casereader_destroy (reader);
516 for (v = 0; v < cmd->n_vars; ++v)
518 struct per_var_ws *pvw = &ws.vws[v];
519 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
520 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
522 pvw->sst = gsl_matrix_get (cm, 0, 0);
526 pvw->sse = gsl_matrix_get (cm, 0, 0);
528 pvw->ssa = pvw->sst - pvw->sse;
530 pvw->n_groups = categoricals_total (cats);
535 for (v = 0; v < cmd->n_vars; ++v)
537 struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
539 categoricals_done (cats);
542 if ( cmd->stats & STATS_HOMOGENEITY )
543 levene (dict, casereader_clone (input), cmd->indep_var,
544 cmd->n_vars, cmd->vars, cmd->exclude);
546 casereader_destroy (input);
548 ws.actual_number_of_groups = hsh_count (ws.group_hash);
550 if (!taint_has_tainted_successor (taint))
551 output_oneway (cmd, &ws);
553 taint_destroy (taint);
556 /* Pre calculations */
558 precalc (const struct oneway_spec *cmd)
562 for (i = 0; i < cmd->n_vars; ++i)
564 struct group_proc *gp = group_proc_get (cmd->vars[i]);
565 struct group_statistics *totals = &gp->ugs;
567 /* Create a hash for each of the dependent variables.
568 The hash contains a group_statistics structure,
569 and is keyed by value of the independent variable */
571 gp->group_hash = hsh_create (4, compare_group, hash_group,
572 (hsh_free_func *) free_group,
578 totals->sum_diff = 0;
579 totals->maximum = -DBL_MAX;
580 totals->minimum = DBL_MAX;
584 /* Post calculations for the ONEWAY command */
586 postcalc (const struct oneway_spec *cmd)
590 for (i = 0; i < cmd->n_vars; ++i)
592 struct group_proc *gp = group_proc_get (cmd->vars[i]);
593 struct hsh_table *group_hash = gp->group_hash;
594 struct group_statistics *totals = &gp->ugs;
596 struct hsh_iterator g;
597 struct group_statistics *gs;
599 for (gs = hsh_first (group_hash, &g);
601 gs = hsh_next (group_hash, &g))
603 gs->mean = gs->sum / gs->n;
604 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
607 gs->n / (gs->n - 1) *
608 ( gs->ssq / gs->n - pow2 (gs->mean))
611 gs->se_mean = gs->std_dev / sqrt (gs->n);
612 gs->mean_diff = gs->sum_diff / gs->n;
615 totals->mean = totals->sum / totals->n;
616 totals->std_dev = sqrt (
617 totals->n / (totals->n - 1) *
618 (totals->ssq / totals->n - pow2 (totals->mean))
621 totals->se_mean = totals->std_dev / sqrt (totals->n);
625 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
626 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
629 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
633 /* Check the sanity of the given contrast values */
634 struct contrasts_node *coeff_list = NULL;
635 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
637 struct coeff_node *cn = NULL;
639 struct ll_list *cl = &coeff_list->coefficient_list;
642 if (ll_count (cl) != ws->actual_number_of_groups)
645 _("Number of contrast coefficients must equal the number of groups"));
646 coeff_list->bad_count = true;
650 ll_for_each (cn, struct coeff_node, ll, cl)
654 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
657 if (cmd->stats & STATS_DESCRIPTIVES)
658 show_descriptives (cmd, ws);
660 if (cmd->stats & STATS_HOMOGENEITY)
661 show_homogeneity (cmd, ws);
663 show_anova_table (cmd, ws);
666 if (ll_count (&cmd->contrast_list) > 0)
668 show_contrast_coeffs (cmd, ws);
669 show_contrast_tests (cmd, ws);
674 for (i = 0; i < cmd->n_vars; ++i )
676 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
678 hsh_destroy (group_hash);
681 hsh_destroy (ws->group_hash);
685 /* Show the ANOVA table */
687 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
691 size_t n_rows = cmd->n_vars * 3 + 1;
693 struct tab_table *t = tab_create (n_cols, n_rows);
695 tab_headers (t, 2, 0, 1, 0);
701 n_cols - 1, n_rows - 1);
703 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
704 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
705 tab_vline (t, TAL_0, 1, 0, 0);
707 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
708 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
709 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
710 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
711 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
714 for (i = 0; i < cmd->n_vars; ++i)
716 const struct per_var_ws *pvw = &ws->vws[i];
717 struct group_proc *gp = group_proc_get (cmd->vars[i]);
718 const double df1 = pvw->n_groups - 1;
719 const double df2 = pvw->cc - pvw->n_groups;
720 const double msa = pvw->ssa / df1;
722 const char *s = var_to_string (cmd->vars[i]);
724 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
725 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
726 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
727 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
730 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
733 gp->mse = (pvw->sst - pvw->ssa) / df2;
735 /* Sums of Squares */
736 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
737 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
738 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
741 /* Degrees of freedom */
742 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
743 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
744 tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
747 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
748 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
751 const double F = msa / gp->mse ;
754 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
756 /* The significance */
757 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
761 tab_title (t, _("ANOVA"));
766 /* Show the descriptives table */
768 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
775 const double confidence = 0.95;
776 const double q = (1.0 - confidence) / 2.0;
778 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
782 for (v = 0; v < cmd->n_vars; ++v)
783 n_rows += ws->actual_number_of_groups + 1;
785 t = tab_create (n_cols, n_rows);
786 tab_headers (t, 2, 0, 2, 0);
788 /* Put a frame around the entire box, and vertical lines inside */
793 n_cols - 1, n_rows - 1);
795 /* Underline headers */
796 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
797 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
799 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
800 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
801 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
802 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
805 tab_vline (t, TAL_0, 7, 0, 0);
806 tab_hline (t, TAL_1, 6, 7, 1);
807 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
808 _("%g%% Confidence Interval for Mean"),
811 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
812 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
814 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
815 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
817 tab_title (t, _("Descriptives"));
820 for (v = 0; v < cmd->n_vars; ++v)
822 const char *s = var_to_string (cmd->vars[v]);
823 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
827 struct per_var_ws *pvw = &ws->vws[v];
828 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
830 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
832 tab_hline (t, TAL_1, 0, n_cols - 1, row);
834 for (count = 0; count < categoricals_total (cats); ++count)
837 double n, mean, variance;
839 const union value *gval = categoricals_get_value_by_subscript (cats, count);
840 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
842 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
844 double std_dev = sqrt (variance);
845 double std_error = std_dev / sqrt (n) ;
849 ds_init_empty (&vstr);
851 var_append_value_name (cmd->indep_var, gval, &vstr);
853 tab_text (t, 1, row + count,
854 TAB_LEFT | TAT_TITLE,
859 /* Now fill in the numbers ... */
861 tab_fixed (t, 2, row + count, 0, n, 8, 0);
863 tab_double (t, 3, row + count, 0, mean, NULL);
865 tab_double (t, 4, row + count, 0, std_dev, NULL);
868 tab_double (t, 5, row + count, 0, std_error, NULL);
870 /* Now the confidence interval */
872 T = gsl_cdf_tdist_Qinv (q, n - 1);
874 tab_double (t, 6, row + count, 0,
875 mean - T * std_error, NULL);
877 tab_double (t, 7, row + count, 0,
878 mean + T * std_error, NULL);
882 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
883 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
888 double n, mean, variance;
890 moments1_calculate (ws->totals, &n, &mean, &variance, NULL, NULL);
892 double std_dev = sqrt (variance);
893 double std_error = std_dev / sqrt (n) ;
895 tab_text (t, 1, row + count,
896 TAB_LEFT | TAT_TITLE, _("Total"));
898 tab_double (t, 2, row + count, 0, n, wfmt);
900 tab_double (t, 3, row + count, 0, mean, NULL);
902 tab_double (t, 4, row + count, 0, std_dev, NULL);
904 tab_double (t, 5, row + count, 0, std_error, NULL);
906 /* Now the confidence interval */
908 T = gsl_cdf_tdist_Qinv (q, n - 1);
910 tab_double (t, 6, row + count, 0,
911 mean - T * std_error, NULL);
913 tab_double (t, 7, row + count, 0,
914 mean + T * std_error, NULL);
918 tab_double (t, 8, row + count, 0, ws->minimum, fmt);
919 tab_double (t, 9, row + count, 0, ws->maximum, fmt);
922 row += categoricals_total (cats) + 1;
928 /* Show the homogeneity table */
930 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
934 size_t n_rows = cmd->n_vars + 1;
936 struct tab_table *t = tab_create (n_cols, n_rows);
937 tab_headers (t, 1, 0, 1, 0);
939 /* Put a frame around the entire box, and vertical lines inside */
944 n_cols - 1, n_rows - 1);
947 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
948 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
950 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
951 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
952 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
953 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
955 tab_title (t, _("Test of Homogeneity of Variances"));
957 for (v = 0; v < cmd->n_vars; ++v)
959 const struct per_var_ws *pvw = &ws->vws[v];
960 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
962 const struct variable *var = cmd->vars[v];
963 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
964 const char *s = var_to_string (var);
966 const double df1 = pvw->n_groups - 1;
967 const double df2 = pvw->cc - pvw->n_groups;
968 double F = gp->levene;
970 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
973 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
974 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
975 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
977 /* Now the significance */
978 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
985 /* Show the contrast coefficients table */
987 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
992 int n_contrasts = ll_count (&cmd->contrast_list);
993 int n_cols = 2 + ws->actual_number_of_groups;
994 int n_rows = 2 + n_contrasts;
998 const struct covariance *cov = ws->vws[0].cov ;
1000 t = tab_create (n_cols, n_rows);
1001 tab_headers (t, 2, 0, 2, 0);
1003 /* Put a frame around the entire box, and vertical lines inside */
1008 n_cols - 1, n_rows - 1);
1022 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1023 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1025 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1027 tab_title (t, _("Contrast Coefficients"));
1029 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1032 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1033 var_to_string (cmd->indep_var));
1035 for ( cli = ll_head (&cmd->contrast_list);
1036 cli != ll_null (&cmd->contrast_list);
1037 cli = ll_next (cli))
1040 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1043 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1045 for (coeffi = ll_head (&cn->coefficient_list);
1046 coeffi != ll_null (&cn->coefficient_list);
1047 ++count, coeffi = ll_next (coeffi))
1049 const struct categoricals *cats = covariance_get_categoricals (cov);
1050 const union value *val = categoricals_get_value_by_subscript (cats, count);
1053 ds_init_empty (&vstr);
1055 var_append_value_name (cmd->indep_var, val, &vstr);
1057 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1062 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
1065 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1067 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1077 /* Show the results of the contrast tests */
1079 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1081 int n_contrasts = ll_count (&cmd->contrast_list);
1084 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1086 struct tab_table *t;
1088 t = tab_create (n_cols, n_rows);
1089 tab_headers (t, 3, 0, 1, 0);
1091 /* Put a frame around the entire box, and vertical lines inside */
1096 n_cols - 1, n_rows - 1);
1104 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1105 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1107 tab_title (t, _("Contrast Tests"));
1109 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1110 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1111 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1112 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1113 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1114 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1116 for (v = 0; v < cmd->n_vars; ++v)
1120 int lines_per_variable = 2 * n_contrasts;
1122 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1123 var_to_string (cmd->vars[v]));
1125 for ( cli = ll_head (&cmd->contrast_list);
1126 cli != ll_null (&cmd->contrast_list);
1127 ++i, cli = ll_next (cli))
1129 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1130 struct ll *coeffi = ll_head (&cn->coefficient_list);
1132 double contrast_value = 0.0;
1133 double coef_msq = 0.0;
1134 struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
1135 struct hsh_table *group_hash = grp_data->group_hash;
1137 void *const *group_stat_array;
1140 double std_error_contrast;
1142 double sec_vneq = 0.0;
1144 /* Note: The calculation of the degrees of freedom in the
1145 "variances not equal" case is painfull!!
1146 The following formula may help to understand it:
1147 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1150 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1155 double df_denominator = 0.0;
1156 double df_numerator = 0.0;
1159 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1160 TAB_LEFT | TAT_TITLE,
1161 _("Assume equal variances"));
1163 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1164 TAB_LEFT | TAT_TITLE,
1165 _("Does not assume equal"));
1168 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1169 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1172 tab_text_format (t, 2,
1173 (v * lines_per_variable) + i + 1 + n_contrasts,
1174 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1179 group_stat_array = hsh_sort (group_hash);
1182 coeffi != ll_null (&cn->coefficient_list) &&
1183 ci < hsh_count (group_hash);
1184 ++ci, coeffi = ll_next (coeffi))
1186 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1187 const double coef = cn->coeff;
1188 struct group_statistics *gs = group_stat_array[ci];
1190 const double winv = pow2 (gs->std_dev) / gs->n;
1192 contrast_value += coef * gs->mean;
1194 coef_msq += (coef * coef) / gs->n;
1196 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
1198 df_numerator += (coef * coef) * winv;
1199 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
1202 sec_vneq = sqrt (sec_vneq);
1204 df_numerator = pow2 (df_numerator);
1206 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1207 TAB_RIGHT, contrast_value, NULL);
1209 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1211 TAB_RIGHT, contrast_value, NULL);
1213 std_error_contrast = sqrt (grp_data->mse * coef_msq);
1216 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1217 TAB_RIGHT, std_error_contrast,
1220 T = fabs (contrast_value / std_error_contrast);
1224 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1228 df = grp_data->ugs.n - grp_data->n_groups;
1230 /* Degrees of Freedom */
1231 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1236 /* Significance TWO TAILED !!*/
1237 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1238 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1241 /* Now for the Variances NOT Equal case */
1245 (v * lines_per_variable) + i + 1 + n_contrasts,
1246 TAB_RIGHT, sec_vneq,
1249 T = contrast_value / sec_vneq;
1251 (v * lines_per_variable) + i + 1 + n_contrasts,
1255 df = df_numerator / df_denominator;
1258 (v * lines_per_variable) + i + 1 + n_contrasts,
1262 /* The Significance */
1263 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1264 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1269 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);