1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013 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 "tukey/tukey.h"
41 #include "math/categoricals.h"
42 #include "math/interaction.h"
43 #include "math/covariance.h"
44 #include "math/levene.h"
45 #include "math/moments.h"
46 #include "output/tab.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
52 /* Workspace variable for each dependent variable */
55 struct interaction *iact;
56 struct categoricals *cat;
57 struct covariance *cov;
71 /* Per category data */
72 struct descriptive_data
74 const struct variable *var;
89 STATS_DESCRIPTIVES = 0x0001,
90 STATS_HOMOGENEITY = 0x0002
100 struct contrasts_node
103 struct ll_list coefficient_list;
109 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
110 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
111 typedef double p1tail_func (double ts, double df1, double df2);
113 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
131 const struct variable **vars;
133 const struct variable *indep_var;
135 enum statistics stats;
137 enum missing_type missing_type;
138 enum mv_class exclude;
140 /* List of contrasts */
141 struct ll_list contrast_list;
143 /* The weight variable */
144 const struct variable *wv;
146 /* The confidence level for multiple comparisons */
154 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
156 return pvw->n - pvw->n_groups;
160 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
166 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
167 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
169 if ( n_i <= 1.0 || n_j <= 1.0)
172 nom = pow2 (var_i/n_i + var_j/n_j);
173 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
178 static double lsd_pinv (double std_err, double alpha, double df, int k UNUSED, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
180 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
183 static double bonferroni_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
185 const int m = k * (k - 1) / 2;
186 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
189 static double sidak_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
191 const double m = k * (k - 1) / 2;
192 double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
193 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
196 static double tukey_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
198 if ( k < 2 || df < 2)
201 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
204 static double scheffe_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
206 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
207 return std_err * sqrt (x);
210 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
212 double n_i, mean_i, var_i;
213 double n_j, mean_j, var_j;
216 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
217 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
219 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
221 if ( k < 2 || df < 2)
224 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
229 multiple_comparison_sig (double std_err,
230 const struct per_var_ws *pvw,
231 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
232 const struct posthoc *ph)
234 int k = pvw->n_groups;
235 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
236 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
239 return ph->p1f (ts, k - 1, df);
243 mc_half_range (const struct oneway_spec *cmd, const struct per_var_ws *pvw, double std_err, const struct descriptive_data *dd_i, const struct descriptive_data *dd_j, const struct posthoc *ph)
245 int k = pvw->n_groups;
246 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
250 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
253 static double tukey_1tailsig (double ts, double df1, double df2)
257 if (df2 < 2 || df1 < 1)
260 twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
262 return twotailedsig / 2.0;
265 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
267 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
270 static double sidak_1tailsig (double ts, double df1, double df2)
272 double ex = (df1 + 1.0) * df1 / 2.0;
273 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
275 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
278 static double bonferroni_1tailsig (double ts, double df1, double df2)
280 const int m = (df1 + 1) * df1 / 2;
282 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
285 return p > 0.5 ? 0.5 : p;
288 static double scheffe_1tailsig (double ts, double df1, double df2)
290 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
294 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
297 double n_i, mean_i, var_i;
298 double n_j, mean_j, var_j;
300 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
301 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
303 ts = (mean_i - mean_j) / std_err;
304 ts = fabs (ts) * sqrt (2.0);
309 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
311 double n_i, mean_i, var_i;
312 double n_j, mean_j, var_j;
314 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
315 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
317 return (mean_i - mean_j) / std_err;
320 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
323 double n_i, mean_i, var_i;
324 double n_j, mean_j, var_j;
326 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
327 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
329 t = (mean_i - mean_j) / std_err;
336 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
340 double n_i, mean_i, var_i;
341 double n_j, mean_j, var_j;
343 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
344 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
346 thing = var_i / n_i + var_j / n_j;
348 thing = sqrt (thing);
350 ts = (mean_i - mean_j) / thing;
357 static const struct posthoc ph_tests [] =
359 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
360 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
361 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
362 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
363 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
364 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
368 struct oneway_workspace
370 /* The number of distinct values of the independent variable, when all
371 missing values are disregarded */
372 int actual_number_of_groups;
374 struct per_var_ws *vws;
376 /* An array of descriptive data. One for each dependent variable */
377 struct descriptive_data **dd_total;
380 /* Routines to show the output tables */
381 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
382 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
383 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
385 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
386 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
390 destroy_coeff_list (struct contrasts_node *coeff_list)
392 struct coeff_node *cn = NULL;
393 struct coeff_node *cnx = NULL;
394 struct ll_list *cl = &coeff_list->coefficient_list;
396 ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
405 oneway_cleanup (struct oneway_spec *cmd)
407 struct contrasts_node *coeff_list = NULL;
408 struct contrasts_node *coeff_next = NULL;
409 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
411 destroy_coeff_list (coeff_list);
420 cmd_oneway (struct lexer *lexer, struct dataset *ds)
422 const struct dictionary *dict = dataset_dict (ds);
423 struct oneway_spec oneway ;
426 oneway.indep_var = NULL;
428 oneway.missing_type = MISS_ANALYSIS;
429 oneway.exclude = MV_ANY;
430 oneway.wv = dict_get_weight (dict);
432 oneway.posthoc = NULL;
433 oneway.n_posthoc = 0;
435 ll_init (&oneway.contrast_list);
438 if ( lex_match (lexer, T_SLASH))
440 if (!lex_force_match_id (lexer, "VARIABLES"))
444 lex_match (lexer, T_EQUALS);
447 if (!parse_variables_const (lexer, dict,
448 &oneway.vars, &oneway.n_vars,
449 PV_NO_DUPLICATE | PV_NUMERIC))
452 lex_force_match (lexer, T_BY);
454 oneway.indep_var = parse_variable_const (lexer, dict);
456 while (lex_token (lexer) != T_ENDCMD)
458 lex_match (lexer, T_SLASH);
460 if (lex_match_id (lexer, "STATISTICS"))
462 lex_match (lexer, T_EQUALS);
463 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
465 if (lex_match_id (lexer, "DESCRIPTIVES"))
467 oneway.stats |= STATS_DESCRIPTIVES;
469 else if (lex_match_id (lexer, "HOMOGENEITY"))
471 oneway.stats |= STATS_HOMOGENEITY;
475 lex_error (lexer, NULL);
480 else if (lex_match_id (lexer, "POSTHOC"))
482 lex_match (lexer, T_EQUALS);
483 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
487 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
489 if (lex_match_id (lexer, ph_tests[p].syntax))
492 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
493 oneway.posthoc[oneway.n_posthoc - 1] = p;
498 if ( method == false)
500 if (lex_match_id (lexer, "ALPHA"))
502 if ( !lex_force_match (lexer, T_LPAREN))
504 lex_force_num (lexer);
505 oneway.alpha = lex_number (lexer);
507 if ( !lex_force_match (lexer, T_RPAREN))
512 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
513 lex_error (lexer, NULL);
519 else if (lex_match_id (lexer, "CONTRAST"))
521 struct contrasts_node *cl = xzalloc (sizeof *cl);
523 struct ll_list *coefficient_list = &cl->coefficient_list;
524 lex_match (lexer, T_EQUALS);
526 ll_init (coefficient_list);
528 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
530 if ( lex_is_number (lexer))
532 struct coeff_node *cc = xmalloc (sizeof *cc);
533 cc->coeff = lex_number (lexer);
535 ll_push_tail (coefficient_list, &cc->ll);
540 destroy_coeff_list (cl);
541 lex_error (lexer, NULL);
546 ll_push_tail (&oneway.contrast_list, &cl->ll);
548 else if (lex_match_id (lexer, "MISSING"))
550 lex_match (lexer, T_EQUALS);
551 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
553 if (lex_match_id (lexer, "INCLUDE"))
555 oneway.exclude = MV_SYSTEM;
557 else if (lex_match_id (lexer, "EXCLUDE"))
559 oneway.exclude = MV_ANY;
561 else if (lex_match_id (lexer, "LISTWISE"))
563 oneway.missing_type = MISS_LISTWISE;
565 else if (lex_match_id (lexer, "ANALYSIS"))
567 oneway.missing_type = MISS_ANALYSIS;
571 lex_error (lexer, NULL);
578 lex_error (lexer, NULL);
585 struct casegrouper *grouper;
586 struct casereader *group;
589 grouper = casegrouper_create_splits (proc_open (ds), dict);
590 while (casegrouper_get_next_group (grouper, &group))
591 run_oneway (&oneway, group, ds);
592 ok = casegrouper_destroy (grouper);
593 ok = proc_commit (ds) && ok;
596 oneway_cleanup (&oneway);
601 oneway_cleanup (&oneway);
610 static struct descriptive_data *
611 dd_create (const struct variable *var)
613 struct descriptive_data *dd = xmalloc (sizeof *dd);
615 dd->mom = moments1_create (MOMENT_VARIANCE);
616 dd->minimum = DBL_MAX;
617 dd->maximum = -DBL_MAX;
624 dd_destroy (struct descriptive_data *dd)
626 moments1_destroy (dd->mom);
631 makeit (const void *aux1, void *aux2 UNUSED)
633 const struct variable *var = aux1;
635 struct descriptive_data *dd = dd_create (var);
641 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
643 struct descriptive_data *dd = user_data;
650 updateit (const void *aux1, void *aux2, void *user_data,
651 const struct ccase *c, double weight)
653 struct descriptive_data *dd = user_data;
655 const struct variable *varp = aux1;
657 const union value *valx = case_data (c, varp);
659 struct descriptive_data *dd_total = aux2;
661 moments1_add (dd->mom, valx->f, weight);
662 if (valx->f < dd->minimum)
663 dd->minimum = valx->f;
665 if (valx->f > dd->maximum)
666 dd->maximum = valx->f;
669 const struct variable *var = dd_total->var;
670 const union value *val = case_data (c, var);
672 moments1_add (dd_total->mom,
676 if (val->f < dd_total->minimum)
677 dd_total->minimum = val->f;
679 if (val->f > dd_total->maximum)
680 dd_total->maximum = val->f;
685 run_oneway (const struct oneway_spec *cmd,
686 struct casereader *input,
687 const struct dataset *ds)
691 struct dictionary *dict = dataset_dict (ds);
692 struct casereader *reader;
695 struct oneway_workspace ws;
697 ws.actual_number_of_groups = 0;
698 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
699 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
701 for (v = 0 ; v < cmd->n_vars; ++v)
702 ws.dd_total[v] = dd_create (cmd->vars[v]);
704 for (v = 0; v < cmd->n_vars; ++v)
706 struct payload payload;
707 payload.create = makeit;
708 payload.update = updateit;
709 payload.calculate = NULL;
710 payload.destroy = killit;
712 ws.vws[v].iact = interaction_create (cmd->indep_var);
713 ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
714 cmd->exclude, cmd->exclude);
716 categoricals_set_payload (ws.vws[v].cat, &payload,
717 CONST_CAST (struct variable *, cmd->vars[v]),
721 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
723 cmd->wv, cmd->exclude);
724 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
727 c = casereader_peek (input, 0);
730 casereader_destroy (input);
733 output_split_file_values (ds, c);
736 taint = taint_clone (casereader_get_taint (input));
738 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
739 cmd->exclude, NULL, NULL);
740 if (cmd->missing_type == MISS_LISTWISE)
741 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
742 cmd->exclude, NULL, NULL);
743 input = casereader_create_filter_weight (input, dict, NULL, NULL);
745 reader = casereader_clone (input);
746 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
749 double w = dict_get_case_weight (dict, c, NULL);
751 for (i = 0; i < cmd->n_vars; ++i)
753 struct per_var_ws *pvw = &ws.vws[i];
754 const struct variable *v = cmd->vars[i];
755 const union value *val = case_data (c, v);
757 if ( MISS_ANALYSIS == cmd->missing_type)
759 if ( var_is_value_missing (v, val, cmd->exclude))
763 covariance_accumulate_pass1 (pvw->cov, c);
764 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
767 casereader_destroy (reader);
769 reader = casereader_clone (input);
770 for ( ; (c = casereader_read (reader) ); case_unref (c))
773 double w = dict_get_case_weight (dict, c, NULL);
774 for (i = 0; i < cmd->n_vars; ++i)
776 struct per_var_ws *pvw = &ws.vws[i];
777 const struct variable *v = cmd->vars[i];
778 const union value *val = case_data (c, v);
780 if ( MISS_ANALYSIS == cmd->missing_type)
782 if ( var_is_value_missing (v, val, cmd->exclude))
786 covariance_accumulate_pass2 (pvw->cov, c);
787 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
790 casereader_destroy (reader);
792 reader = casereader_clone (input);
793 for ( ; (c = casereader_read (reader) ); case_unref (c))
796 double w = dict_get_case_weight (dict, c, NULL);
798 for (i = 0; i < cmd->n_vars; ++i)
800 struct per_var_ws *pvw = &ws.vws[i];
801 const struct variable *v = cmd->vars[i];
802 const union value *val = case_data (c, v);
804 if ( MISS_ANALYSIS == cmd->missing_type)
806 if ( var_is_value_missing (v, val, cmd->exclude))
810 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
813 casereader_destroy (reader);
816 for (v = 0; v < cmd->n_vars; ++v)
819 struct per_var_ws *pvw = &ws.vws[v];
820 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
821 const bool ok = categoricals_sane (cats);
826 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
827 var_get_name (cmd->vars[v]));
831 cm = covariance_calculate_unnormalized (pvw->cov);
833 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
835 pvw->sst = gsl_matrix_get (cm, 0, 0);
839 pvw->sse = gsl_matrix_get (cm, 0, 0);
841 pvw->ssa = pvw->sst - pvw->sse;
843 pvw->n_groups = categoricals_n_total (cats);
845 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
847 gsl_matrix_free (cm);
850 for (v = 0; v < cmd->n_vars; ++v)
852 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
854 if ( ! categoricals_is_complete (cats))
859 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
860 ws.actual_number_of_groups = categoricals_n_total (cats);
863 casereader_destroy (input);
865 if (!taint_has_tainted_successor (taint))
866 output_oneway (cmd, &ws);
868 taint_destroy (taint);
872 for (v = 0; v < cmd->n_vars; ++v)
874 covariance_destroy (ws.vws[v].cov);
875 levene_destroy (ws.vws[v].nl);
876 dd_destroy (ws.dd_total[v]);
877 interaction_destroy (ws.vws[v].iact);
884 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
885 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
886 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
889 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
893 /* Check the sanity of the given contrast values */
894 struct contrasts_node *coeff_list = NULL;
895 struct contrasts_node *coeff_next = NULL;
896 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
898 struct coeff_node *cn = NULL;
900 struct ll_list *cl = &coeff_list->coefficient_list;
903 if (ll_count (cl) != ws->actual_number_of_groups)
906 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
907 i, ll_count (cl), ws->actual_number_of_groups);
909 ll_remove (&coeff_list->ll);
910 destroy_coeff_list (coeff_list);
914 ll_for_each (cn, struct coeff_node, ll, cl)
918 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
921 if (cmd->stats & STATS_DESCRIPTIVES)
922 show_descriptives (cmd, ws);
924 if (cmd->stats & STATS_HOMOGENEITY)
925 show_homogeneity (cmd, ws);
927 show_anova_table (cmd, ws);
929 if (ll_count (&cmd->contrast_list) > 0)
931 show_contrast_coeffs (cmd, ws);
932 show_contrast_tests (cmd, ws);
938 for (v = 0 ; v < cmd->n_vars; ++v)
940 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
942 if ( categoricals_is_complete (cats))
943 show_comparisons (cmd, ws, v);
949 /* Show the ANOVA table */
951 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
955 size_t n_rows = cmd->n_vars * 3 + 1;
957 struct tab_table *t = tab_create (n_cols, n_rows);
959 tab_headers (t, 2, 0, 1, 0);
965 n_cols - 1, n_rows - 1);
967 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
968 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
969 tab_vline (t, TAL_0, 1, 0, 0);
971 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
972 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
973 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
974 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
975 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
978 for (i = 0; i < cmd->n_vars; ++i)
983 const char *s = var_to_string (cmd->vars[i]);
984 const struct per_var_ws *pvw = &ws->vws[i];
986 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
988 df1 = pvw->n_groups - 1;
989 df2 = n - pvw->n_groups;
990 msa = pvw->ssa / df1;
992 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
993 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
994 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
995 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
998 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
1001 /* Sums of Squares */
1002 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
1003 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
1004 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
1007 /* Degrees of freedom */
1008 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
1009 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
1010 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
1013 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
1014 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
1017 const double F = msa / pvw->mse ;
1020 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
1022 /* The significance */
1023 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1027 tab_title (t, _("ANOVA"));
1032 /* Show the descriptives table */
1034 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1038 struct tab_table *t;
1041 const double confidence = 0.95;
1042 const double q = (1.0 - confidence) / 2.0;
1044 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
1048 for (v = 0; v < cmd->n_vars; ++v)
1049 n_rows += ws->actual_number_of_groups + 1;
1051 t = tab_create (n_cols, n_rows);
1052 tab_headers (t, 2, 0, 2, 0);
1054 /* Put a frame around the entire box, and vertical lines inside */
1059 n_cols - 1, n_rows - 1);
1061 /* Underline headers */
1062 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1063 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1065 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
1066 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
1067 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1068 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1071 tab_vline (t, TAL_0, 7, 0, 0);
1072 tab_hline (t, TAL_1, 6, 7, 1);
1073 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1074 _("%g%% Confidence Interval for Mean"),
1077 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1078 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1080 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
1081 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
1083 tab_title (t, _("Descriptives"));
1086 for (v = 0; v < cmd->n_vars; ++v)
1088 const char *s = var_to_string (cmd->vars[v]);
1089 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
1093 struct per_var_ws *pvw = &ws->vws[v];
1094 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1096 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
1098 tab_hline (t, TAL_1, 0, n_cols - 1, row);
1100 for (count = 0; count < categoricals_n_total (cats); ++count)
1103 double n, mean, variance;
1104 double std_dev, std_error ;
1108 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1109 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
1111 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1113 std_dev = sqrt (variance);
1114 std_error = std_dev / sqrt (n) ;
1116 ds_init_empty (&vstr);
1118 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1120 tab_text (t, 1, row + count,
1121 TAB_LEFT | TAT_TITLE,
1126 /* Now fill in the numbers ... */
1128 tab_double (t, 2, row + count, 0, n, wfmt);
1130 tab_double (t, 3, row + count, 0, mean, NULL);
1132 tab_double (t, 4, row + count, 0, std_dev, NULL);
1135 tab_double (t, 5, row + count, 0, std_error, NULL);
1137 /* Now the confidence interval */
1139 T = gsl_cdf_tdist_Qinv (q, n - 1);
1141 tab_double (t, 6, row + count, 0,
1142 mean - T * std_error, NULL);
1144 tab_double (t, 7, row + count, 0,
1145 mean + T * std_error, NULL);
1149 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
1150 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
1153 if (categoricals_is_complete (cats))
1156 double n, mean, variance;
1160 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
1162 std_dev = sqrt (variance);
1163 std_error = std_dev / sqrt (n) ;
1165 tab_text (t, 1, row + count,
1166 TAB_LEFT | TAT_TITLE, _("Total"));
1168 tab_double (t, 2, row + count, 0, n, wfmt);
1170 tab_double (t, 3, row + count, 0, mean, NULL);
1172 tab_double (t, 4, row + count, 0, std_dev, NULL);
1174 tab_double (t, 5, row + count, 0, std_error, NULL);
1176 /* Now the confidence interval */
1177 T = gsl_cdf_tdist_Qinv (q, n - 1);
1179 tab_double (t, 6, row + count, 0,
1180 mean - T * std_error, NULL);
1182 tab_double (t, 7, row + count, 0,
1183 mean + T * std_error, NULL);
1187 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
1188 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
1191 row += categoricals_n_total (cats) + 1;
1197 /* Show the homogeneity table */
1199 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1203 size_t n_rows = cmd->n_vars + 1;
1205 struct tab_table *t = tab_create (n_cols, n_rows);
1206 tab_headers (t, 1, 0, 1, 0);
1208 /* Put a frame around the entire box, and vertical lines inside */
1213 n_cols - 1, n_rows - 1);
1216 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1217 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
1219 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
1220 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1221 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1222 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
1224 tab_title (t, _("Test of Homogeneity of Variances"));
1226 for (v = 0; v < cmd->n_vars; ++v)
1229 const struct per_var_ws *pvw = &ws->vws[v];
1230 double F = levene_calculate (pvw->nl);
1232 const struct variable *var = cmd->vars[v];
1233 const char *s = var_to_string (var);
1236 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1238 df1 = pvw->n_groups - 1;
1239 df2 = n - pvw->n_groups;
1241 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1243 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1244 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1245 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1247 /* Now the significance */
1248 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1255 /* Show the contrast coefficients table */
1257 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1262 int n_contrasts = ll_count (&cmd->contrast_list);
1263 int n_cols = 2 + ws->actual_number_of_groups;
1264 int n_rows = 2 + n_contrasts;
1266 struct tab_table *t;
1268 const struct covariance *cov = ws->vws[0].cov ;
1270 t = tab_create (n_cols, n_rows);
1271 tab_headers (t, 2, 0, 2, 0);
1273 /* Put a frame around the entire box, and vertical lines inside */
1278 n_cols - 1, n_rows - 1);
1292 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1293 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1295 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1297 tab_title (t, _("Contrast Coefficients"));
1299 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1302 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1303 var_to_string (cmd->indep_var));
1305 for ( cli = ll_head (&cmd->contrast_list);
1306 cli != ll_null (&cmd->contrast_list);
1307 cli = ll_next (cli))
1310 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1313 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1315 for (coeffi = ll_head (&cn->coefficient_list);
1316 coeffi != ll_null (&cn->coefficient_list);
1317 ++count, coeffi = ll_next (coeffi))
1319 const struct categoricals *cats = covariance_get_categoricals (cov);
1320 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1321 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1324 ds_init_empty (&vstr);
1326 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1328 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1332 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1341 /* Show the results of the contrast tests */
1343 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1345 int n_contrasts = ll_count (&cmd->contrast_list);
1348 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1350 struct tab_table *t;
1352 t = tab_create (n_cols, n_rows);
1353 tab_headers (t, 3, 0, 1, 0);
1355 /* Put a frame around the entire box, and vertical lines inside */
1360 n_cols - 1, n_rows - 1);
1368 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1369 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1371 tab_title (t, _("Contrast Tests"));
1373 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1374 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1375 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1376 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1377 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1378 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1380 for (v = 0; v < cmd->n_vars; ++v)
1382 const struct per_var_ws *pvw = &ws->vws[v];
1383 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1386 int lines_per_variable = 2 * n_contrasts;
1388 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1389 var_to_string (cmd->vars[v]));
1391 for ( cli = ll_head (&cmd->contrast_list);
1392 cli != ll_null (&cmd->contrast_list);
1393 ++i, cli = ll_next (cli))
1395 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1398 double contrast_value = 0.0;
1399 double coef_msq = 0.0;
1402 double std_error_contrast;
1404 double sec_vneq = 0.0;
1406 /* Note: The calculation of the degrees of freedom in the
1407 "variances not equal" case is painfull!!
1408 The following formula may help to understand it:
1409 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1412 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1417 double df_denominator = 0.0;
1418 double df_numerator = 0.0;
1421 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1422 df = grand_n - pvw->n_groups;
1426 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1427 TAB_LEFT | TAT_TITLE,
1428 _("Assume equal variances"));
1430 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1431 TAB_LEFT | TAT_TITLE,
1432 _("Does not assume equal"));
1435 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1436 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1439 tab_text_format (t, 2,
1440 (v * lines_per_variable) + i + 1 + n_contrasts,
1441 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1443 for (coeffi = ll_head (&cn->coefficient_list);
1444 coeffi != ll_null (&cn->coefficient_list);
1445 ++ci, coeffi = ll_next (coeffi))
1447 double n, mean, variance;
1448 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
1449 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1450 const double coef = cn->coeff;
1453 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1455 winv = variance / n;
1457 contrast_value += coef * mean;
1459 coef_msq += (pow2 (coef)) / n;
1461 sec_vneq += (pow2 (coef)) * variance / n;
1463 df_numerator += (pow2 (coef)) * winv;
1464 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1467 sec_vneq = sqrt (sec_vneq);
1469 df_numerator = pow2 (df_numerator);
1471 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1472 TAB_RIGHT, contrast_value, NULL);
1474 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1476 TAB_RIGHT, contrast_value, NULL);
1478 std_error_contrast = sqrt (pvw->mse * coef_msq);
1481 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1482 TAB_RIGHT, std_error_contrast,
1485 T = fabs (contrast_value / std_error_contrast);
1489 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1494 /* Degrees of Freedom */
1495 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1500 /* Significance TWO TAILED !!*/
1501 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1502 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1505 /* Now for the Variances NOT Equal case */
1509 (v * lines_per_variable) + i + 1 + n_contrasts,
1510 TAB_RIGHT, sec_vneq,
1513 T = contrast_value / sec_vneq;
1515 (v * lines_per_variable) + i + 1 + n_contrasts,
1519 df = df_numerator / df_denominator;
1522 (v * lines_per_variable) + i + 1 + n_contrasts,
1526 /* The Significance */
1527 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1528 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1533 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1542 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1544 const int n_cols = 8;
1545 const int heading_rows = 2;
1546 const int heading_cols = 3;
1549 int r = heading_rows ;
1551 const struct per_var_ws *pvw = &ws->vws[v];
1552 const struct categoricals *cat = pvw->cat;
1553 const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
1555 struct tab_table *t = tab_create (n_cols, n_rows);
1557 tab_headers (t, heading_cols, 0, heading_rows, 0);
1559 /* Put a frame around the entire box, and vertical lines inside */
1564 n_cols - 1, n_rows - 1);
1570 n_cols - 1, n_rows - 1);
1572 tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
1574 tab_title (t, _("Multiple Comparisons (%s)"), var_to_string (cmd->vars[v]));
1576 tab_text_format (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
1577 tab_text_format (t, 2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
1578 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1579 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
1580 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1581 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
1583 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1584 _("%g%% Confidence Interval"),
1585 (1 - cmd->alpha) * 100.0);
1587 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1588 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1591 for (p = 0; p < cmd->n_posthoc; ++p)
1594 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1596 tab_hline (t, TAL_2, 0, n_cols - 1, r);
1598 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
1600 for (i = 0; i < pvw->n_groups ; ++i)
1602 double weight_i, mean_i, var_i;
1606 struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
1607 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1610 ds_init_empty (&vstr);
1611 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1614 tab_hline (t, TAL_1, 1, n_cols - 1, r);
1615 tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1617 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1619 for (j = 0 ; j < pvw->n_groups; ++j)
1622 double weight_j, mean_j, var_j;
1624 const struct ccase *cc;
1625 struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
1630 cc = categoricals_get_case_by_category (cat, j);
1631 var_append_value_name (cmd->indep_var, case_data (cc, cmd->indep_var), &vstr);
1632 tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1634 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1636 tab_double (t, 3, r + rx, 0, mean_i - mean_j, 0);
1639 std_err *= weight_i + weight_j;
1640 std_err /= weight_i * weight_j;
1641 std_err = sqrt (std_err);
1643 tab_double (t, 4, r + rx, 0, std_err, 0);
1645 tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), 0);
1647 half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
1649 tab_double (t, 6, r + rx, 0,
1650 (mean_i - mean_j) - half_range, 0 );
1652 tab_double (t, 7, r + rx, 0,
1653 (mean_i - mean_j) + half_range, 0 );
1658 r += pvw->n_groups - 1;