1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 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 <gsl/gsl_cdf.h>
20 #include <gsl/gsl_matrix.h>
23 #include "data/case.h"
24 #include "data/casegrouper.h"
25 #include "data/casereader.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/format.h"
29 #include "data/value.h"
30 #include "language/command.h"
31 #include "language/dictionary/split-file.h"
32 #include "language/lexer/lexer.h"
33 #include "language/lexer/value-parser.h"
34 #include "language/lexer/variable-parser.h"
35 #include "libpspp/ll.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "libpspp/taint.h"
39 #include "linreg/sweep.h"
40 #include "math/categoricals.h"
41 #include "math/covariance.h"
42 #include "math/levene.h"
43 #include "math/moments.h"
44 #include "output/tab.h"
47 #define _(msgid) gettext (msgid)
58 STATS_DESCRIPTIVES = 0x0001,
59 STATS_HOMOGENEITY = 0x0002
72 struct ll_list coefficient_list;
74 bool bad_count; /* True if the number of coefficients does not equal the number of groups */
80 const struct variable **vars;
82 const struct variable *indep_var;
84 enum statistics stats;
86 enum missing_type missing_type;
87 enum mv_class exclude;
89 /* List of contrasts */
90 struct ll_list contrast_list;
92 /* The weight variable */
93 const struct variable *wv;
97 /* Per category data */
98 struct descriptive_data
100 const struct variable *var;
101 struct moments1 *mom;
107 /* Workspace variable for each dependent variable */
110 struct categoricals *cat;
111 struct covariance *cov;
123 struct oneway_workspace
125 /* The number of distinct values of the independent variable, when all
126 missing values are disregarded */
127 int actual_number_of_groups;
129 struct per_var_ws *vws;
131 /* An array of descriptive data. One for each dependent variable */
132 struct descriptive_data **dd_total;
135 /* Routines to show the output tables */
136 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
137 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
138 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
140 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
141 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
144 cmd_oneway (struct lexer *lexer, struct dataset *ds)
146 const struct dictionary *dict = dataset_dict (ds);
147 struct oneway_spec oneway ;
150 oneway.indep_var = NULL;
152 oneway.missing_type = MISS_ANALYSIS;
153 oneway.exclude = MV_ANY;
154 oneway.wv = dict_get_weight (dict);
156 ll_init (&oneway.contrast_list);
159 if ( lex_match (lexer, T_SLASH))
161 if (!lex_force_match_id (lexer, "VARIABLES"))
165 lex_match (lexer, T_EQUALS);
168 if (!parse_variables_const (lexer, dict,
169 &oneway.vars, &oneway.n_vars,
170 PV_NO_DUPLICATE | PV_NUMERIC))
173 lex_force_match (lexer, T_BY);
175 oneway.indep_var = parse_variable_const (lexer, dict);
177 while (lex_token (lexer) != T_ENDCMD)
179 lex_match (lexer, T_SLASH);
181 if (lex_match_id (lexer, "STATISTICS"))
183 lex_match (lexer, T_EQUALS);
184 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
186 if (lex_match_id (lexer, "DESCRIPTIVES"))
188 oneway.stats |= STATS_DESCRIPTIVES;
190 else if (lex_match_id (lexer, "HOMOGENEITY"))
192 oneway.stats |= STATS_HOMOGENEITY;
196 lex_error (lexer, NULL);
201 else if (lex_match_id (lexer, "CONTRAST"))
203 struct contrasts_node *cl = xzalloc (sizeof *cl);
205 struct ll_list *coefficient_list = &cl->coefficient_list;
206 lex_match (lexer, T_EQUALS);
208 ll_init (coefficient_list);
210 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
212 if ( lex_is_number (lexer))
214 struct coeff_node *cc = xmalloc (sizeof *cc);
215 cc->coeff = lex_number (lexer);
217 ll_push_tail (coefficient_list, &cc->ll);
222 lex_error (lexer, NULL);
227 ll_push_tail (&oneway.contrast_list, &cl->ll);
229 else if (lex_match_id (lexer, "MISSING"))
231 lex_match (lexer, T_EQUALS);
232 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
234 if (lex_match_id (lexer, "INCLUDE"))
236 oneway.exclude = MV_SYSTEM;
238 else if (lex_match_id (lexer, "EXCLUDE"))
240 oneway.exclude = MV_ANY;
242 else if (lex_match_id (lexer, "LISTWISE"))
244 oneway.missing_type = MISS_LISTWISE;
246 else if (lex_match_id (lexer, "ANALYSIS"))
248 oneway.missing_type = MISS_ANALYSIS;
252 lex_error (lexer, NULL);
259 lex_error (lexer, NULL);
266 struct casegrouper *grouper;
267 struct casereader *group;
270 grouper = casegrouper_create_splits (proc_open (ds), dict);
271 while (casegrouper_get_next_group (grouper, &group))
272 run_oneway (&oneway, group, ds);
273 ok = casegrouper_destroy (grouper);
274 ok = proc_commit (ds) && ok;
289 static struct descriptive_data *
290 dd_create (const struct variable *var)
292 struct descriptive_data *dd = xmalloc (sizeof *dd);
294 dd->mom = moments1_create (MOMENT_VARIANCE);
295 dd->minimum = DBL_MAX;
296 dd->maximum = -DBL_MAX;
303 dd_destroy (struct descriptive_data *dd)
305 moments1_destroy (dd->mom);
310 makeit (void *aux1, void *aux2 UNUSED)
312 const struct variable *var = aux1;
314 struct descriptive_data *dd = dd_create (var);
320 updateit (void *user_data,
321 enum mv_class exclude,
322 const struct variable *wv,
323 const struct variable *catvar UNUSED,
324 const struct ccase *c,
325 void *aux1, void *aux2)
327 struct descriptive_data *dd = user_data;
329 const struct variable *varp = aux1;
331 const union value *valx = case_data (c, varp);
333 struct descriptive_data *dd_total = aux2;
337 if ( var_is_value_missing (varp, valx, exclude))
340 weight = wv != NULL ? case_data (c, wv)->f : 1.0;
342 moments1_add (dd->mom, valx->f, weight);
343 if (valx->f < dd->minimum)
344 dd->minimum = valx->f;
346 if (valx->f > dd->maximum)
347 dd->maximum = valx->f;
350 const struct variable *var = dd_total->var;
351 const union value *val = case_data (c, var);
353 moments1_add (dd_total->mom,
357 if (val->f < dd_total->minimum)
358 dd_total->minimum = val->f;
360 if (val->f > dd_total->maximum)
361 dd_total->maximum = val->f;
366 run_oneway (const struct oneway_spec *cmd,
367 struct casereader *input,
368 const struct dataset *ds)
372 struct dictionary *dict = dataset_dict (ds);
373 struct casereader *reader;
376 struct oneway_workspace ws;
378 ws.actual_number_of_groups = 0;
379 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
380 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
382 for (v = 0 ; v < cmd->n_vars; ++v)
383 ws.dd_total[v] = dd_create (cmd->vars[v]);
385 for (v = 0; v < cmd->n_vars; ++v)
387 ws.vws[v].cat = categoricals_create (&cmd->indep_var, 1, cmd->wv,
388 cmd->exclude, makeit, updateit,
389 CONST_CAST (struct variable *,
393 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
395 cmd->wv, cmd->exclude);
398 c = casereader_peek (input, 0);
401 casereader_destroy (input);
404 output_split_file_values (ds, c);
407 taint = taint_clone (casereader_get_taint (input));
409 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
410 cmd->exclude, NULL, NULL);
411 if (cmd->missing_type == MISS_LISTWISE)
412 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
413 cmd->exclude, NULL, NULL);
414 input = casereader_create_filter_weight (input, dict, NULL, NULL);
417 if (cmd->stats & STATS_HOMOGENEITY)
418 for (v = 0; v < cmd->n_vars; ++v)
420 struct per_var_ws *pvw = &ws.vws[v];
422 pvw->levene_w = levene (input, cmd->indep_var, cmd->vars[v], cmd->wv, cmd->exclude);
425 reader = casereader_clone (input);
427 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
431 for (i = 0; i < cmd->n_vars; ++i)
433 struct per_var_ws *pvw = &ws.vws[i];
434 const struct variable *v = cmd->vars[i];
435 const union value *val = case_data (c, v);
437 if ( MISS_ANALYSIS == cmd->missing_type)
439 if ( var_is_value_missing (v, val, cmd->exclude))
443 covariance_accumulate_pass1 (pvw->cov, c);
446 casereader_destroy (reader);
447 reader = casereader_clone (input);
448 for ( ; (c = casereader_read (reader) ); case_unref (c))
451 for (i = 0; i < cmd->n_vars; ++i)
453 struct per_var_ws *pvw = &ws.vws[i];
454 const struct variable *v = cmd->vars[i];
455 const union value *val = case_data (c, v);
457 if ( MISS_ANALYSIS == cmd->missing_type)
459 if ( var_is_value_missing (v, val, cmd->exclude))
463 covariance_accumulate_pass2 (pvw->cov, c);
466 casereader_destroy (reader);
468 for (v = 0; v < cmd->n_vars; ++v)
470 struct per_var_ws *pvw = &ws.vws[v];
471 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
472 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
475 moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
477 pvw->sst = gsl_matrix_get (cm, 0, 0);
479 // gsl_matrix_fprintf (stdout, cm, "%g ");
483 pvw->sse = gsl_matrix_get (cm, 0, 0);
485 pvw->ssa = pvw->sst - pvw->sse;
487 pvw->n_groups = categoricals_total (cats);
489 pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
491 gsl_matrix_free (cm);
494 for (v = 0; v < cmd->n_vars; ++v)
496 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
498 categoricals_done (cats);
500 if (categoricals_total (cats) > ws.actual_number_of_groups)
501 ws.actual_number_of_groups = categoricals_total (cats);
504 casereader_destroy (input);
506 if (!taint_has_tainted_successor (taint))
507 output_oneway (cmd, &ws);
509 taint_destroy (taint);
512 for (v = 0; v < cmd->n_vars; ++v)
514 covariance_destroy (ws.vws[v].cov);
515 dd_destroy (ws.dd_total[v]);
522 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
523 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
526 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
530 /* Check the sanity of the given contrast values */
531 struct contrasts_node *coeff_list = NULL;
532 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
534 struct coeff_node *cn = NULL;
536 struct ll_list *cl = &coeff_list->coefficient_list;
539 if (ll_count (cl) != ws->actual_number_of_groups)
542 _("Number of contrast coefficients must equal the number of groups"));
543 coeff_list->bad_count = true;
547 ll_for_each (cn, struct coeff_node, ll, cl)
551 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
554 if (cmd->stats & STATS_DESCRIPTIVES)
555 show_descriptives (cmd, ws);
557 if (cmd->stats & STATS_HOMOGENEITY)
558 show_homogeneity (cmd, ws);
560 show_anova_table (cmd, ws);
562 if (ll_count (&cmd->contrast_list) > 0)
564 show_contrast_coeffs (cmd, ws);
565 show_contrast_tests (cmd, ws);
570 /* Show the ANOVA table */
572 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
576 size_t n_rows = cmd->n_vars * 3 + 1;
578 struct tab_table *t = tab_create (n_cols, n_rows);
580 tab_headers (t, 2, 0, 1, 0);
586 n_cols - 1, n_rows - 1);
588 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
589 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
590 tab_vline (t, TAL_0, 1, 0, 0);
592 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
593 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
594 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
595 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
596 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
599 for (i = 0; i < cmd->n_vars; ++i)
604 const char *s = var_to_string (cmd->vars[i]);
605 const struct per_var_ws *pvw = &ws->vws[i];
607 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
609 df1 = pvw->n_groups - 1;
610 df2 = n - pvw->n_groups;
611 msa = pvw->ssa / df1;
613 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
614 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
615 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
616 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
619 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
622 /* Sums of Squares */
623 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
624 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
625 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
628 /* Degrees of freedom */
629 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
630 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
631 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
634 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
635 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
638 const double F = msa / pvw->mse ;
641 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
643 /* The significance */
644 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
648 tab_title (t, _("ANOVA"));
653 /* Show the descriptives table */
655 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
662 const double confidence = 0.95;
663 const double q = (1.0 - confidence) / 2.0;
665 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
669 for (v = 0; v < cmd->n_vars; ++v)
670 n_rows += ws->actual_number_of_groups + 1;
672 t = tab_create (n_cols, n_rows);
673 tab_headers (t, 2, 0, 2, 0);
675 /* Put a frame around the entire box, and vertical lines inside */
680 n_cols - 1, n_rows - 1);
682 /* Underline headers */
683 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
684 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
686 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
687 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
688 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
689 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
692 tab_vline (t, TAL_0, 7, 0, 0);
693 tab_hline (t, TAL_1, 6, 7, 1);
694 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
695 _("%g%% Confidence Interval for Mean"),
698 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
699 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
701 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
702 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
704 tab_title (t, _("Descriptives"));
707 for (v = 0; v < cmd->n_vars; ++v)
709 const char *s = var_to_string (cmd->vars[v]);
710 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
714 struct per_var_ws *pvw = &ws->vws[v];
715 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
717 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
719 tab_hline (t, TAL_1, 0, n_cols - 1, row);
721 for (count = 0; count < categoricals_total (cats); ++count)
724 double n, mean, variance;
725 double std_dev, std_error ;
729 const union value *gval = categoricals_get_value_by_subscript (cats, count);
730 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
732 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
734 std_dev = sqrt (variance);
735 std_error = std_dev / sqrt (n) ;
737 ds_init_empty (&vstr);
739 var_append_value_name (cmd->indep_var, gval, &vstr);
741 tab_text (t, 1, row + count,
742 TAB_LEFT | TAT_TITLE,
747 /* Now fill in the numbers ... */
749 tab_double (t, 2, row + count, 0, n, wfmt);
751 tab_double (t, 3, row + count, 0, mean, NULL);
753 tab_double (t, 4, row + count, 0, std_dev, NULL);
756 tab_double (t, 5, row + count, 0, std_error, NULL);
758 /* Now the confidence interval */
760 T = gsl_cdf_tdist_Qinv (q, n - 1);
762 tab_double (t, 6, row + count, 0,
763 mean - T * std_error, NULL);
765 tab_double (t, 7, row + count, 0,
766 mean + T * std_error, NULL);
770 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
771 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
776 double n, mean, variance;
780 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
782 std_dev = sqrt (variance);
783 std_error = std_dev / sqrt (n) ;
785 tab_text (t, 1, row + count,
786 TAB_LEFT | TAT_TITLE, _("Total"));
788 tab_double (t, 2, row + count, 0, n, wfmt);
790 tab_double (t, 3, row + count, 0, mean, NULL);
792 tab_double (t, 4, row + count, 0, std_dev, NULL);
794 tab_double (t, 5, row + count, 0, std_error, NULL);
796 /* Now the confidence interval */
797 T = gsl_cdf_tdist_Qinv (q, n - 1);
799 tab_double (t, 6, row + count, 0,
800 mean - T * std_error, NULL);
802 tab_double (t, 7, row + count, 0,
803 mean + T * std_error, NULL);
806 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
807 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
810 row += categoricals_total (cats) + 1;
816 /* Show the homogeneity table */
818 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
822 size_t n_rows = cmd->n_vars + 1;
824 struct tab_table *t = tab_create (n_cols, n_rows);
825 tab_headers (t, 1, 0, 1, 0);
827 /* Put a frame around the entire box, and vertical lines inside */
832 n_cols - 1, n_rows - 1);
835 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
836 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
838 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
839 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
840 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
841 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
843 tab_title (t, _("Test of Homogeneity of Variances"));
845 for (v = 0; v < cmd->n_vars; ++v)
848 const struct per_var_ws *pvw = &ws->vws[v];
849 double F = pvw->levene_w;
851 const struct variable *var = cmd->vars[v];
852 const char *s = var_to_string (var);
855 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
857 df1 = pvw->n_groups - 1;
858 df2 = n - pvw->n_groups;
860 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
862 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
863 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
864 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
866 /* Now the significance */
867 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
874 /* Show the contrast coefficients table */
876 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
881 int n_contrasts = ll_count (&cmd->contrast_list);
882 int n_cols = 2 + ws->actual_number_of_groups;
883 int n_rows = 2 + n_contrasts;
887 const struct covariance *cov = ws->vws[0].cov ;
889 t = tab_create (n_cols, n_rows);
890 tab_headers (t, 2, 0, 2, 0);
892 /* Put a frame around the entire box, and vertical lines inside */
897 n_cols - 1, n_rows - 1);
911 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
912 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
914 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
916 tab_title (t, _("Contrast Coefficients"));
918 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
921 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
922 var_to_string (cmd->indep_var));
924 for ( cli = ll_head (&cmd->contrast_list);
925 cli != ll_null (&cmd->contrast_list);
929 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
932 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
934 for (coeffi = ll_head (&cn->coefficient_list);
935 coeffi != ll_null (&cn->coefficient_list);
936 ++count, coeffi = ll_next (coeffi))
938 const struct categoricals *cats = covariance_get_categoricals (cov);
939 const union value *val = categoricals_get_value_by_subscript (cats, count);
942 ds_init_empty (&vstr);
944 var_append_value_name (cmd->indep_var, val, &vstr);
946 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
951 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
954 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
956 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
966 /* Show the results of the contrast tests */
968 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
970 int n_contrasts = ll_count (&cmd->contrast_list);
973 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
977 t = tab_create (n_cols, n_rows);
978 tab_headers (t, 3, 0, 1, 0);
980 /* Put a frame around the entire box, and vertical lines inside */
985 n_cols - 1, n_rows - 1);
993 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
994 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
996 tab_title (t, _("Contrast Tests"));
998 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
999 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1000 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1001 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1002 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1003 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1005 for (v = 0; v < cmd->n_vars; ++v)
1007 const struct per_var_ws *pvw = &ws->vws[v];
1008 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1011 int lines_per_variable = 2 * n_contrasts;
1013 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1014 var_to_string (cmd->vars[v]));
1016 for ( cli = ll_head (&cmd->contrast_list);
1017 cli != ll_null (&cmd->contrast_list);
1018 ++i, cli = ll_next (cli))
1020 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1023 double contrast_value = 0.0;
1024 double coef_msq = 0.0;
1027 double std_error_contrast;
1029 double sec_vneq = 0.0;
1031 /* Note: The calculation of the degrees of freedom in the
1032 "variances not equal" case is painfull!!
1033 The following formula may help to understand it:
1034 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1037 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1042 double df_denominator = 0.0;
1043 double df_numerator = 0.0;
1046 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1047 df = grand_n - pvw->n_groups;
1051 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1052 TAB_LEFT | TAT_TITLE,
1053 _("Assume equal variances"));
1055 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1056 TAB_LEFT | TAT_TITLE,
1057 _("Does not assume equal"));
1060 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1061 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1064 tab_text_format (t, 2,
1065 (v * lines_per_variable) + i + 1 + n_contrasts,
1066 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1071 for (coeffi = ll_head (&cn->coefficient_list);
1072 coeffi != ll_null (&cn->coefficient_list);
1073 ++ci, coeffi = ll_next (coeffi))
1075 double n, mean, variance;
1076 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1077 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1078 const double coef = cn->coeff;
1081 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1083 winv = variance / n;
1085 contrast_value += coef * mean;
1087 coef_msq += (pow2 (coef)) / n;
1089 sec_vneq += (pow2 (coef)) * variance / n;
1091 df_numerator += (pow2 (coef)) * winv;
1092 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1095 sec_vneq = sqrt (sec_vneq);
1097 df_numerator = pow2 (df_numerator);
1099 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1100 TAB_RIGHT, contrast_value, NULL);
1102 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1104 TAB_RIGHT, contrast_value, NULL);
1106 std_error_contrast = sqrt (pvw->mse * coef_msq);
1109 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1110 TAB_RIGHT, std_error_contrast,
1113 T = fabs (contrast_value / std_error_contrast);
1117 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1122 /* Degrees of Freedom */
1123 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1128 /* Significance TWO TAILED !!*/
1129 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1130 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1133 /* Now for the Variances NOT Equal case */
1137 (v * lines_per_variable) + i + 1 + n_contrasts,
1138 TAB_RIGHT, sec_vneq,
1141 T = contrast_value / sec_vneq;
1143 (v * lines_per_variable) + i + 1 + n_contrasts,
1147 df = df_numerator / df_denominator;
1150 (v * lines_per_variable) + i + 1 + n_contrasts,
1154 /* The Significance */
1155 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1156 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1161 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);