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/manager.h>
44 #include <output/table.h>
45 #include "sort-criteria.h"
46 #include <data/format.h>
51 #define _(msgid) gettext (msgid)
58 missing=miss:!analysis/listwise,
59 incl:include/!exclude;
60 +contrast= double list;
61 +statistics[st_]=descriptives,homogeneity.
66 static struct cmd_oneway cmd;
68 /* The independent variable */
69 static const struct variable *indep_var;
71 /* Number of dependent variables */
74 /* The dependent variables */
75 static const struct variable **vars;
78 /* A hash table containing all the distinct values of the independent
80 static struct hsh_table *global_group_hash;
82 /* The number of distinct values of the independent variable, when all
83 missing values are disregarded */
84 static int ostensible_number_of_groups = -1;
87 static void run_oneway (struct cmd_oneway *, struct casereader *,
88 const struct dataset *);
91 /* Routines to show the output tables */
92 static void show_anova_table(void);
93 static void show_descriptives (const struct dictionary *dict);
94 static void show_homogeneity(void);
96 static void show_contrast_coeffs (short *);
97 static void show_contrast_tests (short *);
100 enum stat_table_t {STAT_DESC = 1, STAT_HOMO = 2};
102 static enum stat_table_t stat_tables;
104 static void output_oneway (const struct dictionary *dict);
108 cmd_oneway (struct lexer *lexer, struct dataset *ds)
110 struct casegrouper *grouper;
111 struct casereader *group;
115 if ( !parse_oneway (lexer, ds, &cmd, NULL))
118 /* What statistics were requested */
119 if ( cmd.sbc_statistics)
122 for (i = 0; i < ONEWAY_ST_count; ++i)
124 if (! cmd.a_statistics[i]) continue;
128 case ONEWAY_ST_DESCRIPTIVES:
129 stat_tables |= STAT_DESC;
131 case ONEWAY_ST_HOMOGENEITY:
132 stat_tables |= STAT_HOMO;
138 /* Data pass. FIXME: error handling. */
139 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
140 while (casegrouper_get_next_group (grouper, &group))
141 run_oneway (&cmd, group, ds);
142 ok = casegrouper_destroy (grouper);
143 ok = proc_commit (ds) && ok;
148 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
153 output_oneway (const struct dictionary *dict)
158 bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast);
160 /* Check the sanity of the given contrast values */
161 for (i = 0; i < cmd.sbc_contrast; ++i)
167 if (subc_list_double_count (&cmd.dl_contrast[i]) !=
168 ostensible_number_of_groups)
171 _("Number of contrast coefficients must equal the number of groups"));
176 for (j = 0; j < ostensible_number_of_groups; ++j)
177 sum += subc_list_double_at (&cmd.dl_contrast[i], j);
180 msg (SW, _("Coefficients for contrast %zu do not total zero"), i + 1);
183 if ( stat_tables & STAT_DESC )
184 show_descriptives (dict);
186 if ( stat_tables & STAT_HOMO )
191 if (cmd.sbc_contrast )
193 show_contrast_coeffs (bad_contrast);
194 show_contrast_tests (bad_contrast);
200 for (i = 0; i < n_vars; ++i )
202 struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
204 hsh_destroy (group_hash);
207 hsh_destroy (global_group_hash);
211 /* Parser for the variables sub command */
213 oneway_custom_variables (struct lexer *lexer,
214 struct dataset *ds, struct cmd_oneway *cmd UNUSED,
217 struct dictionary *dict = dataset_dict (ds);
219 lex_match (lexer, '=');
221 if ((lex_token (lexer) != T_ID ||
222 dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
223 && lex_token (lexer) != T_ALL)
226 if (!parse_variables_const (lexer, dict, &vars, &n_vars,
228 | PV_NUMERIC | PV_NO_SCRATCH) )
236 if ( ! lex_match (lexer, T_BY))
239 indep_var = parse_variable (lexer, dict);
243 msg (SE, _("`%s' is not a variable name"), lex_tokid (lexer));
251 /* Show the ANOVA table */
253 show_anova_table (void)
257 size_t n_rows = n_vars * 3 + 1;
262 t = tab_create (n_cols, n_rows, 0);
263 tab_headers (t, 2, 0, 1, 0);
264 tab_dim (t, tab_natural_dimensions, NULL);
271 n_cols - 1, n_rows - 1);
273 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
274 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
275 tab_vline (t, TAL_0, 1, 0, 0);
277 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
278 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
279 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
280 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
281 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
284 for (i = 0; i < n_vars; ++i)
286 struct group_statistics *totals = &group_proc_get (vars[i])->ugs;
287 struct hsh_table *group_hash = group_proc_get (vars[i])->group_hash;
288 struct hsh_iterator g;
289 struct group_statistics *gs;
291 const char *s = var_to_string (vars[i]);
293 for (gs = hsh_first (group_hash, &g);
295 gs = hsh_next (group_hash, &g))
297 ssa += pow2 (gs->sum) / gs->n;
300 ssa -= pow2 (totals->sum) / totals->n;
302 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
303 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
304 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
305 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
308 tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
311 struct group_proc *gp = group_proc_get (vars[i]);
312 const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
313 const double df1 = gp->n_groups - 1;
314 const double df2 = totals->n - gp->n_groups;
315 const double msa = ssa / df1;
317 gp->mse = (sst - ssa) / df2;
320 /* Sums of Squares */
321 tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
322 tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
323 tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
326 /* Degrees of freedom */
327 tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
328 tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
329 tab_fixed (t, 3, i * 3 + 3, 0, totals->n - 1, 4, 0);
332 tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
333 tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
336 const double F = msa / gp->mse ;
339 tab_double (t, 5, i * 3 + 1, 0, F, NULL);
341 /* The significance */
342 tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
348 tab_title (t, _("ANOVA"));
353 /* Show the descriptives table */
355 show_descriptives (const struct dictionary *dict)
362 const double confidence = 0.95;
363 const double q = (1.0 - confidence) / 2.0;
365 const struct variable *wv = dict_get_weight (dict);
366 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
370 for ( v = 0; v < n_vars; ++v )
371 n_rows += group_proc_get (vars[v])->n_groups + 1;
373 t = tab_create (n_cols, n_rows, 0);
374 tab_headers (t, 2, 0, 2, 0);
375 tab_dim (t, tab_natural_dimensions, NULL);
378 /* Put a frame around the entire box, and vertical lines inside */
383 n_cols - 1, n_rows - 1);
385 /* Underline headers */
386 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
387 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
389 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
390 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
391 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
392 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
395 tab_vline (t, TAL_0, 7, 0, 0);
396 tab_hline (t, TAL_1, 6, 7, 1);
397 tab_joint_text (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
398 _("%g%% Confidence Interval for Mean"), confidence*100.0);
400 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
401 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
403 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
404 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
407 tab_title (t, _("Descriptives"));
411 for (v = 0; v < n_vars; ++v)
416 struct group_proc *gp = group_proc_get (vars[v]);
418 struct group_statistics *gs;
419 struct group_statistics *totals = &gp->ugs;
421 const char *s = var_to_string (vars[v]);
422 const struct fmt_spec *fmt = var_get_print_format (vars[v]);
424 struct group_statistics *const *gs_array =
425 (struct group_statistics *const *) hsh_sort (gp->group_hash);
428 tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
430 tab_hline (t, TAL_1, 0, n_cols - 1, row);
432 for (count = 0; count < hsh_count (gp->group_hash); ++count)
435 ds_init_empty (&vstr);
436 gs = gs_array[count];
438 var_append_value_name (indep_var, &gs->id, &vstr);
440 tab_text (t, 1, row + count,
441 TAB_LEFT | TAT_TITLE,
446 /* Now fill in the numbers ... */
448 tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
450 tab_double (t, 3, row + count, 0, gs->mean, NULL);
452 tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
454 std_error = gs->std_dev / sqrt (gs->n) ;
455 tab_double (t, 5, row + count, 0,
458 /* Now the confidence interval */
460 T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
462 tab_double (t, 6, row + count, 0,
463 gs->mean - T * std_error, NULL);
465 tab_double (t, 7, row + count, 0,
466 gs->mean + T * std_error, NULL);
470 tab_double (t, 8, row + count, 0, gs->minimum, fmt);
471 tab_double (t, 9, row + count, 0, gs->maximum, fmt);
474 tab_text (t, 1, row + count,
475 TAB_LEFT | TAT_TITLE, _("Total"));
477 tab_double (t, 2, row + count, 0, totals->n, wfmt);
479 tab_double (t, 3, row + count, 0, totals->mean, NULL);
481 tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
483 std_error = totals->std_dev / sqrt (totals->n) ;
485 tab_double (t, 5, row + count, 0, std_error, NULL);
487 /* Now the confidence interval */
489 T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
491 tab_double (t, 6, row + count, 0,
492 totals->mean - T * std_error, NULL);
494 tab_double (t, 7, row + count, 0,
495 totals->mean + T * std_error, NULL);
499 tab_double (t, 8, row + count, 0, totals->minimum, fmt);
500 tab_double (t, 9, row + count, 0, totals->maximum, fmt);
502 row += gp->n_groups + 1;
508 /* Show the homogeneity table */
510 show_homogeneity (void)
514 size_t n_rows = n_vars + 1;
519 t = tab_create (n_cols, n_rows, 0);
520 tab_headers (t, 1, 0, 1, 0);
521 tab_dim (t, tab_natural_dimensions, NULL);
523 /* Put a frame around the entire box, and vertical lines inside */
528 n_cols - 1, n_rows - 1);
531 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
532 tab_vline (t, TAL_2, 1, 0, n_rows - 1);
535 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
536 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
537 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
538 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
540 tab_title (t, _("Test of Homogeneity of Variances"));
542 for (v = 0; v < n_vars; ++v)
545 const struct variable *var = vars[v];
546 const struct group_proc *gp = group_proc_get (vars[v]);
547 const char *s = var_to_string (var);
548 const struct group_statistics *totals = &gp->ugs;
550 const double df1 = gp->n_groups - 1;
551 const double df2 = totals->n - gp->n_groups;
553 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
556 tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
557 tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
558 tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
560 /* Now the significance */
561 tab_double (t, 4, v + 1, TAB_RIGHT,gsl_cdf_fdist_Q (F, df1, df2), NULL);
568 /* Show the contrast coefficients table */
570 show_contrast_coeffs (short *bad_contrast)
572 int n_cols = 2 + ostensible_number_of_groups;
573 int n_rows = 2 + cmd.sbc_contrast;
575 void *const *group_values;
579 t = tab_create (n_cols, n_rows, 0);
580 tab_headers (t, 2, 0, 2, 0);
581 tab_dim (t, tab_natural_dimensions, NULL);
583 /* Put a frame around the entire box, and vertical lines inside */
588 n_cols - 1, n_rows - 1);
602 tab_hline (t, TAL_1, 2, n_cols - 1, 1);
603 tab_hline (t, TAL_2, 0, n_cols - 1, 2);
605 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
607 tab_title (t, _("Contrast Coefficients"));
609 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
612 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
613 var_to_string (indep_var));
615 group_values = hsh_sort (global_group_hash);
617 count < hsh_count (global_group_hash);
620 double *group_value_p;
621 union value group_value;
625 ds_init_empty (&vstr);
627 group_value_p = group_values[count];
628 group_value.f = *group_value_p;
629 var_append_value_name (indep_var, &group_value, &vstr);
631 tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
637 for (i = 0; i < cmd.sbc_contrast; ++i )
639 tab_text (t, 1, i + 2, TAB_CENTER | TAT_PRINTF, "%d", i + 1);
641 if ( bad_contrast[i] )
642 tab_text (t, count + 2, i + 2, TAB_RIGHT, "?" );
644 tab_text (t, count + 2, i + 2, TAB_RIGHT | TAT_PRINTF, "%g",
645 subc_list_double_at (&cmd.dl_contrast[i], count)
654 /* Show the results of the contrast tests */
656 show_contrast_tests (short *bad_contrast)
660 size_t n_rows = 1 + n_vars * 2 * cmd.sbc_contrast;
664 t = tab_create (n_cols, n_rows, 0);
665 tab_headers (t, 3, 0, 1, 0);
666 tab_dim (t, tab_natural_dimensions, NULL);
668 /* Put a frame around the entire box, and vertical lines inside */
673 n_cols - 1, n_rows - 1);
681 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
682 tab_vline (t, TAL_2, 3, 0, n_rows - 1);
685 tab_title (t, _("Contrast Tests"));
687 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
688 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
689 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
690 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
691 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
692 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
694 for (v = 0; v < n_vars; ++v)
697 int lines_per_variable = 2 * cmd.sbc_contrast;
700 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
701 var_to_string (vars[v]));
703 for (i = 0; i < cmd.sbc_contrast; ++i)
706 double contrast_value = 0.0;
707 double coef_msq = 0.0;
708 struct group_proc *grp_data = group_proc_get (vars[v]);
709 struct hsh_table *group_hash = grp_data->group_hash;
711 void *const *group_stat_array;
714 double std_error_contrast;
716 double sec_vneq = 0.0;
719 /* Note: The calculation of the degrees of freedom in the
720 "variances not equal" case is painfull!!
721 The following formula may help to understand it:
722 \frac{\left (\sum_{i=1}^k{c_i^2\frac{s_i^2}{n_i}}\right)^2}
725 \frac{\left (c_i^2\frac{s_i^2}{n_i}\right)^2} {n_i-1}
730 double df_denominator = 0.0;
731 double df_numerator = 0.0;
734 tab_text (t, 1, (v * lines_per_variable) + i + 1,
735 TAB_LEFT | TAT_TITLE,
736 _("Assume equal variances"));
738 tab_text (t, 1, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
739 TAB_LEFT | TAT_TITLE,
740 _("Does not assume equal"));
743 tab_text (t, 2, (v * lines_per_variable) + i + 1,
744 TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d", i + 1);
747 tab_text (t, 2, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
748 TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d", i + 1);
751 if ( bad_contrast[i])
754 group_stat_array = hsh_sort (group_hash);
756 for (ci = 0; ci < hsh_count (group_hash); ++ci)
758 const double coef = subc_list_double_at (&cmd.dl_contrast[i], ci);
759 struct group_statistics *gs = group_stat_array[ci];
761 const double winv = pow2 (gs->std_dev) / gs->n;
763 contrast_value += coef * gs->mean;
765 coef_msq += (coef * coef) / gs->n;
767 sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
769 df_numerator += (coef * coef) * winv;
770 df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
772 sec_vneq = sqrt (sec_vneq);
774 df_numerator = pow2 (df_numerator);
776 tab_double (t, 3, (v * lines_per_variable) + i + 1,
777 TAB_RIGHT, contrast_value, NULL);
779 tab_double (t, 3, (v * lines_per_variable) + i + 1 +
781 TAB_RIGHT, contrast_value, NULL);
783 std_error_contrast = sqrt (grp_data->mse * coef_msq);
786 tab_double (t, 4, (v * lines_per_variable) + i + 1,
787 TAB_RIGHT, std_error_contrast,
790 T = fabs (contrast_value / std_error_contrast);
794 tab_double (t, 5, (v * lines_per_variable) + i + 1,
798 df = grp_data->ugs.n - grp_data->n_groups;
800 /* Degrees of Freedom */
801 tab_fixed (t, 6, (v * lines_per_variable) + i + 1,
806 /* Significance TWO TAILED !!*/
807 tab_double (t, 7, (v * lines_per_variable) + i + 1,
808 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T, df),
811 /* Now for the Variances NOT Equal case */
815 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
819 T = contrast_value / sec_vneq;
821 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
825 df = df_numerator / df_denominator;
828 (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
832 /* The Significance */
834 tab_double (t, 7, (v * lines_per_variable) + i + 1 + cmd.sbc_contrast,
835 TAB_RIGHT, 2 * gsl_cdf_tdist_Q (T,df),
840 tab_hline (t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);
847 /* ONEWAY ANOVA Calculations */
849 static void postcalc (struct cmd_oneway *cmd UNUSED);
851 static void precalc (struct cmd_oneway *cmd UNUSED);
855 /* Pre calculations */
857 precalc (struct cmd_oneway *cmd UNUSED)
861 for (i = 0; i < n_vars; ++i)
863 struct group_proc *gp = group_proc_get (vars[i]);
864 struct group_statistics *totals = &gp->ugs;
866 /* Create a hash for each of the dependent variables.
867 The hash contains a group_statistics structure,
868 and is keyed by value of the independent variable */
870 gp->group_hash = hsh_create (4, compare_group, hash_group,
871 (hsh_free_func *) free_group,
877 totals->sum_diff = 0;
878 totals->maximum = -DBL_MAX;
879 totals->minimum = DBL_MAX;
884 compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
886 const double *a = a_;
887 const double *b = b_;
888 return *a < *b ? -1 : *a > *b;
892 do_hash_double (const void *value_, const void *aux UNUSED)
894 const double *value = value_;
895 return hash_double (*value, 0);
899 free_double (void *value_, const void *aux UNUSED)
901 double *value = value_;
906 run_oneway (struct cmd_oneway *cmd,
907 struct casereader *input,
908 const struct dataset *ds)
911 struct dictionary *dict = dataset_dict (ds);
912 enum mv_class exclude;
913 struct casereader *reader;
916 c = casereader_peek (input, 0);
919 casereader_destroy (input);
922 output_split_file_values (ds, c);
925 taint = taint_clone (casereader_get_taint (input));
927 global_group_hash = hsh_create (4,
935 exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
936 input = casereader_create_filter_missing (input, &indep_var, 1,
937 exclude, NULL, NULL);
938 if (cmd->miss == ONEWAY_LISTWISE)
939 input = casereader_create_filter_missing (input, vars, n_vars,
940 exclude, NULL, NULL);
941 input = casereader_create_filter_weight (input, dict, NULL, NULL);
943 reader = casereader_clone (input);
944 for (; (c = casereader_read (reader)) != NULL; case_unref (c))
948 const double weight = dict_get_case_weight (dict, c, NULL);
950 const union value *indep_val = case_data (c, indep_var);
951 void **p = hsh_probe (global_group_hash, &indep_val->f);
954 double *value = *p = xmalloc (sizeof *value);
955 *value = indep_val->f;
958 for (i = 0; i < n_vars; ++i)
960 const struct variable *v = vars[i];
962 const union value *val = case_data (c, v);
964 struct group_proc *gp = group_proc_get (vars[i]);
965 struct hsh_table *group_hash = gp->group_hash;
967 struct group_statistics *gs;
969 gs = hsh_find (group_hash, indep_val );
973 gs = xmalloc (sizeof *gs);
979 gs->minimum = DBL_MAX;
980 gs->maximum = -DBL_MAX;
982 hsh_insert ( group_hash, gs );
985 if (!var_is_value_missing (v, val, exclude))
987 struct group_statistics *totals = &gp->ugs;
990 totals->sum += weight * val->f;
991 totals->ssq += weight * pow2 (val->f);
993 if ( val->f * weight < totals->minimum )
994 totals->minimum = val->f * weight;
996 if ( val->f * weight > totals->maximum )
997 totals->maximum = val->f * weight;
1000 gs->sum += weight * val->f;
1001 gs->ssq += weight * pow2 (val->f);
1003 if ( val->f * weight < gs->minimum )
1004 gs->minimum = val->f * weight;
1006 if ( val->f * weight > gs->maximum )
1007 gs->maximum = val->f * weight;
1010 gp->n_groups = hsh_count ( group_hash );
1014 casereader_destroy (reader);
1019 if ( stat_tables & STAT_HOMO )
1020 levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
1022 casereader_destroy (input);
1024 ostensible_number_of_groups = hsh_count (global_group_hash);
1026 if (!taint_has_tainted_successor (taint))
1027 output_oneway (dict);
1029 taint_destroy (taint);
1033 /* Post calculations for the ONEWAY command */
1035 postcalc ( struct cmd_oneway *cmd UNUSED )
1039 for (i = 0; i < n_vars; ++i)
1041 struct group_proc *gp = group_proc_get (vars[i]);
1042 struct hsh_table *group_hash = gp->group_hash;
1043 struct group_statistics *totals = &gp->ugs;
1045 struct hsh_iterator g;
1046 struct group_statistics *gs;
1048 for (gs = hsh_first (group_hash, &g);
1050 gs = hsh_next (group_hash, &g))
1052 gs->mean = gs->sum / gs->n;
1053 gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
1055 gs->std_dev = sqrt (
1056 gs->n / (gs->n - 1) *
1057 ( gs->ssq / gs->n - pow2 (gs->mean))
1060 gs->se_mean = gs->std_dev / sqrt (gs->n);
1061 gs->mean_diff = gs->sum_diff / gs->n;
1064 totals->mean = totals->sum / totals->n;
1065 totals->std_dev = sqrt (
1066 totals->n / (totals->n - 1) *
1067 (totals->ssq / totals->n - pow2 (totals->mean))
1070 totals->se_mean = totals->std_dev / sqrt (totals->n);