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>
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/value.h>
27 #include <math/covariance.h>
28 #include <math/categoricals.h>
29 #include <math/levene.h>
30 #include <math/moments.h>
31 #include <gsl/gsl_matrix.h>
32 #include <linreg/sweep.h>
34 #include <libpspp/ll.h>
36 #include <language/lexer/lexer.h>
37 #include <language/lexer/variable-parser.h>
38 #include <language/lexer/value-parser.h>
39 #include <language/command.h>
42 #include <language/dictionary/split-file.h>
43 #include <libpspp/taint.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)
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 /* Per category data */
106 struct descriptive_data
108 const struct variable *var;
109 struct moments1 *mom;
115 /* Workspace variable for each dependent variable */
118 struct categoricals *cat;
119 struct covariance *cov;
131 struct oneway_workspace
133 /* The number of distinct values of the independent variable, when all
134 missing values are disregarded */
135 int actual_number_of_groups;
137 struct per_var_ws *vws;
139 /* An array of descriptive data. One for each dependent variable */
140 struct descriptive_data **dd_total;
143 /* Routines to show the output tables */
144 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
145 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
146 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
148 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
149 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
152 cmd_oneway (struct lexer *lexer, struct dataset *ds)
154 const struct dictionary *dict = dataset_dict (ds);
155 struct oneway_spec oneway ;
158 oneway.indep_var = NULL;
160 oneway.missing_type = MISS_ANALYSIS;
161 oneway.exclude = MV_ANY;
162 oneway.wv = dict_get_weight (dict);
164 ll_init (&oneway.contrast_list);
167 if ( lex_match (lexer, T_SLASH))
169 if (!lex_force_match_id (lexer, "VARIABLES"))
173 lex_match (lexer, T_EQUALS);
176 if (!parse_variables_const (lexer, dict,
177 &oneway.vars, &oneway.n_vars,
178 PV_NO_DUPLICATE | PV_NUMERIC))
181 lex_force_match (lexer, T_BY);
183 oneway.indep_var = parse_variable_const (lexer, dict);
185 while (lex_token (lexer) != T_ENDCMD)
187 lex_match (lexer, T_SLASH);
189 if (lex_match_id (lexer, "STATISTICS"))
191 lex_match (lexer, T_EQUALS);
192 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
194 if (lex_match_id (lexer, "DESCRIPTIVES"))
196 oneway.stats |= STATS_DESCRIPTIVES;
198 else if (lex_match_id (lexer, "HOMOGENEITY"))
200 oneway.stats |= STATS_HOMOGENEITY;
204 lex_error (lexer, NULL);
209 else if (lex_match_id (lexer, "CONTRAST"))
211 struct contrasts_node *cl = xzalloc (sizeof *cl);
213 struct ll_list *coefficient_list = &cl->coefficient_list;
214 lex_match (lexer, T_EQUALS);
216 ll_init (coefficient_list);
218 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
220 if ( lex_is_number (lexer))
222 struct coeff_node *cc = xmalloc (sizeof *cc);
223 cc->coeff = lex_number (lexer);
225 ll_push_tail (coefficient_list, &cc->ll);
230 lex_error (lexer, NULL);
235 ll_push_tail (&oneway.contrast_list, &cl->ll);
237 else if (lex_match_id (lexer, "MISSING"))
239 lex_match (lexer, T_EQUALS);
240 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
242 if (lex_match_id (lexer, "INCLUDE"))
244 oneway.exclude = MV_SYSTEM;
246 else if (lex_match_id (lexer, "EXCLUDE"))
248 oneway.exclude = MV_ANY;
250 else if (lex_match_id (lexer, "LISTWISE"))
252 oneway.missing_type = MISS_LISTWISE;
254 else if (lex_match_id (lexer, "ANALYSIS"))
256 oneway.missing_type = MISS_ANALYSIS;
260 lex_error (lexer, NULL);
267 lex_error (lexer, NULL);
274 struct casegrouper *grouper;
275 struct casereader *group;
278 grouper = casegrouper_create_splits (proc_open (ds), dict);
279 while (casegrouper_get_next_group (grouper, &group))
280 run_oneway (&oneway, group, ds);
281 ok = casegrouper_destroy (grouper);
282 ok = proc_commit (ds) && ok;
297 static struct descriptive_data *
298 dd_create (const struct variable *var)
300 struct descriptive_data *dd = xmalloc (sizeof *dd);
302 dd->mom = moments1_create (MOMENT_VARIANCE);
303 dd->minimum = DBL_MAX;
304 dd->maximum = -DBL_MAX;
311 dd_destroy (struct descriptive_data *dd)
313 moments1_destroy (dd->mom);
318 makeit (void *aux1, void *aux2 UNUSED)
320 const struct variable *var = aux1;
322 struct descriptive_data *dd = dd_create (var);
328 updateit (void *user_data,
329 enum mv_class exclude,
330 const struct variable *wv,
331 const struct variable *catvar UNUSED,
332 const struct ccase *c,
333 void *aux1, void *aux2)
335 struct descriptive_data *dd = user_data;
337 const struct variable *varp = aux1;
339 const union value *valx = case_data (c, varp);
341 struct descriptive_data *dd_total = aux2;
345 if ( var_is_value_missing (varp, valx, exclude))
348 weight = wv != NULL ? case_data (c, wv)->f : 1.0;
350 moments1_add (dd->mom, valx->f, weight);
351 if (valx->f < dd->minimum)
352 dd->minimum = valx->f;
354 if (valx->f > dd->maximum)
355 dd->maximum = valx->f;
358 const struct variable *var = dd_total->var;
359 const union value *val = case_data (c, var);
361 moments1_add (dd_total->mom,
365 if (val->f < dd_total->minimum)
366 dd_total->minimum = val->f;
368 if (val->f > dd_total->maximum)
369 dd_total->maximum = val->f;
374 run_oneway (const struct oneway_spec *cmd,
375 struct casereader *input,
376 const struct dataset *ds)
380 struct dictionary *dict = dataset_dict (ds);
381 struct casereader *reader;
384 struct oneway_workspace ws;
386 ws.actual_number_of_groups = 0;
387 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
388 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
390 for (v = 0 ; v < cmd->n_vars; ++v)
391 ws.dd_total[v] = dd_create (cmd->vars[v]);
393 for (v = 0; v < cmd->n_vars; ++v)
395 ws.vws[v].cat = categoricals_create (&cmd->indep_var, 1, cmd->wv,
396 cmd->exclude, makeit, updateit,
397 CONST_CAST (struct variable *,
401 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
403 cmd->wv, cmd->exclude);
406 c = casereader_peek (input, 0);
409 casereader_destroy (input);
412 output_split_file_values (ds, c);
415 taint = taint_clone (casereader_get_taint (input));
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);
425 if (cmd->stats & STATS_HOMOGENEITY)
426 for (v = 0; v < cmd->n_vars; ++v)
428 struct per_var_ws *pvw = &ws.vws[v];
430 pvw->levene_w = levene (input, cmd->indep_var, cmd->vars[v], cmd->wv, cmd->exclude);
433 reader = casereader_clone (input);
435 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
439 for (i = 0; i < cmd->n_vars; ++i)
441 struct per_var_ws *pvw = &ws.vws[i];
442 const struct variable *v = cmd->vars[i];
443 const union value *val = case_data (c, v);
445 if ( MISS_ANALYSIS == cmd->missing_type)
447 if ( var_is_value_missing (v, val, cmd->exclude))
451 covariance_accumulate_pass1 (pvw->cov, c);
454 casereader_destroy (reader);
455 reader = casereader_clone (input);
456 for ( ; (c = casereader_read (reader) ); case_unref (c))
459 for (i = 0; i < cmd->n_vars; ++i)
461 struct per_var_ws *pvw = &ws.vws[i];
462 const struct variable *v = cmd->vars[i];
463 const union value *val = case_data (c, v);
465 if ( MISS_ANALYSIS == cmd->missing_type)
467 if ( var_is_value_missing (v, val, cmd->exclude))
471 covariance_accumulate_pass2 (pvw->cov, c);
474 casereader_destroy (reader);
476 for (v = 0; v < cmd->n_vars; ++v)
478 struct per_var_ws *pvw = &ws.vws[v];
479 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
480 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
483 moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
485 pvw->sst = gsl_matrix_get (cm, 0, 0);
487 // gsl_matrix_fprintf (stdout, cm, "%g ");
491 pvw->sse = gsl_matrix_get (cm, 0, 0);
493 pvw->ssa = pvw->sst - pvw->sse;
495 pvw->n_groups = categoricals_total (cats);
497 pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
499 gsl_matrix_free (cm);
502 for (v = 0; v < cmd->n_vars; ++v)
504 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
506 categoricals_done (cats);
508 if (categoricals_total (cats) > ws.actual_number_of_groups)
509 ws.actual_number_of_groups = categoricals_total (cats);
512 casereader_destroy (input);
514 if (!taint_has_tainted_successor (taint))
515 output_oneway (cmd, &ws);
517 taint_destroy (taint);
520 for (v = 0; v < cmd->n_vars; ++v)
522 covariance_destroy (ws.vws[v].cov);
523 dd_destroy (ws.dd_total[v]);
530 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
531 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
534 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
538 /* Check the sanity of the given contrast values */
539 struct contrasts_node *coeff_list = NULL;
540 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
542 struct coeff_node *cn = NULL;
544 struct ll_list *cl = &coeff_list->coefficient_list;
547 if (ll_count (cl) != ws->actual_number_of_groups)
550 _("Number of contrast coefficients must equal the number of groups"));
551 coeff_list->bad_count = true;
555 ll_for_each (cn, struct coeff_node, ll, cl)
559 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
562 if (cmd->stats & STATS_DESCRIPTIVES)
563 show_descriptives (cmd, ws);
565 if (cmd->stats & STATS_HOMOGENEITY)
566 show_homogeneity (cmd, ws);
568 show_anova_table (cmd, ws);
570 if (ll_count (&cmd->contrast_list) > 0)
572 show_contrast_coeffs (cmd, ws);
573 show_contrast_tests (cmd, ws);
578 /* Show the ANOVA table */
580 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
584 size_t n_rows = cmd->n_vars * 3 + 1;
586 struct tab_table *t = tab_create (n_cols, n_rows);
588 tab_headers (t, 2, 0, 1, 0);
594 n_cols - 1, n_rows - 1);
596 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
597 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
598 tab_vline (t, TAL_0, 1, 0, 0);
600 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
601 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
602 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
603 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
604 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
607 for (i = 0; i < cmd->n_vars; ++i)
612 const char *s = var_to_string (cmd->vars[i]);
613 const struct per_var_ws *pvw = &ws->vws[i];
615 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
617 df1 = pvw->n_groups - 1;
618 df2 = n - pvw->n_groups;
619 msa = pvw->ssa / df1;
621 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
622 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
623 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
624 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
627 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
630 /* Sums of Squares */
631 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
632 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
633 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
636 /* Degrees of freedom */
637 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
638 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
639 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
642 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
643 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
646 const double F = msa / pvw->mse ;
649 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
651 /* The significance */
652 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
656 tab_title (t, _("ANOVA"));
661 /* Show the descriptives table */
663 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
670 const double confidence = 0.95;
671 const double q = (1.0 - confidence) / 2.0;
673 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
677 for (v = 0; v < cmd->n_vars; ++v)
678 n_rows += ws->actual_number_of_groups + 1;
680 t = tab_create (n_cols, n_rows);
681 tab_headers (t, 2, 0, 2, 0);
683 /* Put a frame around the entire box, and vertical lines inside */
688 n_cols - 1, n_rows - 1);
690 /* Underline headers */
691 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
692 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
694 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
695 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
696 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
697 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
700 tab_vline (t, TAL_0, 7, 0, 0);
701 tab_hline (t, TAL_1, 6, 7, 1);
702 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
703 _("%g%% Confidence Interval for Mean"),
706 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
707 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
709 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
710 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
712 tab_title (t, _("Descriptives"));
715 for (v = 0; v < cmd->n_vars; ++v)
717 const char *s = var_to_string (cmd->vars[v]);
718 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
722 struct per_var_ws *pvw = &ws->vws[v];
723 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
725 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
727 tab_hline (t, TAL_1, 0, n_cols - 1, row);
729 for (count = 0; count < categoricals_total (cats); ++count)
732 double n, mean, variance;
733 double std_dev, std_error ;
737 const union value *gval = categoricals_get_value_by_subscript (cats, count);
738 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
740 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
742 std_dev = sqrt (variance);
743 std_error = std_dev / sqrt (n) ;
745 ds_init_empty (&vstr);
747 var_append_value_name (cmd->indep_var, gval, &vstr);
749 tab_text (t, 1, row + count,
750 TAB_LEFT | TAT_TITLE,
755 /* Now fill in the numbers ... */
757 tab_double (t, 2, row + count, 0, n, wfmt);
759 tab_double (t, 3, row + count, 0, mean, NULL);
761 tab_double (t, 4, row + count, 0, std_dev, NULL);
764 tab_double (t, 5, row + count, 0, std_error, NULL);
766 /* Now the confidence interval */
768 T = gsl_cdf_tdist_Qinv (q, n - 1);
770 tab_double (t, 6, row + count, 0,
771 mean - T * std_error, NULL);
773 tab_double (t, 7, row + count, 0,
774 mean + T * std_error, NULL);
778 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
779 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
784 double n, mean, variance;
788 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
790 std_dev = sqrt (variance);
791 std_error = std_dev / sqrt (n) ;
793 tab_text (t, 1, row + count,
794 TAB_LEFT | TAT_TITLE, _("Total"));
796 tab_double (t, 2, row + count, 0, n, wfmt);
798 tab_double (t, 3, row + count, 0, mean, NULL);
800 tab_double (t, 4, row + count, 0, std_dev, NULL);
802 tab_double (t, 5, row + count, 0, std_error, NULL);
804 /* Now the confidence interval */
805 T = gsl_cdf_tdist_Qinv (q, n - 1);
807 tab_double (t, 6, row + count, 0,
808 mean - T * std_error, NULL);
810 tab_double (t, 7, row + count, 0,
811 mean + T * std_error, NULL);
814 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
815 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
818 row += categoricals_total (cats) + 1;
824 /* Show the homogeneity table */
826 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
830 size_t n_rows = cmd->n_vars + 1;
832 struct tab_table *t = tab_create (n_cols, n_rows);
833 tab_headers (t, 1, 0, 1, 0);
835 /* Put a frame around the entire box, and vertical lines inside */
840 n_cols - 1, n_rows - 1);
843 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
844 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
846 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
847 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
848 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
849 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
851 tab_title (t, _("Test of Homogeneity of Variances"));
853 for (v = 0; v < cmd->n_vars; ++v)
856 const struct per_var_ws *pvw = &ws->vws[v];
857 double F = pvw->levene_w;
859 const struct variable *var = cmd->vars[v];
860 const char *s = var_to_string (var);
863 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
865 df1 = pvw->n_groups - 1;
866 df2 = n - pvw->n_groups;
868 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
870 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
871 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
872 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
874 /* Now the significance */
875 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
882 /* Show the contrast coefficients table */
884 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
889 int n_contrasts = ll_count (&cmd->contrast_list);
890 int n_cols = 2 + ws->actual_number_of_groups;
891 int n_rows = 2 + n_contrasts;
895 const struct covariance *cov = ws->vws[0].cov ;
897 t = tab_create (n_cols, n_rows);
898 tab_headers (t, 2, 0, 2, 0);
900 /* Put a frame around the entire box, and vertical lines inside */
905 n_cols - 1, n_rows - 1);
919 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
920 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
922 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
924 tab_title (t, _("Contrast Coefficients"));
926 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
929 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
930 var_to_string (cmd->indep_var));
932 for ( cli = ll_head (&cmd->contrast_list);
933 cli != ll_null (&cmd->contrast_list);
937 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
940 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
942 for (coeffi = ll_head (&cn->coefficient_list);
943 coeffi != ll_null (&cn->coefficient_list);
944 ++count, coeffi = ll_next (coeffi))
946 const struct categoricals *cats = covariance_get_categoricals (cov);
947 const union value *val = categoricals_get_value_by_subscript (cats, count);
950 ds_init_empty (&vstr);
952 var_append_value_name (cmd->indep_var, val, &vstr);
954 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
959 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
962 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
964 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
974 /* Show the results of the contrast tests */
976 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
978 int n_contrasts = ll_count (&cmd->contrast_list);
981 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
985 t = tab_create (n_cols, n_rows);
986 tab_headers (t, 3, 0, 1, 0);
988 /* Put a frame around the entire box, and vertical lines inside */
993 n_cols - 1, n_rows - 1);
1001 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1002 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1004 tab_title (t, _("Contrast Tests"));
1006 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1007 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1008 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1009 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1010 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1011 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1013 for (v = 0; v < cmd->n_vars; ++v)
1015 const struct per_var_ws *pvw = &ws->vws[v];
1016 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1019 int lines_per_variable = 2 * n_contrasts;
1021 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1022 var_to_string (cmd->vars[v]));
1024 for ( cli = ll_head (&cmd->contrast_list);
1025 cli != ll_null (&cmd->contrast_list);
1026 ++i, cli = ll_next (cli))
1028 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1031 double contrast_value = 0.0;
1032 double coef_msq = 0.0;
1035 double std_error_contrast;
1037 double sec_vneq = 0.0;
1039 /* Note: The calculation of the degrees of freedom in the
1040 "variances not equal" case is painfull!!
1041 The following formula may help to understand it:
1042 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1045 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1050 double df_denominator = 0.0;
1051 double df_numerator = 0.0;
1054 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1055 df = grand_n - pvw->n_groups;
1059 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1060 TAB_LEFT | TAT_TITLE,
1061 _("Assume equal variances"));
1063 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1064 TAB_LEFT | TAT_TITLE,
1065 _("Does not assume equal"));
1068 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1069 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1072 tab_text_format (t, 2,
1073 (v * lines_per_variable) + i + 1 + n_contrasts,
1074 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1079 for (coeffi = ll_head (&cn->coefficient_list);
1080 coeffi != ll_null (&cn->coefficient_list);
1081 ++ci, coeffi = ll_next (coeffi))
1083 double n, mean, variance;
1084 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1085 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1086 const double coef = cn->coeff;
1089 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1091 winv = variance / n;
1093 contrast_value += coef * mean;
1095 coef_msq += (pow2 (coef)) / n;
1097 sec_vneq += (pow2 (coef)) * variance / n;
1099 df_numerator += (pow2 (coef)) * winv;
1100 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1103 sec_vneq = sqrt (sec_vneq);
1105 df_numerator = pow2 (df_numerator);
1107 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1108 TAB_RIGHT, contrast_value, NULL);
1110 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1112 TAB_RIGHT, contrast_value, NULL);
1114 std_error_contrast = sqrt (pvw->mse * coef_msq);
1117 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1118 TAB_RIGHT, std_error_contrast,
1121 T = fabs (contrast_value / std_error_contrast);
1125 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1130 /* Degrees of Freedom */
1131 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1136 /* Significance TWO TAILED !!*/
1137 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1138 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1141 /* Now for the Variances NOT Equal case */
1145 (v * lines_per_variable) + i + 1 + n_contrasts,
1146 TAB_RIGHT, sec_vneq,
1149 T = contrast_value / sec_vneq;
1151 (v * lines_per_variable) + i + 1 + n_contrasts,
1155 df = df_numerator / df_denominator;
1158 (v * lines_per_variable) + i + 1 + n_contrasts,
1162 /* The Significance */
1163 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1164 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1169 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);