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/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 nom = pow2 (var_i/n_i + var_j/n_j);
169 denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
174 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)
176 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
179 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)
181 const int m = k * (k - 1) / 2;
182 return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
185 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)
187 const double m = k * (k - 1) / 2;
188 double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
189 return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
192 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)
194 return std_err / sqrt (2.0) * qtukey (1 - alpha, 1.0, k, df, 1, 0);
197 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)
199 double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
200 return std_err * sqrt (x);
203 static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
205 double n_i, mean_i, var_i;
206 double n_j, mean_j, var_j;
209 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
210 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
212 m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
214 return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
219 multiple_comparison_sig (double std_err,
220 const struct per_var_ws *pvw,
221 const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
222 const struct posthoc *ph)
224 int k = pvw->n_groups;
225 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
226 double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
227 return ph->p1f (ts, k - 1, df);
231 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)
233 int k = pvw->n_groups;
234 double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
236 return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
239 static double tukey_1tailsig (double ts, double df1, double df2)
241 double twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
243 return twotailedsig / 2.0;
246 static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
248 return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
251 static double sidak_1tailsig (double ts, double df1, double df2)
253 double ex = (df1 + 1.0) * df1 / 2.0;
254 double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
256 return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
259 static double bonferroni_1tailsig (double ts, double df1, double df2)
261 const int m = (df1 + 1) * df1 / 2;
263 double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
266 return p > 0.5 ? 0.5 : p;
269 static double scheffe_1tailsig (double ts, double df1, double df2)
271 return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
275 static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
278 double n_i, mean_i, var_i;
279 double n_j, mean_j, var_j;
281 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
282 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
284 ts = (mean_i - mean_j) / std_err;
285 ts = fabs (ts) * sqrt (2.0);
290 static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
292 double n_i, mean_i, var_i;
293 double n_j, mean_j, var_j;
295 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
296 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
298 return (mean_i - mean_j) / std_err;
301 static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
304 double n_i, mean_i, var_i;
305 double n_j, mean_j, var_j;
307 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
308 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
310 t = (mean_i - mean_j) / std_err;
317 static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
321 double n_i, mean_i, var_i;
322 double n_j, mean_j, var_j;
324 moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);
325 moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
327 thing = var_i / n_i + var_j / n_j;
329 thing = sqrt (thing);
331 ts = (mean_i - mean_j) / thing;
338 static const struct posthoc ph_tests [] =
340 { "LSD", N_("LSD"), df_common, lsd_test_stat, lsd_1tailsig, lsd_pinv},
341 { "TUKEY", N_("Tukey HSD"), df_common, tukey_test_stat, tukey_1tailsig, tukey_pinv},
342 { "BONFERRONI", N_("Bonferroni"), df_common, lsd_test_stat, bonferroni_1tailsig, bonferroni_pinv},
343 { "SCHEFFE", N_("Scheffé"), df_common, scheffe_test_stat, scheffe_1tailsig, scheffe_pinv},
344 { "GH", N_("Games-Howell"), df_individual, gh_test_stat, tukey_1tailsig, gh_pinv},
345 { "SIDAK", N_("Šidák"), df_common, lsd_test_stat, sidak_1tailsig, sidak_pinv}
349 struct oneway_workspace
351 /* The number of distinct values of the independent variable, when all
352 missing values are disregarded */
353 int actual_number_of_groups;
355 struct per_var_ws *vws;
357 /* An array of descriptive data. One for each dependent variable */
358 struct descriptive_data **dd_total;
361 /* Routines to show the output tables */
362 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
363 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
364 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
366 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
367 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
370 cmd_oneway (struct lexer *lexer, struct dataset *ds)
372 const struct dictionary *dict = dataset_dict (ds);
373 struct oneway_spec oneway ;
376 oneway.indep_var = NULL;
378 oneway.missing_type = MISS_ANALYSIS;
379 oneway.exclude = MV_ANY;
380 oneway.wv = dict_get_weight (dict);
382 oneway.posthoc = NULL;
383 oneway.n_posthoc = 0;
385 ll_init (&oneway.contrast_list);
388 if ( lex_match (lexer, T_SLASH))
390 if (!lex_force_match_id (lexer, "VARIABLES"))
394 lex_match (lexer, T_EQUALS);
397 if (!parse_variables_const (lexer, dict,
398 &oneway.vars, &oneway.n_vars,
399 PV_NO_DUPLICATE | PV_NUMERIC))
402 lex_force_match (lexer, T_BY);
404 oneway.indep_var = parse_variable_const (lexer, dict);
406 while (lex_token (lexer) != T_ENDCMD)
408 lex_match (lexer, T_SLASH);
410 if (lex_match_id (lexer, "STATISTICS"))
412 lex_match (lexer, T_EQUALS);
413 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
415 if (lex_match_id (lexer, "DESCRIPTIVES"))
417 oneway.stats |= STATS_DESCRIPTIVES;
419 else if (lex_match_id (lexer, "HOMOGENEITY"))
421 oneway.stats |= STATS_HOMOGENEITY;
425 lex_error (lexer, NULL);
430 else if (lex_match_id (lexer, "POSTHOC"))
432 lex_match (lexer, T_EQUALS);
433 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
437 for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
439 if (lex_match_id (lexer, ph_tests[p].syntax))
442 oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
443 oneway.posthoc[oneway.n_posthoc - 1] = p;
448 if ( method == false)
450 if (lex_match_id (lexer, "ALPHA"))
452 if ( !lex_force_match (lexer, T_LPAREN))
454 lex_force_num (lexer);
455 oneway.alpha = lex_number (lexer);
457 if ( !lex_force_match (lexer, T_RPAREN))
462 msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
463 lex_error (lexer, NULL);
469 else if (lex_match_id (lexer, "CONTRAST"))
471 struct contrasts_node *cl = xzalloc (sizeof *cl);
473 struct ll_list *coefficient_list = &cl->coefficient_list;
474 lex_match (lexer, T_EQUALS);
476 ll_init (coefficient_list);
478 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
480 if ( lex_is_number (lexer))
482 struct coeff_node *cc = xmalloc (sizeof *cc);
483 cc->coeff = lex_number (lexer);
485 ll_push_tail (coefficient_list, &cc->ll);
490 lex_error (lexer, NULL);
495 ll_push_tail (&oneway.contrast_list, &cl->ll);
497 else if (lex_match_id (lexer, "MISSING"))
499 lex_match (lexer, T_EQUALS);
500 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
502 if (lex_match_id (lexer, "INCLUDE"))
504 oneway.exclude = MV_SYSTEM;
506 else if (lex_match_id (lexer, "EXCLUDE"))
508 oneway.exclude = MV_ANY;
510 else if (lex_match_id (lexer, "LISTWISE"))
512 oneway.missing_type = MISS_LISTWISE;
514 else if (lex_match_id (lexer, "ANALYSIS"))
516 oneway.missing_type = MISS_ANALYSIS;
520 lex_error (lexer, NULL);
527 lex_error (lexer, NULL);
534 struct casegrouper *grouper;
535 struct casereader *group;
538 grouper = casegrouper_create_splits (proc_open (ds), dict);
539 while (casegrouper_get_next_group (grouper, &group))
540 run_oneway (&oneway, group, ds);
541 ok = casegrouper_destroy (grouper);
542 ok = proc_commit (ds) && ok;
557 static struct descriptive_data *
558 dd_create (const struct variable *var)
560 struct descriptive_data *dd = xmalloc (sizeof *dd);
562 dd->mom = moments1_create (MOMENT_VARIANCE);
563 dd->minimum = DBL_MAX;
564 dd->maximum = -DBL_MAX;
571 dd_destroy (struct descriptive_data *dd)
573 moments1_destroy (dd->mom);
578 makeit (void *aux1, void *aux2 UNUSED)
580 const struct variable *var = aux1;
582 struct descriptive_data *dd = dd_create (var);
588 updateit (void *user_data,
589 enum mv_class exclude,
590 const struct variable *wv,
591 const struct variable *catvar UNUSED,
592 const struct ccase *c,
593 void *aux1, void *aux2)
595 struct descriptive_data *dd = user_data;
597 const struct variable *varp = aux1;
599 const union value *valx = case_data (c, varp);
601 struct descriptive_data *dd_total = aux2;
605 if ( var_is_value_missing (varp, valx, exclude))
608 weight = wv != NULL ? case_data (c, wv)->f : 1.0;
610 moments1_add (dd->mom, valx->f, weight);
611 if (valx->f < dd->minimum)
612 dd->minimum = valx->f;
614 if (valx->f > dd->maximum)
615 dd->maximum = valx->f;
618 const struct variable *var = dd_total->var;
619 const union value *val = case_data (c, var);
621 moments1_add (dd_total->mom,
625 if (val->f < dd_total->minimum)
626 dd_total->minimum = val->f;
628 if (val->f > dd_total->maximum)
629 dd_total->maximum = val->f;
634 run_oneway (const struct oneway_spec *cmd,
635 struct casereader *input,
636 const struct dataset *ds)
640 struct dictionary *dict = dataset_dict (ds);
641 struct casereader *reader;
644 struct oneway_workspace ws;
646 ws.actual_number_of_groups = 0;
647 ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
648 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
650 for (v = 0 ; v < cmd->n_vars; ++v)
651 ws.dd_total[v] = dd_create (cmd->vars[v]);
653 for (v = 0; v < cmd->n_vars; ++v)
655 struct interaction *inter = interaction_create (cmd->indep_var);
656 ws.vws[v].cat = categoricals_create (&inter, 1, cmd->wv,
657 cmd->exclude, makeit, updateit,
658 CONST_CAST (struct variable *, cmd->vars[v]),
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)
759 struct per_var_ws *pvw = &ws.vws[v];
760 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
761 categoricals_done (cats);
763 cm = covariance_calculate_unnormalized (pvw->cov);
765 moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
767 pvw->sst = gsl_matrix_get (cm, 0, 0);
771 pvw->sse = gsl_matrix_get (cm, 0, 0);
773 pvw->ssa = pvw->sst - pvw->sse;
775 pvw->n_groups = categoricals_n_total (cats);
777 pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
779 gsl_matrix_free (cm);
782 for (v = 0; v < cmd->n_vars; ++v)
784 const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
786 if (categoricals_n_total (cats) > ws.actual_number_of_groups)
787 ws.actual_number_of_groups = categoricals_n_total (cats);
790 casereader_destroy (input);
792 if (!taint_has_tainted_successor (taint))
793 output_oneway (cmd, &ws);
795 taint_destroy (taint);
798 for (v = 0; v < cmd->n_vars; ++v)
800 covariance_destroy (ws.vws[v].cov);
801 levene_destroy (ws.vws[v].nl);
802 dd_destroy (ws.dd_total[v]);
808 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
809 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
810 static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
813 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
817 /* Check the sanity of the given contrast values */
818 struct contrasts_node *coeff_list = NULL;
819 struct contrasts_node *coeff_next = NULL;
820 ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
822 struct coeff_node *cn = NULL;
824 struct ll_list *cl = &coeff_list->coefficient_list;
827 if (ll_count (cl) != ws->actual_number_of_groups)
830 _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
831 i, ll_count (cl), ws->actual_number_of_groups);
833 ll_remove (&coeff_list->ll);
837 ll_for_each (cn, struct coeff_node, ll, cl)
841 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
844 if (cmd->stats & STATS_DESCRIPTIVES)
845 show_descriptives (cmd, ws);
847 if (cmd->stats & STATS_HOMOGENEITY)
848 show_homogeneity (cmd, ws);
850 show_anova_table (cmd, ws);
852 if (ll_count (&cmd->contrast_list) > 0)
854 show_contrast_coeffs (cmd, ws);
855 show_contrast_tests (cmd, ws);
861 for (v = 0 ; v < cmd->n_vars; ++v)
862 show_comparisons (cmd, ws, v);
867 /* Show the ANOVA table */
869 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
873 size_t n_rows = cmd->n_vars * 3 + 1;
875 struct tab_table *t = tab_create (n_cols, n_rows);
877 tab_headers (t, 2, 0, 1, 0);
883 n_cols - 1, n_rows - 1);
885 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
886 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
887 tab_vline (t, TAL_0, 1, 0, 0);
889 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
890 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
891 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
892 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
893 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
896 for (i = 0; i < cmd->n_vars; ++i)
901 const char *s = var_to_string (cmd->vars[i]);
902 const struct per_var_ws *pvw = &ws->vws[i];
904 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
906 df1 = pvw->n_groups - 1;
907 df2 = n - pvw->n_groups;
908 msa = pvw->ssa / df1;
910 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
911 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
912 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
913 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
916 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
919 /* Sums of Squares */
920 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
921 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
922 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
925 /* Degrees of freedom */
926 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
927 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
928 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
931 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
932 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
935 const double F = msa / pvw->mse ;
938 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
940 /* The significance */
941 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
945 tab_title (t, _("ANOVA"));
950 /* Show the descriptives table */
952 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
959 const double confidence = 0.95;
960 const double q = (1.0 - confidence) / 2.0;
962 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
966 for (v = 0; v < cmd->n_vars; ++v)
967 n_rows += ws->actual_number_of_groups + 1;
969 t = tab_create (n_cols, n_rows);
970 tab_headers (t, 2, 0, 2, 0);
972 /* Put a frame around the entire box, and vertical lines inside */
977 n_cols - 1, n_rows - 1);
979 /* Underline headers */
980 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
981 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
983 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
984 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
985 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
986 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
989 tab_vline (t, TAL_0, 7, 0, 0);
990 tab_hline (t, TAL_1, 6, 7, 1);
991 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
992 _("%g%% Confidence Interval for Mean"),
995 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
996 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
998 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
999 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
1001 tab_title (t, _("Descriptives"));
1004 for (v = 0; v < cmd->n_vars; ++v)
1006 const char *s = var_to_string (cmd->vars[v]);
1007 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
1011 struct per_var_ws *pvw = &ws->vws[v];
1012 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1014 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
1016 tab_hline (t, TAL_1, 0, n_cols - 1, row);
1018 for (count = 0; count < categoricals_n_total (cats); ++count)
1021 double n, mean, variance;
1022 double std_dev, std_error ;
1026 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1027 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
1029 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1031 std_dev = sqrt (variance);
1032 std_error = std_dev / sqrt (n) ;
1034 ds_init_empty (&vstr);
1036 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1038 tab_text (t, 1, row + count,
1039 TAB_LEFT | TAT_TITLE,
1044 /* Now fill in the numbers ... */
1046 tab_double (t, 2, row + count, 0, n, wfmt);
1048 tab_double (t, 3, row + count, 0, mean, NULL);
1050 tab_double (t, 4, row + count, 0, std_dev, NULL);
1053 tab_double (t, 5, row + count, 0, std_error, NULL);
1055 /* Now the confidence interval */
1057 T = gsl_cdf_tdist_Qinv (q, n - 1);
1059 tab_double (t, 6, row + count, 0,
1060 mean - T * std_error, NULL);
1062 tab_double (t, 7, row + count, 0,
1063 mean + T * std_error, NULL);
1067 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
1068 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
1073 double n, mean, variance;
1077 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
1079 std_dev = sqrt (variance);
1080 std_error = std_dev / sqrt (n) ;
1082 tab_text (t, 1, row + count,
1083 TAB_LEFT | TAT_TITLE, _("Total"));
1085 tab_double (t, 2, row + count, 0, n, wfmt);
1087 tab_double (t, 3, row + count, 0, mean, NULL);
1089 tab_double (t, 4, row + count, 0, std_dev, NULL);
1091 tab_double (t, 5, row + count, 0, std_error, NULL);
1093 /* Now the confidence interval */
1094 T = gsl_cdf_tdist_Qinv (q, n - 1);
1096 tab_double (t, 6, row + count, 0,
1097 mean - T * std_error, NULL);
1099 tab_double (t, 7, row + count, 0,
1100 mean + T * std_error, NULL);
1103 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
1104 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
1107 row += categoricals_n_total (cats) + 1;
1113 /* Show the homogeneity table */
1115 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1119 size_t n_rows = cmd->n_vars + 1;
1121 struct tab_table *t = tab_create (n_cols, n_rows);
1122 tab_headers (t, 1, 0, 1, 0);
1124 /* Put a frame around the entire box, and vertical lines inside */
1129 n_cols - 1, n_rows - 1);
1132 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1133 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
1135 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
1136 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1137 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1138 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
1140 tab_title (t, _("Test of Homogeneity of Variances"));
1142 for (v = 0; v < cmd->n_vars; ++v)
1145 const struct per_var_ws *pvw = &ws->vws[v];
1146 double F = levene_calculate (pvw->nl);
1148 const struct variable *var = cmd->vars[v];
1149 const char *s = var_to_string (var);
1152 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1154 df1 = pvw->n_groups - 1;
1155 df2 = n - pvw->n_groups;
1157 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1159 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1160 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1161 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1163 /* Now the significance */
1164 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1171 /* Show the contrast coefficients table */
1173 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1178 int n_contrasts = ll_count (&cmd->contrast_list);
1179 int n_cols = 2 + ws->actual_number_of_groups;
1180 int n_rows = 2 + n_contrasts;
1182 struct tab_table *t;
1184 const struct covariance *cov = ws->vws[0].cov ;
1186 t = tab_create (n_cols, n_rows);
1187 tab_headers (t, 2, 0, 2, 0);
1189 /* Put a frame around the entire box, and vertical lines inside */
1194 n_cols - 1, n_rows - 1);
1208 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1209 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1211 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1213 tab_title (t, _("Contrast Coefficients"));
1215 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1218 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1219 var_to_string (cmd->indep_var));
1221 for ( cli = ll_head (&cmd->contrast_list);
1222 cli != ll_null (&cmd->contrast_list);
1223 cli = ll_next (cli))
1226 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1229 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1231 for (coeffi = ll_head (&cn->coefficient_list);
1232 coeffi != ll_null (&cn->coefficient_list);
1233 ++count, coeffi = ll_next (coeffi))
1235 const struct categoricals *cats = covariance_get_categoricals (cov);
1236 const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
1237 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1240 ds_init_empty (&vstr);
1242 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1244 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1248 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1257 /* Show the results of the contrast tests */
1259 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1261 int n_contrasts = ll_count (&cmd->contrast_list);
1264 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1266 struct tab_table *t;
1268 t = tab_create (n_cols, n_rows);
1269 tab_headers (t, 3, 0, 1, 0);
1271 /* Put a frame around the entire box, and vertical lines inside */
1276 n_cols - 1, n_rows - 1);
1284 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1285 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1287 tab_title (t, _("Contrast Tests"));
1289 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1290 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1291 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1292 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1293 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1294 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1296 for (v = 0; v < cmd->n_vars; ++v)
1298 const struct per_var_ws *pvw = &ws->vws[v];
1299 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1302 int lines_per_variable = 2 * n_contrasts;
1304 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1305 var_to_string (cmd->vars[v]));
1307 for ( cli = ll_head (&cmd->contrast_list);
1308 cli != ll_null (&cmd->contrast_list);
1309 ++i, cli = ll_next (cli))
1311 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1314 double contrast_value = 0.0;
1315 double coef_msq = 0.0;
1318 double std_error_contrast;
1320 double sec_vneq = 0.0;
1322 /* Note: The calculation of the degrees of freedom in the
1323 "variances not equal" case is painfull!!
1324 The following formula may help to understand it:
1325 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1328 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1333 double df_denominator = 0.0;
1334 double df_numerator = 0.0;
1337 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1338 df = grand_n - pvw->n_groups;
1342 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1343 TAB_LEFT | TAT_TITLE,
1344 _("Assume equal variances"));
1346 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1347 TAB_LEFT | TAT_TITLE,
1348 _("Does not assume equal"));
1351 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1352 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1355 tab_text_format (t, 2,
1356 (v * lines_per_variable) + i + 1 + n_contrasts,
1357 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1359 for (coeffi = ll_head (&cn->coefficient_list);
1360 coeffi != ll_null (&cn->coefficient_list);
1361 ++ci, coeffi = ll_next (coeffi))
1363 double n, mean, variance;
1364 const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
1365 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1366 const double coef = cn->coeff;
1369 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1371 winv = variance / n;
1373 contrast_value += coef * mean;
1375 coef_msq += (pow2 (coef)) / n;
1377 sec_vneq += (pow2 (coef)) * variance / n;
1379 df_numerator += (pow2 (coef)) * winv;
1380 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1383 sec_vneq = sqrt (sec_vneq);
1385 df_numerator = pow2 (df_numerator);
1387 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1388 TAB_RIGHT, contrast_value, NULL);
1390 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1392 TAB_RIGHT, contrast_value, NULL);
1394 std_error_contrast = sqrt (pvw->mse * coef_msq);
1397 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1398 TAB_RIGHT, std_error_contrast,
1401 T = fabs (contrast_value / std_error_contrast);
1405 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1410 /* Degrees of Freedom */
1411 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1416 /* Significance TWO TAILED !!*/
1417 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1418 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1421 /* Now for the Variances NOT Equal case */
1425 (v * lines_per_variable) + i + 1 + n_contrasts,
1426 TAB_RIGHT, sec_vneq,
1429 T = contrast_value / sec_vneq;
1431 (v * lines_per_variable) + i + 1 + n_contrasts,
1435 df = df_numerator / df_denominator;
1438 (v * lines_per_variable) + i + 1 + n_contrasts,
1442 /* The Significance */
1443 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1444 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1449 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
1458 show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
1460 const int n_cols = 8;
1461 const int heading_rows = 2;
1462 const int heading_cols = 3;
1465 int r = heading_rows ;
1467 const struct per_var_ws *pvw = &ws->vws[v];
1468 const struct categoricals *cat = pvw->cat;
1469 const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
1471 struct tab_table *t = tab_create (n_cols, n_rows);
1473 tab_headers (t, heading_cols, 0, heading_rows, 0);
1475 /* Put a frame around the entire box, and vertical lines inside */
1480 n_cols - 1, n_rows - 1);
1486 n_cols - 1, n_rows - 1);
1488 tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
1490 tab_title (t, _("Multiple Comparisons"));
1492 tab_text_format (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
1493 tab_text_format (t, 2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
1494 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1495 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
1496 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1497 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
1499 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
1500 _("%g%% Confidence Interval"),
1501 (1 - cmd->alpha) * 100.0);
1503 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
1504 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
1507 for (p = 0; p < cmd->n_posthoc; ++p)
1510 const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
1512 tab_hline (t, TAL_2, 0, n_cols - 1, r);
1514 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
1516 for (i = 0; i < pvw->n_groups ; ++i)
1518 double weight_i, mean_i, var_i;
1522 struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
1523 const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
1526 ds_init_empty (&vstr);
1527 var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
1530 tab_hline (t, TAL_1, 1, n_cols - 1, r);
1531 tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1533 moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
1535 for (j = 0 ; j < pvw->n_groups; ++j)
1538 double weight_j, mean_j, var_j;
1540 const struct ccase *cc;
1541 struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
1546 cc = categoricals_get_case_by_category (cat, j);
1547 var_append_value_name (cmd->indep_var, case_data (cc, cmd->indep_var), &vstr);
1548 tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
1550 moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
1552 tab_double (t, 3, r + rx, 0, mean_i - mean_j, 0);
1555 std_err *= weight_i + weight_j;
1556 std_err /= weight_i * weight_j;
1557 std_err = sqrt (std_err);
1559 tab_double (t, 4, r + rx, 0, std_err, 0);
1561 tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), 0);
1563 half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
1565 tab_double (t, 6, r + rx, 0,
1566 (mean_i - mean_j) - half_range, 0 );
1568 tab_double (t, 7, r + rx, 0,
1569 (mean_i - mean_j) + half_range, 0 );
1574 r += pvw->n_groups - 1;