1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013, 2014,
3 2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <gsl/gsl_cdf.h>
22 #include <gsl/gsl_matrix.h>
25 #include "data/case.h"
26 #include "data/casegrouper.h"
27 #include "data/casereader.h"
28 #include "data/dataset.h"
29 #include "data/dictionary.h"
30 #include "data/format.h"
31 #include "data/value.h"
32 #include "language/command.h"
33 #include "language/dictionary/split-file.h"
34 #include "language/lexer/lexer.h"
35 #include "language/lexer/value-parser.h"
36 #include "language/lexer/variable-parser.h"
37 #include "libpspp/ll.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/taint.h"
41 #include "linreg/sweep.h"
42 #include "tukey/tukey.h"
43 #include "math/categoricals.h"
44 #include "math/interaction.h"
45 #include "math/covariance.h"
46 #include "math/levene.h"
47 #include "math/moments.h"
48 #include "output/pivot-table.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
54 /* Workspace variable for each dependent variable */
57 struct interaction *iact;
58 struct categoricals *cat;
59 struct covariance *cov;
73 /* Per category data */
74 struct descriptive_data
76 const struct variable *var;
91 STATS_DESCRIPTIVES = 0x0001,
92 STATS_HOMOGENEITY = 0x0002
102 struct contrasts_node
105 struct ll_list coefficient_list;
111 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
112 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
113 typedef double p1tail_func (double ts, double df1, double df2);
115 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
133 const struct variable **vars;
135 const struct variable *indep_var;
137 enum statistics stats;
139 enum missing_type missing_type;
140 enum mv_class exclude;
142 /* List of contrasts */
143 struct ll_list contrast_list;
145 /* The weight variable */
146 const struct variable *wv;
147 const struct fmt_spec *wfmt;
149 /* The confidence level for multiple comparisons */
157 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
159 return pvw->n - pvw->n_groups;
163 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
169 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
170 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
172 if (n_i <= 1.0 || n_j <= 1.0)
175 nom = pow2 (var_i/n_i + var_j/n_j);
176 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
181 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)
183 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
186 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)
188 const int m = k * (k - 1) / 2;
189 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
192 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)
194 const double m = k * (k - 1) / 2;
195 double lp = 1.0 - exp (log (1.0 - alpha) / m) ;
196 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
199 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)
204 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
207 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)
209 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
210 return std_err * sqrt (x);
213 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
215 double n_i, mean_i, var_i;
216 double n_j, mean_j, var_j;
219 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
220 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
222 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
227 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
232 multiple_comparison_sig (double std_err,
233 const struct per_var_ws *pvw,
234 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
235 const struct posthoc *ph)
237 int k = pvw->n_groups;
238 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
239 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
242 return ph->p1f (ts, k - 1, df);
246 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)
248 int k = pvw->n_groups;
249 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
253 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
256 static double tukey_1tailsig (double ts, double df1, double df2)
260 if (df2 < 2 || df1 < 1)
263 twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
265 return twotailedsig / 2.0;
268 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
270 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
273 static double sidak_1tailsig (double ts, double df1, double df2)
275 double ex = (df1 + 1.0) * df1 / 2.0;
276 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
278 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
281 static double bonferroni_1tailsig (double ts, double df1, double df2)
283 const int m = (df1 + 1) * df1 / 2;
285 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
288 return p > 0.5 ? 0.5 : p;
291 static double scheffe_1tailsig (double ts, double df1, double df2)
293 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
297 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
300 double n_i, mean_i, var_i;
301 double n_j, mean_j, var_j;
303 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
304 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
306 ts = (mean_i - mean_j) / std_err;
307 ts = fabs (ts) * sqrt (2.0);
312 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
314 double n_i, mean_i, var_i;
315 double n_j, mean_j, var_j;
317 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
318 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
320 return (mean_i - mean_j) / std_err;
323 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
326 double n_i, mean_i, var_i;
327 double n_j, mean_j, var_j;
329 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
330 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
332 t = (mean_i - mean_j) / std_err;
339 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
343 double n_i, mean_i, var_i;
344 double n_j, mean_j, var_j;
346 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
347 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
349 thing = var_i / n_i + var_j / n_j;
351 thing = sqrt (thing);
353 ts = (mean_i - mean_j) / thing;
360 static const struct posthoc ph_tests [] =
362 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
363 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
364 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
365 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
366 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
367 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
371 struct oneway_workspace
373 /* The number of distinct values of the independent variable, when all
374 missing values are disregarded */
375 int actual_number_of_groups;
377 struct per_var_ws *vws;
379 /* An array of descriptive data. One for each dependent variable */
380 struct descriptive_data **dd_total;
383 /* Routines to show the output tables */
384 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
385 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
386 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
388 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
389 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
393 destroy_coeff_list (struct contrasts_node *coeff_list)
395 struct coeff_node *cn = NULL;
396 struct coeff_node *cnx = NULL;
397 struct ll_list *cl = &coeff_list->coefficient_list;
399 ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
408 oneway_cleanup (struct oneway_spec *cmd)
410 struct contrasts_node *coeff_list = NULL;
411 struct contrasts_node *coeff_next = NULL;
412 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
414 destroy_coeff_list (coeff_list);
423 cmd_oneway (struct lexer *lexer, struct dataset *ds)
425 const struct dictionary *dict = dataset_dict (ds);
426 struct oneway_spec oneway ;
429 oneway.indep_var = NULL;
431 oneway.missing_type = MISS_ANALYSIS;
432 oneway.exclude = MV_ANY;
433 oneway.wv = dict_get_weight (dict);
434 oneway.wfmt = dict_get_weight_format (dict);
436 oneway.posthoc = NULL;
437 oneway.n_posthoc = 0;
439 ll_init (&oneway.contrast_list);
442 if (lex_match (lexer, T_SLASH))
444 if (!lex_force_match_id (lexer, "VARIABLES"))
448 lex_match (lexer, T_EQUALS);
451 if (!parse_variables_const (lexer, dict,
452 &oneway.vars, &oneway.n_vars,
453 PV_NO_DUPLICATE | PV_NUMERIC))
456 if (!lex_force_match (lexer, T_BY))
459 oneway.indep_var = parse_variable_const (lexer, dict);
460 if (oneway.indep_var == NULL)
463 while (lex_token (lexer) != T_ENDCMD)
465 lex_match (lexer, T_SLASH);
467 if (lex_match_id (lexer, "STATISTICS"))
469 lex_match (lexer, T_EQUALS);
470 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
472 if (lex_match_id (lexer, "DESCRIPTIVES"))
474 oneway.stats |= STATS_DESCRIPTIVES;
476 else if (lex_match_id (lexer, "HOMOGENEITY"))
478 oneway.stats |= STATS_HOMOGENEITY;
482 lex_error (lexer, NULL);
487 else if (lex_match_id (lexer, "POSTHOC"))
489 lex_match (lexer, T_EQUALS);
490 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
494 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
496 if (lex_match_id (lexer, ph_tests[p].syntax))
499 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
500 oneway.posthoc[oneway.n_posthoc - 1] = p;
507 if (lex_match_id (lexer, "ALPHA"))
509 if (!lex_force_match (lexer, T_LPAREN))
511 if (! lex_force_num (lexer))
513 oneway.alpha = lex_number (lexer);
515 if (!lex_force_match (lexer, T_RPAREN))
520 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
521 lex_error (lexer, NULL);
527 else if (lex_match_id (lexer, "CONTRAST"))
529 struct contrasts_node *cl = xzalloc (sizeof *cl);
531 struct ll_list *coefficient_list = &cl->coefficient_list;
532 lex_match (lexer, T_EQUALS);
534 ll_init (coefficient_list);
536 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
538 if (lex_is_number (lexer))
540 struct coeff_node *cc = xmalloc (sizeof *cc);
541 cc->coeff = lex_number (lexer);
543 ll_push_tail (coefficient_list, &cc->ll);
548 destroy_coeff_list (cl);
549 lex_error (lexer, NULL);
554 if (ll_count (coefficient_list) <= 0)
556 destroy_coeff_list (cl);
560 ll_push_tail (&oneway.contrast_list, &cl->ll);
562 else if (lex_match_id (lexer, "MISSING"))
564 lex_match (lexer, T_EQUALS);
565 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
567 if (lex_match_id (lexer, "INCLUDE"))
569 oneway.exclude = MV_SYSTEM;
571 else if (lex_match_id (lexer, "EXCLUDE"))
573 oneway.exclude = MV_ANY;
575 else if (lex_match_id (lexer, "LISTWISE"))
577 oneway.missing_type = MISS_LISTWISE;
579 else if (lex_match_id (lexer, "ANALYSIS"))
581 oneway.missing_type = MISS_ANALYSIS;
585 lex_error (lexer, NULL);
592 lex_error (lexer, NULL);
599 struct casegrouper *grouper;
600 struct casereader *group;
603 grouper = casegrouper_create_splits (proc_open (ds), dict);
604 while (casegrouper_get_next_group (grouper, &group))
605 run_oneway (&oneway, group, ds);
606 ok = casegrouper_destroy (grouper);
607 ok = proc_commit (ds) && ok;
610 oneway_cleanup (&oneway);
615 oneway_cleanup (&oneway);
624 static struct descriptive_data *
625 dd_create (const struct variable *var)
627 struct descriptive_data *dd = xmalloc (sizeof *dd);
629 dd->mom = moments1_create (MOMENT_VARIANCE);
630 dd->minimum = DBL_MAX;
631 dd->maximum = -DBL_MAX;
638 dd_destroy (struct descriptive_data *dd)
640 moments1_destroy (dd->mom);
645 makeit (const void *aux1, void *aux2 UNUSED)
647 const struct variable *var = aux1;
649 struct descriptive_data *dd = dd_create (var);
655 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
657 struct descriptive_data *dd = user_data;
664 updateit (const void *aux1, void *aux2, void *user_data,
665 const struct ccase *c, double weight)
667 struct descriptive_data *dd = user_data;
669 const struct variable *varp = aux1;
671 const union value *valx = case_data (c, varp);
673 struct descriptive_data *dd_total = aux2;
675 moments1_add (dd->mom, valx->f, weight);
676 if (valx->f < dd->minimum)
677 dd->minimum = valx->f;
679 if (valx->f > dd->maximum)
680 dd->maximum = valx->f;
683 const struct variable *var = dd_total->var;
684 const union value *val = case_data (c, var);
686 moments1_add (dd_total->mom,
690 if (val->f < dd_total->minimum)
691 dd_total->minimum = val->f;
693 if (val->f > dd_total->maximum)
694 dd_total->maximum = val->f;
699 run_oneway (const struct oneway_spec *cmd,
700 struct casereader *input,
701 const struct dataset *ds)
705 struct dictionary *dict = dataset_dict (ds);
706 struct casereader *reader;
709 struct oneway_workspace ws;
711 ws.actual_number_of_groups = 0;
712 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
713 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
715 for (v = 0 ; v < cmd->n_vars; ++v)
716 ws.dd_total[v] = dd_create (cmd->vars[v]);
718 for (v = 0; v < cmd->n_vars; ++v)
720 static const struct payload payload =
728 ws.vws[v].iact = interaction_create (cmd->indep_var);
729 ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
732 categoricals_set_payload (ws.vws[v].cat, &payload,
733 CONST_CAST (struct variable *, cmd->vars[v]),
737 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
739 cmd->wv, cmd->exclude, true);
740 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
743 c = casereader_peek (input, 0);
746 casereader_destroy (input);
749 output_split_file_values (ds, c);
752 taint = taint_clone (casereader_get_taint (input));
754 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
755 cmd->exclude, NULL, NULL);
756 if (cmd->missing_type == MISS_LISTWISE)
757 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
758 cmd->exclude, NULL, NULL);
759 input = casereader_create_filter_weight (input, dict, NULL, NULL);
761 reader = casereader_clone (input);
762 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
765 double w = dict_get_case_weight (dict, c, NULL);
767 for (i = 0; i < cmd->n_vars; ++i)
769 struct per_var_ws *pvw = &ws.vws[i];
770 const struct variable *v = cmd->vars[i];
771 const union value *val = case_data (c, v);
773 if (MISS_ANALYSIS == cmd->missing_type)
775 if (var_is_value_missing (v, val, cmd->exclude))
779 covariance_accumulate_pass1 (pvw->cov, c);
780 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
783 casereader_destroy (reader);
785 reader = casereader_clone (input);
786 for (; (c = casereader_read (reader)); case_unref (c))
789 double w = dict_get_case_weight (dict, c, NULL);
790 for (i = 0; i < cmd->n_vars; ++i)
792 struct per_var_ws *pvw = &ws.vws[i];
793 const struct variable *v = cmd->vars[i];
794 const union value *val = case_data (c, v);
796 if (MISS_ANALYSIS == cmd->missing_type)
798 if (var_is_value_missing (v, val, cmd->exclude))
802 covariance_accumulate_pass2 (pvw->cov, c);
803 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
806 casereader_destroy (reader);
808 reader = casereader_clone (input);
809 for (; (c = casereader_read (reader)); case_unref (c))
812 double w = dict_get_case_weight (dict, c, NULL);
814 for (i = 0; i < cmd->n_vars; ++i)
816 struct per_var_ws *pvw = &ws.vws[i];
817 const struct variable *v = cmd->vars[i];
818 const union value *val = case_data (c, v);
820 if (MISS_ANALYSIS == cmd->missing_type)
822 if (var_is_value_missing (v, val, cmd->exclude))
826 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
829 casereader_destroy (reader);
832 for (v = 0; v < cmd->n_vars; ++v)
834 const gsl_matrix *ucm;
836 struct per_var_ws *pvw = &ws.vws[v];
837 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
838 const bool ok = categoricals_sane (cats);
843 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
844 var_get_name (cmd->vars[v]));
848 ucm = covariance_calculate_unnormalized (pvw->cov);
850 cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
851 gsl_matrix_memcpy (cm, ucm);
853 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
855 pvw->sst = gsl_matrix_get (cm, 0, 0);
859 pvw->sse = gsl_matrix_get (cm, 0, 0);
860 gsl_matrix_free (cm);
862 pvw->ssa = pvw->sst - pvw->sse;
864 pvw->n_groups = categoricals_n_total (cats);
866 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
869 for (v = 0; v < cmd->n_vars; ++v)
871 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
873 if (! categoricals_is_complete (cats))
878 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
879 ws.actual_number_of_groups = categoricals_n_total (cats);
882 casereader_destroy (input);
884 if (!taint_has_tainted_successor (taint))
885 output_oneway (cmd, &ws);
887 taint_destroy (taint);
891 for (v = 0; v < cmd->n_vars; ++v)
893 covariance_destroy (ws.vws[v].cov);
894 levene_destroy (ws.vws[v].nl);
895 dd_destroy (ws.dd_total[v]);
896 interaction_destroy (ws.vws[v].iact);
903 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
904 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
905 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
908 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
912 /* Check the sanity of the given contrast values */
913 struct contrasts_node *coeff_list = NULL;
914 struct contrasts_node *coeff_next = NULL;
915 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
917 struct coeff_node *cn = NULL;
919 struct ll_list *cl = &coeff_list->coefficient_list;
922 if (ll_count (cl) != ws->actual_number_of_groups)
925 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
926 i, ll_count (cl), ws->actual_number_of_groups);
928 ll_remove (&coeff_list->ll);
929 destroy_coeff_list (coeff_list);
933 ll_for_each (cn, struct coeff_node, ll, cl)
937 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
940 if (cmd->stats & STATS_DESCRIPTIVES)
941 show_descriptives (cmd, ws);
943 if (cmd->stats & STATS_HOMOGENEITY)
944 show_homogeneity (cmd, ws);
946 show_anova_table (cmd, ws);
948 if (ll_count (&cmd->contrast_list) > 0)
950 show_contrast_coeffs (cmd, ws);
951 show_contrast_tests (cmd, ws);
957 for (v = 0 ; v < cmd->n_vars; ++v)
959 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
961 if (categoricals_is_complete (cats))
962 show_comparisons (cmd, ws, v);
968 /* Show the ANOVA table */
970 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
972 struct pivot_table *table = pivot_table_create (N_("ANOVA"));
974 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
975 N_("Sum of Squares"), PIVOT_RC_OTHER,
976 N_("df"), PIVOT_RC_INTEGER,
977 N_("Mean Square"), PIVOT_RC_OTHER,
978 N_("F"), PIVOT_RC_OTHER,
979 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
981 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
982 N_("Between Groups"), N_("Within Groups"),
985 struct pivot_dimension *variables = pivot_dimension_create (
986 table, PIVOT_AXIS_ROW, N_("Variables"));
988 for (size_t i = 0; i < cmd->n_vars; ++i)
990 int var_idx = pivot_category_create_leaf (
991 variables->root, pivot_value_new_variable (cmd->vars[i]));
993 const struct per_var_ws *pvw = &ws->vws[i];
996 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
998 double df1 = pvw->n_groups - 1;
999 double df2 = n - pvw->n_groups;
1000 double msa = pvw->ssa / df1;
1001 double F = msa / pvw->mse ;
1010 /* Sums of Squares. */
1014 /* Degrees of Freedom. */
1024 { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
1026 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1028 const struct entry *e = &entries[j];
1029 pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
1030 pivot_value_new_number (e->x));
1034 pivot_table_submit (table);
1037 /* Show the descriptives table */
1039 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1043 const struct categoricals *cats = covariance_get_categoricals (
1046 struct pivot_table *table = pivot_table_create (N_("Descriptives"));
1047 pivot_table_set_weight_format (table, cmd->wfmt);
1049 const double confidence = 0.95;
1051 struct pivot_dimension *statistics = pivot_dimension_create (
1052 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1053 N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
1055 struct pivot_category *interval = pivot_category_create_group__ (
1057 pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
1058 confidence * 100.0));
1059 pivot_category_create_leaves (interval, N_("Lower Bound"),
1061 pivot_category_create_leaves (statistics->root,
1062 N_("Minimum"), N_("Maximum"));
1064 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1065 table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
1066 indep_var->root->show_label = true;
1068 union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
1069 for (size_t j = 0; j < n; j++)
1070 pivot_category_create_leaf (
1071 indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1072 pivot_category_create_leaf (
1073 indep_var->root, pivot_value_new_text_format (N_("Total")));
1075 struct pivot_dimension *dep_var = pivot_dimension_create (
1076 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1078 const double q = (1.0 - confidence) / 2.0;
1079 for (int v = 0; v < cmd->n_vars; ++v)
1081 int dep_var_idx = pivot_category_create_leaf (
1082 dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1084 struct per_var_ws *pvw = &ws->vws[v];
1085 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1088 for (count = 0; count < categoricals_n_total (cats); ++count)
1090 const struct descriptive_data *dd
1091 = categoricals_get_user_data_by_category (cats, count);
1093 double n, mean, variance;
1094 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1096 double std_dev = sqrt (variance);
1097 double std_error = std_dev / sqrt (n) ;
1098 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1100 double entries[] = {
1105 mean - T * std_error,
1106 mean + T * std_error,
1110 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1111 pivot_table_put3 (table, i, count, dep_var_idx,
1112 pivot_value_new_number (entries[i]));
1115 if (categoricals_is_complete (cats))
1117 double n, mean, variance;
1118 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1121 double std_dev = sqrt (variance);
1122 double std_error = std_dev / sqrt (n) ;
1123 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1125 double entries[] = {
1130 mean - T * std_error,
1131 mean + T * std_error,
1132 ws->dd_total[v]->minimum,
1133 ws->dd_total[v]->maximum,
1135 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1136 pivot_table_put3 (table, i, count, dep_var_idx,
1137 pivot_value_new_number (entries[i]));
1141 pivot_table_submit (table);
1144 /* Show the homogeneity table */
1146 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1148 struct pivot_table *table = pivot_table_create (
1149 N_("Test of Homogeneity of Variances"));
1151 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1152 N_("Levene Statistic"), PIVOT_RC_OTHER,
1153 N_("df1"), PIVOT_RC_INTEGER,
1154 N_("df2"), PIVOT_RC_INTEGER,
1155 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1157 struct pivot_dimension *variables = pivot_dimension_create (
1158 table, PIVOT_AXIS_ROW, N_("Variables"));
1160 for (int v = 0; v < cmd->n_vars; ++v)
1162 int var_idx = pivot_category_create_leaf (
1163 variables->root, pivot_value_new_variable (cmd->vars[v]));
1166 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1168 const struct per_var_ws *pvw = &ws->vws[v];
1169 double df1 = pvw->n_groups - 1;
1170 double df2 = n - pvw->n_groups;
1171 double F = levene_calculate (pvw->nl);
1178 gsl_cdf_fdist_Q (F, df1, df2),
1180 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1181 pivot_table_put2 (table, i, var_idx,
1182 pivot_value_new_number (entries[i]));
1185 pivot_table_submit (table);
1189 /* Show the contrast coefficients table */
1191 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1193 struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1195 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1196 table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1197 indep_var->root->show_label = true;
1199 struct pivot_dimension *contrast = pivot_dimension_create (
1200 table, PIVOT_AXIS_ROW, N_("Contrast"));
1201 contrast->root->show_label = true;
1203 const struct covariance *cov = ws->vws[0].cov;
1205 const struct contrasts_node *cn;
1207 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1209 int contrast_idx = pivot_category_create_leaf (
1210 contrast->root, pivot_value_new_integer (c_num++));
1212 const struct coeff_node *coeffn;
1214 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1216 const struct categoricals *cats = covariance_get_categoricals (cov);
1217 const struct ccase *gcc = categoricals_get_case_by_category (
1221 pivot_category_create_leaf (
1222 indep_var->root, pivot_value_new_var_value (
1223 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1225 pivot_table_put2 (table, indep_idx++, contrast_idx,
1226 pivot_value_new_integer (coeffn->coeff));
1230 pivot_table_submit (table);
1233 /* Show the results of the contrast tests */
1235 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1237 struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1239 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1240 N_("Value of Contrast"), PIVOT_RC_OTHER,
1241 N_("Std. Error"), PIVOT_RC_OTHER,
1242 N_("t"), PIVOT_RC_OTHER,
1243 N_("df"), PIVOT_RC_OTHER,
1244 N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1246 struct pivot_dimension *contrasts = pivot_dimension_create (
1247 table, PIVOT_AXIS_ROW, N_("Contrast"));
1248 contrasts->root->show_label = true;
1249 int n_contrasts = ll_count (&cmd->contrast_list);
1250 for (int i = 1; i <= n_contrasts; i++)
1251 pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1253 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1254 N_("Assume equal variances"),
1255 N_("Does not assume equal variances"));
1257 struct pivot_dimension *variables = pivot_dimension_create (
1258 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1260 for (int v = 0; v < cmd->n_vars; ++v)
1262 const struct per_var_ws *pvw = &ws->vws[v];
1263 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1264 if (!categoricals_is_complete (cats))
1267 int var_idx = pivot_category_create_leaf (
1268 variables->root, pivot_value_new_variable (cmd->vars[v]));
1270 struct contrasts_node *cn;
1271 int contrast_idx = 0;
1272 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1275 /* Note: The calculation of the degrees of freedom in the
1276 "variances not equal" case is painfull!!
1277 The following formula may help to understand it:
1278 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1281 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1287 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1289 double df = grand_n - pvw->n_groups;
1291 double contrast_value = 0.0;
1292 double coef_msq = 0.0;
1293 double sec_vneq = 0.0;
1294 double df_denominator = 0.0;
1295 double df_numerator = 0.0;
1296 struct coeff_node *coeffn;
1298 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1300 const struct descriptive_data *dd
1301 = categoricals_get_user_data_by_category (cats, ci);
1302 const double coef = coeffn->coeff;
1304 double n, mean, variance;
1305 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1307 double winv = variance / n;
1308 contrast_value += coef * mean;
1309 coef_msq += pow2 (coef) / n;
1310 sec_vneq += pow2 (coef) * variance / n;
1311 df_numerator += pow2 (coef) * winv;
1312 df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1316 sec_vneq = sqrt (sec_vneq);
1317 df_numerator = pow2 (df_numerator);
1319 double std_error_contrast = sqrt (pvw->mse * coef_msq);
1320 double T = contrast_value / std_error_contrast;
1321 double T_ne = contrast_value / sec_vneq;
1322 double df_ne = df_numerator / df_denominator;
1333 { 0, 0, contrast_value },
1334 { 1, 0, std_error_contrast },
1337 { 4, 0, 2 * gsl_cdf_tdist_Q (fabs(T), df) },
1338 /* Do not assume equal. */
1339 { 0, 1, contrast_value },
1343 { 4, 1, 2 * gsl_cdf_tdist_Q (fabs(T_ne), df_ne) },
1346 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1348 const struct entry *e = &entries[i];
1350 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1351 pivot_value_new_number (e->x));
1358 pivot_table_submit (table);
1362 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1364 struct pivot_table *table = pivot_table_create__ (
1365 pivot_value_new_user_text_nocopy (xasprintf (
1366 _("Multiple Comparisons (%s)"),
1367 var_to_string (cmd->vars[v]))),
1368 "Multiple Comparisons");
1370 struct pivot_dimension *statistics = pivot_dimension_create (
1371 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1372 N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1373 N_("Std. Error"), PIVOT_RC_OTHER,
1374 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1375 struct pivot_category *interval = pivot_category_create_group__ (
1377 pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1378 (1 - cmd->alpha) * 100.0));
1379 pivot_category_create_leaves (interval,
1380 N_("Lower Bound"), PIVOT_RC_OTHER,
1381 N_("Upper Bound"), PIVOT_RC_OTHER);
1383 struct pivot_dimension *j_family = pivot_dimension_create (
1384 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1385 j_family->root->show_label = true;
1387 struct pivot_dimension *i_family = pivot_dimension_create (
1388 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1389 i_family->root->show_label = true;
1391 const struct per_var_ws *pvw = &ws->vws[v];
1392 const struct categoricals *cat = pvw->cat;
1393 for (int i = 0; i < pvw->n_groups; i++)
1395 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1396 for (int j = 0; j < 2; j++)
1397 pivot_category_create_leaf (
1398 j ? j_family->root : i_family->root,
1399 pivot_value_new_var_value (cmd->indep_var,
1400 case_data (gcc, cmd->indep_var)));
1403 struct pivot_dimension *test = pivot_dimension_create (
1404 table, PIVOT_AXIS_ROW, N_("Test"));
1406 for (int p = 0; p < cmd->n_posthoc; ++p)
1408 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1410 int test_idx = pivot_category_create_leaf (
1411 test->root, pivot_value_new_text (ph->label));
1413 for (int i = 0; i < pvw->n_groups ; ++i)
1415 struct descriptive_data *dd_i
1416 = categoricals_get_user_data_by_category (cat, i);
1417 double weight_i, mean_i, var_i;
1418 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1420 for (int j = 0 ; j < pvw->n_groups; ++j)
1425 struct descriptive_data *dd_j
1426 = categoricals_get_user_data_by_category (cat, j);
1427 double weight_j, mean_j, var_j;
1428 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1430 double std_err = pvw->mse;
1431 std_err *= weight_i + weight_j;
1432 std_err /= weight_i * weight_j;
1433 std_err = sqrt (std_err);
1435 double sig = 2 * multiple_comparison_sig (std_err, pvw,
1437 double half_range = mc_half_range (cmd, pvw, std_err,
1439 double entries[] = {
1443 (mean_i - mean_j) - half_range,
1444 (mean_i - mean_j) + half_range,
1446 for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1447 pivot_table_put4 (table, k, j, i, test_idx,
1448 pivot_value_new_number (entries[k]));
1453 pivot_table_submit (table);