1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009 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>
24 #include <data/case.h>
25 #include <data/casegrouper.h>
26 #include <data/casereader.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/dictionary/split-file.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/str.h>
39 #include <libpspp/taint.h>
40 #include <math/group-proc.h>
41 #include <math/group.h>
42 #include <math/levene.h>
43 #include <output/tab.h>
44 #include "sort-criteria.h"
45 #include <data/format.h>
50 #define _(msgid) gettext (msgid)
57 missing=miss:!analysis/listwise,
58 incl:include/!exclude;
59 +contrast= double list;
60 +statistics[st_]=descriptives,homogeneity.
65 static struct cmd_oneway cmd;
67 /* The independent variable */
68 static const struct variable *indep_var;
70 /* Number of dependent variables */
73 /* The dependent variables */
74 static const struct variable **vars;
77 /* A hash table containing all the distinct values of the independent
79 static struct hsh_table *global_group_hash;
81 /* The number of distinct values of the independent variable, when all
82 missing values are disregarded */
83 static int ostensible_number_of_groups = -1;
86 static void run_oneway (struct cmd_oneway *, struct casereader *,
87 const struct dataset *);
90 /* Routines to show the output tables */
91 static void show_anova_table(void);
92 static void show_descriptives (const struct dictionary *dict);
93 static void show_homogeneity(void);
95 static void show_contrast_coeffs (short *);
96 static void show_contrast_tests (short *);
99 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
101 static enum stat_table_t stat_tables;
103 static void output_oneway (const struct dictionary *dict);
107 cmd_oneway (struct lexer *lexer, struct dataset *ds)
109 struct casegrouper *grouper;
110 struct casereader *group;
114 if ( !parse_oneway (lexer, ds, &cmd, NULL))
117 /* What statistics were requested */
118 if ( cmd.sbc_statistics)
121 for (i = 0; i < ONEWAY_ST_count; ++i)
123 if (! cmd.a_statistics[i]) continue;
127 case ONEWAY_ST_DESCRIPTIVES:
128 stat_tables |= STAT_DESC;
130 case ONEWAY_ST_HOMOGENEITY:
131 stat_tables |= STAT_HOMO;
137 /* Data pass. FIXME: error handling. */
138 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
139 while (casegrouper_get_next_group (grouper, &group))
140 run_oneway (&cmd, group, ds);
141 ok = casegrouper_destroy (grouper);
142 ok = proc_commit (ds) && ok;
147 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
152 output_oneway (const struct dictionary *dict)
157 bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
159 /* Check the sanity of the given contrast values */
160 for (i = 0; i < cmd.sbc_contrast; ++i)
166 if (subc_list_double_count (&cmd.dl_contrast[i]) !=
167 ostensible_number_of_groups)
170 _("Number of contrast coefficients must equal the number of groups"));
175 for (j = 0; j < ostensible_number_of_groups; ++j)
176 sum += subc_list_double_at (&cmd.dl_contrast[i], j);
179 msg (SW, _("Coefficients for contrast %zu do not total zero"), i + 1);
182 if ( stat_tables & STAT_DESC )
183 show_descriptives (dict);
185 if ( stat_tables & STAT_HOMO )
190 if (cmd.sbc_contrast )
192 show_contrast_coeffs (bad_contrast);
193 show_contrast_tests (bad_contrast);
199 for (i = 0; i < n_vars; ++i )
201 struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
203 hsh_destroy (group_hash);
206 hsh_destroy (global_group_hash);
210 /* Parser for the variables sub command */
212 oneway_custom_variables (struct lexer *lexer,
213 struct dataset *ds, struct cmd_oneway *cmd UNUSED,
216 struct dictionary *dict = dataset_dict (ds);
218 lex_match (lexer, '=');
220 if ((lex_token (lexer) != T_ID ||
221 dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
222 && lex_token (lexer) != T_ALL)
225 if (!parse_variables_const (lexer, dict, &vars, &n_vars,
227 | PV_NUMERIC | PV_NO_SCRATCH) )
235 if ( ! lex_match (lexer, T_BY))
238 indep_var = parse_variable (lexer, dict);
242 msg (SE, _("`%s' is not a variable name"), lex_tokid (lexer));
250 /* Show the ANOVA table */
252 show_anova_table (void)
256 size_t n_rows = n_vars * 3 + 1;
261 t = tab_create (n_cols, n_rows);
262 tab_headers (t, 2, 0, 1, 0);
268 n_cols - 1, n_rows - 1);
270 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
271 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
272 tab_vline (t, TAL_0, 1, 0, 0);
274 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
275 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
276 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
277 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
278 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
281 for (i = 0; i < n_vars; ++i)
283 struct group_statistics *totals = &group_proc_get (vars[i])->ugs;
284 struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
285 struct hsh_iterator g;
286 struct group_statistics *gs;
288 const char *s = var_to_string (vars[i]);
290 for (gs = hsh_first (group_hash, &g);
292 gs = hsh_next (group_hash, &g))
294 ssa += pow2 (gs->sum) / gs->n;
297 ssa -= pow2 (totals->sum) / totals->n;
299 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
300 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
301 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
302 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
305 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
308 struct group_proc *gp = group_proc_get (vars[i]);
309 const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
310 const double df1 = gp->n_groups - 1;
311 const double df2 = totals->n - gp->n_groups;
312 const double msa = ssa / df1;
314 gp->mse = (sst - ssa) / df2;
317 /* Sums of Squares */
318 tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
319 tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
320 tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
323 /* Degrees of freedom */
324 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
325 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
326 tab_fixed (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
329 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
330 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
333 const double F = msa / gp->mse ;
336 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
338 /* The significance */
339 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
345 tab_title (t, _("ANOVA"));
350 /* Show the descriptives table */
352 show_descriptives (const struct dictionary *dict)
359 const double confidence = 0.95;
360 const double q = (1.0 - confidence) / 2.0;
362 const struct variable *wv = dict_get_weight (dict);
363 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
367 for ( v = 0; v < n_vars; ++v )
368 n_rows += group_proc_get (vars[v])->n_groups + 1;
370 t = tab_create (n_cols, n_rows);
371 tab_headers (t, 2, 0, 2, 0);
374 /* Put a frame around the entire box, and vertical lines inside */
379 n_cols - 1, n_rows - 1);
381 /* Underline headers */
382 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
383 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
385 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
386 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
387 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
388 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
391 tab_vline (t, TAL_0, 7, 0, 0);
392 tab_hline (t, TAL_1, 6, 7, 1);
393 tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
394 _("%g%% Confidence Interval for Mean"),
397 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
398 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
400 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
401 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
404 tab_title (t, _("Descriptives"));
408 for (v = 0; v < n_vars; ++v)
413 struct group_proc *gp = group_proc_get (vars[v]);
415 struct group_statistics *gs;
416 struct group_statistics *totals = &gp->ugs;
418 const char *s = var_to_string (vars[v]);
419 const struct fmt_spec *fmt = var_get_print_format (vars[v]);
421 struct group_statistics *const *gs_array =
422 (struct group_statistics *const *) hsh_sort (gp->group_hash);
425 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
427 tab_hline (t, TAL_1, 0, n_cols - 1, row);
429 for (count = 0; count < hsh_count (gp->group_hash); ++count)
432 ds_init_empty (&vstr);
433 gs = gs_array[count];
435 var_append_value_name (indep_var, &gs->id, &vstr);
437 tab_text (t, 1, row + count,
438 TAB_LEFT | TAT_TITLE,
443 /* Now fill in the numbers ... */
445 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
447 tab_double (t, 3, row + count, 0, gs->mean, NULL);
449 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
451 std_error = gs->std_dev / sqrt (gs->n) ;
452 tab_double (t, 5, row + count, 0,
455 /* Now the confidence interval */
457 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
459 tab_double (t, 6, row + count, 0,
460 gs->mean - T * std_error, NULL);
462 tab_double (t, 7, row + count, 0,
463 gs->mean + T * std_error, NULL);
467 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
468 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
471 tab_text (t, 1, row + count,
472 TAB_LEFT | TAT_TITLE, _("Total"));
474 tab_double (t, 2, row + count, 0, totals->n, wfmt);
476 tab_double (t, 3, row + count, 0, totals->mean, NULL);
478 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
480 std_error = totals->std_dev / sqrt (totals->n) ;
482 tab_double (t, 5, row + count, 0, std_error, NULL);
484 /* Now the confidence interval */
486 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
488 tab_double (t, 6, row + count, 0,
489 totals->mean - T * std_error, NULL);
491 tab_double (t, 7, row + count, 0,
492 totals->mean + T * std_error, NULL);
496 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
497 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
499 row += gp->n_groups + 1;
505 /* Show the homogeneity table */
507 show_homogeneity (void)
511 size_t n_rows = n_vars + 1;
516 t = tab_create (n_cols, n_rows);
517 tab_headers (t, 1, 0, 1, 0);
520 /* Put a frame around the entire box, and vertical lines inside */
525 n_cols - 1, n_rows - 1);
528 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
529 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
532 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
533 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
534 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
535 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
537 tab_title (t, _("Test of Homogeneity of Variances"));
539 for (v = 0; v < n_vars; ++v)
542 const struct variable *var = vars[v];
543 const struct group_proc *gp = group_proc_get (vars[v]);
544 const char *s = var_to_string (var);
545 const struct group_statistics *totals = &gp->ugs;
547 const double df1 = gp->n_groups - 1;
548 const double df2 = totals->n - gp->n_groups;
550 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
553 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
554 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
555 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
557 /* Now the significance */
558 tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
565 /* Show the contrast coefficients table */
567 show_contrast_coeffs (short *bad_contrast)
569 int n_cols = 2 + ostensible_number_of_groups;
570 int n_rows = 2 + cmd.sbc_contrast;
572 void *const *group_values;
576 t = tab_create (n_cols, n_rows);
577 tab_headers (t, 2, 0, 2, 0);
579 /* Put a frame around the entire box, and vertical lines inside */
584 n_cols - 1, n_rows - 1);
598 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
599 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
601 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
603 tab_title (t, _("Contrast Coefficients"));
605 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
608 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
609 var_to_string (indep_var));
611 group_values = hsh_sort (global_group_hash);
613 count < hsh_count (global_group_hash);
616 double *group_value_p;
617 union value group_value;
621 ds_init_empty (&vstr);
623 group_value_p = group_values[count];
624 group_value.f = *group_value_p;
625 var_append_value_name (indep_var, &group_value, &vstr);
627 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
633 for (i = 0; i < cmd.sbc_contrast; ++i )
635 tab_text_format (t, 1, i + 2, TAB_CENTER, "%d", i + 1);
637 if ( bad_contrast[i] )
638 tab_text (t, count + 2, i + 2, TAB_RIGHT, "?" );
640 tab_text_format (t, count + 2, i + 2, TAB_RIGHT, "%g",
641 subc_list_double_at (&cmd.dl_contrast[i], count));
649 /* Show the results of the contrast tests */
651 show_contrast_tests (short *bad_contrast)
655 size_t n_rows = 1 + n_vars * 2 * cmd.sbc_contrast;
659 t = tab_create (n_cols, n_rows);
660 tab_headers (t, 3, 0, 1, 0);
662 /* Put a frame around the entire box, and vertical lines inside */
667 n_cols - 1, n_rows - 1);
675 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
676 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
679 tab_title (t, _("Contrast Tests"));
681 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
682 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
683 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
684 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
685 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
686 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
688 for (v = 0; v < n_vars; ++v)
691 int lines_per_variable = 2 * cmd.sbc_contrast;
694 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
695 var_to_string (vars[v]));
697 for (i = 0; i < cmd.sbc_contrast; ++i)
700 double contrast_value = 0.0;
701 double coef_msq = 0.0;
702 struct group_proc *grp_data = group_proc_get (vars[v]);
703 struct hsh_table *group_hash = grp_data->group_hash;
705 void *const *group_stat_array;
708 double std_error_contrast;
710 double sec_vneq = 0.0;
713 /* Note: The calculation of the degrees of freedom in the
714 "variances not equal" case is painfull!!
715 The following formula may help to understand it:
716 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
719 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
724 double df_denominator = 0.0;
725 double df_numerator = 0.0;
728 tab_text (t, 1, (v * lines_per_variable) + i + 1,
729 TAB_LEFT | TAT_TITLE,
730 _("Assume equal variances"));
732 tab_text (t, 1, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
733 TAB_LEFT | TAT_TITLE,
734 _("Does not assume equal"));
737 tab_text_format (t, 2, (v * lines_per_variable) + i + 1,
738 TAB_CENTER | TAT_TITLE, "%d", i + 1);
741 tab_text_format (t, 2,
742 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
743 TAB_CENTER | TAT_TITLE, "%d", i + 1);
746 if ( bad_contrast[i])
749 group_stat_array = hsh_sort (group_hash);
751 for (ci = 0; ci < hsh_count (group_hash); ++ci)
753 const double coef = subc_list_double_at (&cmd.dl_contrast[i], ci);
754 struct group_statistics *gs = group_stat_array[ci];
756 const double winv = pow2 (gs->std_dev) / gs->n;
758 contrast_value += coef * gs->mean;
760 coef_msq += (coef * coef) / gs->n;
762 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
764 df_numerator += (coef * coef) * winv;
765 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
767 sec_vneq = sqrt (sec_vneq);
769 df_numerator = pow2 (df_numerator);
771 tab_double (t, 3, (v * lines_per_variable) + i + 1,
772 TAB_RIGHT, contrast_value, NULL);
774 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
776 TAB_RIGHT, contrast_value, NULL);
778 std_error_contrast = sqrt (grp_data->mse * coef_msq);
781 tab_double (t, 4, (v * lines_per_variable) + i + 1,
782 TAB_RIGHT, std_error_contrast,
785 T = fabs (contrast_value / std_error_contrast);
789 tab_double (t, 5, (v * lines_per_variable) + i + 1,
793 df = grp_data->ugs.n - grp_data->n_groups;
795 /* Degrees of Freedom */
796 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
801 /* Significance TWO TAILED !!*/
802 tab_double (t, 7, (v * lines_per_variable) + i + 1,
803 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
806 /* Now for the Variances NOT Equal case */
810 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
814 T = contrast_value / sec_vneq;
816 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
820 df = df_numerator / df_denominator;
823 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
827 /* The Significance */
829 tab_double (t, 7, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
830 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
835 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
842 /* ONEWAY ANOVA Calculations */
844 static void postcalc (struct cmd_oneway *cmd UNUSED);
846 static void precalc (struct cmd_oneway *cmd UNUSED);
850 /* Pre calculations */
852 precalc (struct cmd_oneway *cmd UNUSED)
856 for (i = 0; i < n_vars; ++i)
858 struct group_proc *gp = group_proc_get (vars[i]);
859 struct group_statistics *totals = &gp->ugs;
861 /* Create a hash for each of the dependent variables.
862 The hash contains a group_statistics structure,
863 and is keyed by value of the independent variable */
865 gp->group_hash = hsh_create (4, compare_group, hash_group,
866 (hsh_free_func *) free_group,
872 totals->sum_diff = 0;
873 totals->maximum = -DBL_MAX;
874 totals->minimum = DBL_MAX;
879 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
881 const double *a = a_;
882 const double *b = b_;
883 return *a < *b ? -1 : *a > *b;
887 do_hash_double (const void *value_, const void *aux UNUSED)
889 const double *value = value_;
890 return hash_double (*value, 0);
894 free_double (void *value_, const void *aux UNUSED)
896 double *value = value_;
901 run_oneway (struct cmd_oneway *cmd,
902 struct casereader *input,
903 const struct dataset *ds)
906 struct dictionary *dict = dataset_dict (ds);
907 enum mv_class exclude;
908 struct casereader *reader;
911 c = casereader_peek (input, 0);
914 casereader_destroy (input);
917 output_split_file_values (ds, c);
920 taint = taint_clone (casereader_get_taint (input));
922 global_group_hash = hsh_create (4,
930 exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
931 input = casereader_create_filter_missing (input, &indep_var, 1,
932 exclude, NULL, NULL);
933 if (cmd->miss == ONEWAY_LISTWISE)
934 input = casereader_create_filter_missing (input, vars, n_vars,
935 exclude, NULL, NULL);
936 input = casereader_create_filter_weight (input, dict, NULL, NULL);
938 reader = casereader_clone (input);
939 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
943 const double weight = dict_get_case_weight (dict, c, NULL);
945 const union value *indep_val = case_data (c, indep_var);
946 void **p = hsh_probe (global_group_hash, &indep_val->f);
949 double *value = *p = xmalloc (sizeof *value);
950 *value = indep_val->f;
953 for (i = 0; i < n_vars; ++i)
955 const struct variable *v = vars[i];
957 const union value *val = case_data (c, v);
959 struct group_proc *gp = group_proc_get (vars[i]);
960 struct hsh_table *group_hash = gp->group_hash;
962 struct group_statistics *gs;
964 gs = hsh_find (group_hash, indep_val );
968 gs = xmalloc (sizeof *gs);
974 gs->minimum = DBL_MAX;
975 gs->maximum = -DBL_MAX;
977 hsh_insert ( group_hash, gs );
980 if (!var_is_value_missing (v, val, exclude))
982 struct group_statistics *totals = &gp->ugs;
985 totals->sum += weight * val->f;
986 totals->ssq += weight * pow2 (val->f);
988 if ( val->f * weight < totals->minimum )
989 totals->minimum = val->f * weight;
991 if ( val->f * weight > totals->maximum )
992 totals->maximum = val->f * weight;
995 gs->sum += weight * val->f;
996 gs->ssq += weight * pow2 (val->f);
998 if ( val->f * weight < gs->minimum )
999 gs->minimum = val->f * weight;
1001 if ( val->f * weight > gs->maximum )
1002 gs->maximum = val->f * weight;
1005 gp->n_groups = hsh_count ( group_hash );
1009 casereader_destroy (reader);
1014 if ( stat_tables & STAT_HOMO )
1015 levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
1017 casereader_destroy (input);
1019 ostensible_number_of_groups = hsh_count (global_group_hash);
1021 if (!taint_has_tainted_successor (taint))
1022 output_oneway (dict);
1024 taint_destroy (taint);
1028 /* Post calculations for the ONEWAY command */
1030 postcalc ( struct cmd_oneway *cmd UNUSED )
1034 for (i = 0; i < n_vars; ++i)
1036 struct group_proc *gp = group_proc_get (vars[i]);
1037 struct hsh_table *group_hash = gp->group_hash;
1038 struct group_statistics *totals = &gp->ugs;
1040 struct hsh_iterator g;
1041 struct group_statistics *gs;
1043 for (gs = hsh_first (group_hash, &g);
1045 gs = hsh_next (group_hash, &g))
1047 gs->mean = gs->sum / gs->n;
1048 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
1050 gs->std_dev = sqrt (
1051 gs->n / (gs->n - 1) *
1052 ( gs->ssq / gs->n - pow2 (gs->mean))
1055 gs->se_mean = gs->std_dev / sqrt (gs->n);
1056 gs->mean_diff = gs->sum_diff / gs->n;
1059 totals->mean = totals->sum / totals->n;
1060 totals->std_dev = sqrt (
1061 totals->n / (totals->n - 1) *
1062 (totals->ssq / totals->n - pow2 (totals->mean))
1065 totals->se_mean = totals->std_dev / sqrt (totals->n);