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)
555 destroy_coeff_list (cl);
559 ll_push_tail (&oneway.contrast_list, &cl->ll);
561 else if (lex_match_id (lexer, "MISSING"))
563 lex_match (lexer, T_EQUALS);
564 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
566 if (lex_match_id (lexer, "INCLUDE"))
568 oneway.exclude = MV_SYSTEM;
570 else if (lex_match_id (lexer, "EXCLUDE"))
572 oneway.exclude = MV_ANY;
574 else if (lex_match_id (lexer, "LISTWISE"))
576 oneway.missing_type = MISS_LISTWISE;
578 else if (lex_match_id (lexer, "ANALYSIS"))
580 oneway.missing_type = MISS_ANALYSIS;
584 lex_error (lexer, NULL);
591 lex_error (lexer, NULL);
598 struct casegrouper *grouper;
599 struct casereader *group;
602 grouper = casegrouper_create_splits (proc_open (ds), dict);
603 while (casegrouper_get_next_group (grouper, &group))
604 run_oneway (&oneway, group, ds);
605 ok = casegrouper_destroy (grouper);
606 ok = proc_commit (ds) && ok;
609 oneway_cleanup (&oneway);
614 oneway_cleanup (&oneway);
623 static struct descriptive_data *
624 dd_create (const struct variable *var)
626 struct descriptive_data *dd = xmalloc (sizeof *dd);
628 dd->mom = moments1_create (MOMENT_VARIANCE);
629 dd->minimum = DBL_MAX;
630 dd->maximum = -DBL_MAX;
637 dd_destroy (struct descriptive_data *dd)
639 moments1_destroy (dd->mom);
644 makeit (const void *aux1, void *aux2 UNUSED)
646 const struct variable *var = aux1;
648 struct descriptive_data *dd = dd_create (var);
654 killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
656 struct descriptive_data *dd = user_data;
663 updateit (const void *aux1, void *aux2, void *user_data,
664 const struct ccase *c, double weight)
666 struct descriptive_data *dd = user_data;
668 const struct variable *varp = aux1;
670 const union value *valx = case_data (c, varp);
672 struct descriptive_data *dd_total = aux2;
674 moments1_add (dd->mom, valx->f, weight);
675 if (valx->f < dd->minimum)
676 dd->minimum = valx->f;
678 if (valx->f > dd->maximum)
679 dd->maximum = valx->f;
682 const struct variable *var = dd_total->var;
683 const union value *val = case_data (c, var);
685 moments1_add (dd_total->mom,
689 if (val->f < dd_total->minimum)
690 dd_total->minimum = val->f;
692 if (val->f > dd_total->maximum)
693 dd_total->maximum = val->f;
698 run_oneway (const struct oneway_spec *cmd,
699 struct casereader *input,
700 const struct dataset *ds)
704 struct dictionary *dict = dataset_dict (ds);
705 struct casereader *reader;
708 struct oneway_workspace ws;
710 ws.actual_number_of_groups = 0;
711 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
712 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
714 for (v = 0 ; v < cmd->n_vars; ++v)
715 ws.dd_total[v] = dd_create (cmd->vars[v]);
717 for (v = 0; v < cmd->n_vars; ++v)
719 static const struct payload payload =
727 ws.vws[v].iact = interaction_create (cmd->indep_var);
728 ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
731 categoricals_set_payload (ws.vws[v].cat, &payload,
732 CONST_CAST (struct variable *, cmd->vars[v]),
736 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
738 cmd->wv, cmd->exclude, true);
739 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
742 c = casereader_peek (input, 0);
745 casereader_destroy (input);
748 output_split_file_values (ds, c);
751 taint = taint_clone (casereader_get_taint (input));
753 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
754 cmd->exclude, NULL, NULL);
755 if (cmd->missing_type == MISS_LISTWISE)
756 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
757 cmd->exclude, NULL, NULL);
758 input = casereader_create_filter_weight (input, dict, NULL, NULL);
760 reader = casereader_clone (input);
761 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
764 double w = dict_get_case_weight (dict, c, NULL);
766 for (i = 0; i < cmd->n_vars; ++i)
768 struct per_var_ws *pvw = &ws.vws[i];
769 const struct variable *v = cmd->vars[i];
770 const union value *val = case_data (c, v);
772 if ( MISS_ANALYSIS == cmd->missing_type)
774 if ( var_is_value_missing (v, val, cmd->exclude))
778 covariance_accumulate_pass1 (pvw->cov, c);
779 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
782 casereader_destroy (reader);
784 reader = casereader_clone (input);
785 for ( ; (c = casereader_read (reader) ); case_unref (c))
788 double w = dict_get_case_weight (dict, c, NULL);
789 for (i = 0; i < cmd->n_vars; ++i)
791 struct per_var_ws *pvw = &ws.vws[i];
792 const struct variable *v = cmd->vars[i];
793 const union value *val = case_data (c, v);
795 if ( MISS_ANALYSIS == cmd->missing_type)
797 if ( var_is_value_missing (v, val, cmd->exclude))
801 covariance_accumulate_pass2 (pvw->cov, c);
802 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
805 casereader_destroy (reader);
807 reader = casereader_clone (input);
808 for ( ; (c = casereader_read (reader) ); case_unref (c))
811 double w = dict_get_case_weight (dict, c, NULL);
813 for (i = 0; i < cmd->n_vars; ++i)
815 struct per_var_ws *pvw = &ws.vws[i];
816 const struct variable *v = cmd->vars[i];
817 const union value *val = case_data (c, v);
819 if ( MISS_ANALYSIS == cmd->missing_type)
821 if ( var_is_value_missing (v, val, cmd->exclude))
825 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
828 casereader_destroy (reader);
831 for (v = 0; v < cmd->n_vars; ++v)
833 const gsl_matrix *ucm;
835 struct per_var_ws *pvw = &ws.vws[v];
836 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
837 const bool ok = categoricals_sane (cats);
842 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
843 var_get_name (cmd->vars[v]));
847 ucm = covariance_calculate_unnormalized (pvw->cov);
849 cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
850 gsl_matrix_memcpy (cm, ucm);
852 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
854 pvw->sst = gsl_matrix_get (cm, 0, 0);
858 pvw->sse = gsl_matrix_get (cm, 0, 0);
859 gsl_matrix_free (cm);
861 pvw->ssa = pvw->sst - pvw->sse;
863 pvw->n_groups = categoricals_n_total (cats);
865 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
868 for (v = 0; v < cmd->n_vars; ++v)
870 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
872 if ( ! categoricals_is_complete (cats))
877 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
878 ws.actual_number_of_groups = categoricals_n_total (cats);
881 casereader_destroy (input);
883 if (!taint_has_tainted_successor (taint))
884 output_oneway (cmd, &ws);
886 taint_destroy (taint);
890 for (v = 0; v < cmd->n_vars; ++v)
892 covariance_destroy (ws.vws[v].cov);
893 levene_destroy (ws.vws[v].nl);
894 dd_destroy (ws.dd_total[v]);
895 interaction_destroy (ws.vws[v].iact);
902 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
903 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
904 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
907 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
911 /* Check the sanity of the given contrast values */
912 struct contrasts_node *coeff_list = NULL;
913 struct contrasts_node *coeff_next = NULL;
914 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
916 struct coeff_node *cn = NULL;
918 struct ll_list *cl = &coeff_list->coefficient_list;
921 if (ll_count (cl) != ws->actual_number_of_groups)
924 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
925 i, ll_count (cl), ws->actual_number_of_groups);
927 ll_remove (&coeff_list->ll);
928 destroy_coeff_list (coeff_list);
932 ll_for_each (cn, struct coeff_node, ll, cl)
936 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
939 if (cmd->stats & STATS_DESCRIPTIVES)
940 show_descriptives (cmd, ws);
942 if (cmd->stats & STATS_HOMOGENEITY)
943 show_homogeneity (cmd, ws);
945 show_anova_table (cmd, ws);
947 if (ll_count (&cmd->contrast_list) > 0)
949 show_contrast_coeffs (cmd, ws);
950 show_contrast_tests (cmd, ws);
956 for (v = 0 ; v < cmd->n_vars; ++v)
958 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
960 if ( categoricals_is_complete (cats))
961 show_comparisons (cmd, ws, v);
967 /* Show the ANOVA table */
969 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
971 struct pivot_table *table = pivot_table_create (N_("ANOVA"));
973 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
974 N_("Sum of Squares"), PIVOT_RC_OTHER,
975 N_("df"), PIVOT_RC_INTEGER,
976 N_("Mean Square"), PIVOT_RC_OTHER,
977 N_("F"), PIVOT_RC_OTHER,
978 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
980 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Type"),
981 N_("Between Groups"), N_("Within Groups"),
984 struct pivot_dimension *variables = pivot_dimension_create (
985 table, PIVOT_AXIS_ROW, N_("Variables"));
987 for (size_t i = 0; i < cmd->n_vars; ++i)
989 int var_idx = pivot_category_create_leaf (
990 variables->root, pivot_value_new_variable (cmd->vars[i]));
992 const struct per_var_ws *pvw = &ws->vws[i];
995 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
997 double df1 = pvw->n_groups - 1;
998 double df2 = n - pvw->n_groups;
999 double msa = pvw->ssa / df1;
1000 double F = msa / pvw->mse ;
1009 /* Sums of Squares. */
1013 /* Degrees of Freedom. */
1023 { 4, 0, gsl_cdf_fdist_Q (F, df1, df2) },
1025 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1027 const struct entry *e = &entries[j];
1028 pivot_table_put3 (table, e->stat_idx, e->type_idx, var_idx,
1029 pivot_value_new_number (e->x));
1033 pivot_table_submit (table);
1036 /* Show the descriptives table */
1038 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1042 const struct categoricals *cats = covariance_get_categoricals (
1045 struct pivot_table *table = pivot_table_create (N_("Descriptives"));
1046 pivot_table_set_weight_format (table, cmd->wfmt);
1048 const double confidence = 0.95;
1050 struct pivot_dimension *statistics = pivot_dimension_create (
1051 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1052 N_("N"), PIVOT_RC_COUNT, N_("Mean"), N_("Std. Deviation"),
1054 struct pivot_category *interval = pivot_category_create_group__ (
1056 pivot_value_new_text_format (N_("%g%% Confidence Interval for Mean"),
1057 confidence * 100.0));
1058 pivot_category_create_leaves (interval, N_("Lower Bound"),
1060 pivot_category_create_leaves (statistics->root,
1061 N_("Minimum"), N_("Maximum"));
1063 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1064 table, PIVOT_AXIS_ROW, pivot_value_new_variable (cmd->indep_var));
1065 indep_var->root->show_label = true;
1067 union value *values = categoricals_get_var_values (cats, cmd->indep_var, &n);
1068 for (size_t j = 0; j < n; j++)
1069 pivot_category_create_leaf (
1070 indep_var->root, pivot_value_new_var_value (cmd->indep_var, &values[j]));
1071 pivot_category_create_leaf (
1072 indep_var->root, pivot_value_new_text_format (N_("Total")));
1074 struct pivot_dimension *dep_var = pivot_dimension_create (
1075 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1077 const double q = (1.0 - confidence) / 2.0;
1078 for (int v = 0; v < cmd->n_vars; ++v)
1080 int dep_var_idx = pivot_category_create_leaf (
1081 dep_var->root, pivot_value_new_variable (cmd->vars[v]));
1083 struct per_var_ws *pvw = &ws->vws[v];
1084 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1087 for (count = 0; count < categoricals_n_total (cats); ++count)
1089 const struct descriptive_data *dd
1090 = categoricals_get_user_data_by_category (cats, count);
1092 double n, mean, variance;
1093 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1095 double std_dev = sqrt (variance);
1096 double std_error = std_dev / sqrt (n) ;
1097 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1099 double entries[] = {
1104 mean - T * std_error,
1105 mean + T * std_error,
1109 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1110 pivot_table_put3 (table, i, count, dep_var_idx,
1111 pivot_value_new_number (entries[i]));
1114 if (categoricals_is_complete (cats))
1116 double n, mean, variance;
1117 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance,
1120 double std_dev = sqrt (variance);
1121 double std_error = std_dev / sqrt (n) ;
1122 double T = gsl_cdf_tdist_Qinv (q, n - 1);
1124 double entries[] = {
1129 mean - T * std_error,
1130 mean + T * std_error,
1131 ws->dd_total[v]->minimum,
1132 ws->dd_total[v]->maximum,
1134 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1135 pivot_table_put3 (table, i, count, dep_var_idx,
1136 pivot_value_new_number (entries[i]));
1140 pivot_table_submit (table);
1143 /* Show the homogeneity table */
1145 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1147 struct pivot_table *table = pivot_table_create (
1148 N_("Test of Homogeneity of Variances"));
1150 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1151 N_("Levene Statistic"), PIVOT_RC_OTHER,
1152 N_("df1"), PIVOT_RC_INTEGER,
1153 N_("df2"), PIVOT_RC_INTEGER,
1154 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1156 struct pivot_dimension *variables = pivot_dimension_create (
1157 table, PIVOT_AXIS_ROW, N_("Variables"));
1159 for (int v = 0; v < cmd->n_vars; ++v)
1161 int var_idx = pivot_category_create_leaf (
1162 variables->root, pivot_value_new_variable (cmd->vars[v]));
1165 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1167 const struct per_var_ws *pvw = &ws->vws[v];
1168 double df1 = pvw->n_groups - 1;
1169 double df2 = n - pvw->n_groups;
1170 double F = levene_calculate (pvw->nl);
1177 gsl_cdf_fdist_Q (F, df1, df2),
1179 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1180 pivot_table_put2 (table, i, var_idx,
1181 pivot_value_new_number (entries[i]));
1184 pivot_table_submit (table);
1188 /* Show the contrast coefficients table */
1190 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1192 struct pivot_table *table = pivot_table_create (N_("Contrast Coefficients"));
1194 struct pivot_dimension *indep_var = pivot_dimension_create__ (
1195 table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (cmd->indep_var));
1196 indep_var->root->show_label = true;
1198 struct pivot_dimension *contrast = pivot_dimension_create (
1199 table, PIVOT_AXIS_ROW, N_("Contrast"));
1200 contrast->root->show_label = true;
1202 const struct covariance *cov = ws->vws[0].cov;
1204 const struct contrasts_node *cn;
1206 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1208 int contrast_idx = pivot_category_create_leaf (
1209 contrast->root, pivot_value_new_integer (c_num++));
1211 const struct coeff_node *coeffn;
1213 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1215 const struct categoricals *cats = covariance_get_categoricals (cov);
1216 const struct ccase *gcc = categoricals_get_case_by_category (
1220 pivot_category_create_leaf (
1221 indep_var->root, pivot_value_new_var_value (
1222 cmd->indep_var, case_data (gcc, cmd->indep_var)));
1224 pivot_table_put2 (table, indep_idx++, contrast_idx,
1225 pivot_value_new_integer (coeffn->coeff));
1229 pivot_table_submit (table);
1232 /* Show the results of the contrast tests */
1234 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1236 struct pivot_table *table = pivot_table_create (N_("Contrast Tests"));
1238 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1239 N_("Value of Contrast"), PIVOT_RC_OTHER,
1240 N_("Std. Error"), PIVOT_RC_OTHER,
1241 N_("t"), PIVOT_RC_OTHER,
1242 N_("df"), PIVOT_RC_OTHER,
1243 N_("Sig. (2-tailed)"), PIVOT_RC_SIGNIFICANCE);
1245 struct pivot_dimension *contrasts = pivot_dimension_create (
1246 table, PIVOT_AXIS_ROW, N_("Contrast"));
1247 contrasts->root->show_label = true;
1248 int n_contrasts = ll_count (&cmd->contrast_list);
1249 for (int i = 1; i <= n_contrasts; i++)
1250 pivot_category_create_leaf (contrasts->root, pivot_value_new_integer (i));
1252 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Assumption"),
1253 N_("Assume equal variances"),
1254 N_("Does not assume equal variances"));
1256 struct pivot_dimension *variables = pivot_dimension_create (
1257 table, PIVOT_AXIS_ROW, N_("Dependent Variable"));
1259 for (int v = 0; v < cmd->n_vars; ++v)
1261 const struct per_var_ws *pvw = &ws->vws[v];
1262 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1263 if (!categoricals_is_complete (cats))
1266 int var_idx = pivot_category_create_leaf (
1267 variables->root, pivot_value_new_variable (cmd->vars[v]));
1269 struct contrasts_node *cn;
1270 int contrast_idx = 0;
1271 ll_for_each (cn, struct contrasts_node, ll, &cmd->contrast_list)
1274 /* Note: The calculation of the degrees of freedom in the
1275 "variances not equal" case is painfull!!
1276 The following formula may help to understand it:
1277 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1280 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1286 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL,
1288 double df = grand_n - pvw->n_groups;
1290 double contrast_value = 0.0;
1291 double coef_msq = 0.0;
1292 double sec_vneq = 0.0;
1293 double df_denominator = 0.0;
1294 double df_numerator = 0.0;
1295 struct coeff_node *coeffn;
1297 ll_for_each (coeffn, struct coeff_node, ll, &cn->coefficient_list)
1299 const struct descriptive_data *dd
1300 = categoricals_get_user_data_by_category (cats, ci);
1301 const double coef = coeffn->coeff;
1303 double n, mean, variance;
1304 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1306 double winv = variance / n;
1307 contrast_value += coef * mean;
1308 coef_msq += pow2 (coef) / n;
1309 sec_vneq += pow2 (coef) * variance / n;
1310 df_numerator += pow2 (coef) * winv;
1311 df_denominator += pow2(pow2 (coef) * winv) / (n - 1);
1315 sec_vneq = sqrt (sec_vneq);
1316 df_numerator = pow2 (df_numerator);
1318 double std_error_contrast = sqrt (pvw->mse * coef_msq);
1319 double T = fabs (contrast_value / std_error_contrast);
1320 double T_ne = contrast_value / sec_vneq;
1321 double df_ne = df_numerator / df_denominator;
1322 double p_ne = gsl_cdf_tdist_P (T_ne, df_ne);
1323 double q_ne = gsl_cdf_tdist_Q (T_ne, df_ne);
1334 { 0, 0, contrast_value },
1335 { 1, 0, std_error_contrast },
1338 { 4, 0, 2 * gsl_cdf_tdist_Q (T, df) },
1339 /* Do not assume equal. */
1340 { 0, 1, contrast_value },
1344 { 4, 1, 2 * (T > 0 ? q_ne : p_ne) },
1347 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1349 const struct entry *e = &entries[i];
1351 table, e->stat_idx, contrast_idx, e->assumption_idx, var_idx,
1352 pivot_value_new_number (e->x));
1359 pivot_table_submit (table);
1363 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1365 struct pivot_table *table = pivot_table_create__ (
1366 pivot_value_new_user_text_nocopy (xasprintf (
1367 _("Multiple Comparisons (%s)"),
1368 var_to_string (cmd->vars[v]))));
1369 table->omit_empty = true;
1371 struct pivot_dimension *statistics = pivot_dimension_create (
1372 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1373 N_("Mean Difference (I - J)"), PIVOT_RC_OTHER,
1374 N_("Std. Error"), PIVOT_RC_OTHER,
1375 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1376 struct pivot_category *interval = pivot_category_create_group__ (
1378 pivot_value_new_text_format (N_("%g%% Confidence Interval"),
1379 (1 - cmd->alpha) * 100.0));
1380 pivot_category_create_leaves (interval,
1381 N_("Lower Bound"), PIVOT_RC_OTHER,
1382 N_("Upper Bound"), PIVOT_RC_OTHER);
1384 struct pivot_dimension *j_family = pivot_dimension_create (
1385 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1386 j_family->root->show_label = true;
1388 struct pivot_dimension *i_family = pivot_dimension_create (
1389 table, PIVOT_AXIS_ROW, N_("(J) Family"));
1390 i_family->root->show_label = true;
1392 const struct per_var_ws *pvw = &ws->vws[v];
1393 const struct categoricals *cat = pvw->cat;
1394 for (int i = 0; i < pvw->n_groups; i++)
1396 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1397 for (int j = 0; j < 2; j++)
1398 pivot_category_create_leaf (
1399 j ? j_family->root : i_family->root,
1400 pivot_value_new_var_value (cmd->indep_var,
1401 case_data (gcc, cmd->indep_var)));
1404 struct pivot_dimension *test = pivot_dimension_create (
1405 table, PIVOT_AXIS_ROW, N_("Test"));
1407 for (int p = 0; p < cmd->n_posthoc; ++p)
1409 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1411 int test_idx = pivot_category_create_leaf (
1412 test->root, pivot_value_new_text (ph->label));
1414 for (int i = 0; i < pvw->n_groups ; ++i)
1416 struct descriptive_data *dd_i
1417 = categoricals_get_user_data_by_category (cat, i);
1418 double weight_i, mean_i, var_i;
1419 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1421 for (int j = 0 ; j < pvw->n_groups; ++j)
1426 struct descriptive_data *dd_j
1427 = categoricals_get_user_data_by_category (cat, j);
1428 double weight_j, mean_j, var_j;
1429 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1431 double std_err = pvw->mse;
1432 std_err *= weight_i + weight_j;
1433 std_err /= weight_i * weight_j;
1434 std_err = sqrt (std_err);
1436 double sig = 2 * multiple_comparison_sig (std_err, pvw,
1438 double half_range = mc_half_range (cmd, pvw, std_err,
1440 double entries[] = {
1444 (mean_i - mean_j) - half_range,
1445 (mean_i - mean_j) + half_range,
1447 for (size_t k = 0; k < sizeof entries / sizeof *entries; k++)
1448 pivot_table_put4 (table, k, j, i, test_idx,
1449 pivot_value_new_number (entries[k]));
1454 pivot_table_submit (table);