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;
405 ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
407 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
409 for (v = 0 ; v < cmd->n_vars; ++v)
411 ws.dd_total[v] = dd_create (cmd->vars[v]);
415 for (v = 0; v < cmd->n_vars; ++v)
417 struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
418 cmd->wv, cmd->exclude,
421 cmd->vars[v], ws.dd_total[v]);
423 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
425 cmd->wv, cmd->exclude);
429 c = casereader_peek (input, 0);
432 casereader_destroy (input);
435 output_split_file_values (ds, c);
438 taint = taint_clone (casereader_get_taint (input));
440 ws.group_hash = hsh_create (4,
448 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
449 cmd->exclude, NULL, NULL);
450 if (cmd->missing_type == MISS_LISTWISE)
451 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
452 cmd->exclude, NULL, NULL);
453 input = casereader_create_filter_weight (input, dict, NULL, NULL);
455 reader = casereader_clone (input);
457 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
461 const double weight = dict_get_case_weight (dict, c, NULL);
463 const union value *indep_val = case_data (c, cmd->indep_var);
464 void **p = hsh_probe (ws.group_hash, &indep_val->f);
467 double *value = *p = xmalloc (sizeof *value);
468 *value = indep_val->f;
471 for (i = 0; i < cmd->n_vars; ++i)
474 struct per_var_ws *pvw = &ws.vws[i];
477 covariance_accumulate_pass1 (pvw->cov, c);
480 const struct variable *v = cmd->vars[i];
482 const union value *val = case_data (c, v);
484 struct group_proc *gp = group_proc_get (cmd->vars[i]);
485 struct hsh_table *group_hash = gp->group_hash;
487 struct group_statistics *gs;
489 gs = hsh_find (group_hash, indep_val );
493 gs = xmalloc (sizeof *gs);
499 gs->minimum = DBL_MAX;
500 gs->maximum = -DBL_MAX;
502 hsh_insert ( group_hash, gs );
505 if (!var_is_value_missing (v, val, cmd->exclude))
507 struct group_statistics *totals = &gp->ugs;
510 totals->sum += weight * val->f;
511 totals->ssq += weight * pow2 (val->f);
513 if ( val->f * weight < totals->minimum )
514 totals->minimum = val->f * weight;
516 if ( val->f * weight > totals->maximum )
517 totals->maximum = val->f * weight;
520 gs->sum += weight * val->f;
521 gs->ssq += weight * pow2 (val->f);
523 if ( val->f * weight < gs->minimum )
524 gs->minimum = val->f * weight;
526 if ( val->f * weight > gs->maximum )
527 gs->maximum = val->f * weight;
530 gp->n_groups = hsh_count (group_hash );
534 casereader_destroy (reader);
535 reader = casereader_clone (input);
536 for ( ; (c = casereader_read (reader) ); case_unref (c))
539 for (i = 0; i < cmd->n_vars; ++i)
541 struct per_var_ws *pvw = &ws.vws[i];
542 covariance_accumulate_pass2 (pvw->cov, c);
545 casereader_destroy (reader);
547 for (v = 0; v < cmd->n_vars; ++v)
549 struct per_var_ws *pvw = &ws.vws[v];
550 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
551 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
553 pvw->sst = gsl_matrix_get (cm, 0, 0);
557 pvw->sse = gsl_matrix_get (cm, 0, 0);
559 pvw->ssa = pvw->sst - pvw->sse;
561 pvw->n_groups = categoricals_total (cats);
563 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->cc - pvw->n_groups);
568 for (v = 0; v < cmd->n_vars; ++v)
570 struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
572 categoricals_done (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 ws.actual_number_of_groups = hsh_count (ws.group_hash);
583 if (!taint_has_tainted_successor (taint))
584 output_oneway (cmd, &ws);
586 taint_destroy (taint);
589 /* Pre calculations */
591 precalc (const struct oneway_spec *cmd)
595 for (i = 0; i < cmd->n_vars; ++i)
597 struct group_proc *gp = group_proc_get (cmd->vars[i]);
598 struct group_statistics *totals = &gp->ugs;
600 /* Create a hash for each of the dependent variables.
601 The hash contains a group_statistics structure,
602 and is keyed by value of the independent variable */
604 gp->group_hash = hsh_create (4, compare_group, hash_group,
605 (hsh_free_func *) free_group,
611 totals->sum_diff = 0;
612 totals->maximum = -DBL_MAX;
613 totals->minimum = DBL_MAX;
617 /* Post calculations for the ONEWAY command */
619 postcalc (const struct oneway_spec *cmd)
623 for (i = 0; i < cmd->n_vars; ++i)
625 struct group_proc *gp = group_proc_get (cmd->vars[i]);
626 struct hsh_table *group_hash = gp->group_hash;
627 struct group_statistics *totals = &gp->ugs;
629 struct hsh_iterator g;
630 struct group_statistics *gs;
632 for (gs = hsh_first (group_hash, &g);
634 gs = hsh_next (group_hash, &g))
636 gs->mean = gs->sum / gs->n;
637 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
640 gs->n / (gs->n - 1) *
641 ( gs->ssq / gs->n - pow2 (gs->mean))
644 gs->se_mean = gs->std_dev / sqrt (gs->n);
645 gs->mean_diff = gs->sum_diff / gs->n;
648 totals->mean = totals->sum / totals->n;
649 totals->std_dev = sqrt (
650 totals->n / (totals->n - 1) *
651 (totals->ssq / totals->n - pow2 (totals->mean))
654 totals->se_mean = totals->std_dev / sqrt (totals->n);
658 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
659 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
662 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
666 /* Check the sanity of the given contrast values */
667 struct contrasts_node *coeff_list = NULL;
668 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
670 struct coeff_node *cn = NULL;
672 struct ll_list *cl = &coeff_list->coefficient_list;
675 if (ll_count (cl) != ws->actual_number_of_groups)
678 _("Number of contrast coefficients must equal the number of groups"));
679 coeff_list->bad_count = true;
683 ll_for_each (cn, struct coeff_node, ll, cl)
687 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
690 if (cmd->stats & STATS_DESCRIPTIVES)
691 show_descriptives (cmd, ws);
693 if (cmd->stats & STATS_HOMOGENEITY)
694 show_homogeneity (cmd, ws);
696 show_anova_table (cmd, ws);
699 if (ll_count (&cmd->contrast_list) > 0)
701 show_contrast_coeffs (cmd, ws);
702 show_contrast_tests (cmd, ws);
707 for (i = 0; i < cmd->n_vars; ++i )
709 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
711 hsh_destroy (group_hash);
714 hsh_destroy (ws->group_hash);
718 /* Show the ANOVA table */
720 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
724 size_t n_rows = cmd->n_vars * 3 + 1;
726 struct tab_table *t = tab_create (n_cols, n_rows);
728 tab_headers (t, 2, 0, 1, 0);
734 n_cols - 1, n_rows - 1);
736 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
737 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
738 tab_vline (t, TAL_0, 1, 0, 0);
740 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
741 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
742 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
743 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
744 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
747 for (i = 0; i < cmd->n_vars; ++i)
749 const struct per_var_ws *pvw = &ws->vws[i];
750 const double df1 = pvw->n_groups - 1;
751 const double df2 = pvw->cc - pvw->n_groups;
752 const double msa = pvw->ssa / df1;
754 const char *s = var_to_string (cmd->vars[i]);
756 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
757 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
758 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
759 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
762 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
765 /* Sums of Squares */
766 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
767 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
768 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
771 /* Degrees of freedom */
772 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
773 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
774 tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
777 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
778 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
781 const double F = msa / pvw->mse ;
784 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
786 /* The significance */
787 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
791 tab_title (t, _("ANOVA"));
796 /* Show the descriptives table */
798 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
805 const double confidence = 0.95;
806 const double q = (1.0 - confidence) / 2.0;
808 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
812 for (v = 0; v < cmd->n_vars; ++v)
813 n_rows += ws->actual_number_of_groups + 1;
815 t = tab_create (n_cols, n_rows);
816 tab_headers (t, 2, 0, 2, 0);
818 /* Put a frame around the entire box, and vertical lines inside */
823 n_cols - 1, n_rows - 1);
825 /* Underline headers */
826 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
827 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
829 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
830 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
831 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
832 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
835 tab_vline (t, TAL_0, 7, 0, 0);
836 tab_hline (t, TAL_1, 6, 7, 1);
837 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
838 _("%g%% Confidence Interval for Mean"),
841 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
842 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
844 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
845 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
847 tab_title (t, _("Descriptives"));
850 for (v = 0; v < cmd->n_vars; ++v)
852 const char *s = var_to_string (cmd->vars[v]);
853 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
857 struct per_var_ws *pvw = &ws->vws[v];
858 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
860 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
862 tab_hline (t, TAL_1, 0, n_cols - 1, row);
864 for (count = 0; count < categoricals_total (cats); ++count)
867 double n, mean, variance;
868 double std_dev, std_error ;
872 const union value *gval = categoricals_get_value_by_subscript (cats, count);
873 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
875 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
877 std_dev = sqrt (variance);
878 std_error = std_dev / sqrt (n) ;
880 ds_init_empty (&vstr);
882 var_append_value_name (cmd->indep_var, gval, &vstr);
884 tab_text (t, 1, row + count,
885 TAB_LEFT | TAT_TITLE,
890 /* Now fill in the numbers ... */
892 tab_fixed (t, 2, row + count, 0, n, 8, 0);
894 tab_double (t, 3, row + count, 0, mean, NULL);
896 tab_double (t, 4, row + count, 0, std_dev, NULL);
899 tab_double (t, 5, row + count, 0, std_error, NULL);
901 /* Now the confidence interval */
903 T = gsl_cdf_tdist_Qinv (q, n - 1);
905 tab_double (t, 6, row + count, 0,
906 mean - T * std_error, NULL);
908 tab_double (t, 7, row + count, 0,
909 mean + T * std_error, NULL);
913 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
914 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
919 double n, mean, variance;
923 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
925 std_dev = sqrt (variance);
926 std_error = std_dev / sqrt (n) ;
928 tab_text (t, 1, row + count,
929 TAB_LEFT | TAT_TITLE, _("Total"));
931 tab_double (t, 2, row + count, 0, n, wfmt);
933 tab_double (t, 3, row + count, 0, mean, NULL);
935 tab_double (t, 4, row + count, 0, std_dev, NULL);
937 tab_double (t, 5, row + count, 0, std_error, NULL);
939 /* Now the confidence interval */
940 T = gsl_cdf_tdist_Qinv (q, n - 1);
942 tab_double (t, 6, row + count, 0,
943 mean - T * std_error, NULL);
945 tab_double (t, 7, row + count, 0,
946 mean + T * std_error, NULL);
949 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
950 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
953 row += categoricals_total (cats) + 1;
959 /* Show the homogeneity table */
961 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
965 size_t n_rows = cmd->n_vars + 1;
967 struct tab_table *t = tab_create (n_cols, n_rows);
968 tab_headers (t, 1, 0, 1, 0);
970 /* Put a frame around the entire box, and vertical lines inside */
975 n_cols - 1, n_rows - 1);
978 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
979 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
981 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
982 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
983 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
984 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
986 tab_title (t, _("Test of Homogeneity of Variances"));
988 for (v = 0; v < cmd->n_vars; ++v)
990 const struct per_var_ws *pvw = &ws->vws[v];
991 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
993 const struct variable *var = cmd->vars[v];
994 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
995 const char *s = var_to_string (var);
997 const double df1 = pvw->n_groups - 1;
998 const double df2 = pvw->cc - pvw->n_groups;
999 double F = gp->levene;
1001 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1004 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1005 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1006 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1008 /* Now the significance */
1009 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1016 /* Show the contrast coefficients table */
1018 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1023 int n_contrasts = ll_count (&cmd->contrast_list);
1024 int n_cols = 2 + ws->actual_number_of_groups;
1025 int n_rows = 2 + n_contrasts;
1027 struct tab_table *t;
1029 const struct covariance *cov = ws->vws[0].cov ;
1031 t = tab_create (n_cols, n_rows);
1032 tab_headers (t, 2, 0, 2, 0);
1034 /* Put a frame around the entire box, and vertical lines inside */
1039 n_cols - 1, n_rows - 1);
1053 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1054 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1056 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1058 tab_title (t, _("Contrast Coefficients"));
1060 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1063 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1064 var_to_string (cmd->indep_var));
1066 for ( cli = ll_head (&cmd->contrast_list);
1067 cli != ll_null (&cmd->contrast_list);
1068 cli = ll_next (cli))
1071 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1074 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1076 for (coeffi = ll_head (&cn->coefficient_list);
1077 coeffi != ll_null (&cn->coefficient_list);
1078 ++count, coeffi = ll_next (coeffi))
1080 const struct categoricals *cats = covariance_get_categoricals (cov);
1081 const union value *val = categoricals_get_value_by_subscript (cats, count);
1084 ds_init_empty (&vstr);
1086 var_append_value_name (cmd->indep_var, val, &vstr);
1088 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1093 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
1096 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1098 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1108 /* Show the results of the contrast tests */
1110 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1112 int n_contrasts = ll_count (&cmd->contrast_list);
1115 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1117 struct tab_table *t;
1119 t = tab_create (n_cols, n_rows);
1120 tab_headers (t, 3, 0, 1, 0);
1122 /* Put a frame around the entire box, and vertical lines inside */
1127 n_cols - 1, n_rows - 1);
1135 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1136 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1138 tab_title (t, _("Contrast Tests"));
1140 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1141 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1142 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1143 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1144 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1145 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1147 for (v = 0; v < cmd->n_vars; ++v)
1149 const struct per_var_ws *pvw = &ws->vws[v];
1150 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1153 int lines_per_variable = 2 * n_contrasts;
1155 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1156 var_to_string (cmd->vars[v]));
1158 for ( cli = ll_head (&cmd->contrast_list);
1159 cli != ll_null (&cmd->contrast_list);
1160 ++i, cli = ll_next (cli))
1162 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1165 double contrast_value = 0.0;
1166 double coef_msq = 0.0;
1169 double std_error_contrast;
1171 double sec_vneq = 0.0;
1173 /* Note: The calculation of the degrees of freedom in the
1174 "variances not equal" case is painfull!!
1175 The following formula may help to understand it:
1176 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1179 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1184 double df_denominator = 0.0;
1185 double df_numerator = 0.0;
1188 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1189 df = grand_n - pvw->n_groups;
1193 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1194 TAB_LEFT | TAT_TITLE,
1195 _("Assume equal variances"));
1197 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1198 TAB_LEFT | TAT_TITLE,
1199 _("Does not assume equal"));
1202 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1203 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1206 tab_text_format (t, 2,
1207 (v * lines_per_variable) + i + 1 + n_contrasts,
1208 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1213 for (coeffi = ll_head (&cn->coefficient_list);
1214 coeffi != ll_null (&cn->coefficient_list);
1215 ++ci, coeffi = ll_next (coeffi))
1217 double n, mean, variance;
1218 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1220 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1222 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1223 const double coef = cn->coeff;
1225 const double winv = variance / n;
1227 contrast_value += coef * mean;
1229 coef_msq += (pow2 (coef)) / n;
1231 sec_vneq += (pow2 (coef)) * variance / n;
1233 df_numerator += (pow2 (coef)) * winv;
1234 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1237 sec_vneq = sqrt (sec_vneq);
1239 df_numerator = pow2 (df_numerator);
1241 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1242 TAB_RIGHT, contrast_value, NULL);
1244 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1246 TAB_RIGHT, contrast_value, NULL);
1248 std_error_contrast = sqrt (pvw->mse * coef_msq);
1251 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1252 TAB_RIGHT, std_error_contrast,
1255 T = fabs (contrast_value / std_error_contrast);
1259 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1264 /* Degrees of Freedom */
1265 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1270 /* Significance TWO TAILED !!*/
1271 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1272 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1275 /* Now for the Variances NOT Equal case */
1279 (v * lines_per_variable) + i + 1 + n_contrasts,
1280 TAB_RIGHT, sec_vneq,
1283 T = contrast_value / sec_vneq;
1285 (v * lines_per_variable) + i + 1 + n_contrasts,
1289 df = df_numerator / df_denominator;
1292 (v * lines_per_variable) + i + 1 + n_contrasts,
1296 /* The Significance */
1297 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1298 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1303 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);