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, '/'))
169 if (!lex_force_match_id (lexer, "VARIABLES"))
173 lex_match (lexer, '=');
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) != '.')
187 lex_match (lexer, '/');
189 if (lex_match_id (lexer, "STATISTICS"))
191 lex_match (lexer, '=');
192 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
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, '=');
216 ll_init (coefficient_list);
218 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
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, '=');
240 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
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,
396 cmd->wv, cmd->exclude,
399 cmd->vars[v], ws.dd_total[v]);
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);
500 for (v = 0; v < cmd->n_vars; ++v)
502 struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
504 categoricals_done (cats);
506 if (categoricals_total (cats) > ws.actual_number_of_groups)
507 ws.actual_number_of_groups = categoricals_total (cats);
510 casereader_destroy (input);
512 if (!taint_has_tainted_successor (taint))
513 output_oneway (cmd, &ws);
515 taint_destroy (taint);
518 for (v = 0; v < cmd->n_vars; ++v)
520 covariance_destroy (ws.vws[v].cov);
521 dd_destroy (ws.dd_total[v]);
528 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
529 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
532 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
536 /* Check the sanity of the given contrast values */
537 struct contrasts_node *coeff_list = NULL;
538 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
540 struct coeff_node *cn = NULL;
542 struct ll_list *cl = &coeff_list->coefficient_list;
545 if (ll_count (cl) != ws->actual_number_of_groups)
548 _("Number of contrast coefficients must equal the number of groups"));
549 coeff_list->bad_count = true;
553 ll_for_each (cn, struct coeff_node, ll, cl)
557 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
560 if (cmd->stats & STATS_DESCRIPTIVES)
561 show_descriptives (cmd, ws);
563 if (cmd->stats & STATS_HOMOGENEITY)
564 show_homogeneity (cmd, ws);
566 show_anova_table (cmd, ws);
568 if (ll_count (&cmd->contrast_list) > 0)
570 show_contrast_coeffs (cmd, ws);
571 show_contrast_tests (cmd, ws);
576 /* Show the ANOVA table */
578 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
582 size_t n_rows = cmd->n_vars * 3 + 1;
584 struct tab_table *t = tab_create (n_cols, n_rows);
586 tab_headers (t, 2, 0, 1, 0);
592 n_cols - 1, n_rows - 1);
594 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
595 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
596 tab_vline (t, TAL_0, 1, 0, 0);
598 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
599 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
600 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
601 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
602 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
605 for (i = 0; i < cmd->n_vars; ++i)
610 const char *s = var_to_string (cmd->vars[i]);
611 const struct per_var_ws *pvw = &ws->vws[i];
613 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
615 df1 = pvw->n_groups - 1;
616 df2 = n - pvw->n_groups;
617 msa = pvw->ssa / df1;
619 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
620 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
621 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
622 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
625 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
628 /* Sums of Squares */
629 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
630 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
631 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
634 /* Degrees of freedom */
635 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
636 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
637 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
640 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
641 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
644 const double F = msa / pvw->mse ;
647 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
649 /* The significance */
650 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
654 tab_title (t, _("ANOVA"));
659 /* Show the descriptives table */
661 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
668 const double confidence = 0.95;
669 const double q = (1.0 - confidence) / 2.0;
671 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
675 for (v = 0; v < cmd->n_vars; ++v)
676 n_rows += ws->actual_number_of_groups + 1;
678 t = tab_create (n_cols, n_rows);
679 tab_headers (t, 2, 0, 2, 0);
681 /* Put a frame around the entire box, and vertical lines inside */
686 n_cols - 1, n_rows - 1);
688 /* Underline headers */
689 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
690 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
692 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
693 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
694 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
695 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
698 tab_vline (t, TAL_0, 7, 0, 0);
699 tab_hline (t, TAL_1, 6, 7, 1);
700 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
701 _("%g%% Confidence Interval for Mean"),
704 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
705 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
707 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
708 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
710 tab_title (t, _("Descriptives"));
713 for (v = 0; v < cmd->n_vars; ++v)
715 const char *s = var_to_string (cmd->vars[v]);
716 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
720 struct per_var_ws *pvw = &ws->vws[v];
721 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
723 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
725 tab_hline (t, TAL_1, 0, n_cols - 1, row);
727 for (count = 0; count < categoricals_total (cats); ++count)
730 double n, mean, variance;
731 double std_dev, std_error ;
735 const union value *gval = categoricals_get_value_by_subscript (cats, count);
736 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
738 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
740 std_dev = sqrt (variance);
741 std_error = std_dev / sqrt (n) ;
743 ds_init_empty (&vstr);
745 var_append_value_name (cmd->indep_var, gval, &vstr);
747 tab_text (t, 1, row + count,
748 TAB_LEFT | TAT_TITLE,
753 /* Now fill in the numbers ... */
755 tab_double (t, 2, row + count, 0, n, wfmt);
757 tab_double (t, 3, row + count, 0, mean, NULL);
759 tab_double (t, 4, row + count, 0, std_dev, NULL);
762 tab_double (t, 5, row + count, 0, std_error, NULL);
764 /* Now the confidence interval */
766 T = gsl_cdf_tdist_Qinv (q, n - 1);
768 tab_double (t, 6, row + count, 0,
769 mean - T * std_error, NULL);
771 tab_double (t, 7, row + count, 0,
772 mean + T * std_error, NULL);
776 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
777 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
782 double n, mean, variance;
786 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
788 std_dev = sqrt (variance);
789 std_error = std_dev / sqrt (n) ;
791 tab_text (t, 1, row + count,
792 TAB_LEFT | TAT_TITLE, _("Total"));
794 tab_double (t, 2, row + count, 0, n, wfmt);
796 tab_double (t, 3, row + count, 0, mean, NULL);
798 tab_double (t, 4, row + count, 0, std_dev, NULL);
800 tab_double (t, 5, row + count, 0, std_error, NULL);
802 /* Now the confidence interval */
803 T = gsl_cdf_tdist_Qinv (q, n - 1);
805 tab_double (t, 6, row + count, 0,
806 mean - T * std_error, NULL);
808 tab_double (t, 7, row + count, 0,
809 mean + T * std_error, NULL);
812 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
813 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
816 row += categoricals_total (cats) + 1;
822 /* Show the homogeneity table */
824 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
828 size_t n_rows = cmd->n_vars + 1;
830 struct tab_table *t = tab_create (n_cols, n_rows);
831 tab_headers (t, 1, 0, 1, 0);
833 /* Put a frame around the entire box, and vertical lines inside */
838 n_cols - 1, n_rows - 1);
841 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
842 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
844 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
845 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
846 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
847 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
849 tab_title (t, _("Test of Homogeneity of Variances"));
851 for (v = 0; v < cmd->n_vars; ++v)
854 const struct per_var_ws *pvw = &ws->vws[v];
855 double F = pvw->levene_w;
857 const struct variable *var = cmd->vars[v];
858 const char *s = var_to_string (var);
861 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
863 df1 = pvw->n_groups - 1;
864 df2 = n - pvw->n_groups;
866 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
868 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
869 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
870 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
872 /* Now the significance */
873 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
880 /* Show the contrast coefficients table */
882 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
887 int n_contrasts = ll_count (&cmd->contrast_list);
888 int n_cols = 2 + ws->actual_number_of_groups;
889 int n_rows = 2 + n_contrasts;
893 const struct covariance *cov = ws->vws[0].cov ;
895 t = tab_create (n_cols, n_rows);
896 tab_headers (t, 2, 0, 2, 0);
898 /* Put a frame around the entire box, and vertical lines inside */
903 n_cols - 1, n_rows - 1);
917 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
918 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
920 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
922 tab_title (t, _("Contrast Coefficients"));
924 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
927 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
928 var_to_string (cmd->indep_var));
930 for ( cli = ll_head (&cmd->contrast_list);
931 cli != ll_null (&cmd->contrast_list);
935 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
938 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
940 for (coeffi = ll_head (&cn->coefficient_list);
941 coeffi != ll_null (&cn->coefficient_list);
942 ++count, coeffi = ll_next (coeffi))
944 const struct categoricals *cats = covariance_get_categoricals (cov);
945 const union value *val = categoricals_get_value_by_subscript (cats, count);
948 ds_init_empty (&vstr);
950 var_append_value_name (cmd->indep_var, val, &vstr);
952 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
957 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
960 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
962 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
972 /* Show the results of the contrast tests */
974 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
976 int n_contrasts = ll_count (&cmd->contrast_list);
979 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
983 t = tab_create (n_cols, n_rows);
984 tab_headers (t, 3, 0, 1, 0);
986 /* Put a frame around the entire box, and vertical lines inside */
991 n_cols - 1, n_rows - 1);
999 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1000 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1002 tab_title (t, _("Contrast Tests"));
1004 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1005 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1006 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1007 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1008 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1009 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1011 for (v = 0; v < cmd->n_vars; ++v)
1013 const struct per_var_ws *pvw = &ws->vws[v];
1014 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1017 int lines_per_variable = 2 * n_contrasts;
1019 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1020 var_to_string (cmd->vars[v]));
1022 for ( cli = ll_head (&cmd->contrast_list);
1023 cli != ll_null (&cmd->contrast_list);
1024 ++i, cli = ll_next (cli))
1026 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1029 double contrast_value = 0.0;
1030 double coef_msq = 0.0;
1033 double std_error_contrast;
1035 double sec_vneq = 0.0;
1037 /* Note: The calculation of the degrees of freedom in the
1038 "variances not equal" case is painfull!!
1039 The following formula may help to understand it:
1040 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1043 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1048 double df_denominator = 0.0;
1049 double df_numerator = 0.0;
1052 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1053 df = grand_n - pvw->n_groups;
1057 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1058 TAB_LEFT | TAT_TITLE,
1059 _("Assume equal variances"));
1061 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1062 TAB_LEFT | TAT_TITLE,
1063 _("Does not assume equal"));
1066 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1067 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1070 tab_text_format (t, 2,
1071 (v * lines_per_variable) + i + 1 + n_contrasts,
1072 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1077 for (coeffi = ll_head (&cn->coefficient_list);
1078 coeffi != ll_null (&cn->coefficient_list);
1079 ++ci, coeffi = ll_next (coeffi))
1081 double n, mean, variance;
1082 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1083 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1084 const double coef = cn->coeff;
1087 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1089 winv = variance / n;
1091 contrast_value += coef * mean;
1093 coef_msq += (pow2 (coef)) / n;
1095 sec_vneq += (pow2 (coef)) * variance / n;
1097 df_numerator += (pow2 (coef)) * winv;
1098 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1101 sec_vneq = sqrt (sec_vneq);
1103 df_numerator = pow2 (df_numerator);
1105 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1106 TAB_RIGHT, contrast_value, NULL);
1108 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1110 TAB_RIGHT, contrast_value, NULL);
1112 std_error_contrast = sqrt (pvw->mse * coef_msq);
1115 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1116 TAB_RIGHT, std_error_contrast,
1119 T = fabs (contrast_value / std_error_contrast);
1123 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1128 /* Degrees of Freedom */
1129 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1134 /* Significance TWO TAILED !!*/
1135 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1136 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1139 /* Now for the Variances NOT Equal case */
1143 (v * lines_per_variable) + i + 1 + n_contrasts,
1144 TAB_RIGHT, sec_vneq,
1147 T = contrast_value / sec_vneq;
1149 (v * lines_per_variable) + i + 1 + n_contrasts,
1153 df = df_numerator / df_denominator;
1156 (v * lines_per_variable) + i + 1 + n_contrasts,
1160 /* The Significance */
1161 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1162 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1167 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);