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/commands/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;
99 struct ll_list coefficient_list;
105 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
106 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
107 typedef double p1tail_func (double ts, double df1, double df2);
109 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
127 const struct variable **vars;
129 const struct variable *indep_var;
131 bool descriptive_stats;
132 bool homogeneity_stats;
134 enum missing_type missing_type;
135 enum mv_class exclude;
137 /* List of contrasts */
138 struct ll_list contrast_list;
140 /* The weight variable */
141 const struct variable *wv;
142 const struct fmt_spec wfmt;
144 /* The confidence level for multiple comparisons */
152 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
154 return pvw->n - pvw->n_groups;
158 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
164 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
165 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
167 if (n_i <= 1.0 || n_j <= 1.0)
170 nom = pow2 (var_i/n_i + var_j/n_j);
171 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
176 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)
178 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
181 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)
183 const int m = k * (k - 1) / 2;
184 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
187 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)
189 const double m = k * (k - 1) / 2;
190 double lp = 1.0 - exp (log (1.0 - alpha) / m);
191 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
194 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)
199 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
202 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)
204 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
205 return std_err * sqrt (x);
208 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
210 double n_i, mean_i, var_i;
211 double n_j, mean_j, var_j;
214 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
215 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
217 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
222 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
227 multiple_comparison_sig (double std_err,
228 const struct per_var_ws *pvw,
229 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
230 const struct posthoc *ph)
232 int k = pvw->n_groups;
233 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
234 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
237 return ph->p1f (ts, k - 1, df);
241 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)
243 int k = pvw->n_groups;
244 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
248 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
251 static double tukey_1tailsig (double ts, double df1, double df2)
255 if (df2 < 2 || df1 < 1)
258 twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
260 return twotailedsig / 2.0;
263 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
265 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
268 static double sidak_1tailsig (double ts, double df1, double df2)
270 double ex = (df1 + 1.0) * df1 / 2.0;
271 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
273 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
276 static double bonferroni_1tailsig (double ts, double df1, double df2)
278 const int m = (df1 + 1) * df1 / 2;
280 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
283 return p > 0.5 ? 0.5 : p;
286 static double scheffe_1tailsig (double ts, double df1, double df2)
288 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
292 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
295 double n_i, mean_i, var_i;
296 double n_j, mean_j, var_j;
298 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
299 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
301 ts = (mean_i - mean_j) / std_err;
302 ts = fabs (ts) * sqrt (2.0);
307 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
309 double n_i, mean_i, var_i;
310 double n_j, mean_j, var_j;
312 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
313 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
315 return (mean_i - mean_j) / std_err;
318 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
321 double n_i, mean_i, var_i;
322 double n_j, mean_j, var_j;
324 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
325 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
327 t = (mean_i - mean_j) / std_err;
334 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
338 double n_i, mean_i, var_i;
339 double n_j, mean_j, var_j;
341 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
342 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
344 thing = var_i / n_i + var_j / n_j;
346 thing = sqrt (thing);
348 ts = (mean_i - mean_j) / thing;
355 static const struct posthoc ph_tests [] =
357 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
358 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
359 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
360 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
361 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
362 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
366 struct oneway_workspace
368 /* The number of distinct values of the independent variable, when all
369 missing values are disregarded */
370 int actual_number_of_groups;
372 struct per_var_ws *vws;
374 /* An array of descriptive data. One for each dependent variable */
375 struct descriptive_data **dd_total;
378 /* Routines to show the output tables */
379 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
380 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
381 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
383 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
384 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
388 destroy_coeff_list (struct contrasts_node *coeff_list)
390 struct coeff_node *cn = NULL;
391 struct coeff_node *cnx = NULL;
392 struct ll_list *cl = &coeff_list->coefficient_list;
394 ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
403 oneway_cleanup (struct oneway_spec *cmd)
405 struct contrasts_node *coeff_list = NULL;
406 struct contrasts_node *coeff_next = NULL;
407 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
409 destroy_coeff_list (coeff_list);
418 cmd_oneway (struct lexer *lexer, struct dataset *ds)
420 const struct dictionary *dict = dataset_dict (ds);
421 struct oneway_spec oneway = {
422 .missing_type = MISS_ANALYSIS,
424 .wv = dict_get_weight (dict),
425 .wfmt = dict_get_weight_format (dict),
429 ll_init (&oneway.contrast_list);
430 if (lex_match (lexer, T_SLASH))
432 if (!lex_force_match_id (lexer, "VARIABLES"))
434 lex_match (lexer, T_EQUALS);
437 if (!parse_variables_const (lexer, dict,
438 &oneway.vars, &oneway.n_vars,
439 PV_NO_DUPLICATE | PV_NUMERIC))
442 if (!lex_force_match (lexer, T_BY))
445 oneway.indep_var = parse_variable_const (lexer, dict);
446 if (oneway.indep_var == NULL)
449 while (lex_token (lexer) != T_ENDCMD)
451 lex_match (lexer, T_SLASH);
453 if (lex_match_id (lexer, "STATISTICS"))
455 lex_match (lexer, T_EQUALS);
456 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
458 if (lex_match_id (lexer, "DESCRIPTIVES"))
459 oneway.descriptive_stats = true;
460 else if (lex_match_id (lexer, "HOMOGENEITY"))
461 oneway.homogeneity_stats = true;
464 lex_error_expecting (lexer, "DESCRIPTIVES", "HOMOGENEITY");
469 else if (lex_match_id (lexer, "POSTHOC"))
471 lex_match (lexer, T_EQUALS);
472 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
475 for (size_t p = 0; p < sizeof ph_tests / sizeof *ph_tests; ++p)
476 if (lex_match_id (lexer, ph_tests[p].syntax))
479 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
480 oneway.posthoc[oneway.n_posthoc - 1] = p;
486 if (lex_match_id (lexer, "ALPHA"))
488 if (!lex_force_match (lexer, T_LPAREN)
489 || !lex_force_num (lexer))
491 oneway.alpha = lex_number (lexer);
493 if (!lex_force_match (lexer, T_RPAREN))
498 lex_error (lexer, _("Unknown post hoc analysis method."));
504 else if (lex_match_id (lexer, "CONTRAST"))
506 struct contrasts_node *cl = XZALLOC (struct contrasts_node);
508 struct ll_list *coefficient_list = &cl->coefficient_list;
509 lex_match (lexer, T_EQUALS);
511 ll_init (coefficient_list);
513 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
515 if (!lex_force_num (lexer))
517 destroy_coeff_list (cl);
521 struct coeff_node *cc = xmalloc (sizeof *cc);
522 cc->coeff = lex_number (lexer);
524 ll_push_tail (coefficient_list, &cc->ll);
528 if (ll_count (coefficient_list) <= 0)
530 destroy_coeff_list (cl);
534 ll_push_tail (&oneway.contrast_list, &cl->ll);
536 else if (lex_match_id (lexer, "MISSING"))
538 lex_match (lexer, T_EQUALS);
539 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
541 if (lex_match_id (lexer, "INCLUDE"))
542 oneway.exclude = MV_SYSTEM;
543 else if (lex_match_id (lexer, "EXCLUDE"))
544 oneway.exclude = MV_ANY;
545 else if (lex_match_id (lexer, "LISTWISE"))
546 oneway.missing_type = MISS_LISTWISE;
547 else if (lex_match_id (lexer, "ANALYSIS"))
548 oneway.missing_type = MISS_ANALYSIS;
551 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE",
552 "LISTWISE", "ANALYSIS");
559 lex_error_expecting (lexer, "STATISTICS", "POSTHOC", "CONTRAST",
565 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
566 struct casereader *group;
567 while (casegrouper_get_next_group (grouper, &group))
568 run_oneway (&oneway, group, ds);
569 bool ok = casegrouper_destroy (grouper);
570 ok = proc_commit (ds) && ok;
572 oneway_cleanup (&oneway);
577 oneway_cleanup (&oneway);
582 static struct descriptive_data *
583 dd_create (const struct variable *var)
585 struct descriptive_data *dd = xmalloc (sizeof *dd);
587 dd->mom = moments1_create (MOMENT_VARIANCE);
588 dd->minimum = DBL_MAX;
589 dd->maximum = -DBL_MAX;
596 dd_destroy (struct descriptive_data *dd)
598 moments1_destroy (dd->mom);
603 makeit (const void *aux1, void *aux2 UNUSED)
605 const struct variable *var = aux1;
607 struct descriptive_data *dd = dd_create (var);
613 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
615 struct descriptive_data *dd = user_data;
622 updateit (const void *aux1, void *aux2, void *user_data,
623 const struct ccase *c, double weight)
625 struct descriptive_data *dd = user_data;
627 const struct variable *varp = aux1;
629 const union value *valx = case_data (c, varp);
631 struct descriptive_data *dd_total = aux2;
633 moments1_add (dd->mom, valx->f, weight);
634 if (valx->f < dd->minimum)
635 dd->minimum = valx->f;
637 if (valx->f > dd->maximum)
638 dd->maximum = valx->f;
641 const struct variable *var = dd_total->var;
642 const union value *val = case_data (c, var);
644 moments1_add (dd_total->mom,
648 if (val->f < dd_total->minimum)
649 dd_total->minimum = val->f;
651 if (val->f > dd_total->maximum)
652 dd_total->maximum = val->f;
657 run_oneway (const struct oneway_spec *cmd, struct casereader *input,
658 const struct dataset *ds)
660 struct dictionary *dict = dataset_dict (ds);
661 struct oneway_workspace ws = {
662 .vws = xcalloc (cmd->n_vars, sizeof *ws.vws),
663 .dd_total = XCALLOC (cmd->n_vars, struct descriptive_data *),
666 for (size_t v = 0; v < cmd->n_vars; ++v)
667 ws.dd_total[v] = dd_create (cmd->vars[v]);
669 for (size_t v = 0; v < cmd->n_vars; ++v)
671 static const struct payload payload =
678 ws.vws[v].iact = interaction_create (cmd->indep_var);
679 ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
682 categoricals_set_payload (ws.vws[v].cat, &payload,
683 CONST_CAST (struct variable *, cmd->vars[v]),
687 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
689 cmd->wv, cmd->exclude, true);
690 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
693 output_split_file_values_peek (ds, input);
695 struct taint *taint = taint_clone (casereader_get_taint (input));
696 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
697 cmd->exclude, NULL, NULL);
698 if (cmd->missing_type == MISS_LISTWISE)
699 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
700 cmd->exclude, NULL, NULL);
701 input = casereader_create_filter_weight (input, dict, NULL, NULL);
703 struct casereader *reader = casereader_clone (input);
705 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
707 double w = dict_get_case_weight (dict, c, NULL);
709 for (size_t i = 0; i < cmd->n_vars; ++i)
711 struct per_var_ws *pvw = &ws.vws[i];
712 const struct variable *v = cmd->vars[i];
713 const union value *val = case_data (c, v);
715 if (MISS_ANALYSIS == cmd->missing_type)
717 if (var_is_value_missing (v, val) & cmd->exclude)
721 covariance_accumulate_pass1 (pvw->cov, c);
722 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
725 casereader_destroy (reader);
727 reader = casereader_clone (input);
728 for (; (c = casereader_read (reader)); case_unref (c))
730 double w = dict_get_case_weight (dict, c, NULL);
731 for (size_t i = 0; i < cmd->n_vars; ++i)
733 struct per_var_ws *pvw = &ws.vws[i];
734 const struct variable *v = cmd->vars[i];
735 const union value *val = case_data (c, v);
737 if (MISS_ANALYSIS == cmd->missing_type)
739 if (var_is_value_missing (v, val) & cmd->exclude)
743 covariance_accumulate_pass2 (pvw->cov, c);
744 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
747 casereader_destroy (reader);
749 reader = casereader_clone (input);
750 for (; (c = casereader_read (reader)); case_unref (c))
752 double w = dict_get_case_weight (dict, c, NULL);
754 for (size_t i = 0; i < cmd->n_vars; ++i)
756 struct per_var_ws *pvw = &ws.vws[i];
757 const struct variable *v = cmd->vars[i];
758 const union value *val = case_data (c, v);
760 if (MISS_ANALYSIS == cmd->missing_type)
762 if (var_is_value_missing (v, val) & cmd->exclude)
766 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
769 casereader_destroy (reader);
771 for (size_t v = 0; v < cmd->n_vars; ++v)
773 struct per_var_ws *pvw = &ws.vws[v];
775 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
776 if (!categoricals_sane (cats))
778 msg (MW, _("Dependent variable %s has no non-missing values. "
779 "No analysis for this variable will be done."),
780 var_get_name (cmd->vars[v]));
784 const gsl_matrix *ucm = covariance_calculate_unnormalized (pvw->cov);
786 gsl_matrix *cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
787 gsl_matrix_memcpy (cm, ucm);
789 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
791 pvw->sst = gsl_matrix_get (cm, 0, 0);
795 pvw->sse = gsl_matrix_get (cm, 0, 0);
796 gsl_matrix_free (cm);
798 pvw->ssa = pvw->sst - pvw->sse;
800 pvw->n_groups = categoricals_n_total (cats);
802 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
805 for (size_t v = 0; v < cmd->n_vars; ++v)
807 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
808 if (categoricals_is_complete (cats))
810 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
811 ws.actual_number_of_groups = categoricals_n_total (cats);
814 casereader_destroy (input);
816 if (!taint_has_tainted_successor (taint))
817 output_oneway (cmd, &ws);
819 taint_destroy (taint);
821 for (size_t v = 0; v < cmd->n_vars; ++v)
823 covariance_destroy (ws.vws[v].cov);
824 levene_destroy (ws.vws[v].nl);
825 dd_destroy (ws.dd_total[v]);
826 interaction_destroy (ws.vws[v].iact);
833 static void show_contrast_coeffs (const struct oneway_spec *, const struct oneway_workspace *);
834 static void show_contrast_tests (const struct oneway_spec *, const struct oneway_workspace *);
835 static void show_comparisons (const struct oneway_spec *, const struct oneway_workspace *, int depvar);
838 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
842 /* Check the sanity of the given contrast values */
843 struct contrasts_node *coeff_list = NULL;
844 struct contrasts_node *coeff_next = NULL;
845 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
847 struct coeff_node *cn = NULL;
849 struct ll_list *cl = &coeff_list->coefficient_list;
852 if (ll_count (cl) != ws->actual_number_of_groups)
855 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
856 list_idx, ll_count (cl), ws->actual_number_of_groups);
858 ll_remove (&coeff_list->ll);
859 destroy_coeff_list (coeff_list);
863 ll_for_each (cn, struct coeff_node, ll, cl)
867 msg (SW, _("Coefficients for contrast %zu do not total zero"),
871 if (cmd->descriptive_stats)
872 show_descriptives (cmd, ws);
874 if (cmd->homogeneity_stats)
875 show_homogeneity (cmd, ws);
877 show_anova_table (cmd, ws);
879 if (ll_count (&cmd->contrast_list) > 0)
881 show_contrast_coeffs (cmd, ws);
882 show_contrast_tests (cmd, ws);
886 for (size_t v = 0; v < cmd->n_vars; ++v)
888 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
890 if (categoricals_is_complete (cats))
891 show_comparisons (cmd, ws, v);
896 /* Show the ANOVA table */
898 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
900 struct pivot_table *table = pivot_table_create (N_("ANOVA"));
902 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
903 N_("Sum of Squares"), PIVOT_RC_OTHER,
904 N_("df"), PIVOT_RC_INTEGER,
905 N_("Mean Square"), PIVOT_RC_OTHER,
906 N_("F"), PIVOT_RC_OTHER,
907 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
909 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
910 N_("Between Groups"), N_("Within Groups"),
913 struct pivot_dimension *variables = pivot_dimension_create (
914 table, PIVOT_AXIS_ROW, N_("Variables"));
916 for (size_t i = 0; i < cmd->n_vars; ++i)
918 int var_idx = pivot_category_create_leaf (
919 variables->root, pivot_value_new_variable (cmd->vars[i]));
921 const struct per_var_ws *pvw = &ws->vws[i];
924 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
926 double df1 = pvw->n_groups - 1;
927 double df2 = n - pvw->n_groups;
928 double msa = pvw->ssa / df1;
929 double F = msa / pvw->mse;
938 /* Sums of Squares. */
942 /* Degrees of Freedom. */
952 { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
954 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
956 const struct entry *e = &entries[j];
957 pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
958 pivot_value_new_number (e->x));
962 pivot_table_submit (table);
965 /* Show the descriptives table */
967 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
971 const struct categoricals *cats = covariance_get_categoricals (
974 struct pivot_table *table = pivot_table_create (N_("Descriptives"));
975 pivot_table_set_weight_format (table, cmd->wfmt);
977 const double confidence = 0.95;
979 struct pivot_dimension *statistics = pivot_dimension_create (
980 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
981 N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
983 struct pivot_category *interval = pivot_category_create_group__ (
985 pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
986 confidence * 100.0));
987 pivot_category_create_leaves (interval, N_("Lower Bound"),
989 pivot_category_create_leaves (statistics->root,
990 N_("Minimum"), N_("Maximum"));
992 struct pivot_dimension *indep_var = pivot_dimension_create__ (
993 table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
994 indep_var->root->show_label = true;
996 union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
997 for (size_t j = 0; j < n; j++)
998 pivot_category_create_leaf (
999 indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1000 pivot_category_create_leaf (
1001 indep_var->root, pivot_value_new_text_format (N_("Total")));
1003 struct pivot_dimension *dep_var = pivot_dimension_create (
1004 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1006 const double q = (1.0 - confidence) / 2.0;
1007 for (int v = 0; v < cmd->n_vars; ++v)
1009 int dep_var_idx = pivot_category_create_leaf (
1010 dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1012 struct per_var_ws *pvw = &ws->vws[v];
1013 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1016 for (count = 0; count < categoricals_n_total (cats); ++count)
1018 const struct descriptive_data *dd
1019 = categoricals_get_user_data_by_category (cats, count);
1021 double n, mean, variance;
1022 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1024 double std_dev = sqrt (variance);
1025 double std_error = std_dev / sqrt (n);
1026 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1028 double entries[] = {
1033 mean - T * std_error,
1034 mean + T * std_error,
1038 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1039 pivot_table_put3 (table, i, count, dep_var_idx,
1040 pivot_value_new_number (entries[i]));
1043 if (categoricals_is_complete (cats))
1045 double n, mean, variance;
1046 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1049 double std_dev = sqrt (variance);
1050 double std_error = std_dev / sqrt (n);
1051 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1053 double entries[] = {
1058 mean - T * std_error,
1059 mean + T * std_error,
1060 ws->dd_total[v]->minimum,
1061 ws->dd_total[v]->maximum,
1063 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1064 pivot_table_put3 (table, i, count, dep_var_idx,
1065 pivot_value_new_number (entries[i]));
1069 pivot_table_submit (table);
1072 /* Show the homogeneity table */
1074 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1076 struct pivot_table *table = pivot_table_create (
1077 N_("Test of Homogeneity of Variances"));
1079 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1080 N_("Levene Statistic"), PIVOT_RC_OTHER,
1081 N_("df1"), PIVOT_RC_INTEGER,
1082 N_("df2"), PIVOT_RC_INTEGER,
1083 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1085 struct pivot_dimension *variables = pivot_dimension_create (
1086 table, PIVOT_AXIS_ROW, N_("Variables"));
1088 for (int v = 0; v < cmd->n_vars; ++v)
1090 int var_idx = pivot_category_create_leaf (
1091 variables->root, pivot_value_new_variable (cmd->vars[v]));
1094 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1096 const struct per_var_ws *pvw = &ws->vws[v];
1097 double df1 = pvw->n_groups - 1;
1098 double df2 = n - pvw->n_groups;
1099 double F = levene_calculate (pvw->nl);
1106 gsl_cdf_fdist_Q (F, df1, df2),
1108 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1109 pivot_table_put2 (table, i, var_idx,
1110 pivot_value_new_number (entries[i]));
1113 pivot_table_submit (table);
1117 /* Show the contrast coefficients table */
1119 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1121 struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1123 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1124 table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1125 indep_var->root->show_label = true;
1127 struct pivot_dimension *contrast = pivot_dimension_create (
1128 table, PIVOT_AXIS_ROW, N_("Contrast"));
1129 contrast->root->show_label = true;
1131 const struct covariance *cov = ws->vws[0].cov;
1133 const struct contrasts_node *cn;
1135 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1137 int contrast_idx = pivot_category_create_leaf (
1138 contrast->root, pivot_value_new_integer (c_num++));
1140 const struct coeff_node *coeffn;
1142 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1144 const struct categoricals *cats = covariance_get_categoricals (cov);
1145 const struct ccase *gcc = categoricals_get_case_by_category (
1149 pivot_category_create_leaf (
1150 indep_var->root, pivot_value_new_var_value (
1151 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1153 pivot_table_put2 (table, indep_idx++, contrast_idx,
1154 pivot_value_new_integer (coeffn->coeff));
1158 pivot_table_submit (table);
1161 /* Show the results of the contrast tests */
1163 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1165 struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1167 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1168 N_("Value of Contrast"), PIVOT_RC_OTHER,
1169 N_("Std. Error"), PIVOT_RC_OTHER,
1170 N_("t"), PIVOT_RC_OTHER,
1171 N_("df"), PIVOT_RC_OTHER,
1172 N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1174 struct pivot_dimension *contrasts = pivot_dimension_create (
1175 table, PIVOT_AXIS_ROW, N_("Contrast"));
1176 contrasts->root->show_label = true;
1177 int n_contrasts = ll_count (&cmd->contrast_list);
1178 for (int i = 1; i <= n_contrasts; i++)
1179 pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1181 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1182 N_("Assume equal variances"),
1183 N_("Does not assume equal variances"));
1185 struct pivot_dimension *variables = pivot_dimension_create (
1186 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1188 for (int v = 0; v < cmd->n_vars; ++v)
1190 const struct per_var_ws *pvw = &ws->vws[v];
1191 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1192 if (!categoricals_is_complete (cats))
1195 int var_idx = pivot_category_create_leaf (
1196 variables->root, pivot_value_new_variable (cmd->vars[v]));
1198 struct contrasts_node *cn;
1199 int contrast_idx = 0;
1200 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1203 /* Note: The calculation of the degrees of freedom in the
1204 "variances not equal" case is painfull!!
1205 The following formula may help to understand it:
1206 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1209 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1215 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1217 double df = grand_n - pvw->n_groups;
1219 double contrast_value = 0.0;
1220 double coef_msq = 0.0;
1221 double sec_vneq = 0.0;
1222 double df_denominator = 0.0;
1223 double df_numerator = 0.0;
1224 struct coeff_node *coeffn;
1226 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1228 const struct descriptive_data *dd
1229 = categoricals_get_user_data_by_category (cats, ci);
1230 const double coef = coeffn->coeff;
1232 double n, mean, variance;
1233 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1235 double winv = variance / n;
1236 contrast_value += coef * mean;
1237 coef_msq += pow2 (coef) / n;
1238 sec_vneq += pow2 (coef) * variance / n;
1239 df_numerator += pow2 (coef) * winv;
1240 df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1244 sec_vneq = sqrt (sec_vneq);
1245 df_numerator = pow2 (df_numerator);
1247 double std_error_contrast = sqrt (pvw->mse * coef_msq);
1248 double T = contrast_value / std_error_contrast;
1249 double T_ne = contrast_value / sec_vneq;
1250 double df_ne = df_numerator / df_denominator;
1261 { 0, 0, contrast_value },
1262 { 1, 0, std_error_contrast },
1265 { 4, 0, 2 * gsl_cdf_tdist_Q (fabs(T), df) },
1266 /* Do not assume equal. */
1267 { 0, 1, contrast_value },
1271 { 4, 1, 2 * gsl_cdf_tdist_Q (fabs(T_ne), df_ne) },
1274 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1276 const struct entry *e = &entries[i];
1278 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1279 pivot_value_new_number (e->x));
1286 pivot_table_submit (table);
1290 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1292 struct pivot_table *table = pivot_table_create__ (
1293 pivot_value_new_user_text_nocopy (xasprintf (
1294 _("Multiple Comparisons (%s)"),
1295 var_to_string (cmd->vars[v]))),
1296 "Multiple Comparisons");
1298 struct pivot_dimension *statistics = pivot_dimension_create (
1299 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1300 N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1301 N_("Std. Error"), PIVOT_RC_OTHER,
1302 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1303 struct pivot_category *interval = pivot_category_create_group__ (
1305 pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1306 (1 - cmd->alpha) * 100.0));
1307 pivot_category_create_leaves (interval,
1308 N_("Lower Bound"), PIVOT_RC_OTHER,
1309 N_("Upper Bound"), PIVOT_RC_OTHER);
1311 struct pivot_dimension *j_family = pivot_dimension_create (
1312 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1313 j_family->root->show_label = true;
1315 struct pivot_dimension *i_family = pivot_dimension_create (
1316 table, PIVOT_AXIS_ROW, N_("(I) Family"));
1317 i_family->root->show_label = true;
1319 const struct per_var_ws *pvw = &ws->vws[v];
1320 const struct categoricals *cat = pvw->cat;
1321 for (int i = 0; i < pvw->n_groups; i++)
1323 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1324 for (int j = 0; j < 2; j++)
1325 pivot_category_create_leaf (
1326 j ? j_family->root : i_family->root,
1327 pivot_value_new_var_value (cmd->indep_var,
1328 case_data (gcc, cmd->indep_var)));
1331 struct pivot_dimension *test = pivot_dimension_create (
1332 table, PIVOT_AXIS_ROW, N_("Test"));
1334 for (int p = 0; p < cmd->n_posthoc; ++p)
1336 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1338 int test_idx = pivot_category_create_leaf (
1339 test->root, pivot_value_new_text (ph->label));
1341 for (int i = 0; i < pvw->n_groups; ++i)
1343 struct descriptive_data *dd_i
1344 = categoricals_get_user_data_by_category (cat, i);
1345 double weight_i, mean_i, var_i;
1346 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1348 for (int j = 0; j < pvw->n_groups; ++j)
1353 struct descriptive_data *dd_j
1354 = categoricals_get_user_data_by_category (cat, j);
1355 double weight_j, mean_j, var_j;
1356 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1358 double std_err = pvw->mse;
1359 std_err *= weight_i + weight_j;
1360 std_err /= weight_i * weight_j;
1361 std_err = sqrt (std_err);
1363 double sig = 2 * multiple_comparison_sig (std_err, pvw,
1365 double half_range = mc_half_range (cmd, pvw, std_err,
1367 double entries[] = {
1371 (mean_i - mean_j) - half_range,
1372 (mean_i - mean_j) + half_range,
1374 for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1375 pivot_table_put4 (table, k, j, i, test_idx,
1376 pivot_value_new_number (entries[k]));
1381 pivot_table_submit (table);