1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <gsl/gsl_cdf.h>
21 #include <gsl/gsl_matrix.h>
24 #include "data/case.h"
25 #include "data/casegrouper.h"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/format.h"
30 #include "data/value.h"
31 #include "language/command.h"
32 #include "language/dictionary/split-file.h"
33 #include "language/lexer/lexer.h"
34 #include "language/lexer/value-parser.h"
35 #include "language/lexer/variable-parser.h"
36 #include "libpspp/ll.h"
37 #include "libpspp/message.h"
38 #include "libpspp/misc.h"
39 #include "libpspp/taint.h"
40 #include "linreg/sweep.h"
41 #include "tukey/tukey.h"
42 #include "math/categoricals.h"
43 #include "math/interaction.h"
44 #include "math/covariance.h"
45 #include "math/levene.h"
46 #include "math/moments.h"
47 #include "output/pivot-table.h"
50 #define _(msgid) gettext (msgid)
51 #define N_(msgid) msgid
53 /* Workspace variable for each dependent variable */
56 struct interaction *iact;
57 struct categoricals *cat;
58 struct covariance *cov;
72 /* Per category data */
73 struct descriptive_data
75 const struct variable *var;
90 STATS_DESCRIPTIVES = 0x0001,
91 STATS_HOMOGENEITY = 0x0002
101 struct contrasts_node
104 struct ll_list coefficient_list;
110 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
111 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
112 typedef double p1tail_func (double ts, double df1, double df2);
114 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
132 const struct variable **vars;
134 const struct variable *indep_var;
136 enum statistics stats;
138 enum missing_type missing_type;
139 enum mv_class exclude;
141 /* List of contrasts */
142 struct ll_list contrast_list;
144 /* The weight variable */
145 const struct variable *wv;
146 const struct fmt_spec *wfmt;
148 /* The confidence level for multiple comparisons */
156 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
158 return pvw->n - pvw->n_groups;
162 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
168 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
169 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
171 if ( n_i <= 1.0 || n_j <= 1.0)
174 nom = pow2 (var_i/n_i + var_j/n_j);
175 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
180 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)
182 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
185 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)
187 const int m = k * (k - 1) / 2;
188 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
191 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)
193 const double m = k * (k - 1) / 2;
194 double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
195 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
198 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)
200 if ( k < 2 || df < 2)
203 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
206 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)
208 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
209 return std_err * sqrt (x);
212 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
214 double n_i, mean_i, var_i;
215 double n_j, mean_j, var_j;
218 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
219 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
221 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
223 if ( k < 2 || df < 2)
226 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
231 multiple_comparison_sig (double std_err,
232 const struct per_var_ws *pvw,
233 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
234 const struct posthoc *ph)
236 int k = pvw->n_groups;
237 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
238 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
241 return ph->p1f (ts, k - 1, df);
245 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)
247 int k = pvw->n_groups;
248 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
252 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
255 static double tukey_1tailsig (double ts, double df1, double df2)
259 if (df2 < 2 || df1 < 1)
262 twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
264 return twotailedsig / 2.0;
267 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
269 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
272 static double sidak_1tailsig (double ts, double df1, double df2)
274 double ex = (df1 + 1.0) * df1 / 2.0;
275 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
277 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
280 static double bonferroni_1tailsig (double ts, double df1, double df2)
282 const int m = (df1 + 1) * df1 / 2;
284 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
287 return p > 0.5 ? 0.5 : p;
290 static double scheffe_1tailsig (double ts, double df1, double df2)
292 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
296 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
299 double n_i, mean_i, var_i;
300 double n_j, mean_j, var_j;
302 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
303 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
305 ts = (mean_i - mean_j) / std_err;
306 ts = fabs (ts) * sqrt (2.0);
311 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
313 double n_i, mean_i, var_i;
314 double n_j, mean_j, var_j;
316 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
317 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
319 return (mean_i - mean_j) / std_err;
322 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
325 double n_i, mean_i, var_i;
326 double n_j, mean_j, var_j;
328 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
329 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
331 t = (mean_i - mean_j) / std_err;
338 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
342 double n_i, mean_i, var_i;
343 double n_j, mean_j, var_j;
345 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
346 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
348 thing = var_i / n_i + var_j / n_j;
350 thing = sqrt (thing);
352 ts = (mean_i - mean_j) / thing;
359 static const struct posthoc ph_tests [] =
361 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
362 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
363 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
364 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
365 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
366 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
370 struct oneway_workspace
372 /* The number of distinct values of the independent variable, when all
373 missing values are disregarded */
374 int actual_number_of_groups;
376 struct per_var_ws *vws;
378 /* An array of descriptive data. One for each dependent variable */
379 struct descriptive_data **dd_total;
382 /* Routines to show the output tables */
383 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
384 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
385 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
387 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
388 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
392 destroy_coeff_list (struct contrasts_node *coeff_list)
394 struct coeff_node *cn = NULL;
395 struct coeff_node *cnx = NULL;
396 struct ll_list *cl = &coeff_list->coefficient_list;
398 ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
407 oneway_cleanup (struct oneway_spec *cmd)
409 struct contrasts_node *coeff_list = NULL;
410 struct contrasts_node *coeff_next = NULL;
411 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
413 destroy_coeff_list (coeff_list);
422 cmd_oneway (struct lexer *lexer, struct dataset *ds)
424 const struct dictionary *dict = dataset_dict (ds);
425 struct oneway_spec oneway ;
428 oneway.indep_var = NULL;
430 oneway.missing_type = MISS_ANALYSIS;
431 oneway.exclude = MV_ANY;
432 oneway.wv = dict_get_weight (dict);
433 oneway.wfmt = dict_get_weight_format (dict);
435 oneway.posthoc = NULL;
436 oneway.n_posthoc = 0;
438 ll_init (&oneway.contrast_list);
441 if ( lex_match (lexer, T_SLASH))
443 if (!lex_force_match_id (lexer, "VARIABLES"))
447 lex_match (lexer, T_EQUALS);
450 if (!parse_variables_const (lexer, dict,
451 &oneway.vars, &oneway.n_vars,
452 PV_NO_DUPLICATE | PV_NUMERIC))
455 if (!lex_force_match (lexer, T_BY))
458 oneway.indep_var = parse_variable_const (lexer, dict);
459 if (oneway.indep_var == NULL)
462 while (lex_token (lexer) != T_ENDCMD)
464 lex_match (lexer, T_SLASH);
466 if (lex_match_id (lexer, "STATISTICS"))
468 lex_match (lexer, T_EQUALS);
469 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
471 if (lex_match_id (lexer, "DESCRIPTIVES"))
473 oneway.stats |= STATS_DESCRIPTIVES;
475 else if (lex_match_id (lexer, "HOMOGENEITY"))
477 oneway.stats |= STATS_HOMOGENEITY;
481 lex_error (lexer, NULL);
486 else if (lex_match_id (lexer, "POSTHOC"))
488 lex_match (lexer, T_EQUALS);
489 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
493 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
495 if (lex_match_id (lexer, ph_tests[p].syntax))
498 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
499 oneway.posthoc[oneway.n_posthoc - 1] = p;
504 if ( method == false)
506 if (lex_match_id (lexer, "ALPHA"))
508 if ( !lex_force_match (lexer, T_LPAREN))
510 if (! lex_force_num (lexer))
512 oneway.alpha = lex_number (lexer);
514 if ( !lex_force_match (lexer, T_RPAREN))
519 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
520 lex_error (lexer, NULL);
526 else if (lex_match_id (lexer, "CONTRAST"))
528 struct contrasts_node *cl = xzalloc (sizeof *cl);
530 struct ll_list *coefficient_list = &cl->coefficient_list;
531 lex_match (lexer, T_EQUALS);
533 ll_init (coefficient_list);
535 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
537 if ( lex_is_number (lexer))
539 struct coeff_node *cc = xmalloc (sizeof *cc);
540 cc->coeff = lex_number (lexer);
542 ll_push_tail (coefficient_list, &cc->ll);
547 destroy_coeff_list (cl);
548 lex_error (lexer, NULL);
553 if ( ll_count (coefficient_list) <= 0)
556 ll_push_tail (&oneway.contrast_list, &cl->ll);
558 else if (lex_match_id (lexer, "MISSING"))
560 lex_match (lexer, T_EQUALS);
561 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
563 if (lex_match_id (lexer, "INCLUDE"))
565 oneway.exclude = MV_SYSTEM;
567 else if (lex_match_id (lexer, "EXCLUDE"))
569 oneway.exclude = MV_ANY;
571 else if (lex_match_id (lexer, "LISTWISE"))
573 oneway.missing_type = MISS_LISTWISE;
575 else if (lex_match_id (lexer, "ANALYSIS"))
577 oneway.missing_type = MISS_ANALYSIS;
581 lex_error (lexer, NULL);
588 lex_error (lexer, NULL);
595 struct casegrouper *grouper;
596 struct casereader *group;
599 grouper = casegrouper_create_splits (proc_open (ds), dict);
600 while (casegrouper_get_next_group (grouper, &group))
601 run_oneway (&oneway, group, ds);
602 ok = casegrouper_destroy (grouper);
603 ok = proc_commit (ds) && ok;
606 oneway_cleanup (&oneway);
611 oneway_cleanup (&oneway);
620 static struct descriptive_data *
621 dd_create (const struct variable *var)
623 struct descriptive_data *dd = xmalloc (sizeof *dd);
625 dd->mom = moments1_create (MOMENT_VARIANCE);
626 dd->minimum = DBL_MAX;
627 dd->maximum = -DBL_MAX;
634 dd_destroy (struct descriptive_data *dd)
636 moments1_destroy (dd->mom);
641 makeit (const void *aux1, void *aux2 UNUSED)
643 const struct variable *var = aux1;
645 struct descriptive_data *dd = dd_create (var);
651 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
653 struct descriptive_data *dd = user_data;
660 updateit (const void *aux1, void *aux2, void *user_data,
661 const struct ccase *c, double weight)
663 struct descriptive_data *dd = user_data;
665 const struct variable *varp = aux1;
667 const union value *valx = case_data (c, varp);
669 struct descriptive_data *dd_total = aux2;
671 moments1_add (dd->mom, valx->f, weight);
672 if (valx->f < dd->minimum)
673 dd->minimum = valx->f;
675 if (valx->f > dd->maximum)
676 dd->maximum = valx->f;
679 const struct variable *var = dd_total->var;
680 const union value *val = case_data (c, var);
682 moments1_add (dd_total->mom,
686 if (val->f < dd_total->minimum)
687 dd_total->minimum = val->f;
689 if (val->f > dd_total->maximum)
690 dd_total->maximum = val->f;
695 run_oneway (const struct oneway_spec *cmd,
696 struct casereader *input,
697 const struct dataset *ds)
701 struct dictionary *dict = dataset_dict (ds);
702 struct casereader *reader;
705 struct oneway_workspace ws;
707 ws.actual_number_of_groups = 0;
708 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
709 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
711 for (v = 0 ; v < cmd->n_vars; ++v)
712 ws.dd_total[v] = dd_create (cmd->vars[v]);
714 for (v = 0; v < cmd->n_vars; ++v)
716 static const struct payload payload =
724 ws.vws[v].iact = interaction_create (cmd->indep_var);
725 ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
728 categoricals_set_payload (ws.vws[v].cat, &payload,
729 CONST_CAST (struct variable *, cmd->vars[v]),
733 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
735 cmd->wv, cmd->exclude, true);
736 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
739 c = casereader_peek (input, 0);
742 casereader_destroy (input);
745 output_split_file_values (ds, c);
748 taint = taint_clone (casereader_get_taint (input));
750 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
751 cmd->exclude, NULL, NULL);
752 if (cmd->missing_type == MISS_LISTWISE)
753 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
754 cmd->exclude, NULL, NULL);
755 input = casereader_create_filter_weight (input, dict, NULL, NULL);
757 reader = casereader_clone (input);
758 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
761 double w = dict_get_case_weight (dict, c, NULL);
763 for (i = 0; i < cmd->n_vars; ++i)
765 struct per_var_ws *pvw = &ws.vws[i];
766 const struct variable *v = cmd->vars[i];
767 const union value *val = case_data (c, v);
769 if ( MISS_ANALYSIS == cmd->missing_type)
771 if ( var_is_value_missing (v, val, cmd->exclude))
775 covariance_accumulate_pass1 (pvw->cov, c);
776 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
779 casereader_destroy (reader);
781 reader = casereader_clone (input);
782 for ( ; (c = casereader_read (reader) ); case_unref (c))
785 double w = dict_get_case_weight (dict, c, NULL);
786 for (i = 0; i < cmd->n_vars; ++i)
788 struct per_var_ws *pvw = &ws.vws[i];
789 const struct variable *v = cmd->vars[i];
790 const union value *val = case_data (c, v);
792 if ( MISS_ANALYSIS == cmd->missing_type)
794 if ( var_is_value_missing (v, val, cmd->exclude))
798 covariance_accumulate_pass2 (pvw->cov, c);
799 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
802 casereader_destroy (reader);
804 reader = casereader_clone (input);
805 for ( ; (c = casereader_read (reader) ); case_unref (c))
808 double w = dict_get_case_weight (dict, c, NULL);
810 for (i = 0; i < cmd->n_vars; ++i)
812 struct per_var_ws *pvw = &ws.vws[i];
813 const struct variable *v = cmd->vars[i];
814 const union value *val = case_data (c, v);
816 if ( MISS_ANALYSIS == cmd->missing_type)
818 if ( var_is_value_missing (v, val, cmd->exclude))
822 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
825 casereader_destroy (reader);
828 for (v = 0; v < cmd->n_vars; ++v)
830 const gsl_matrix *ucm;
832 struct per_var_ws *pvw = &ws.vws[v];
833 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
834 const bool ok = categoricals_sane (cats);
839 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
840 var_get_name (cmd->vars[v]));
844 ucm = covariance_calculate_unnormalized (pvw->cov);
846 cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
847 gsl_matrix_memcpy (cm, ucm);
849 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
851 pvw->sst = gsl_matrix_get (cm, 0, 0);
855 pvw->sse = gsl_matrix_get (cm, 0, 0);
856 gsl_matrix_free (cm);
858 pvw->ssa = pvw->sst - pvw->sse;
860 pvw->n_groups = categoricals_n_total (cats);
862 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
865 for (v = 0; v < cmd->n_vars; ++v)
867 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
869 if ( ! categoricals_is_complete (cats))
874 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
875 ws.actual_number_of_groups = categoricals_n_total (cats);
878 casereader_destroy (input);
880 if (!taint_has_tainted_successor (taint))
881 output_oneway (cmd, &ws);
883 taint_destroy (taint);
887 for (v = 0; v < cmd->n_vars; ++v)
889 covariance_destroy (ws.vws[v].cov);
890 levene_destroy (ws.vws[v].nl);
891 dd_destroy (ws.dd_total[v]);
892 interaction_destroy (ws.vws[v].iact);
899 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
900 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
901 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
904 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
908 /* Check the sanity of the given contrast values */
909 struct contrasts_node *coeff_list = NULL;
910 struct contrasts_node *coeff_next = NULL;
911 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
913 struct coeff_node *cn = NULL;
915 struct ll_list *cl = &coeff_list->coefficient_list;
918 if (ll_count (cl) != ws->actual_number_of_groups)
921 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
922 i, ll_count (cl), ws->actual_number_of_groups);
924 ll_remove (&coeff_list->ll);
925 destroy_coeff_list (coeff_list);
929 ll_for_each (cn, struct coeff_node, ll, cl)
933 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
936 if (cmd->stats & STATS_DESCRIPTIVES)
937 show_descriptives (cmd, ws);
939 if (cmd->stats & STATS_HOMOGENEITY)
940 show_homogeneity (cmd, ws);
942 show_anova_table (cmd, ws);
944 if (ll_count (&cmd->contrast_list) > 0)
946 show_contrast_coeffs (cmd, ws);
947 show_contrast_tests (cmd, ws);
953 for (v = 0 ; v < cmd->n_vars; ++v)
955 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
957 if ( categoricals_is_complete (cats))
958 show_comparisons (cmd, ws, v);
964 /* Show the ANOVA table */
966 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
968 struct pivot_table *table = pivot_table_create (N_("ANOVA"));
970 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
971 N_("Sum of Squares"), PIVOT_RC_OTHER,
972 N_("df"), PIVOT_RC_INTEGER,
973 N_("Mean Square"), PIVOT_RC_OTHER,
974 N_("F"), PIVOT_RC_OTHER,
975 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
977 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
978 N_("Between Groups"), N_("Within Groups"),
981 struct pivot_dimension *variables = pivot_dimension_create (
982 table, PIVOT_AXIS_ROW, N_("Variables"));
984 for (size_t i = 0; i < cmd->n_vars; ++i)
986 int var_idx = pivot_category_create_leaf (
987 variables->root, pivot_value_new_variable (cmd->vars[i]));
989 const struct per_var_ws *pvw = &ws->vws[i];
992 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
994 double df1 = pvw->n_groups - 1;
995 double df2 = n - pvw->n_groups;
996 double msa = pvw->ssa / df1;
997 double F = msa / pvw->mse ;
1006 /* Sums of Squares. */
1010 /* Degrees of Freedom. */
1020 { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
1022 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1024 const struct entry *e = &entries[j];
1025 pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
1026 pivot_value_new_number (e->x));
1030 pivot_table_submit (table);
1033 /* Show the descriptives table */
1035 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1039 const struct categoricals *cats = covariance_get_categoricals (
1042 struct pivot_table *table = pivot_table_create (N_("Descriptives"));
1043 pivot_table_set_weight_format (table, cmd->wfmt);
1045 const double confidence = 0.95;
1047 struct pivot_dimension *statistics = pivot_dimension_create (
1048 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1049 N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
1051 struct pivot_category *interval = pivot_category_create_group__ (
1053 pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
1054 confidence * 100.0));
1055 pivot_category_create_leaves (interval, N_("Lower Bound"),
1057 pivot_category_create_leaves (statistics->root,
1058 N_("Minimum"), N_("Maximum"));
1060 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1061 table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
1062 indep_var->root->show_label = true;
1064 union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
1065 for (size_t j = 0; j < n; j++)
1066 pivot_category_create_leaf (
1067 indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1068 pivot_category_create_leaf (
1069 indep_var->root, pivot_value_new_text_format (N_("Total")));
1071 struct pivot_dimension *dep_var = pivot_dimension_create (
1072 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1074 const double q = (1.0 - confidence) / 2.0;
1075 for (int v = 0; v < cmd->n_vars; ++v)
1077 int dep_var_idx = pivot_category_create_leaf (
1078 dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1080 struct per_var_ws *pvw = &ws->vws[v];
1081 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1084 for (count = 0; count < categoricals_n_total (cats); ++count)
1086 const struct descriptive_data *dd
1087 = categoricals_get_user_data_by_category (cats, count);
1089 double n, mean, variance;
1090 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1092 double std_dev = sqrt (variance);
1093 double std_error = std_dev / sqrt (n) ;
1094 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1096 double entries[] = {
1101 mean - T * std_error,
1102 mean + T * std_error,
1106 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1107 pivot_table_put3 (table, i, count, dep_var_idx,
1108 pivot_value_new_number (entries[i]));
1111 if (categoricals_is_complete (cats))
1113 double n, mean, variance;
1114 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1117 double std_dev = sqrt (variance);
1118 double std_error = std_dev / sqrt (n) ;
1119 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1121 double entries[] = {
1126 mean - T * std_error,
1127 mean + T * std_error,
1128 ws->dd_total[v]->minimum,
1129 ws->dd_total[v]->maximum,
1131 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1132 pivot_table_put3 (table, i, count, dep_var_idx,
1133 pivot_value_new_number (entries[i]));
1137 pivot_table_submit (table);
1140 /* Show the homogeneity table */
1142 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1144 struct pivot_table *table = pivot_table_create (
1145 N_("Test of Homogeneity of Variances"));
1147 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1148 N_("Levene Statistic"), PIVOT_RC_OTHER,
1149 N_("df1"), PIVOT_RC_INTEGER,
1150 N_("df2"), PIVOT_RC_INTEGER,
1151 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1153 struct pivot_dimension *variables = pivot_dimension_create (
1154 table, PIVOT_AXIS_ROW, N_("Variables"));
1156 for (int v = 0; v < cmd->n_vars; ++v)
1158 int var_idx = pivot_category_create_leaf (
1159 variables->root, pivot_value_new_variable (cmd->vars[v]));
1162 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1164 const struct per_var_ws *pvw = &ws->vws[v];
1165 double df1 = pvw->n_groups - 1;
1166 double df2 = n - pvw->n_groups;
1167 double F = levene_calculate (pvw->nl);
1174 gsl_cdf_fdist_Q (F, df1, df2),
1176 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1177 pivot_table_put2 (table, i, var_idx,
1178 pivot_value_new_number (entries[i]));
1181 pivot_table_submit (table);
1185 /* Show the contrast coefficients table */
1187 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1189 struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1191 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1192 table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1193 indep_var->root->show_label = true;
1195 struct pivot_dimension *contrast = pivot_dimension_create (
1196 table, PIVOT_AXIS_ROW, N_("Contrast"));
1197 contrast->root->show_label = true;
1199 const struct covariance *cov = ws->vws[0].cov;
1201 const struct contrasts_node *cn;
1203 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1205 int contrast_idx = pivot_category_create_leaf (
1206 contrast->root, pivot_value_new_integer (c_num++));
1208 const struct coeff_node *coeffn;
1210 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1212 const struct categoricals *cats = covariance_get_categoricals (cov);
1213 const struct ccase *gcc = categoricals_get_case_by_category (
1217 pivot_category_create_leaf (
1218 indep_var->root, pivot_value_new_var_value (
1219 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1221 pivot_table_put2 (table, indep_idx++, contrast_idx,
1222 pivot_value_new_integer (coeffn->coeff));
1226 pivot_table_submit (table);
1229 /* Show the results of the contrast tests */
1231 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1233 struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1235 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1236 N_("Value of Contrast"), PIVOT_RC_OTHER,
1237 N_("Std. Error"), PIVOT_RC_OTHER,
1238 N_("t"), PIVOT_RC_OTHER,
1239 N_("df"), PIVOT_RC_OTHER,
1240 N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1242 struct pivot_dimension *contrasts = pivot_dimension_create (
1243 table, PIVOT_AXIS_ROW, N_("Contrast"));
1244 contrasts->root->show_label = true;
1245 int n_contrasts = ll_count (&cmd->contrast_list);
1246 for (int i = 1; i <= n_contrasts; i++)
1247 pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1249 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1250 N_("Assume equal variances"),
1251 N_("Does not assume equal variances"));
1253 struct pivot_dimension *variables = pivot_dimension_create (
1254 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1256 for (int v = 0; v < cmd->n_vars; ++v)
1258 const struct per_var_ws *pvw = &ws->vws[v];
1259 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1260 if (!categoricals_is_complete (cats))
1263 int var_idx = pivot_category_create_leaf (
1264 variables->root, pivot_value_new_variable (cmd->vars[v]));
1266 struct contrasts_node *cn;
1267 int contrast_idx = 0;
1268 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1271 /* Note: The calculation of the degrees of freedom in the
1272 "variances not equal" case is painfull!!
1273 The following formula may help to understand it:
1274 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1277 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1283 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1285 double df = grand_n - pvw->n_groups;
1287 double contrast_value = 0.0;
1288 double coef_msq = 0.0;
1289 double sec_vneq = 0.0;
1290 double df_denominator = 0.0;
1291 double df_numerator = 0.0;
1292 struct coeff_node *coeffn;
1294 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1296 const struct descriptive_data *dd
1297 = categoricals_get_user_data_by_category (cats, ci);
1298 const double coef = coeffn->coeff;
1300 double n, mean, variance;
1301 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1303 double winv = variance / n;
1304 contrast_value += coef * mean;
1305 coef_msq += pow2 (coef) / n;
1306 sec_vneq += pow2 (coef) * variance / n;
1307 df_numerator += pow2 (coef) * winv;
1308 df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1312 sec_vneq = sqrt (sec_vneq);
1313 df_numerator = pow2 (df_numerator);
1315 double std_error_contrast = sqrt (pvw->mse * coef_msq);
1316 double T = fabs (contrast_value / std_error_contrast);
1317 double T_ne = contrast_value / sec_vneq;
1318 double df_ne = df_numerator / df_denominator;
1319 double p_ne = gsl_cdf_tdist_P (T_ne, df_ne);
1320 double q_ne = gsl_cdf_tdist_Q (T_ne, df_ne);
1331 { 0, 0, contrast_value },
1332 { 1, 0, std_error_contrast },
1335 { 4, 0, 2 * gsl_cdf_tdist_Q (T, df) },
1336 /* Do not assume equal. */
1337 { 0, 1, contrast_value },
1341 { 4, 1, 2 * (T > 0 ? q_ne : p_ne) },
1344 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1346 const struct entry *e = &entries[i];
1348 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1349 pivot_value_new_number (e->x));
1356 pivot_table_submit (table);
1360 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1362 struct pivot_table *table = pivot_table_create__ (
1363 pivot_value_new_user_text_nocopy (xasprintf (
1364 _("Multiple Comparisons (%s)"),
1365 var_to_string (cmd->vars[v]))));
1366 table->omit_empty = true;
1368 struct pivot_dimension *statistics = pivot_dimension_create (
1369 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1370 N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1371 N_("Std. Error"), PIVOT_RC_OTHER,
1372 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1373 struct pivot_category *interval = pivot_category_create_group__ (
1375 pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1376 (1 - cmd->alpha) * 100.0));
1377 pivot_category_create_leaves (interval,
1378 N_("Lower Bound"), PIVOT_RC_OTHER,
1379 N_("Upper Bound"), PIVOT_RC_OTHER);
1381 struct pivot_dimension *j_family = pivot_dimension_create (
1382 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1383 j_family->root->show_label = true;
1385 struct pivot_dimension *i_family = pivot_dimension_create (
1386 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1387 i_family->root->show_label = true;
1389 const struct per_var_ws *pvw = &ws->vws[v];
1390 const struct categoricals *cat = pvw->cat;
1391 for (int i = 0; i < pvw->n_groups; i++)
1393 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1394 for (int j = 0; j < 2; j++)
1395 pivot_category_create_leaf (
1396 j ? j_family->root : i_family->root,
1397 pivot_value_new_var_value (cmd->indep_var,
1398 case_data (gcc, cmd->indep_var)));
1401 struct pivot_dimension *test = pivot_dimension_create (
1402 table, PIVOT_AXIS_ROW, N_("Test"));
1404 for (int p = 0; p < cmd->n_posthoc; ++p)
1406 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1408 int test_idx = pivot_category_create_leaf (
1409 test->root, pivot_value_new_text (ph->label));
1411 for (int i = 0; i < pvw->n_groups ; ++i)
1413 struct descriptive_data *dd_i
1414 = categoricals_get_user_data_by_category (cat, i);
1415 double weight_i, mean_i, var_i;
1416 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1418 for (int j = 0 ; j < pvw->n_groups; ++j)
1423 struct descriptive_data *dd_j
1424 = categoricals_get_user_data_by_category (cat, j);
1425 double weight_j, mean_j, var_j;
1426 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1428 double std_err = pvw->mse;
1429 std_err *= weight_i + weight_j;
1430 std_err /= weight_i * weight_j;
1431 std_err = sqrt (std_err);
1433 double sig = 2 * multiple_comparison_sig (std_err, pvw,
1435 double half_range = mc_half_range (cmd, pvw, std_err,
1437 double entries[] = {
1441 (mean_i - mean_j) - half_range,
1442 (mean_i - mean_j) + half_range,
1444 for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1445 pivot_table_put4 (table, k, j, i, test_idx,
1446 pivot_value_new_number (entries[k]));
1451 pivot_table_submit (table);