1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012 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/>. */
19 #include <gsl/gsl_cdf.h>
20 #include <gsl/gsl_matrix.h>
23 #include "data/case.h"
24 #include "data/casegrouper.h"
25 #include "data/casereader.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/format.h"
29 #include "data/value.h"
30 #include "language/command.h"
31 #include "language/dictionary/split-file.h"
32 #include "language/lexer/lexer.h"
33 #include "language/lexer/value-parser.h"
34 #include "language/lexer/variable-parser.h"
35 #include "libpspp/ll.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "libpspp/taint.h"
39 #include "linreg/sweep.h"
40 #include "tukey/tukey.h"
41 #include "math/categoricals.h"
42 #include "math/interaction.h"
43 #include "math/covariance.h"
44 #include "math/levene.h"
45 #include "math/moments.h"
46 #include "output/tab.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
52 /* Workspace variable for each dependent variable */
55 struct categoricals *cat;
56 struct covariance *cov;
70 /* Per category data */
71 struct descriptive_data
73 const struct variable *var;
88 STATS_DESCRIPTIVES = 0x0001,
89 STATS_HOMOGENEITY = 0x0002
102 struct ll_list coefficient_list;
108 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
109 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
110 typedef double p1tail_func (double ts, double df1, double df2);
112 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
130 const struct variable **vars;
132 const struct variable *indep_var;
134 enum statistics stats;
136 enum missing_type missing_type;
137 enum mv_class exclude;
139 /* List of contrasts */
140 struct ll_list contrast_list;
142 /* The weight variable */
143 const struct variable *wv;
145 /* The confidence level for multiple comparisons */
153 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
155 return pvw->n - pvw->n_groups;
159 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
165 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
166 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
168 if ( n_i <= 1.0 || n_j <= 1.0)
171 nom = pow2 (var_i/n_i + var_j/n_j);
172 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
177 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)
179 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
182 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)
184 const int m = k * (k - 1) / 2;
185 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
188 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)
190 const double m = k * (k - 1) / 2;
191 double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
192 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
195 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)
197 if ( k < 2 || df < 2)
200 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
203 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)
205 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
206 return std_err * sqrt (x);
209 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
211 double n_i, mean_i, var_i;
212 double n_j, mean_j, var_j;
215 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
216 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
218 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
220 if ( k < 2 || df < 2)
223 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
228 multiple_comparison_sig (double std_err,
229 const struct per_var_ws *pvw,
230 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
231 const struct posthoc *ph)
233 int k = pvw->n_groups;
234 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
235 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
238 return ph->p1f (ts, k - 1, df);
242 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)
244 int k = pvw->n_groups;
245 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
249 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
252 static double tukey_1tailsig (double ts, double df1, double df2)
256 if (df2 < 2 || df1 < 1)
259 twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
261 return twotailedsig / 2.0;
264 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
266 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
269 static double sidak_1tailsig (double ts, double df1, double df2)
271 double ex = (df1 + 1.0) * df1 / 2.0;
272 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
274 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
277 static double bonferroni_1tailsig (double ts, double df1, double df2)
279 const int m = (df1 + 1) * df1 / 2;
281 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
284 return p > 0.5 ? 0.5 : p;
287 static double scheffe_1tailsig (double ts, double df1, double df2)
289 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
293 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
296 double n_i, mean_i, var_i;
297 double n_j, mean_j, var_j;
299 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
300 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
302 ts = (mean_i - mean_j) / std_err;
303 ts = fabs (ts) * sqrt (2.0);
308 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
310 double n_i, mean_i, var_i;
311 double n_j, mean_j, var_j;
313 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
314 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
316 return (mean_i - mean_j) / std_err;
319 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
322 double n_i, mean_i, var_i;
323 double n_j, mean_j, var_j;
325 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
326 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
328 t = (mean_i - mean_j) / std_err;
335 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
339 double n_i, mean_i, var_i;
340 double n_j, mean_j, var_j;
342 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
343 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
345 thing = var_i / n_i + var_j / n_j;
347 thing = sqrt (thing);
349 ts = (mean_i - mean_j) / thing;
356 static const struct posthoc ph_tests [] =
358 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
359 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
360 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
361 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
362 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
363 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
367 struct oneway_workspace
369 /* The number of distinct values of the independent variable, when all
370 missing values are disregarded */
371 int actual_number_of_groups;
373 struct per_var_ws *vws;
375 /* An array of descriptive data. One for each dependent variable */
376 struct descriptive_data **dd_total;
379 /* Routines to show the output tables */
380 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
381 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
382 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
384 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
385 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
388 oneway_cleanup (struct oneway_spec *cmd)
390 struct contrasts_node *coeff_list = NULL;
391 struct contrasts_node *coeff_next = NULL;
392 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_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)
412 cmd_oneway (struct lexer *lexer, struct dataset *ds)
414 const struct dictionary *dict = dataset_dict (ds);
415 struct oneway_spec oneway ;
418 oneway.indep_var = NULL;
420 oneway.missing_type = MISS_ANALYSIS;
421 oneway.exclude = MV_ANY;
422 oneway.wv = dict_get_weight (dict);
424 oneway.posthoc = NULL;
425 oneway.n_posthoc = 0;
427 ll_init (&oneway.contrast_list);
430 if ( lex_match (lexer, T_SLASH))
432 if (!lex_force_match_id (lexer, "VARIABLES"))
436 lex_match (lexer, T_EQUALS);
439 if (!parse_variables_const (lexer, dict,
440 &oneway.vars, &oneway.n_vars,
441 PV_NO_DUPLICATE | PV_NUMERIC))
444 lex_force_match (lexer, T_BY);
446 oneway.indep_var = parse_variable_const (lexer, dict);
448 while (lex_token (lexer) != T_ENDCMD)
450 lex_match (lexer, T_SLASH);
452 if (lex_match_id (lexer, "STATISTICS"))
454 lex_match (lexer, T_EQUALS);
455 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
457 if (lex_match_id (lexer, "DESCRIPTIVES"))
459 oneway.stats |= STATS_DESCRIPTIVES;
461 else if (lex_match_id (lexer, "HOMOGENEITY"))
463 oneway.stats |= STATS_HOMOGENEITY;
467 lex_error (lexer, NULL);
472 else if (lex_match_id (lexer, "POSTHOC"))
474 lex_match (lexer, T_EQUALS);
475 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
479 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
481 if (lex_match_id (lexer, ph_tests[p].syntax))
484 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
485 oneway.posthoc[oneway.n_posthoc - 1] = p;
490 if ( method == false)
492 if (lex_match_id (lexer, "ALPHA"))
494 if ( !lex_force_match (lexer, T_LPAREN))
496 lex_force_num (lexer);
497 oneway.alpha = lex_number (lexer);
499 if ( !lex_force_match (lexer, T_RPAREN))
504 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
505 lex_error (lexer, NULL);
511 else if (lex_match_id (lexer, "CONTRAST"))
513 struct contrasts_node *cl = xzalloc (sizeof *cl);
515 struct ll_list *coefficient_list = &cl->coefficient_list;
516 lex_match (lexer, T_EQUALS);
518 ll_init (coefficient_list);
520 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
522 if ( lex_is_number (lexer))
524 struct coeff_node *cc = xmalloc (sizeof *cc);
525 cc->coeff = lex_number (lexer);
527 ll_push_tail (coefficient_list, &cc->ll);
532 lex_error (lexer, NULL);
537 ll_push_tail (&oneway.contrast_list, &cl->ll);
539 else if (lex_match_id (lexer, "MISSING"))
541 lex_match (lexer, T_EQUALS);
542 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
544 if (lex_match_id (lexer, "INCLUDE"))
546 oneway.exclude = MV_SYSTEM;
548 else if (lex_match_id (lexer, "EXCLUDE"))
550 oneway.exclude = MV_ANY;
552 else if (lex_match_id (lexer, "LISTWISE"))
554 oneway.missing_type = MISS_LISTWISE;
556 else if (lex_match_id (lexer, "ANALYSIS"))
558 oneway.missing_type = MISS_ANALYSIS;
562 lex_error (lexer, NULL);
569 lex_error (lexer, NULL);
576 struct casegrouper *grouper;
577 struct casereader *group;
580 grouper = casegrouper_create_splits (proc_open (ds), dict);
581 while (casegrouper_get_next_group (grouper, &group))
582 run_oneway (&oneway, group, ds);
583 ok = casegrouper_destroy (grouper);
584 ok = proc_commit (ds) && ok;
587 oneway_cleanup (&oneway);
592 oneway_cleanup (&oneway);
601 static struct descriptive_data *
602 dd_create (const struct variable *var)
604 struct descriptive_data *dd = xmalloc (sizeof *dd);
606 dd->mom = moments1_create (MOMENT_VARIANCE);
607 dd->minimum = DBL_MAX;
608 dd->maximum = -DBL_MAX;
615 dd_destroy (struct descriptive_data *dd)
617 moments1_destroy (dd->mom);
622 makeit (const void *aux1, void *aux2 UNUSED)
624 const struct variable *var = aux1;
626 struct descriptive_data *dd = dd_create (var);
632 killit (const void *aux1 UNUSED, void *user_data)
634 struct descriptive_data *dd = user_data;
641 updateit (const void *aux1, void *aux2, void *user_data,
642 const struct ccase *c, double weight)
644 struct descriptive_data *dd = user_data;
646 const struct variable *varp = aux1;
648 const union value *valx = case_data (c, varp);
650 struct descriptive_data *dd_total = aux2;
652 moments1_add (dd->mom, valx->f, weight);
653 if (valx->f < dd->minimum)
654 dd->minimum = valx->f;
656 if (valx->f > dd->maximum)
657 dd->maximum = valx->f;
660 const struct variable *var = dd_total->var;
661 const union value *val = case_data (c, var);
663 moments1_add (dd_total->mom,
667 if (val->f < dd_total->minimum)
668 dd_total->minimum = val->f;
670 if (val->f > dd_total->maximum)
671 dd_total->maximum = val->f;
676 run_oneway (const struct oneway_spec *cmd,
677 struct casereader *input,
678 const struct dataset *ds)
682 struct dictionary *dict = dataset_dict (ds);
683 struct casereader *reader;
686 struct oneway_workspace ws;
688 ws.actual_number_of_groups = 0;
689 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
690 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
692 for (v = 0 ; v < cmd->n_vars; ++v)
693 ws.dd_total[v] = dd_create (cmd->vars[v]);
695 for (v = 0; v < cmd->n_vars; ++v)
697 struct interaction *inter = interaction_create (cmd->indep_var);
699 struct payload payload;
700 payload.create = makeit;
701 payload.update = updateit;
702 payload.calculate = NULL;
703 payload.destroy = killit;
705 ws.vws[v].cat = categoricals_create (&inter, 1, cmd->wv,
706 cmd->exclude, cmd->exclude);
708 categoricals_set_payload (ws.vws[v].cat, &payload,
709 CONST_CAST (struct variable *, cmd->vars[v]),
713 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
715 cmd->wv, cmd->exclude);
716 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
719 c = casereader_peek (input, 0);
722 casereader_destroy (input);
725 output_split_file_values (ds, c);
728 taint = taint_clone (casereader_get_taint (input));
730 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
731 cmd->exclude, NULL, NULL);
732 if (cmd->missing_type == MISS_LISTWISE)
733 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
734 cmd->exclude, NULL, NULL);
735 input = casereader_create_filter_weight (input, dict, NULL, NULL);
737 reader = casereader_clone (input);
738 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
741 double w = dict_get_case_weight (dict, c, NULL);
743 for (i = 0; i < cmd->n_vars; ++i)
745 struct per_var_ws *pvw = &ws.vws[i];
746 const struct variable *v = cmd->vars[i];
747 const union value *val = case_data (c, v);
749 if ( MISS_ANALYSIS == cmd->missing_type)
751 if ( var_is_value_missing (v, val, cmd->exclude))
755 covariance_accumulate_pass1 (pvw->cov, c);
756 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
759 casereader_destroy (reader);
761 reader = casereader_clone (input);
762 for ( ; (c = casereader_read (reader) ); case_unref (c))
765 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_pass2 (pvw->cov, c);
779 levene_pass_two (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);
790 for (i = 0; i < cmd->n_vars; ++i)
792 struct per_var_ws *pvw = &ws.vws[i];
793 const struct variable *v = cmd->vars[i];
794 const union value *val = case_data (c, v);
796 if ( MISS_ANALYSIS == cmd->missing_type)
798 if ( var_is_value_missing (v, val, cmd->exclude))
802 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
805 casereader_destroy (reader);
808 for (v = 0; v < cmd->n_vars; ++v)
811 struct per_var_ws *pvw = &ws.vws[v];
812 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
813 const bool ok = categoricals_sane (cats);
818 _("Dependent variable %s has no non-missing values. No analysis for this variable will be done."),
819 var_get_name (cmd->vars[v]));
823 cm = covariance_calculate_unnormalized (pvw->cov);
825 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
827 pvw->sst = gsl_matrix_get (cm, 0, 0);
831 pvw->sse = gsl_matrix_get (cm, 0, 0);
833 pvw->ssa = pvw->sst - pvw->sse;
835 pvw->n_groups = categoricals_n_total (cats);
837 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
839 gsl_matrix_free (cm);
842 for (v = 0; v < cmd->n_vars; ++v)
844 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
846 if ( ! categoricals_is_complete (cats))
851 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
852 ws.actual_number_of_groups = categoricals_n_total (cats);
855 casereader_destroy (input);
857 if (!taint_has_tainted_successor (taint))
858 output_oneway (cmd, &ws);
860 taint_destroy (taint);
864 for (v = 0; v < cmd->n_vars; ++v)
866 covariance_destroy (ws.vws[v].cov);
867 levene_destroy (ws.vws[v].nl);
868 dd_destroy (ws.dd_total[v]);
874 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
875 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
876 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
879 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
883 /* Check the sanity of the given contrast values */
884 struct contrasts_node *coeff_list = NULL;
885 struct contrasts_node *coeff_next = NULL;
886 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
888 struct coeff_node *cn = NULL;
890 struct ll_list *cl = &coeff_list->coefficient_list;
893 if (ll_count (cl) != ws->actual_number_of_groups)
896 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
897 i, ll_count (cl), ws->actual_number_of_groups);
899 ll_remove (&coeff_list->ll);
903 ll_for_each (cn, struct coeff_node, ll, cl)
907 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
910 if (cmd->stats & STATS_DESCRIPTIVES)
911 show_descriptives (cmd, ws);
913 if (cmd->stats & STATS_HOMOGENEITY)
914 show_homogeneity (cmd, ws);
916 show_anova_table (cmd, ws);
918 if (ll_count (&cmd->contrast_list) > 0)
920 show_contrast_coeffs (cmd, ws);
921 show_contrast_tests (cmd, ws);
927 for (v = 0 ; v < cmd->n_vars; ++v)
929 const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
931 if ( categoricals_is_complete (cats))
932 show_comparisons (cmd, ws, v);
938 /* Show the ANOVA table */
940 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
944 size_t n_rows = cmd->n_vars * 3 + 1;
946 struct tab_table *t = tab_create (n_cols, n_rows);
948 tab_headers (t, 2, 0, 1, 0);
954 n_cols - 1, n_rows - 1);
956 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
957 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
958 tab_vline (t, TAL_0, 1, 0, 0);
960 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
961 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
962 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
963 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
964 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
967 for (i = 0; i < cmd->n_vars; ++i)
972 const char *s = var_to_string (cmd->vars[i]);
973 const struct per_var_ws *pvw = &ws->vws[i];
975 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
977 df1 = pvw->n_groups - 1;
978 df2 = n - pvw->n_groups;
979 msa = pvw->ssa / df1;
981 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
982 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
983 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
984 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
987 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
990 /* Sums of Squares */
991 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
992 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
993 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
996 /* Degrees of freedom */
997 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
998 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
999 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
1002 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
1003 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
1006 const double F = msa / pvw->mse ;
1009 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
1011 /* The significance */
1012 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1016 tab_title (t, _("ANOVA"));
1021 /* Show the descriptives table */
1023 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1027 struct tab_table *t;
1030 const double confidence = 0.95;
1031 const double q = (1.0 - confidence) / 2.0;
1033 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
1037 for (v = 0; v < cmd->n_vars; ++v)
1038 n_rows += ws->actual_number_of_groups + 1;
1040 t = tab_create (n_cols, n_rows);
1041 tab_headers (t, 2, 0, 2, 0);
1043 /* Put a frame around the entire box, and vertical lines inside */
1048 n_cols - 1, n_rows - 1);
1050 /* Underline headers */
1051 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1052 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1054 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
1055 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
1056 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1057 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1060 tab_vline (t, TAL_0, 7, 0, 0);
1061 tab_hline (t, TAL_1, 6, 7, 1);
1062 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1063 _("%g%% Confidence Interval for Mean"),
1066 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1067 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1069 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
1070 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
1072 tab_title (t, _("Descriptives"));
1075 for (v = 0; v < cmd->n_vars; ++v)
1077 const char *s = var_to_string (cmd->vars[v]);
1078 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
1082 struct per_var_ws *pvw = &ws->vws[v];
1083 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1085 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
1087 tab_hline (t, TAL_1, 0, n_cols - 1, row);
1089 for (count = 0; count < categoricals_n_total (cats); ++count)
1092 double n, mean, variance;
1093 double std_dev, std_error ;
1097 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1098 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
1100 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1102 std_dev = sqrt (variance);
1103 std_error = std_dev / sqrt (n) ;
1105 ds_init_empty (&vstr);
1107 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1109 tab_text (t, 1, row + count,
1110 TAB_LEFT | TAT_TITLE,
1115 /* Now fill in the numbers ... */
1117 tab_double (t, 2, row + count, 0, n, wfmt);
1119 tab_double (t, 3, row + count, 0, mean, NULL);
1121 tab_double (t, 4, row + count, 0, std_dev, NULL);
1124 tab_double (t, 5, row + count, 0, std_error, NULL);
1126 /* Now the confidence interval */
1128 T = gsl_cdf_tdist_Qinv (q, n - 1);
1130 tab_double (t, 6, row + count, 0,
1131 mean - T * std_error, NULL);
1133 tab_double (t, 7, row + count, 0,
1134 mean + T * std_error, NULL);
1138 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
1139 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
1142 if (categoricals_is_complete (cats))
1145 double n, mean, variance;
1149 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
1151 std_dev = sqrt (variance);
1152 std_error = std_dev / sqrt (n) ;
1154 tab_text (t, 1, row + count,
1155 TAB_LEFT | TAT_TITLE, _("Total"));
1157 tab_double (t, 2, row + count, 0, n, wfmt);
1159 tab_double (t, 3, row + count, 0, mean, NULL);
1161 tab_double (t, 4, row + count, 0, std_dev, NULL);
1163 tab_double (t, 5, row + count, 0, std_error, NULL);
1165 /* Now the confidence interval */
1166 T = gsl_cdf_tdist_Qinv (q, n - 1);
1168 tab_double (t, 6, row + count, 0,
1169 mean - T * std_error, NULL);
1171 tab_double (t, 7, row + count, 0,
1172 mean + T * std_error, NULL);
1176 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
1177 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
1180 row += categoricals_n_total (cats) + 1;
1186 /* Show the homogeneity table */
1188 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1192 size_t n_rows = cmd->n_vars + 1;
1194 struct tab_table *t = tab_create (n_cols, n_rows);
1195 tab_headers (t, 1, 0, 1, 0);
1197 /* Put a frame around the entire box, and vertical lines inside */
1202 n_cols - 1, n_rows - 1);
1205 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1206 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
1208 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
1209 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1210 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1211 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
1213 tab_title (t, _("Test of Homogeneity of Variances"));
1215 for (v = 0; v < cmd->n_vars; ++v)
1218 const struct per_var_ws *pvw = &ws->vws[v];
1219 double F = levene_calculate (pvw->nl);
1221 const struct variable *var = cmd->vars[v];
1222 const char *s = var_to_string (var);
1225 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1227 df1 = pvw->n_groups - 1;
1228 df2 = n - pvw->n_groups;
1230 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1232 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1233 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1234 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1236 /* Now the significance */
1237 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1244 /* Show the contrast coefficients table */
1246 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1251 int n_contrasts = ll_count (&cmd->contrast_list);
1252 int n_cols = 2 + ws->actual_number_of_groups;
1253 int n_rows = 2 + n_contrasts;
1255 struct tab_table *t;
1257 const struct covariance *cov = ws->vws[0].cov ;
1259 t = tab_create (n_cols, n_rows);
1260 tab_headers (t, 2, 0, 2, 0);
1262 /* Put a frame around the entire box, and vertical lines inside */
1267 n_cols - 1, n_rows - 1);
1281 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1282 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1284 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1286 tab_title (t, _("Contrast Coefficients"));
1288 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1291 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1292 var_to_string (cmd->indep_var));
1294 for ( cli = ll_head (&cmd->contrast_list);
1295 cli != ll_null (&cmd->contrast_list);
1296 cli = ll_next (cli))
1299 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1302 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1304 for (coeffi = ll_head (&cn->coefficient_list);
1305 coeffi != ll_null (&cn->coefficient_list);
1306 ++count, coeffi = ll_next (coeffi))
1308 const struct categoricals *cats = covariance_get_categoricals (cov);
1309 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1310 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1313 ds_init_empty (&vstr);
1315 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1317 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1321 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1330 /* Show the results of the contrast tests */
1332 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1334 int n_contrasts = ll_count (&cmd->contrast_list);
1337 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1339 struct tab_table *t;
1341 t = tab_create (n_cols, n_rows);
1342 tab_headers (t, 3, 0, 1, 0);
1344 /* Put a frame around the entire box, and vertical lines inside */
1349 n_cols - 1, n_rows - 1);
1357 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1358 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1360 tab_title (t, _("Contrast Tests"));
1362 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1363 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1364 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1365 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1366 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1367 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1369 for (v = 0; v < cmd->n_vars; ++v)
1371 const struct per_var_ws *pvw = &ws->vws[v];
1372 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1375 int lines_per_variable = 2 * n_contrasts;
1377 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1378 var_to_string (cmd->vars[v]));
1380 for ( cli = ll_head (&cmd->contrast_list);
1381 cli != ll_null (&cmd->contrast_list);
1382 ++i, cli = ll_next (cli))
1384 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1387 double contrast_value = 0.0;
1388 double coef_msq = 0.0;
1391 double std_error_contrast;
1393 double sec_vneq = 0.0;
1395 /* Note: The calculation of the degrees of freedom in the
1396 "variances not equal" case is painfull!!
1397 The following formula may help to understand it:
1398 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1401 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1406 double df_denominator = 0.0;
1407 double df_numerator = 0.0;
1410 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1411 df = grand_n - pvw->n_groups;
1415 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1416 TAB_LEFT | TAT_TITLE,
1417 _("Assume equal variances"));
1419 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1420 TAB_LEFT | TAT_TITLE,
1421 _("Does not assume equal"));
1424 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1425 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1428 tab_text_format (t, 2,
1429 (v * lines_per_variable) + i + 1 + n_contrasts,
1430 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1432 for (coeffi = ll_head (&cn->coefficient_list);
1433 coeffi != ll_null (&cn->coefficient_list);
1434 ++ci, coeffi = ll_next (coeffi))
1436 double n, mean, variance;
1437 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
1438 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1439 const double coef = cn->coeff;
1442 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1444 winv = variance / n;
1446 contrast_value += coef * mean;
1448 coef_msq += (pow2 (coef)) / n;
1450 sec_vneq += (pow2 (coef)) * variance / n;
1452 df_numerator += (pow2 (coef)) * winv;
1453 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1456 sec_vneq = sqrt (sec_vneq);
1458 df_numerator = pow2 (df_numerator);
1460 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1461 TAB_RIGHT, contrast_value, NULL);
1463 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1465 TAB_RIGHT, contrast_value, NULL);
1467 std_error_contrast = sqrt (pvw->mse * coef_msq);
1470 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1471 TAB_RIGHT, std_error_contrast,
1474 T = fabs (contrast_value / std_error_contrast);
1478 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1483 /* Degrees of Freedom */
1484 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1489 /* Significance TWO TAILED !!*/
1490 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1491 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1494 /* Now for the Variances NOT Equal case */
1498 (v * lines_per_variable) + i + 1 + n_contrasts,
1499 TAB_RIGHT, sec_vneq,
1502 T = contrast_value / sec_vneq;
1504 (v * lines_per_variable) + i + 1 + n_contrasts,
1508 df = df_numerator / df_denominator;
1511 (v * lines_per_variable) + i + 1 + n_contrasts,
1515 /* The Significance */
1516 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1517 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1522 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1531 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1533 const int n_cols = 8;
1534 const int heading_rows = 2;
1535 const int heading_cols = 3;
1538 int r = heading_rows ;
1540 const struct per_var_ws *pvw = &ws->vws[v];
1541 const struct categoricals *cat = pvw->cat;
1542 const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
1544 struct tab_table *t = tab_create (n_cols, n_rows);
1546 tab_headers (t, heading_cols, 0, heading_rows, 0);
1548 /* Put a frame around the entire box, and vertical lines inside */
1553 n_cols - 1, n_rows - 1);
1559 n_cols - 1, n_rows - 1);
1561 tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
1563 tab_title (t, _("Multiple Comparisons"));
1565 tab_text_format (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
1566 tab_text_format (t, 2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
1567 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1568 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
1569 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1570 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
1572 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1573 _("%g%% Confidence Interval"),
1574 (1 - cmd->alpha) * 100.0);
1576 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1577 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1580 for (p = 0; p < cmd->n_posthoc; ++p)
1583 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1585 tab_hline (t, TAL_2, 0, n_cols - 1, r);
1587 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
1589 for (i = 0; i < pvw->n_groups ; ++i)
1591 double weight_i, mean_i, var_i;
1595 struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
1596 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1599 ds_init_empty (&vstr);
1600 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1603 tab_hline (t, TAL_1, 1, n_cols - 1, r);
1604 tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1606 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1608 for (j = 0 ; j < pvw->n_groups; ++j)
1611 double weight_j, mean_j, var_j;
1613 const struct ccase *cc;
1614 struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
1619 cc = categoricals_get_case_by_category (cat, j);
1620 var_append_value_name (cmd->indep_var, case_data (cc, cmd->indep_var), &vstr);
1621 tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1623 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1625 tab_double (t, 3, r + rx, 0, mean_i - mean_j, 0);
1628 std_err *= weight_i + weight_j;
1629 std_err /= weight_i * weight_j;
1630 std_err = sqrt (std_err);
1632 tab_double (t, 4, r + rx, 0, std_err, 0);
1634 tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), 0);
1636 half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
1638 tab_double (t, 6, r + rx, 0,
1639 (mean_i - mean_j) - half_range, 0 );
1641 tab_double (t, 7, r + rx, 0,
1642 (mean_i - mean_j) + half_range, 0 );
1647 r += pvw->n_groups - 1;