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))
521 _("The post hoc analysis method %s is not supported."),
522 lex_tokcstr (lexer));
528 else if (lex_match_id (lexer, "CONTRAST"))
530 struct contrasts_node *cl = XZALLOC (struct contrasts_node);
532 struct ll_list *coefficient_list = &cl->coefficient_list;
533 lex_match (lexer, T_EQUALS);
535 ll_init (coefficient_list);
537 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
539 if (lex_is_number (lexer))
541 struct coeff_node *cc = xmalloc (sizeof *cc);
542 cc->coeff = lex_number (lexer);
544 ll_push_tail (coefficient_list, &cc->ll);
549 destroy_coeff_list (cl);
550 lex_error (lexer, NULL);
555 if (ll_count (coefficient_list) <= 0)
557 destroy_coeff_list (cl);
561 ll_push_tail (&oneway.contrast_list, &cl->ll);
563 else if (lex_match_id (lexer, "MISSING"))
565 lex_match (lexer, T_EQUALS);
566 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
568 if (lex_match_id (lexer, "INCLUDE"))
570 oneway.exclude = MV_SYSTEM;
572 else if (lex_match_id (lexer, "EXCLUDE"))
574 oneway.exclude = MV_ANY;
576 else if (lex_match_id (lexer, "LISTWISE"))
578 oneway.missing_type = MISS_LISTWISE;
580 else if (lex_match_id (lexer, "ANALYSIS"))
582 oneway.missing_type = MISS_ANALYSIS;
586 lex_error (lexer, NULL);
593 lex_error (lexer, NULL);
600 struct casegrouper *grouper;
601 struct casereader *group;
604 grouper = casegrouper_create_splits (proc_open (ds), dict);
605 while (casegrouper_get_next_group (grouper, &group))
606 run_oneway (&oneway, group, ds);
607 ok = casegrouper_destroy (grouper);
608 ok = proc_commit (ds) && ok;
611 oneway_cleanup (&oneway);
616 oneway_cleanup (&oneway);
625 static struct descriptive_data *
626 dd_create (const struct variable *var)
628 struct descriptive_data *dd = xmalloc (sizeof *dd);
630 dd->mom = moments1_create (MOMENT_VARIANCE);
631 dd->minimum = DBL_MAX;
632 dd->maximum = -DBL_MAX;
639 dd_destroy (struct descriptive_data *dd)
641 moments1_destroy (dd->mom);
646 makeit (const void *aux1, void *aux2 UNUSED)
648 const struct variable *var = aux1;
650 struct descriptive_data *dd = dd_create (var);
656 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
658 struct descriptive_data *dd = user_data;
665 updateit (const void *aux1, void *aux2, void *user_data,
666 const struct ccase *c, double weight)
668 struct descriptive_data *dd = user_data;
670 const struct variable *varp = aux1;
672 const union value *valx = case_data (c, varp);
674 struct descriptive_data *dd_total = aux2;
676 moments1_add (dd->mom, valx->f, weight);
677 if (valx->f < dd->minimum)
678 dd->minimum = valx->f;
680 if (valx->f > dd->maximum)
681 dd->maximum = valx->f;
684 const struct variable *var = dd_total->var;
685 const union value *val = case_data (c, var);
687 moments1_add (dd_total->mom,
691 if (val->f < dd_total->minimum)
692 dd_total->minimum = val->f;
694 if (val->f > dd_total->maximum)
695 dd_total->maximum = val->f;
700 run_oneway (const struct oneway_spec *cmd,
701 struct casereader *input,
702 const struct dataset *ds)
706 struct dictionary *dict = dataset_dict (ds);
707 struct casereader *reader;
709 struct oneway_workspace ws;
711 ws.actual_number_of_groups = 0;
712 ws.vws = xcalloc (cmd->n_vars, sizeof (*ws.vws));
713 ws.dd_total = XCALLOC (cmd->n_vars, struct descriptive_data*);
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 output_split_file_values_peek (ds, input);
745 taint = taint_clone (casereader_get_taint (input));
747 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
748 cmd->exclude, NULL, NULL);
749 if (cmd->missing_type == MISS_LISTWISE)
750 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
751 cmd->exclude, NULL, NULL);
752 input = casereader_create_filter_weight (input, dict, NULL, NULL);
754 reader = casereader_clone (input);
756 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
759 double w = dict_get_case_weight (dict, c, NULL);
761 for (i = 0; i < cmd->n_vars; ++i)
763 struct per_var_ws *pvw = &ws.vws[i];
764 const struct variable *v = cmd->vars[i];
765 const union value *val = case_data (c, v);
767 if (MISS_ANALYSIS == cmd->missing_type)
769 if (var_is_value_missing (v, val) & cmd->exclude)
773 covariance_accumulate_pass1 (pvw->cov, c);
774 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
777 casereader_destroy (reader);
779 reader = casereader_clone (input);
780 for (; (c = casereader_read (reader)); case_unref (c))
783 double w = dict_get_case_weight (dict, c, NULL);
784 for (i = 0; i < cmd->n_vars; ++i)
786 struct per_var_ws *pvw = &ws.vws[i];
787 const struct variable *v = cmd->vars[i];
788 const union value *val = case_data (c, v);
790 if (MISS_ANALYSIS == cmd->missing_type)
792 if (var_is_value_missing (v, val) & cmd->exclude)
796 covariance_accumulate_pass2 (pvw->cov, c);
797 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
800 casereader_destroy (reader);
802 reader = casereader_clone (input);
803 for (; (c = casereader_read (reader)); case_unref (c))
806 double w = dict_get_case_weight (dict, c, NULL);
808 for (i = 0; i < cmd->n_vars; ++i)
810 struct per_var_ws *pvw = &ws.vws[i];
811 const struct variable *v = cmd->vars[i];
812 const union value *val = case_data (c, v);
814 if (MISS_ANALYSIS == cmd->missing_type)
816 if (var_is_value_missing (v, val) & cmd->exclude)
820 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
823 casereader_destroy (reader);
826 for (v = 0; v < cmd->n_vars; ++v)
828 const gsl_matrix *ucm;
830 struct per_var_ws *pvw = &ws.vws[v];
831 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
832 const bool ok = categoricals_sane (cats);
837 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
838 var_get_name (cmd->vars[v]));
842 ucm = covariance_calculate_unnormalized (pvw->cov);
844 cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
845 gsl_matrix_memcpy (cm, ucm);
847 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
849 pvw->sst = gsl_matrix_get (cm, 0, 0);
853 pvw->sse = gsl_matrix_get (cm, 0, 0);
854 gsl_matrix_free (cm);
856 pvw->ssa = pvw->sst - pvw->sse;
858 pvw->n_groups = categoricals_n_total (cats);
860 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
863 for (v = 0; v < cmd->n_vars; ++v)
865 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
867 if (! categoricals_is_complete (cats))
872 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
873 ws.actual_number_of_groups = categoricals_n_total (cats);
876 casereader_destroy (input);
878 if (!taint_has_tainted_successor (taint))
879 output_oneway (cmd, &ws);
881 taint_destroy (taint);
883 for (v = 0; v < cmd->n_vars; ++v)
885 covariance_destroy (ws.vws[v].cov);
886 levene_destroy (ws.vws[v].nl);
887 dd_destroy (ws.dd_total[v]);
888 interaction_destroy (ws.vws[v].iact);
895 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
896 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
897 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
900 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
904 /* Check the sanity of the given contrast values */
905 struct contrasts_node *coeff_list = NULL;
906 struct contrasts_node *coeff_next = NULL;
907 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
909 struct coeff_node *cn = NULL;
911 struct ll_list *cl = &coeff_list->coefficient_list;
914 if (ll_count (cl) != ws->actual_number_of_groups)
917 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
918 i, ll_count (cl), ws->actual_number_of_groups);
920 ll_remove (&coeff_list->ll);
921 destroy_coeff_list (coeff_list);
925 ll_for_each (cn, struct coeff_node, ll, cl)
929 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
932 if (cmd->stats & STATS_DESCRIPTIVES)
933 show_descriptives (cmd, ws);
935 if (cmd->stats & STATS_HOMOGENEITY)
936 show_homogeneity (cmd, ws);
938 show_anova_table (cmd, ws);
940 if (ll_count (&cmd->contrast_list) > 0)
942 show_contrast_coeffs (cmd, ws);
943 show_contrast_tests (cmd, ws);
949 for (v = 0 ; v < cmd->n_vars; ++v)
951 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
953 if (categoricals_is_complete (cats))
954 show_comparisons (cmd, ws, v);
960 /* Show the ANOVA table */
962 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
964 struct pivot_table *table = pivot_table_create (N_("ANOVA"));
966 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
967 N_("Sum of Squares"), PIVOT_RC_OTHER,
968 N_("df"), PIVOT_RC_INTEGER,
969 N_("Mean Square"), PIVOT_RC_OTHER,
970 N_("F"), PIVOT_RC_OTHER,
971 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
973 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
974 N_("Between Groups"), N_("Within Groups"),
977 struct pivot_dimension *variables = pivot_dimension_create (
978 table, PIVOT_AXIS_ROW, N_("Variables"));
980 for (size_t i = 0; i < cmd->n_vars; ++i)
982 int var_idx = pivot_category_create_leaf (
983 variables->root, pivot_value_new_variable (cmd->vars[i]));
985 const struct per_var_ws *pvw = &ws->vws[i];
988 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
990 double df1 = pvw->n_groups - 1;
991 double df2 = n - pvw->n_groups;
992 double msa = pvw->ssa / df1;
993 double F = msa / pvw->mse ;
1002 /* Sums of Squares. */
1006 /* Degrees of Freedom. */
1016 { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
1018 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1020 const struct entry *e = &entries[j];
1021 pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
1022 pivot_value_new_number (e->x));
1026 pivot_table_submit (table);
1029 /* Show the descriptives table */
1031 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1035 const struct categoricals *cats = covariance_get_categoricals (
1038 struct pivot_table *table = pivot_table_create (N_("Descriptives"));
1039 pivot_table_set_weight_format (table, cmd->wfmt);
1041 const double confidence = 0.95;
1043 struct pivot_dimension *statistics = pivot_dimension_create (
1044 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1045 N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
1047 struct pivot_category *interval = pivot_category_create_group__ (
1049 pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
1050 confidence * 100.0));
1051 pivot_category_create_leaves (interval, N_("Lower Bound"),
1053 pivot_category_create_leaves (statistics->root,
1054 N_("Minimum"), N_("Maximum"));
1056 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1057 table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
1058 indep_var->root->show_label = true;
1060 union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
1061 for (size_t j = 0; j < n; j++)
1062 pivot_category_create_leaf (
1063 indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1064 pivot_category_create_leaf (
1065 indep_var->root, pivot_value_new_text_format (N_("Total")));
1067 struct pivot_dimension *dep_var = pivot_dimension_create (
1068 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1070 const double q = (1.0 - confidence) / 2.0;
1071 for (int v = 0; v < cmd->n_vars; ++v)
1073 int dep_var_idx = pivot_category_create_leaf (
1074 dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1076 struct per_var_ws *pvw = &ws->vws[v];
1077 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1080 for (count = 0; count < categoricals_n_total (cats); ++count)
1082 const struct descriptive_data *dd
1083 = categoricals_get_user_data_by_category (cats, count);
1085 double n, mean, variance;
1086 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1088 double std_dev = sqrt (variance);
1089 double std_error = std_dev / sqrt (n) ;
1090 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1092 double entries[] = {
1097 mean - T * std_error,
1098 mean + T * std_error,
1102 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1103 pivot_table_put3 (table, i, count, dep_var_idx,
1104 pivot_value_new_number (entries[i]));
1107 if (categoricals_is_complete (cats))
1109 double n, mean, variance;
1110 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1113 double std_dev = sqrt (variance);
1114 double std_error = std_dev / sqrt (n) ;
1115 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1117 double entries[] = {
1122 mean - T * std_error,
1123 mean + T * std_error,
1124 ws->dd_total[v]->minimum,
1125 ws->dd_total[v]->maximum,
1127 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1128 pivot_table_put3 (table, i, count, dep_var_idx,
1129 pivot_value_new_number (entries[i]));
1133 pivot_table_submit (table);
1136 /* Show the homogeneity table */
1138 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1140 struct pivot_table *table = pivot_table_create (
1141 N_("Test of Homogeneity of Variances"));
1143 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1144 N_("Levene Statistic"), PIVOT_RC_OTHER,
1145 N_("df1"), PIVOT_RC_INTEGER,
1146 N_("df2"), PIVOT_RC_INTEGER,
1147 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1149 struct pivot_dimension *variables = pivot_dimension_create (
1150 table, PIVOT_AXIS_ROW, N_("Variables"));
1152 for (int v = 0; v < cmd->n_vars; ++v)
1154 int var_idx = pivot_category_create_leaf (
1155 variables->root, pivot_value_new_variable (cmd->vars[v]));
1158 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1160 const struct per_var_ws *pvw = &ws->vws[v];
1161 double df1 = pvw->n_groups - 1;
1162 double df2 = n - pvw->n_groups;
1163 double F = levene_calculate (pvw->nl);
1170 gsl_cdf_fdist_Q (F, df1, df2),
1172 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1173 pivot_table_put2 (table, i, var_idx,
1174 pivot_value_new_number (entries[i]));
1177 pivot_table_submit (table);
1181 /* Show the contrast coefficients table */
1183 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1185 struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1187 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1188 table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1189 indep_var->root->show_label = true;
1191 struct pivot_dimension *contrast = pivot_dimension_create (
1192 table, PIVOT_AXIS_ROW, N_("Contrast"));
1193 contrast->root->show_label = true;
1195 const struct covariance *cov = ws->vws[0].cov;
1197 const struct contrasts_node *cn;
1199 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1201 int contrast_idx = pivot_category_create_leaf (
1202 contrast->root, pivot_value_new_integer (c_num++));
1204 const struct coeff_node *coeffn;
1206 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1208 const struct categoricals *cats = covariance_get_categoricals (cov);
1209 const struct ccase *gcc = categoricals_get_case_by_category (
1213 pivot_category_create_leaf (
1214 indep_var->root, pivot_value_new_var_value (
1215 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1217 pivot_table_put2 (table, indep_idx++, contrast_idx,
1218 pivot_value_new_integer (coeffn->coeff));
1222 pivot_table_submit (table);
1225 /* Show the results of the contrast tests */
1227 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1229 struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1231 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1232 N_("Value of Contrast"), PIVOT_RC_OTHER,
1233 N_("Std. Error"), PIVOT_RC_OTHER,
1234 N_("t"), PIVOT_RC_OTHER,
1235 N_("df"), PIVOT_RC_OTHER,
1236 N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1238 struct pivot_dimension *contrasts = pivot_dimension_create (
1239 table, PIVOT_AXIS_ROW, N_("Contrast"));
1240 contrasts->root->show_label = true;
1241 int n_contrasts = ll_count (&cmd->contrast_list);
1242 for (int i = 1; i <= n_contrasts; i++)
1243 pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1245 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1246 N_("Assume equal variances"),
1247 N_("Does not assume equal variances"));
1249 struct pivot_dimension *variables = pivot_dimension_create (
1250 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1252 for (int v = 0; v < cmd->n_vars; ++v)
1254 const struct per_var_ws *pvw = &ws->vws[v];
1255 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1256 if (!categoricals_is_complete (cats))
1259 int var_idx = pivot_category_create_leaf (
1260 variables->root, pivot_value_new_variable (cmd->vars[v]));
1262 struct contrasts_node *cn;
1263 int contrast_idx = 0;
1264 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1267 /* Note: The calculation of the degrees of freedom in the
1268 "variances not equal" case is painfull!!
1269 The following formula may help to understand it:
1270 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1273 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1279 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1281 double df = grand_n - pvw->n_groups;
1283 double contrast_value = 0.0;
1284 double coef_msq = 0.0;
1285 double sec_vneq = 0.0;
1286 double df_denominator = 0.0;
1287 double df_numerator = 0.0;
1288 struct coeff_node *coeffn;
1290 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1292 const struct descriptive_data *dd
1293 = categoricals_get_user_data_by_category (cats, ci);
1294 const double coef = coeffn->coeff;
1296 double n, mean, variance;
1297 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1299 double winv = variance / n;
1300 contrast_value += coef * mean;
1301 coef_msq += pow2 (coef) / n;
1302 sec_vneq += pow2 (coef) * variance / n;
1303 df_numerator += pow2 (coef) * winv;
1304 df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1308 sec_vneq = sqrt (sec_vneq);
1309 df_numerator = pow2 (df_numerator);
1311 double std_error_contrast = sqrt (pvw->mse * coef_msq);
1312 double T = contrast_value / std_error_contrast;
1313 double T_ne = contrast_value / sec_vneq;
1314 double df_ne = df_numerator / df_denominator;
1325 { 0, 0, contrast_value },
1326 { 1, 0, std_error_contrast },
1329 { 4, 0, 2 * gsl_cdf_tdist_Q (fabs(T), df) },
1330 /* Do not assume equal. */
1331 { 0, 1, contrast_value },
1335 { 4, 1, 2 * gsl_cdf_tdist_Q (fabs(T_ne), df_ne) },
1338 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1340 const struct entry *e = &entries[i];
1342 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1343 pivot_value_new_number (e->x));
1350 pivot_table_submit (table);
1354 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1356 struct pivot_table *table = pivot_table_create__ (
1357 pivot_value_new_user_text_nocopy (xasprintf (
1358 _("Multiple Comparisons (%s)"),
1359 var_to_string (cmd->vars[v]))),
1360 "Multiple Comparisons");
1362 struct pivot_dimension *statistics = pivot_dimension_create (
1363 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1364 N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1365 N_("Std. Error"), PIVOT_RC_OTHER,
1366 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1367 struct pivot_category *interval = pivot_category_create_group__ (
1369 pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1370 (1 - cmd->alpha) * 100.0));
1371 pivot_category_create_leaves (interval,
1372 N_("Lower Bound"), PIVOT_RC_OTHER,
1373 N_("Upper Bound"), PIVOT_RC_OTHER);
1375 struct pivot_dimension *j_family = pivot_dimension_create (
1376 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1377 j_family->root->show_label = true;
1379 struct pivot_dimension *i_family = pivot_dimension_create (
1380 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1381 i_family->root->show_label = true;
1383 const struct per_var_ws *pvw = &ws->vws[v];
1384 const struct categoricals *cat = pvw->cat;
1385 for (int i = 0; i < pvw->n_groups; i++)
1387 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1388 for (int j = 0; j < 2; j++)
1389 pivot_category_create_leaf (
1390 j ? j_family->root : i_family->root,
1391 pivot_value_new_var_value (cmd->indep_var,
1392 case_data (gcc, cmd->indep_var)));
1395 struct pivot_dimension *test = pivot_dimension_create (
1396 table, PIVOT_AXIS_ROW, N_("Test"));
1398 for (int p = 0; p < cmd->n_posthoc; ++p)
1400 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1402 int test_idx = pivot_category_create_leaf (
1403 test->root, pivot_value_new_text (ph->label));
1405 for (int i = 0; i < pvw->n_groups ; ++i)
1407 struct descriptive_data *dd_i
1408 = categoricals_get_user_data_by_category (cat, i);
1409 double weight_i, mean_i, var_i;
1410 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1412 for (int j = 0 ; j < pvw->n_groups; ++j)
1417 struct descriptive_data *dd_j
1418 = categoricals_get_user_data_by_category (cat, j);
1419 double weight_j, mean_j, var_j;
1420 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1422 double std_err = pvw->mse;
1423 std_err *= weight_i + weight_j;
1424 std_err /= weight_i * weight_j;
1425 std_err = sqrt (std_err);
1427 double sig = 2 * multiple_comparison_sig (std_err, pvw,
1429 double half_range = mc_half_range (cmd, pvw, std_err,
1431 double entries[] = {
1435 (mean_i - mean_j) - half_range,
1436 (mean_i - mean_j) + half_range,
1438 for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1439 pivot_table_put4 (table, k, j, i, test_idx,
1440 pivot_value_new_number (entries[k]));
1445 pivot_table_submit (table);