1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010 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 <data/case.h>
20 #include <data/casegrouper.h>
21 #include <data/casereader.h>
23 #include <math/covariance.h>
24 #include <math/categoricals.h>
25 #include <math/moments.h>
26 #include <gsl/gsl_matrix.h>
27 #include <linreg/sweep.h>
29 #include <libpspp/ll.h>
31 #include <language/lexer/lexer.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/value-parser.h>
34 #include <language/command.h>
36 #include <data/procedure.h>
37 #include <data/value.h>
38 #include <data/dictionary.h>
40 #include <language/dictionary/split-file.h>
41 #include <libpspp/hash.h>
42 #include <libpspp/taint.h>
43 #include <math/group-proc.h>
44 #include <math/levene.h>
45 #include <libpspp/misc.h>
47 #include <output/tab.h>
49 #include <gsl/gsl_cdf.h>
51 #include <data/format.h>
53 #include <libpspp/message.h>
56 #define _(msgid) gettext (msgid)
66 STATS_DESCRIPTIVES = 0x0001,
67 STATS_HOMOGENEITY = 0x0002
80 struct ll_list coefficient_list;
82 bool bad_count; /* True if the number of coefficients does not equal the number of groups */
88 const struct variable **vars;
90 const struct variable *indep_var;
92 enum statistics stats;
94 enum missing_type missing_type;
95 enum mv_class exclude;
97 /* List of contrasts */
98 struct ll_list contrast_list;
100 /* The weight variable */
101 const struct variable *wv;
105 /* Per category data */
106 struct descriptive_data
108 const struct variable *var;
109 struct moments1 *mom;
115 /* Workspace variable for each dependent variable */
118 struct covariance *cov;
129 struct oneway_workspace
131 /* The number of distinct values of the independent variable, when all
132 missing values are disregarded */
133 int actual_number_of_groups;
135 /* A hash table containing all the distinct values of the independent
137 struct hsh_table *group_hash;
139 struct per_var_ws *vws;
141 /* An array of descriptive data. One for each dependent variable */
142 struct descriptive_data **dd_total;
145 /* Routines to show the output tables */
146 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
147 static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
148 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
150 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
151 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
154 cmd_oneway (struct lexer *lexer, struct dataset *ds)
156 const struct dictionary *dict = dataset_dict (ds);
157 struct oneway_spec oneway ;
160 oneway.indep_var = NULL;
162 oneway.missing_type = MISS_ANALYSIS;
163 oneway.exclude = MV_ANY;
164 oneway.wv = dict_get_weight (dict);
166 ll_init (&oneway.contrast_list);
169 if ( lex_match (lexer, '/'))
171 if (!lex_force_match_id (lexer, "VARIABLES"))
175 lex_match (lexer, '=');
178 if (!parse_variables_const (lexer, dict,
179 &oneway.vars, &oneway.n_vars,
180 PV_NO_DUPLICATE | PV_NUMERIC))
183 lex_force_match (lexer, T_BY);
185 oneway.indep_var = parse_variable_const (lexer, dict);
187 while (lex_token (lexer) != '.')
189 lex_match (lexer, '/');
191 if (lex_match_id (lexer, "STATISTICS"))
193 lex_match (lexer, '=');
194 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
196 if (lex_match_id (lexer, "DESCRIPTIVES"))
198 oneway.stats |= STATS_DESCRIPTIVES;
200 else if (lex_match_id (lexer, "HOMOGENEITY"))
202 oneway.stats |= STATS_HOMOGENEITY;
206 lex_error (lexer, NULL);
211 else if (lex_match_id (lexer, "CONTRAST"))
213 struct contrasts_node *cl = xzalloc (sizeof *cl);
215 struct ll_list *coefficient_list = &cl->coefficient_list;
216 lex_match (lexer, '=');
218 ll_init (coefficient_list);
220 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
222 if ( lex_is_number (lexer))
224 struct coeff_node *cc = xmalloc (sizeof *cc);
225 cc->coeff = lex_number (lexer);
227 ll_push_tail (coefficient_list, &cc->ll);
232 lex_error (lexer, NULL);
237 ll_push_tail (&oneway.contrast_list, &cl->ll);
239 else if (lex_match_id (lexer, "MISSING"))
241 lex_match (lexer, '=');
242 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
244 if (lex_match_id (lexer, "INCLUDE"))
246 oneway.exclude = MV_SYSTEM;
248 else if (lex_match_id (lexer, "EXCLUDE"))
250 oneway.exclude = MV_ANY;
252 else if (lex_match_id (lexer, "LISTWISE"))
254 oneway.missing_type = MISS_LISTWISE;
256 else if (lex_match_id (lexer, "ANALYSIS"))
258 oneway.missing_type = MISS_ANALYSIS;
262 lex_error (lexer, NULL);
269 lex_error (lexer, NULL);
276 struct casegrouper *grouper;
277 struct casereader *group;
280 grouper = casegrouper_create_splits (proc_open (ds), dict);
281 while (casegrouper_get_next_group (grouper, &group))
282 run_oneway (&oneway, group, ds);
283 ok = casegrouper_destroy (grouper);
284 ok = proc_commit (ds) && ok;
299 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
301 const double *a = a_;
302 const double *b = b_;
303 return *a < *b ? -1 : *a > *b;
307 do_hash_double (const void *value_, const void *aux UNUSED)
309 const double *value = value_;
310 return hash_double (*value, 0);
314 free_double (void *value_, const void *aux UNUSED)
316 double *value = value_;
322 static void postcalc (const struct oneway_spec *cmd);
323 static void precalc (const struct oneway_spec *cmd);
325 static struct descriptive_data *
326 dd_create (const struct variable *var)
328 struct descriptive_data *dd = xmalloc (sizeof *dd);
330 dd->mom = moments1_create (MOMENT_VARIANCE);
331 dd->minimum = DBL_MAX;
332 dd->maximum = -DBL_MAX;
340 makeit (void *aux1, void *aux2 UNUSED)
342 const struct variable *var = aux1;
344 struct descriptive_data *dd = dd_create (var);
350 updateit (void *user_data,
351 enum mv_class exclude,
352 const struct variable *wv,
353 const struct variable *catvar UNUSED,
354 const struct ccase *c,
355 void *aux1, void *aux2)
357 struct descriptive_data *dd = user_data;
359 const struct variable *varp = aux1;
361 const union value *valx = case_data (c, varp);
363 struct descriptive_data *dd_total = aux2;
367 if ( var_is_value_missing (varp, valx, exclude))
370 weight = wv != NULL ? case_data (c, wv)->f : 1.0;
372 moments1_add (dd->mom, valx->f, weight);
373 if (valx->f * weight < dd->minimum)
374 dd->minimum = valx->f * weight;
376 if (valx->f * weight > dd->maximum)
377 dd->maximum = valx->f * weight;
380 const struct variable *var = dd_total->var;
381 const union value *val = case_data (c, var);
383 moments1_add (dd_total->mom,
387 if (val->f * weight < dd_total->minimum)
388 dd_total->minimum = val->f * weight;
390 if (val->f * weight > dd_total->maximum)
391 dd_total->maximum = val->f * weight;
396 run_oneway (const struct oneway_spec *cmd,
397 struct casereader *input,
398 const struct dataset *ds)
402 struct dictionary *dict = dataset_dict (ds);
403 struct casereader *reader;
406 struct oneway_workspace ws;
408 ws.actual_number_of_groups = 0;
409 ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
410 ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
412 for (v = 0 ; v < cmd->n_vars; ++v)
413 ws.dd_total[v] = dd_create (cmd->vars[v]);
415 for (v = 0; v < cmd->n_vars; ++v)
417 struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
418 cmd->wv, cmd->exclude,
421 cmd->vars[v], ws.dd_total[v]);
423 ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
425 cmd->wv, cmd->exclude);
428 c = casereader_peek (input, 0);
431 casereader_destroy (input);
434 output_split_file_values (ds, c);
437 taint = taint_clone (casereader_get_taint (input));
439 ws.group_hash = hsh_create (4,
447 input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
448 cmd->exclude, NULL, NULL);
449 if (cmd->missing_type == MISS_LISTWISE)
450 input = casereader_create_filter_missing (input, cmd->vars, cmd->n_vars,
451 cmd->exclude, NULL, NULL);
452 input = casereader_create_filter_weight (input, dict, NULL, NULL);
454 reader = casereader_clone (input);
456 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
460 const double weight = dict_get_case_weight (dict, c, NULL);
462 const union value *indep_val = case_data (c, cmd->indep_var);
463 void **p = hsh_probe (ws.group_hash, &indep_val->f);
466 double *value = *p = xmalloc (sizeof *value);
467 *value = indep_val->f;
470 for (i = 0; i < cmd->n_vars; ++i)
472 struct per_var_ws *pvw = &ws.vws[i];
473 const struct variable *v = cmd->vars[i];
474 const union value *val = case_data (c, v);
475 struct group_proc *gp = group_proc_get (cmd->vars[i]);
476 struct hsh_table *group_hash = gp->group_hash;
477 struct group_statistics *gs;
479 if ( MISS_ANALYSIS == cmd->missing_type)
481 if ( var_is_value_missing (v, val, cmd->exclude))
485 covariance_accumulate_pass1 (pvw->cov, c);
487 gs = hsh_find (group_hash, indep_val );
491 gs = xmalloc (sizeof *gs);
497 gs->minimum = DBL_MAX;
498 gs->maximum = -DBL_MAX;
500 hsh_insert ( group_hash, gs );
503 if (!var_is_value_missing (v, val, cmd->exclude))
505 struct group_statistics *totals = &gp->ugs;
508 totals->sum += weight * val->f;
509 totals->ssq += weight * pow2 (val->f);
511 if ( val->f * weight < totals->minimum )
512 totals->minimum = val->f * weight;
514 if ( val->f * weight > totals->maximum )
515 totals->maximum = val->f * weight;
518 gs->sum += weight * val->f;
519 gs->ssq += weight * pow2 (val->f);
521 if ( val->f * weight < gs->minimum )
522 gs->minimum = val->f * weight;
524 if ( val->f * weight > gs->maximum )
525 gs->maximum = val->f * weight;
528 gp->n_groups = hsh_count (group_hash );
532 casereader_destroy (reader);
533 reader = casereader_clone (input);
534 for ( ; (c = casereader_read (reader) ); case_unref (c))
537 for (i = 0; i < cmd->n_vars; ++i)
539 struct per_var_ws *pvw = &ws.vws[i];
540 const struct variable *v = cmd->vars[i];
541 const union value *val = case_data (c, v);
543 if ( MISS_ANALYSIS == cmd->missing_type)
545 if ( var_is_value_missing (v, val, cmd->exclude))
549 covariance_accumulate_pass2 (pvw->cov, c);
552 casereader_destroy (reader);
554 for (v = 0; v < cmd->n_vars; ++v)
556 struct per_var_ws *pvw = &ws.vws[v];
557 gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
558 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
561 moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
563 pvw->sst = gsl_matrix_get (cm, 0, 0);
565 // gsl_matrix_fprintf (stdout, cm, "%g ");
569 pvw->sse = gsl_matrix_get (cm, 0, 0);
571 pvw->ssa = pvw->sst - pvw->sse;
573 pvw->n_groups = categoricals_total (cats);
575 pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
581 for (v = 0; v < cmd->n_vars; ++v)
583 struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
585 categoricals_done (cats);
587 if (categoricals_total (cats) > ws.actual_number_of_groups)
588 ws.actual_number_of_groups = categoricals_total (cats);
591 if ( cmd->stats & STATS_HOMOGENEITY )
592 levene (dict, casereader_clone (input), cmd->indep_var,
593 cmd->n_vars, cmd->vars, cmd->exclude);
595 casereader_destroy (input);
597 if (!taint_has_tainted_successor (taint))
598 output_oneway (cmd, &ws);
600 taint_destroy (taint);
603 /* Pre calculations */
605 precalc (const struct oneway_spec *cmd)
609 for (i = 0; i < cmd->n_vars; ++i)
611 struct group_proc *gp = group_proc_get (cmd->vars[i]);
612 struct group_statistics *totals = &gp->ugs;
614 /* Create a hash for each of the dependent variables.
615 The hash contains a group_statistics structure,
616 and is keyed by value of the independent variable */
618 gp->group_hash = hsh_create (4, compare_group, hash_group,
619 (hsh_free_func *) free_group,
625 totals->sum_diff = 0;
626 totals->maximum = -DBL_MAX;
627 totals->minimum = DBL_MAX;
631 /* Post calculations for the ONEWAY command */
633 postcalc (const struct oneway_spec *cmd)
637 for (i = 0; i < cmd->n_vars; ++i)
639 struct group_proc *gp = group_proc_get (cmd->vars[i]);
640 struct hsh_table *group_hash = gp->group_hash;
641 struct group_statistics *totals = &gp->ugs;
643 struct hsh_iterator g;
644 struct group_statistics *gs;
646 for (gs = hsh_first (group_hash, &g);
648 gs = hsh_next (group_hash, &g))
650 gs->mean = gs->sum / gs->n;
651 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
654 gs->n / (gs->n - 1) *
655 ( gs->ssq / gs->n - pow2 (gs->mean))
658 gs->se_mean = gs->std_dev / sqrt (gs->n);
659 gs->mean_diff = gs->sum_diff / gs->n;
662 totals->mean = totals->sum / totals->n;
663 totals->std_dev = sqrt (
664 totals->n / (totals->n - 1) *
665 (totals->ssq / totals->n - pow2 (totals->mean))
668 totals->se_mean = totals->std_dev / sqrt (totals->n);
672 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
673 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
676 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
680 /* Check the sanity of the given contrast values */
681 struct contrasts_node *coeff_list = NULL;
682 ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
684 struct coeff_node *cn = NULL;
686 struct ll_list *cl = &coeff_list->coefficient_list;
689 if (ll_count (cl) != ws->actual_number_of_groups)
692 _("Number of contrast coefficients must equal the number of groups"));
693 coeff_list->bad_count = true;
697 ll_for_each (cn, struct coeff_node, ll, cl)
701 msg (SW, _("Coefficients for contrast %zu do not total zero"), i);
704 if (cmd->stats & STATS_DESCRIPTIVES)
705 show_descriptives (cmd, ws);
707 if (cmd->stats & STATS_HOMOGENEITY)
708 show_homogeneity (cmd, ws);
710 show_anova_table (cmd, ws);
712 if (ll_count (&cmd->contrast_list) > 0)
714 show_contrast_coeffs (cmd, ws);
715 show_contrast_tests (cmd, ws);
719 for (i = 0; i < cmd->n_vars; ++i )
721 struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
723 hsh_destroy (group_hash);
726 hsh_destroy (ws->group_hash);
730 /* Show the ANOVA table */
732 show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
736 size_t n_rows = cmd->n_vars * 3 + 1;
738 struct tab_table *t = tab_create (n_cols, n_rows);
740 tab_headers (t, 2, 0, 1, 0);
746 n_cols - 1, n_rows - 1);
748 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
749 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
750 tab_vline (t, TAL_0, 1, 0, 0);
752 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
753 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
754 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
755 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
756 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
759 for (i = 0; i < cmd->n_vars; ++i)
764 const char *s = var_to_string (cmd->vars[i]);
765 const struct per_var_ws *pvw = &ws->vws[i];
767 moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
769 df1 = pvw->n_groups - 1;
770 df2 = n - pvw->n_groups;
771 msa = pvw->ssa / df1;
773 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
774 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
775 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
776 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
779 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
782 /* Sums of Squares */
783 tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
784 tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
785 tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
788 /* Degrees of freedom */
789 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
790 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
791 tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
794 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
795 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
798 const double F = msa / pvw->mse ;
801 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
803 /* The significance */
804 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
808 tab_title (t, _("ANOVA"));
813 /* Show the descriptives table */
815 show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
822 const double confidence = 0.95;
823 const double q = (1.0 - confidence) / 2.0;
825 const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
829 for (v = 0; v < cmd->n_vars; ++v)
830 n_rows += ws->actual_number_of_groups + 1;
832 t = tab_create (n_cols, n_rows);
833 tab_headers (t, 2, 0, 2, 0);
835 /* Put a frame around the entire box, and vertical lines inside */
840 n_cols - 1, n_rows - 1);
842 /* Underline headers */
843 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
844 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
846 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
847 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
848 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
849 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
852 tab_vline (t, TAL_0, 7, 0, 0);
853 tab_hline (t, TAL_1, 6, 7, 1);
854 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
855 _("%g%% Confidence Interval for Mean"),
858 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
859 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
861 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
862 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
864 tab_title (t, _("Descriptives"));
867 for (v = 0; v < cmd->n_vars; ++v)
869 const char *s = var_to_string (cmd->vars[v]);
870 const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
874 struct per_var_ws *pvw = &ws->vws[v];
875 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
877 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
879 tab_hline (t, TAL_1, 0, n_cols - 1, row);
881 for (count = 0; count < categoricals_total (cats); ++count)
884 double n, mean, variance;
885 double std_dev, std_error ;
889 const union value *gval = categoricals_get_value_by_subscript (cats, count);
890 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
892 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
894 std_dev = sqrt (variance);
895 std_error = std_dev / sqrt (n) ;
897 ds_init_empty (&vstr);
899 var_append_value_name (cmd->indep_var, gval, &vstr);
901 tab_text (t, 1, row + count,
902 TAB_LEFT | TAT_TITLE,
907 /* Now fill in the numbers ... */
909 tab_fixed (t, 2, row + count, 0, n, 8, 0);
911 tab_double (t, 3, row + count, 0, mean, NULL);
913 tab_double (t, 4, row + count, 0, std_dev, NULL);
916 tab_double (t, 5, row + count, 0, std_error, NULL);
918 /* Now the confidence interval */
920 T = gsl_cdf_tdist_Qinv (q, n - 1);
922 tab_double (t, 6, row + count, 0,
923 mean - T * std_error, NULL);
925 tab_double (t, 7, row + count, 0,
926 mean + T * std_error, NULL);
930 tab_double (t, 8, row + count, 0, dd->minimum, fmt);
931 tab_double (t, 9, row + count, 0, dd->maximum, fmt);
936 double n, mean, variance;
940 moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
942 std_dev = sqrt (variance);
943 std_error = std_dev / sqrt (n) ;
945 tab_text (t, 1, row + count,
946 TAB_LEFT | TAT_TITLE, _("Total"));
948 tab_double (t, 2, row + count, 0, n, wfmt);
950 tab_double (t, 3, row + count, 0, mean, NULL);
952 tab_double (t, 4, row + count, 0, std_dev, NULL);
954 tab_double (t, 5, row + count, 0, std_error, NULL);
956 /* Now the confidence interval */
957 T = gsl_cdf_tdist_Qinv (q, n - 1);
959 tab_double (t, 6, row + count, 0,
960 mean - T * std_error, NULL);
962 tab_double (t, 7, row + count, 0,
963 mean + T * std_error, NULL);
966 tab_double (t, 8, row + count, 0, ws->dd_total[v]->minimum, fmt);
967 tab_double (t, 9, row + count, 0, ws->dd_total[v]->maximum, fmt);
970 row += categoricals_total (cats) + 1;
976 /* Show the homogeneity table */
978 show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
982 size_t n_rows = cmd->n_vars + 1;
984 struct tab_table *t = tab_create (n_cols, n_rows);
985 tab_headers (t, 1, 0, 1, 0);
987 /* Put a frame around the entire box, and vertical lines inside */
992 n_cols - 1, n_rows - 1);
995 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
996 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
998 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
999 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
1000 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
1001 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
1003 tab_title (t, _("Test of Homogeneity of Variances"));
1005 for (v = 0; v < cmd->n_vars; ++v)
1010 const struct per_var_ws *pvw = &ws->vws[v];
1012 const struct variable *var = cmd->vars[v];
1013 const struct group_proc *gp = group_proc_get (cmd->vars[v]);
1014 const char *s = var_to_string (var);
1016 double F = gp->levene;
1018 moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
1020 df1 = pvw->n_groups - 1;
1021 df2 = n - pvw->n_groups;
1023 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
1025 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
1026 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
1027 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
1029 /* Now the significance */
1030 tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
1037 /* Show the contrast coefficients table */
1039 show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1044 int n_contrasts = ll_count (&cmd->contrast_list);
1045 int n_cols = 2 + ws->actual_number_of_groups;
1046 int n_rows = 2 + n_contrasts;
1048 struct tab_table *t;
1050 const struct covariance *cov = ws->vws[0].cov ;
1052 t = tab_create (n_cols, n_rows);
1053 tab_headers (t, 2, 0, 2, 0);
1055 /* Put a frame around the entire box, and vertical lines inside */
1060 n_cols - 1, n_rows - 1);
1074 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
1075 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
1077 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1079 tab_title (t, _("Contrast Coefficients"));
1081 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
1084 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
1085 var_to_string (cmd->indep_var));
1087 for ( cli = ll_head (&cmd->contrast_list);
1088 cli != ll_null (&cmd->contrast_list);
1089 cli = ll_next (cli))
1092 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1095 tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
1097 for (coeffi = ll_head (&cn->coefficient_list);
1098 coeffi != ll_null (&cn->coefficient_list);
1099 ++count, coeffi = ll_next (coeffi))
1101 const struct categoricals *cats = covariance_get_categoricals (cov);
1102 const union value *val = categoricals_get_value_by_subscript (cats, count);
1105 ds_init_empty (&vstr);
1107 var_append_value_name (cmd->indep_var, val, &vstr);
1109 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
1114 tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
1117 struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
1119 tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
1129 /* Show the results of the contrast tests */
1131 show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
1133 int n_contrasts = ll_count (&cmd->contrast_list);
1136 size_t n_rows = 1 + cmd->n_vars * 2 * n_contrasts;
1138 struct tab_table *t;
1140 t = tab_create (n_cols, n_rows);
1141 tab_headers (t, 3, 0, 1, 0);
1143 /* Put a frame around the entire box, and vertical lines inside */
1148 n_cols - 1, n_rows - 1);
1156 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1157 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
1159 tab_title (t, _("Contrast Tests"));
1161 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
1162 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
1163 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1164 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
1165 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
1166 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1168 for (v = 0; v < cmd->n_vars; ++v)
1170 const struct per_var_ws *pvw = &ws->vws[v];
1171 const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
1174 int lines_per_variable = 2 * n_contrasts;
1176 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
1177 var_to_string (cmd->vars[v]));
1179 for ( cli = ll_head (&cmd->contrast_list);
1180 cli != ll_null (&cmd->contrast_list);
1181 ++i, cli = ll_next (cli))
1183 struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
1186 double contrast_value = 0.0;
1187 double coef_msq = 0.0;
1190 double std_error_contrast;
1192 double sec_vneq = 0.0;
1194 /* Note: The calculation of the degrees of freedom in the
1195 "variances not equal" case is painfull!!
1196 The following formula may help to understand it:
1197 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
1200 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
1205 double df_denominator = 0.0;
1206 double df_numerator = 0.0;
1209 moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
1210 df = grand_n - pvw->n_groups;
1214 tab_text (t, 1, (v * lines_per_variable) + i + 1,
1215 TAB_LEFT | TAT_TITLE,
1216 _("Assume equal variances"));
1218 tab_text (t, 1, (v * lines_per_variable) + i + 1 + n_contrasts,
1219 TAB_LEFT | TAT_TITLE,
1220 _("Does not assume equal"));
1223 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
1224 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1227 tab_text_format (t, 2,
1228 (v * lines_per_variable) + i + 1 + n_contrasts,
1229 TAB_CENTER | TAT_TITLE, "%d", i + 1);
1234 for (coeffi = ll_head (&cn->coefficient_list);
1235 coeffi != ll_null (&cn->coefficient_list);
1236 ++ci, coeffi = ll_next (coeffi))
1238 double n, mean, variance;
1239 const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
1240 struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
1241 const double coef = cn->coeff;
1244 moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
1246 winv = variance / n;
1248 contrast_value += coef * mean;
1250 coef_msq += (pow2 (coef)) / n;
1252 sec_vneq += (pow2 (coef)) * variance / n;
1254 df_numerator += (pow2 (coef)) * winv;
1255 df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
1258 sec_vneq = sqrt (sec_vneq);
1260 df_numerator = pow2 (df_numerator);
1262 tab_double (t, 3, (v * lines_per_variable) + i + 1,
1263 TAB_RIGHT, contrast_value, NULL);
1265 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
1267 TAB_RIGHT, contrast_value, NULL);
1269 std_error_contrast = sqrt (pvw->mse * coef_msq);
1272 tab_double (t, 4, (v * lines_per_variable) + i + 1,
1273 TAB_RIGHT, std_error_contrast,
1276 T = fabs (contrast_value / std_error_contrast);
1280 tab_double (t, 5, (v * lines_per_variable) + i + 1,
1285 /* Degrees of Freedom */
1286 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
1291 /* Significance TWO TAILED !!*/
1292 tab_double (t, 7, (v * lines_per_variable) + i + 1,
1293 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
1296 /* Now for the Variances NOT Equal case */
1300 (v * lines_per_variable) + i + 1 + n_contrasts,
1301 TAB_RIGHT, sec_vneq,
1304 T = contrast_value / sec_vneq;
1306 (v * lines_per_variable) + i + 1 + n_contrasts,
1310 df = df_numerator / df_denominator;
1313 (v * lines_per_variable) + i + 1 + n_contrasts,
1317 /* The Significance */
1318 tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
1319 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
1324 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);