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;
106 /* Workspace variable for each dependent variable */
109 struct covariance *cov;
121 struct oneway_workspace
123 /* The number of distinct values of the independent variable, when all
124 missing values are disregarded */
125 int actual_number_of_groups;
127 /* A hash table containing all the distinct values of the independent
129 struct hsh_table *group_hash;
131 struct per_var_ws *vws;
133 /* An array of descriptive data. One for each dependent variable */
134 struct descriptive_data **dd_total;
137 /* Routines to show the output tables */
138 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
139 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
140 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
142 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
143 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
146 cmd_oneway (struct lexer *lexer, struct dataset *ds)
148 const struct dictionary *dict = dataset_dict (ds);
149 struct oneway_spec oneway ;
152 oneway.indep_var = NULL;
154 oneway.missing_type = MISS_ANALYSIS;
155 oneway.exclude = MV_ANY;
156 oneway.wv = dict_get_weight (dict);
158 ll_init (&oneway.contrast_list);
161 if ( lex_match (lexer, '/'))
163 if (!lex_force_match_id (lexer, "VARIABLES"))
167 lex_match (lexer, '=');
170 if (!parse_variables_const (lexer, dict,
171 &oneway.vars, &oneway.n_vars,
172 PV_NO_DUPLICATE | PV_NUMERIC))
175 lex_force_match (lexer, T_BY);
177 oneway.indep_var = parse_variable_const (lexer, dict);
179 while (lex_token (lexer) != '.')
181 lex_match (lexer, '/');
183 if (lex_match_id (lexer, "STATISTICS"))
185 lex_match (lexer, '=');
186 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
188 if (lex_match_id (lexer, "DESCRIPTIVES"))
190 oneway.stats |= STATS_DESCRIPTIVES;
192 else if (lex_match_id (lexer, "HOMOGENEITY"))
194 oneway.stats |= STATS_HOMOGENEITY;
198 lex_error (lexer, NULL);
203 else if (lex_match_id (lexer, "CONTRAST"))
205 struct contrasts_node *cl = xzalloc (sizeof *cl);
207 struct ll_list *coefficient_list = &cl->coefficient_list;
208 lex_match (lexer, '=');
210 ll_init (coefficient_list);
212 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
214 if ( lex_is_number (lexer))
216 struct coeff_node *cc = xmalloc (sizeof *cc);
217 cc->coeff = lex_number (lexer);
219 ll_push_tail (coefficient_list, &cc->ll);
224 lex_error (lexer, NULL);
229 ll_push_tail (&oneway.contrast_list, &cl->ll);
231 else if (lex_match_id (lexer, "MISSING"))
233 lex_match (lexer, '=');
234 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
236 if (lex_match_id (lexer, "INCLUDE"))
238 oneway.exclude = MV_SYSTEM;
240 else if (lex_match_id (lexer, "EXCLUDE"))
242 oneway.exclude = MV_ANY;
244 else if (lex_match_id (lexer, "LISTWISE"))
246 oneway.missing_type = MISS_LISTWISE;
248 else if (lex_match_id (lexer, "ANALYSIS"))
250 oneway.missing_type = MISS_ANALYSIS;
254 lex_error (lexer, NULL);
261 lex_error (lexer, NULL);
268 struct casegrouper *grouper;
269 struct casereader *group;
272 grouper = casegrouper_create_splits (proc_open (ds), dict);
273 while (casegrouper_get_next_group (grouper, &group))
274 run_oneway (&oneway, group, ds);
275 ok = casegrouper_destroy (grouper);
276 ok = proc_commit (ds) && ok;
291 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
293 const double *a = a_;
294 const double *b = b_;
295 return *a < *b ? -1 : *a > *b;
299 do_hash_double (const void *value_, const void *aux UNUSED)
301 const double *value = value_;
302 return hash_double (*value, 0);
306 free_double (void *value_, const void *aux UNUSED)
308 double *value = value_;
314 static void postcalc (const struct oneway_spec *cmd);
315 static void precalc (const struct oneway_spec *cmd);
317 struct descriptive_data
319 const struct variable *var;
320 struct moments1 *mom;
326 static struct descriptive_data *
327 dd_create (const struct variable *var)
329 struct descriptive_data *dd = xmalloc (sizeof *dd);
331 dd->mom = moments1_create (MOMENT_VARIANCE);
332 dd->minimum = DBL_MAX;
333 dd->maximum = -DBL_MAX;
341 makeit (void *aux1, void *aux2 UNUSED)
343 const struct variable *var = aux1;
345 struct descriptive_data *dd = dd_create (var);
351 updateit (void *user_data, const struct variable *wv,
352 const struct variable *catvar UNUSED,
353 const struct ccase *c,
354 void *aux1, void *aux2)
356 struct descriptive_data *dd = user_data;
358 const struct variable *varp = aux1;
360 const union value *valx = case_data (c, varp);
362 struct descriptive_data *dd_total = aux2;
366 weight = case_data (c, wv)->f;
368 moments1_add (dd->mom, valx->f, weight);
369 if (valx->f * weight < dd->minimum)
370 dd->minimum = valx->f * weight;
372 if (valx->f * weight > dd->maximum)
373 dd->maximum = valx->f * weight;
376 const struct variable *var = dd_total->var;
377 const union value *val = case_data (c, var);
379 moments1_add (dd_total->mom,
383 if (val->f * weight < dd_total->minimum)
384 dd_total->minimum = val->f * weight;
386 if (val->f * weight > dd_total->maximum)
387 dd_total->maximum = val->f * weight;
392 run_oneway (const struct oneway_spec *cmd,
393 struct casereader *input,
394 const struct dataset *ds)
398 struct dictionary *dict = dataset_dict (ds);
399 struct casereader *reader;
402 struct oneway_workspace ws;
404 ws.actual_number_of_groups = 0;
405 ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
406 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
408 for (v = 0 ; v < cmd->n_vars; ++v)
409 ws.dd_total[v] = dd_create (cmd->vars[v]);
411 for (v = 0; v < cmd->n_vars; ++v)
413 struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
414 cmd->wv, cmd->exclude,
417 cmd->vars[v], ws.dd_total[v]);
419 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
421 cmd->wv, cmd->exclude);
425 c = casereader_peek (input, 0);
428 casereader_destroy (input);
431 output_split_file_values (ds, c);
434 taint = taint_clone (casereader_get_taint (input));
436 ws.group_hash = hsh_create (4,
444 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
445 cmd->exclude, NULL, NULL);
446 if (cmd->missing_type == MISS_LISTWISE)
447 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
448 cmd->exclude, NULL, NULL);
449 input = casereader_create_filter_weight (input, dict, NULL, NULL);
451 reader = casereader_clone (input);
453 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
457 const double weight = dict_get_case_weight (dict, c, NULL);
459 const union value *indep_val = case_data (c, cmd->indep_var);
460 void **p = hsh_probe (ws.group_hash, &indep_val->f);
463 double *value = *p = xmalloc (sizeof *value);
464 *value = indep_val->f;
467 for (i = 0; i < cmd->n_vars; ++i)
470 struct per_var_ws *pvw = &ws.vws[i];
473 covariance_accumulate_pass1 (pvw->cov, c);
476 const struct variable *v = cmd->vars[i];
478 const union value *val = case_data (c, v);
480 struct group_proc *gp = group_proc_get (cmd->vars[i]);
481 struct hsh_table *group_hash = gp->group_hash;
483 struct group_statistics *gs;
485 gs = hsh_find (group_hash, indep_val );
489 gs = xmalloc (sizeof *gs);
495 gs->minimum = DBL_MAX;
496 gs->maximum = -DBL_MAX;
498 hsh_insert ( group_hash, gs );
501 if (!var_is_value_missing (v, val, cmd->exclude))
503 struct group_statistics *totals = &gp->ugs;
506 totals->sum += weight * val->f;
507 totals->ssq += weight * pow2 (val->f);
509 if ( val->f * weight < totals->minimum )
510 totals->minimum = val->f * weight;
512 if ( val->f * weight > totals->maximum )
513 totals->maximum = val->f * weight;
516 gs->sum += weight * val->f;
517 gs->ssq += weight * pow2 (val->f);
519 if ( val->f * weight < gs->minimum )
520 gs->minimum = val->f * weight;
522 if ( val->f * weight > gs->maximum )
523 gs->maximum = val->f * weight;
526 gp->n_groups = hsh_count (group_hash );
530 casereader_destroy (reader);
531 reader = casereader_clone (input);
532 for ( ; (c = casereader_read (reader) ); case_unref (c))
535 for (i = 0; i < cmd->n_vars; ++i)
537 struct per_var_ws *pvw = &ws.vws[i];
538 covariance_accumulate_pass2 (pvw->cov, c);
541 casereader_destroy (reader);
543 for (v = 0; v < cmd->n_vars; ++v)
545 struct per_var_ws *pvw = &ws.vws[v];
546 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
547 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
549 pvw->sst = gsl_matrix_get (cm, 0, 0);
553 pvw->sse = gsl_matrix_get (cm, 0, 0);
555 pvw->ssa = pvw->sst - pvw->sse;
557 pvw->n_groups = categoricals_total (cats);
559 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->cc - pvw->n_groups);
565 for (v = 0; v < cmd->n_vars; ++v)
567 struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
569 categoricals_done (cats);
571 if (categoricals_total (cats) > ws.actual_number_of_groups)
572 ws.actual_number_of_groups = categoricals_total (cats);
575 if ( cmd->stats & STATS_HOMOGENEITY )
576 levene (dict, casereader_clone (input), cmd->indep_var,
577 cmd->n_vars, cmd->vars, cmd->exclude);
579 casereader_destroy (input);
581 if (!taint_has_tainted_successor (taint))
582 output_oneway (cmd, &ws);
584 taint_destroy (taint);
587 /* Pre calculations */
589 precalc (const struct oneway_spec *cmd)
593 for (i = 0; i < cmd->n_vars; ++i)
595 struct group_proc *gp = group_proc_get (cmd->vars[i]);
596 struct group_statistics *totals = &gp->ugs;
598 /* Create a hash for each of the dependent variables.
599 The hash contains a group_statistics structure,
600 and is keyed by value of the independent variable */
602 gp->group_hash = hsh_create (4, compare_group, hash_group,
603 (hsh_free_func *) free_group,
609 totals->sum_diff = 0;
610 totals->maximum = -DBL_MAX;
611 totals->minimum = DBL_MAX;
615 /* Post calculations for the ONEWAY command */
617 postcalc (const struct oneway_spec *cmd)
621 for (i = 0; i < cmd->n_vars; ++i)
623 struct group_proc *gp = group_proc_get (cmd->vars[i]);
624 struct hsh_table *group_hash = gp->group_hash;
625 struct group_statistics *totals = &gp->ugs;
627 struct hsh_iterator g;
628 struct group_statistics *gs;
630 for (gs = hsh_first (group_hash, &g);
632 gs = hsh_next (group_hash, &g))
634 gs->mean = gs->sum / gs->n;
635 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
638 gs->n / (gs->n - 1) *
639 ( gs->ssq / gs->n - pow2 (gs->mean))
642 gs->se_mean = gs->std_dev / sqrt (gs->n);
643 gs->mean_diff = gs->sum_diff / gs->n;
646 totals->mean = totals->sum / totals->n;
647 totals->std_dev = sqrt (
648 totals->n / (totals->n - 1) *
649 (totals->ssq / totals->n - pow2 (totals->mean))
652 totals->se_mean = totals->std_dev / sqrt (totals->n);
656 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
657 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
660 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
664 /* Check the sanity of the given contrast values */
665 struct contrasts_node *coeff_list = NULL;
666 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
668 struct coeff_node *cn = NULL;
670 struct ll_list *cl = &coeff_list->coefficient_list;
673 if (ll_count (cl) != ws->actual_number_of_groups)
676 _("Number of contrast coefficients must equal the number of groups"));
677 coeff_list->bad_count = true;
681 ll_for_each (cn, struct coeff_node, ll, cl)
685 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
688 if (cmd->stats & STATS_DESCRIPTIVES)
689 show_descriptives (cmd, ws);
691 if (cmd->stats & STATS_HOMOGENEITY)
692 show_homogeneity (cmd, ws);
694 show_anova_table (cmd, ws);
697 if (ll_count (&cmd->contrast_list) > 0)
699 show_contrast_coeffs (cmd, ws);
700 show_contrast_tests (cmd, ws);
705 for (i = 0; i < cmd->n_vars; ++i )
707 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
709 hsh_destroy (group_hash);
712 hsh_destroy (ws->group_hash);
716 /* Show the ANOVA table */
718 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
722 size_t n_rows = cmd->n_vars * 3 + 1;
724 struct tab_table *t = tab_create (n_cols, n_rows);
726 tab_headers (t, 2, 0, 1, 0);
732 n_cols - 1, n_rows - 1);
734 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
735 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
736 tab_vline (t, TAL_0, 1, 0, 0);
738 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
739 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
740 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
741 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
742 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
745 for (i = 0; i < cmd->n_vars; ++i)
747 const struct per_var_ws *pvw = &ws->vws[i];
748 const double df1 = pvw->n_groups - 1;
749 const double df2 = pvw->cc - pvw->n_groups;
750 const double msa = pvw->ssa / df1;
752 const char *s = var_to_string (cmd->vars[i]);
754 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
755 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
756 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
757 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
760 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
763 /* Sums of Squares */
764 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
765 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
766 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
769 /* Degrees of freedom */
770 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
771 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
772 tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
775 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
776 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
779 const double F = msa / pvw->mse ;
782 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
784 /* The significance */
785 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
789 tab_title (t, _("ANOVA"));
794 /* Show the descriptives table */
796 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
803 const double confidence = 0.95;
804 const double q = (1.0 - confidence) / 2.0;
806 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
810 for (v = 0; v < cmd->n_vars; ++v)
811 n_rows += ws->actual_number_of_groups + 1;
813 t = tab_create (n_cols, n_rows);
814 tab_headers (t, 2, 0, 2, 0);
816 /* Put a frame around the entire box, and vertical lines inside */
821 n_cols - 1, n_rows - 1);
823 /* Underline headers */
824 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
825 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
827 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
828 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
829 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
830 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
833 tab_vline (t, TAL_0, 7, 0, 0);
834 tab_hline (t, TAL_1, 6, 7, 1);
835 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
836 _("%g%% Confidence Interval for Mean"),
839 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
840 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
842 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
843 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
845 tab_title (t, _("Descriptives"));
848 for (v = 0; v < cmd->n_vars; ++v)
850 const char *s = var_to_string (cmd->vars[v]);
851 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
855 struct per_var_ws *pvw = &ws->vws[v];
856 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
858 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
860 tab_hline (t, TAL_1, 0, n_cols - 1, row);
862 for (count = 0; count < categoricals_total (cats); ++count)
865 double n, mean, variance;
866 double std_dev, std_error ;
870 const union value *gval = categoricals_get_value_by_subscript (cats, count);
871 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
873 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
875 std_dev = sqrt (variance);
876 std_error = std_dev / sqrt (n) ;
878 ds_init_empty (&vstr);
880 var_append_value_name (cmd->indep_var, gval, &vstr);
882 tab_text (t, 1, row + count,
883 TAB_LEFT | TAT_TITLE,
888 /* Now fill in the numbers ... */
890 tab_fixed (t, 2, row + count, 0, n, 8, 0);
892 tab_double (t, 3, row + count, 0, mean, NULL);
894 tab_double (t, 4, row + count, 0, std_dev, NULL);
897 tab_double (t, 5, row + count, 0, std_error, NULL);
899 /* Now the confidence interval */
901 T = gsl_cdf_tdist_Qinv (q, n - 1);
903 tab_double (t, 6, row + count, 0,
904 mean - T * std_error, NULL);
906 tab_double (t, 7, row + count, 0,
907 mean + T * std_error, NULL);
911 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
912 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
917 double n, mean, variance;
921 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
923 std_dev = sqrt (variance);
924 std_error = std_dev / sqrt (n) ;
926 tab_text (t, 1, row + count,
927 TAB_LEFT | TAT_TITLE, _("Total"));
929 tab_double (t, 2, row + count, 0, n, wfmt);
931 tab_double (t, 3, row + count, 0, mean, NULL);
933 tab_double (t, 4, row + count, 0, std_dev, NULL);
935 tab_double (t, 5, row + count, 0, std_error, NULL);
937 /* Now the confidence interval */
938 T = gsl_cdf_tdist_Qinv (q, n - 1);
940 tab_double (t, 6, row + count, 0,
941 mean - T * std_error, NULL);
943 tab_double (t, 7, row + count, 0,
944 mean + T * std_error, NULL);
947 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
948 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
951 row += categoricals_total (cats) + 1;
957 /* Show the homogeneity table */
959 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
963 size_t n_rows = cmd->n_vars + 1;
965 struct tab_table *t = tab_create (n_cols, n_rows);
966 tab_headers (t, 1, 0, 1, 0);
968 /* Put a frame around the entire box, and vertical lines inside */
973 n_cols - 1, n_rows - 1);
976 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
977 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
979 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
980 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
981 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
982 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
984 tab_title (t, _("Test of Homogeneity of Variances"));
986 for (v = 0; v < cmd->n_vars; ++v)
988 const struct per_var_ws *pvw = &ws->vws[v];
989 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
991 const struct variable *var = cmd->vars[v];
992 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
993 const char *s = var_to_string (var);
995 const double df1 = pvw->n_groups - 1;
996 const double df2 = pvw->cc - pvw->n_groups;
997 double F = gp->levene;
999 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1002 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1003 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1004 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1006 /* Now the significance */
1007 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1014 /* Show the contrast coefficients table */
1016 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1021 int n_contrasts = ll_count (&cmd->contrast_list);
1022 int n_cols = 2 + ws->actual_number_of_groups;
1023 int n_rows = 2 + n_contrasts;
1025 struct tab_table *t;
1027 const struct covariance *cov = ws->vws[0].cov ;
1029 t = tab_create (n_cols, n_rows);
1030 tab_headers (t, 2, 0, 2, 0);
1032 /* Put a frame around the entire box, and vertical lines inside */
1037 n_cols - 1, n_rows - 1);
1051 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1052 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1054 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1056 tab_title (t, _("Contrast Coefficients"));
1058 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1061 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1062 var_to_string (cmd->indep_var));
1064 for ( cli = ll_head (&cmd->contrast_list);
1065 cli != ll_null (&cmd->contrast_list);
1066 cli = ll_next (cli))
1069 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1072 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1074 for (coeffi = ll_head (&cn->coefficient_list);
1075 coeffi != ll_null (&cn->coefficient_list);
1076 ++count, coeffi = ll_next (coeffi))
1078 const struct categoricals *cats = covariance_get_categoricals (cov);
1079 const union value *val = categoricals_get_value_by_subscript (cats, count);
1082 ds_init_empty (&vstr);
1084 var_append_value_name (cmd->indep_var, val, &vstr);
1086 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1091 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
1094 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1096 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1106 /* Show the results of the contrast tests */
1108 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1110 int n_contrasts = ll_count (&cmd->contrast_list);
1113 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1115 struct tab_table *t;
1117 t = tab_create (n_cols, n_rows);
1118 tab_headers (t, 3, 0, 1, 0);
1120 /* Put a frame around the entire box, and vertical lines inside */
1125 n_cols - 1, n_rows - 1);
1133 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1134 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1136 tab_title (t, _("Contrast Tests"));
1138 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1139 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1140 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1141 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1142 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1143 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1145 for (v = 0; v < cmd->n_vars; ++v)
1147 const struct per_var_ws *pvw = &ws->vws[v];
1148 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1151 int lines_per_variable = 2 * n_contrasts;
1153 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1154 var_to_string (cmd->vars[v]));
1156 for ( cli = ll_head (&cmd->contrast_list);
1157 cli != ll_null (&cmd->contrast_list);
1158 ++i, cli = ll_next (cli))
1160 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1163 double contrast_value = 0.0;
1164 double coef_msq = 0.0;
1167 double std_error_contrast;
1169 double sec_vneq = 0.0;
1171 /* Note: The calculation of the degrees of freedom in the
1172 "variances not equal" case is painfull!!
1173 The following formula may help to understand it:
1174 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1177 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1182 double df_denominator = 0.0;
1183 double df_numerator = 0.0;
1186 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1187 df = grand_n - pvw->n_groups;
1191 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1192 TAB_LEFT | TAT_TITLE,
1193 _("Assume equal variances"));
1195 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1196 TAB_LEFT | TAT_TITLE,
1197 _("Does not assume equal"));
1200 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1201 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1204 tab_text_format (t, 2,
1205 (v * lines_per_variable) + i + 1 + n_contrasts,
1206 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1211 for (coeffi = ll_head (&cn->coefficient_list);
1212 coeffi != ll_null (&cn->coefficient_list);
1213 ++ci, coeffi = ll_next (coeffi))
1215 double n, mean, variance;
1216 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1218 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1220 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1221 const double coef = cn->coeff;
1223 const double winv = variance / n;
1225 contrast_value += coef * mean;
1227 coef_msq += (pow2 (coef)) / n;
1229 sec_vneq += (pow2 (coef)) * variance / n;
1231 df_numerator += (pow2 (coef)) * winv;
1232 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1235 sec_vneq = sqrt (sec_vneq);
1237 df_numerator = pow2 (df_numerator);
1239 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1240 TAB_RIGHT, contrast_value, NULL);
1242 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1244 TAB_RIGHT, contrast_value, NULL);
1246 std_error_contrast = sqrt (pvw->mse * coef_msq);
1249 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1250 TAB_RIGHT, std_error_contrast,
1253 T = fabs (contrast_value / std_error_contrast);
1257 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1262 /* Degrees of Freedom */
1263 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1268 /* Significance TWO TAILED !!*/
1269 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1270 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1273 /* Now for the Variances NOT Equal case */
1277 (v * lines_per_variable) + i + 1 + n_contrasts,
1278 TAB_RIGHT, sec_vneq,
1281 T = contrast_value / sec_vneq;
1283 (v * lines_per_variable) + i + 1 + n_contrasts,
1287 df = df_numerator / df_denominator;
1290 (v * lines_per_variable) + i + 1 + n_contrasts,
1294 /* The Significance */
1295 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1296 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1301 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);