1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 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/covariance.h"
43 #include "math/levene.h"
44 #include "math/moments.h"
45 #include "output/tab.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
51 /* Workspace variable for each dependent variable */
54 struct categoricals *cat;
55 struct covariance *cov;
69 /* Per category data */
70 struct descriptive_data
72 const struct variable *var;
87 STATS_DESCRIPTIVES = 0x0001,
88 STATS_HOMOGENEITY = 0x0002
101 struct ll_list coefficient_list;
107 typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
108 typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
109 typedef double p1tail_func (double ts, double df1, double df2);
111 typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
129 const struct variable **vars;
131 const struct variable *indep_var;
133 enum statistics stats;
135 enum missing_type missing_type;
136 enum mv_class exclude;
138 /* List of contrasts */
139 struct ll_list contrast_list;
141 /* The weight variable */
142 const struct variable *wv;
144 /* The confidence level for multiple comparisons */
152 df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
154 return pvw->n - pvw->n_groups;
158 df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
164 moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);
165 moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
167 nom = pow2 (var_i/n_i + var_j/n_j);
168 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
173 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)
175 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
178 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)
180 const int m = k * (k - 1) / 2;
181 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
184 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)
186 const double m = k * (k - 1) / 2;
187 double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
188 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
191 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)
193 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
196 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)
198 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
199 return std_err * sqrt (x);
202 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
204 double n_i, mean_i, var_i;
205 double n_j, mean_j, var_j;
208 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
209 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
211 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
213 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
218 multiple_comparison_sig (double std_err,
219 const struct per_var_ws *pvw,
220 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
221 const struct posthoc *ph)
223 int k = pvw->n_groups;
224 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
225 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
226 return ph->p1f (ts, k - 1, df);
230 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)
232 int k = pvw->n_groups;
233 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
235 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
238 static double tukey_1tailsig (double ts, double df1, double df2)
240 double twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
242 return twotailedsig / 2.0;
245 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
247 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
250 static double sidak_1tailsig (double ts, double df1, double df2)
252 double ex = (df1 + 1.0) * df1 / 2.0;
253 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
255 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
258 static double bonferroni_1tailsig (double ts, double df1, double df2)
260 const int m = (df1 + 1) * df1 / 2;
262 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
265 return p > 0.5 ? 0.5 : p;
268 static double scheffe_1tailsig (double ts, double df1, double df2)
270 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
274 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
277 double n_i, mean_i, var_i;
278 double n_j, mean_j, var_j;
280 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
281 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
283 ts = (mean_i - mean_j) / std_err;
284 ts = fabs (ts) * sqrt (2.0);
289 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
291 double n_i, mean_i, var_i;
292 double n_j, mean_j, var_j;
294 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
295 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
297 return (mean_i - mean_j) / std_err;
300 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
303 double n_i, mean_i, var_i;
304 double n_j, mean_j, var_j;
306 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
307 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
309 t = (mean_i - mean_j) / std_err;
316 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
320 double n_i, mean_i, var_i;
321 double n_j, mean_j, var_j;
323 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
324 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
326 thing = var_i / n_i + var_j / n_j;
328 thing = sqrt (thing);
330 ts = (mean_i - mean_j) / thing;
337 static const struct posthoc ph_tests [] =
339 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
340 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
341 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
342 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
343 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
344 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
348 struct oneway_workspace
350 /* The number of distinct values of the independent variable, when all
351 missing values are disregarded */
352 int actual_number_of_groups;
354 struct per_var_ws *vws;
356 /* An array of descriptive data. One for each dependent variable */
357 struct descriptive_data **dd_total;
360 /* Routines to show the output tables */
361 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
362 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
363 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
365 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
366 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
369 cmd_oneway (struct lexer *lexer, struct dataset *ds)
371 const struct dictionary *dict = dataset_dict (ds);
372 struct oneway_spec oneway ;
375 oneway.indep_var = NULL;
377 oneway.missing_type = MISS_ANALYSIS;
378 oneway.exclude = MV_ANY;
379 oneway.wv = dict_get_weight (dict);
381 oneway.posthoc = NULL;
382 oneway.n_posthoc = 0;
384 ll_init (&oneway.contrast_list);
387 if ( lex_match (lexer, T_SLASH))
389 if (!lex_force_match_id (lexer, "VARIABLES"))
393 lex_match (lexer, T_EQUALS);
396 if (!parse_variables_const (lexer, dict,
397 &oneway.vars, &oneway.n_vars,
398 PV_NO_DUPLICATE | PV_NUMERIC))
401 lex_force_match (lexer, T_BY);
403 oneway.indep_var = parse_variable_const (lexer, dict);
405 while (lex_token (lexer) != T_ENDCMD)
407 lex_match (lexer, T_SLASH);
409 if (lex_match_id (lexer, "STATISTICS"))
411 lex_match (lexer, T_EQUALS);
412 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
414 if (lex_match_id (lexer, "DESCRIPTIVES"))
416 oneway.stats |= STATS_DESCRIPTIVES;
418 else if (lex_match_id (lexer, "HOMOGENEITY"))
420 oneway.stats |= STATS_HOMOGENEITY;
424 lex_error (lexer, NULL);
429 else if (lex_match_id (lexer, "POSTHOC"))
431 lex_match (lexer, T_EQUALS);
432 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
436 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
438 if (lex_match_id (lexer, ph_tests[p].syntax))
441 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
442 oneway.posthoc[oneway.n_posthoc - 1] = p;
447 if ( method == false)
449 if (lex_match_id (lexer, "ALPHA"))
451 if ( !lex_force_match (lexer, T_LPAREN))
453 lex_force_num (lexer);
454 oneway.alpha = lex_number (lexer);
456 if ( !lex_force_match (lexer, T_RPAREN))
461 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
462 lex_error (lexer, NULL);
468 else if (lex_match_id (lexer, "CONTRAST"))
470 struct contrasts_node *cl = xzalloc (sizeof *cl);
472 struct ll_list *coefficient_list = &cl->coefficient_list;
473 lex_match (lexer, T_EQUALS);
475 ll_init (coefficient_list);
477 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
479 if ( lex_is_number (lexer))
481 struct coeff_node *cc = xmalloc (sizeof *cc);
482 cc->coeff = lex_number (lexer);
484 ll_push_tail (coefficient_list, &cc->ll);
489 lex_error (lexer, NULL);
494 ll_push_tail (&oneway.contrast_list, &cl->ll);
496 else if (lex_match_id (lexer, "MISSING"))
498 lex_match (lexer, T_EQUALS);
499 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
501 if (lex_match_id (lexer, "INCLUDE"))
503 oneway.exclude = MV_SYSTEM;
505 else if (lex_match_id (lexer, "EXCLUDE"))
507 oneway.exclude = MV_ANY;
509 else if (lex_match_id (lexer, "LISTWISE"))
511 oneway.missing_type = MISS_LISTWISE;
513 else if (lex_match_id (lexer, "ANALYSIS"))
515 oneway.missing_type = MISS_ANALYSIS;
519 lex_error (lexer, NULL);
526 lex_error (lexer, NULL);
533 struct casegrouper *grouper;
534 struct casereader *group;
537 grouper = casegrouper_create_splits (proc_open (ds), dict);
538 while (casegrouper_get_next_group (grouper, &group))
539 run_oneway (&oneway, group, ds);
540 ok = casegrouper_destroy (grouper);
541 ok = proc_commit (ds) && ok;
556 static struct descriptive_data *
557 dd_create (const struct variable *var)
559 struct descriptive_data *dd = xmalloc (sizeof *dd);
561 dd->mom = moments1_create (MOMENT_VARIANCE);
562 dd->minimum = DBL_MAX;
563 dd->maximum = -DBL_MAX;
570 dd_destroy (struct descriptive_data *dd)
572 moments1_destroy (dd->mom);
577 makeit (void *aux1, void *aux2 UNUSED)
579 const struct variable *var = aux1;
581 struct descriptive_data *dd = dd_create (var);
587 updateit (void *user_data,
588 enum mv_class exclude,
589 const struct variable *wv,
590 const struct variable *catvar UNUSED,
591 const struct ccase *c,
592 void *aux1, void *aux2)
594 struct descriptive_data *dd = user_data;
596 const struct variable *varp = aux1;
598 const union value *valx = case_data (c, varp);
600 struct descriptive_data *dd_total = aux2;
604 if ( var_is_value_missing (varp, valx, exclude))
607 weight = wv != NULL ? case_data (c, wv)->f : 1.0;
609 moments1_add (dd->mom, valx->f, weight);
610 if (valx->f < dd->minimum)
611 dd->minimum = valx->f;
613 if (valx->f > dd->maximum)
614 dd->maximum = valx->f;
617 const struct variable *var = dd_total->var;
618 const union value *val = case_data (c, var);
620 moments1_add (dd_total->mom,
624 if (val->f < dd_total->minimum)
625 dd_total->minimum = val->f;
627 if (val->f > dd_total->maximum)
628 dd_total->maximum = val->f;
633 run_oneway (const struct oneway_spec *cmd,
634 struct casereader *input,
635 const struct dataset *ds)
639 struct dictionary *dict = dataset_dict (ds);
640 struct casereader *reader;
643 struct oneway_workspace ws;
645 ws.actual_number_of_groups = 0;
646 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
647 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
649 for (v = 0 ; v < cmd->n_vars; ++v)
650 ws.dd_total[v] = dd_create (cmd->vars[v]);
652 for (v = 0; v < cmd->n_vars; ++v)
654 struct interaction *inter = interaction_create (cmd->indep_var);
655 ws.vws[v].cat = categoricals_create (&inter, 1, cmd->wv,
656 cmd->exclude, makeit, updateit,
657 CONST_CAST (struct variable *,
661 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
663 cmd->wv, cmd->exclude);
664 ws.vws[v].nl = levene_create (var_get_width (cmd->indep_var), NULL);
667 c = casereader_peek (input, 0);
670 casereader_destroy (input);
673 output_split_file_values (ds, c);
676 taint = taint_clone (casereader_get_taint (input));
678 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
679 cmd->exclude, NULL, NULL);
680 if (cmd->missing_type == MISS_LISTWISE)
681 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
682 cmd->exclude, NULL, NULL);
683 input = casereader_create_filter_weight (input, dict, NULL, NULL);
685 reader = casereader_clone (input);
686 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
689 double w = dict_get_case_weight (dict, c, NULL);
691 for (i = 0; i < cmd->n_vars; ++i)
693 struct per_var_ws *pvw = &ws.vws[i];
694 const struct variable *v = cmd->vars[i];
695 const union value *val = case_data (c, v);
697 if ( MISS_ANALYSIS == cmd->missing_type)
699 if ( var_is_value_missing (v, val, cmd->exclude))
703 covariance_accumulate_pass1 (pvw->cov, c);
704 levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
707 casereader_destroy (reader);
709 reader = casereader_clone (input);
710 for ( ; (c = casereader_read (reader) ); case_unref (c))
713 double w = dict_get_case_weight (dict, c, NULL);
714 for (i = 0; i < cmd->n_vars; ++i)
716 struct per_var_ws *pvw = &ws.vws[i];
717 const struct variable *v = cmd->vars[i];
718 const union value *val = case_data (c, v);
720 if ( MISS_ANALYSIS == cmd->missing_type)
722 if ( var_is_value_missing (v, val, cmd->exclude))
726 covariance_accumulate_pass2 (pvw->cov, c);
727 levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
730 casereader_destroy (reader);
732 reader = casereader_clone (input);
733 for ( ; (c = casereader_read (reader) ); case_unref (c))
736 double w = dict_get_case_weight (dict, c, NULL);
738 for (i = 0; i < cmd->n_vars; ++i)
740 struct per_var_ws *pvw = &ws.vws[i];
741 const struct variable *v = cmd->vars[i];
742 const union value *val = case_data (c, v);
744 if ( MISS_ANALYSIS == cmd->missing_type)
746 if ( var_is_value_missing (v, val, cmd->exclude))
750 levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
753 casereader_destroy (reader);
756 for (v = 0; v < cmd->n_vars; ++v)
758 struct per_var_ws *pvw = &ws.vws[v];
759 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
760 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
762 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
764 pvw->sst = gsl_matrix_get (cm, 0, 0);
768 pvw->sse = gsl_matrix_get (cm, 0, 0);
770 pvw->ssa = pvw->sst - pvw->sse;
772 pvw->n_groups = categoricals_total (cats);
774 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
776 gsl_matrix_free (cm);
779 for (v = 0; v < cmd->n_vars; ++v)
781 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
783 categoricals_done (cats);
785 if (categoricals_total (cats) > ws.actual_number_of_groups)
786 ws.actual_number_of_groups = categoricals_total (cats);
789 casereader_destroy (input);
791 if (!taint_has_tainted_successor (taint))
792 output_oneway (cmd, &ws);
794 taint_destroy (taint);
797 for (v = 0; v < cmd->n_vars; ++v)
799 covariance_destroy (ws.vws[v].cov);
800 levene_destroy (ws.vws[v].nl);
801 dd_destroy (ws.dd_total[v]);
807 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
808 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
809 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
812 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
816 /* Check the sanity of the given contrast values */
817 struct contrasts_node *coeff_list = NULL;
818 struct contrasts_node *coeff_next = NULL;
819 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
821 struct coeff_node *cn = NULL;
823 struct ll_list *cl = &coeff_list->coefficient_list;
826 if (ll_count (cl) != ws->actual_number_of_groups)
829 _("In contrast list %zu, the number of coefficients (%d) does not equal the number of groups (%d). This contrast list will be ignored."),
830 i, ll_count (cl), ws->actual_number_of_groups);
832 ll_remove (&coeff_list->ll);
836 ll_for_each (cn, struct coeff_node, ll, cl)
840 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
843 if (cmd->stats & STATS_DESCRIPTIVES)
844 show_descriptives (cmd, ws);
846 if (cmd->stats & STATS_HOMOGENEITY)
847 show_homogeneity (cmd, ws);
849 show_anova_table (cmd, ws);
851 if (ll_count (&cmd->contrast_list) > 0)
853 show_contrast_coeffs (cmd, ws);
854 show_contrast_tests (cmd, ws);
860 for (v = 0 ; v < cmd->n_vars; ++v)
861 show_comparisons (cmd, ws, v);
866 /* Show the ANOVA table */
868 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
872 size_t n_rows = cmd->n_vars * 3 + 1;
874 struct tab_table *t = tab_create (n_cols, n_rows);
876 tab_headers (t, 2, 0, 1, 0);
882 n_cols - 1, n_rows - 1);
884 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
885 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
886 tab_vline (t, TAL_0, 1, 0, 0);
888 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
889 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
890 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
891 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
892 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
895 for (i = 0; i < cmd->n_vars; ++i)
900 const char *s = var_to_string (cmd->vars[i]);
901 const struct per_var_ws *pvw = &ws->vws[i];
903 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
905 df1 = pvw->n_groups - 1;
906 df2 = n - pvw->n_groups;
907 msa = pvw->ssa / df1;
909 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
910 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
911 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
912 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
915 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
918 /* Sums of Squares */
919 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
920 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
921 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
924 /* Degrees of freedom */
925 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
926 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
927 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
930 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
931 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
934 const double F = msa / pvw->mse ;
937 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
939 /* The significance */
940 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
944 tab_title (t, _("ANOVA"));
949 /* Show the descriptives table */
951 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
958 const double confidence = 0.95;
959 const double q = (1.0 - confidence) / 2.0;
961 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
965 for (v = 0; v < cmd->n_vars; ++v)
966 n_rows += ws->actual_number_of_groups + 1;
968 t = tab_create (n_cols, n_rows);
969 tab_headers (t, 2, 0, 2, 0);
971 /* Put a frame around the entire box, and vertical lines inside */
976 n_cols - 1, n_rows - 1);
978 /* Underline headers */
979 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
980 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
982 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
983 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
984 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
985 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
988 tab_vline (t, TAL_0, 7, 0, 0);
989 tab_hline (t, TAL_1, 6, 7, 1);
990 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
991 _("%g%% Confidence Interval for Mean"),
994 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
995 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
997 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
998 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
1000 tab_title (t, _("Descriptives"));
1003 for (v = 0; v < cmd->n_vars; ++v)
1005 const char *s = var_to_string (cmd->vars[v]);
1006 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
1010 struct per_var_ws *pvw = &ws->vws[v];
1011 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1013 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
1015 tab_hline (t, TAL_1, 0, n_cols - 1, row);
1017 for (count = 0; count < categoricals_total (cats); ++count)
1020 double n, mean, variance;
1021 double std_dev, std_error ;
1025 const union value *gval = categoricals_get_value_by_category (cats, count);
1026 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
1028 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1030 std_dev = sqrt (variance);
1031 std_error = std_dev / sqrt (n) ;
1033 ds_init_empty (&vstr);
1035 var_append_value_name (cmd->indep_var, gval, &vstr);
1037 tab_text (t, 1, row + count,
1038 TAB_LEFT | TAT_TITLE,
1043 /* Now fill in the numbers ... */
1045 tab_double (t, 2, row + count, 0, n, wfmt);
1047 tab_double (t, 3, row + count, 0, mean, NULL);
1049 tab_double (t, 4, row + count, 0, std_dev, NULL);
1052 tab_double (t, 5, row + count, 0, std_error, NULL);
1054 /* Now the confidence interval */
1056 T = gsl_cdf_tdist_Qinv (q, n - 1);
1058 tab_double (t, 6, row + count, 0,
1059 mean - T * std_error, NULL);
1061 tab_double (t, 7, row + count, 0,
1062 mean + T * std_error, NULL);
1066 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
1067 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
1072 double n, mean, variance;
1076 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
1078 std_dev = sqrt (variance);
1079 std_error = std_dev / sqrt (n) ;
1081 tab_text (t, 1, row + count,
1082 TAB_LEFT | TAT_TITLE, _("Total"));
1084 tab_double (t, 2, row + count, 0, n, wfmt);
1086 tab_double (t, 3, row + count, 0, mean, NULL);
1088 tab_double (t, 4, row + count, 0, std_dev, NULL);
1090 tab_double (t, 5, row + count, 0, std_error, NULL);
1092 /* Now the confidence interval */
1093 T = gsl_cdf_tdist_Qinv (q, n - 1);
1095 tab_double (t, 6, row + count, 0,
1096 mean - T * std_error, NULL);
1098 tab_double (t, 7, row + count, 0,
1099 mean + T * std_error, NULL);
1102 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
1103 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
1106 row += categoricals_total (cats) + 1;
1112 /* Show the homogeneity table */
1114 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1118 size_t n_rows = cmd->n_vars + 1;
1120 struct tab_table *t = tab_create (n_cols, n_rows);
1121 tab_headers (t, 1, 0, 1, 0);
1123 /* Put a frame around the entire box, and vertical lines inside */
1128 n_cols - 1, n_rows - 1);
1131 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1132 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
1134 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
1135 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1136 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1137 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
1139 tab_title (t, _("Test of Homogeneity of Variances"));
1141 for (v = 0; v < cmd->n_vars; ++v)
1144 const struct per_var_ws *pvw = &ws->vws[v];
1145 double F = levene_calculate (pvw->nl);
1147 const struct variable *var = cmd->vars[v];
1148 const char *s = var_to_string (var);
1151 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1153 df1 = pvw->n_groups - 1;
1154 df2 = n - pvw->n_groups;
1156 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1158 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1159 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1160 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1162 /* Now the significance */
1163 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1170 /* Show the contrast coefficients table */
1172 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1177 int n_contrasts = ll_count (&cmd->contrast_list);
1178 int n_cols = 2 + ws->actual_number_of_groups;
1179 int n_rows = 2 + n_contrasts;
1181 struct tab_table *t;
1183 const struct covariance *cov = ws->vws[0].cov ;
1185 t = tab_create (n_cols, n_rows);
1186 tab_headers (t, 2, 0, 2, 0);
1188 /* Put a frame around the entire box, and vertical lines inside */
1193 n_cols - 1, n_rows - 1);
1207 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1208 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1210 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1212 tab_title (t, _("Contrast Coefficients"));
1214 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1217 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1218 var_to_string (cmd->indep_var));
1220 for ( cli = ll_head (&cmd->contrast_list);
1221 cli != ll_null (&cmd->contrast_list);
1222 cli = ll_next (cli))
1225 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1228 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1230 for (coeffi = ll_head (&cn->coefficient_list);
1231 coeffi != ll_null (&cn->coefficient_list);
1232 ++count, coeffi = ll_next (coeffi))
1234 const struct categoricals *cats = covariance_get_categoricals (cov);
1235 const union value *val = categoricals_get_value_by_category (cats, count);
1236 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1239 ds_init_empty (&vstr);
1241 var_append_value_name (cmd->indep_var, val, &vstr);
1243 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1247 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1256 /* Show the results of the contrast tests */
1258 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1260 int n_contrasts = ll_count (&cmd->contrast_list);
1263 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1265 struct tab_table *t;
1267 t = tab_create (n_cols, n_rows);
1268 tab_headers (t, 3, 0, 1, 0);
1270 /* Put a frame around the entire box, and vertical lines inside */
1275 n_cols - 1, n_rows - 1);
1283 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1284 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1286 tab_title (t, _("Contrast Tests"));
1288 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1289 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1290 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1291 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1292 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1293 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1295 for (v = 0; v < cmd->n_vars; ++v)
1297 const struct per_var_ws *pvw = &ws->vws[v];
1298 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1301 int lines_per_variable = 2 * n_contrasts;
1303 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1304 var_to_string (cmd->vars[v]));
1306 for ( cli = ll_head (&cmd->contrast_list);
1307 cli != ll_null (&cmd->contrast_list);
1308 ++i, cli = ll_next (cli))
1310 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1313 double contrast_value = 0.0;
1314 double coef_msq = 0.0;
1317 double std_error_contrast;
1319 double sec_vneq = 0.0;
1321 /* Note: The calculation of the degrees of freedom in the
1322 "variances not equal" case is painfull!!
1323 The following formula may help to understand it:
1324 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1327 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1332 double df_denominator = 0.0;
1333 double df_numerator = 0.0;
1336 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1337 df = grand_n - pvw->n_groups;
1341 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1342 TAB_LEFT | TAT_TITLE,
1343 _("Assume equal variances"));
1345 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1346 TAB_LEFT | TAT_TITLE,
1347 _("Does not assume equal"));
1350 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1351 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1354 tab_text_format (t, 2,
1355 (v * lines_per_variable) + i + 1 + n_contrasts,
1356 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1358 for (coeffi = ll_head (&cn->coefficient_list);
1359 coeffi != ll_null (&cn->coefficient_list);
1360 ++ci, coeffi = ll_next (coeffi))
1362 double n, mean, variance;
1363 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
1364 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1365 const double coef = cn->coeff;
1368 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1370 winv = variance / n;
1372 contrast_value += coef * mean;
1374 coef_msq += (pow2 (coef)) / n;
1376 sec_vneq += (pow2 (coef)) * variance / n;
1378 df_numerator += (pow2 (coef)) * winv;
1379 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1382 sec_vneq = sqrt (sec_vneq);
1384 df_numerator = pow2 (df_numerator);
1386 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1387 TAB_RIGHT, contrast_value, NULL);
1389 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1391 TAB_RIGHT, contrast_value, NULL);
1393 std_error_contrast = sqrt (pvw->mse * coef_msq);
1396 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1397 TAB_RIGHT, std_error_contrast,
1400 T = fabs (contrast_value / std_error_contrast);
1404 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1409 /* Degrees of Freedom */
1410 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1415 /* Significance TWO TAILED !!*/
1416 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1417 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1420 /* Now for the Variances NOT Equal case */
1424 (v * lines_per_variable) + i + 1 + n_contrasts,
1425 TAB_RIGHT, sec_vneq,
1428 T = contrast_value / sec_vneq;
1430 (v * lines_per_variable) + i + 1 + n_contrasts,
1434 df = df_numerator / df_denominator;
1437 (v * lines_per_variable) + i + 1 + n_contrasts,
1441 /* The Significance */
1442 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1443 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1448 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1457 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1459 const int n_cols = 8;
1460 const int heading_rows = 2;
1461 const int heading_cols = 3;
1464 int r = heading_rows ;
1466 const struct per_var_ws *pvw = &ws->vws[v];
1467 const struct categoricals *cat = pvw->cat;
1468 const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
1470 struct tab_table *t = tab_create (n_cols, n_rows);
1472 tab_headers (t, heading_cols, 0, heading_rows, 0);
1474 /* Put a frame around the entire box, and vertical lines inside */
1479 n_cols - 1, n_rows - 1);
1485 n_cols - 1, n_rows - 1);
1487 tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
1489 tab_title (t, _("Multiple Comparisons"));
1491 tab_text_format (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
1492 tab_text_format (t, 2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
1493 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1494 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
1495 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1496 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
1498 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1499 _("%g%% Confidence Interval"),
1500 (1 - cmd->alpha) * 100.0);
1502 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1503 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1506 for (p = 0; p < cmd->n_posthoc; ++p)
1509 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1511 tab_hline (t, TAL_2, 0, n_cols - 1, r);
1513 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
1515 for (i = 0; i < pvw->n_groups ; ++i)
1517 double weight_i, mean_i, var_i;
1521 struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
1522 const union value *gval = categoricals_get_value_by_category (cat, i);
1524 ds_init_empty (&vstr);
1525 var_append_value_name (cmd->indep_var, gval, &vstr);
1528 tab_hline (t, TAL_1, 1, n_cols - 1, r);
1529 tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1531 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1533 for (j = 0 ; j < pvw->n_groups; ++j)
1536 double weight_j, mean_j, var_j;
1538 struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
1543 gval = categoricals_get_value_by_category (cat, j);
1544 var_append_value_name (cmd->indep_var, gval, &vstr);
1545 tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1547 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1549 tab_double (t, 3, r + rx, 0, mean_i - mean_j, 0);
1552 std_err *= weight_i + weight_j;
1553 std_err /= weight_i * weight_j;
1554 std_err = sqrt (std_err);
1556 tab_double (t, 4, r + rx, 0, std_err, 0);
1558 tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), 0);
1560 half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
1562 tab_double (t, 6, r + rx, 0,
1563 (mean_i - mean_j) - half_range, 0 );
1565 tab_double (t, 7, r + rx, 0,
1566 (mean_i - mean_j) + half_range, 0 );
1571 r += pvw->n_groups - 1;