Rename procedure.[ch] to dataset.[ch].
[pspp-builds.git] / src / language / stats / oneway.c
index e210bc4772214912ef0341e28a2a0820d1ef00c2..a63d1a29e532876ebadb1fbf4d2f8100e0f83021 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2007, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 
-#include <data/case.h>
-#include <data/casegrouper.h>
-#include <data/casereader.h>
-
-#include <math/covariance.h>
-#include <math/categoricals.h>
-#include <gsl/gsl_matrix.h>
-#include <linreg/sweep.h>
-
-#include <libpspp/ll.h>
-
-#include <language/lexer/lexer.h>
-#include <language/lexer/variable-parser.h>
-#include <language/lexer/value-parser.h>
-#include <language/command.h>
-
-#include <data/procedure.h>
-#include <data/dictionary.h>
-
-
-#include <language/dictionary/split-file.h>
-#include <libpspp/hash.h>
-#include <libpspp/taint.h>
-#include <math/group-proc.h>
-#include <math/levene.h>
-#include <libpspp/misc.h>
-
-#include <output/tab.h>
-
 #include <gsl/gsl_cdf.h>
+#include <gsl/gsl_matrix.h>
 #include <math.h>
-#include <data/format.h>
 
-#include <libpspp/message.h>
+#include "data/case.h"
+#include "data/casegrouper.h"
+#include "data/casereader.h"
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/format.h"
+#include "data/value.h"
+#include "language/command.h"
+#include "language/dictionary/split-file.h"
+#include "language/lexer/lexer.h"
+#include "language/lexer/value-parser.h"
+#include "language/lexer/variable-parser.h"
+#include "libpspp/ll.h"
+#include "libpspp/message.h"
+#include "libpspp/misc.h"
+#include "libpspp/taint.h"
+#include "linreg/sweep.h"
+#include "math/categoricals.h"
+#include "math/covariance.h"
+#include "math/levene.h"
+#include "math/moments.h"
+#include "output/tab.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
+
 enum missing_type
   {
     MISS_LISTWISE,
@@ -98,12 +91,23 @@ struct oneway_spec
 
   /* The weight variable */
   const struct variable *wv;
+
 };
 
+/* Per category data */
+struct descriptive_data
+{
+  const struct variable *var;
+  struct moments1 *mom;
+
+  double minimum;
+  double maximum;
+};
 
 /* Workspace variable for each dependent variable */
 struct per_var_ws
 {
+  struct categoricals *cat;
   struct covariance *cov;
 
   double sst;
@@ -112,7 +116,8 @@ struct per_var_ws
 
   int n_groups;
 
-  double cc;
+  double mse;
+  double levene_w;
 };
 
 struct oneway_workspace
@@ -121,16 +126,15 @@ struct oneway_workspace
      missing values are disregarded */
   int actual_number_of_groups;
 
-  /* A  hash table containing all the distinct values of the independent
-     variable */
-  struct hsh_table *group_hash;
-
   struct per_var_ws *vws;
+
+  /* An array of descriptive data.  One for each dependent variable */
+  struct descriptive_data **dd_total;
 };
 
 /* Routines to show the output tables */
 static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
-static void show_descriptives (const struct oneway_spec *);
+static void show_descriptives (const struct oneway_spec *, const struct oneway_workspace *);
 static void show_homogeneity (const struct oneway_spec *, const struct oneway_workspace *);
 
 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
@@ -152,13 +156,13 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
   ll_init (&oneway.contrast_list);
 
   
-  if ( lex_match (lexer, '/'))
+  if ( lex_match (lexer, T_SLASH))
     {
       if (!lex_force_match_id (lexer, "VARIABLES"))
        {
          goto error;
        }
-      lex_match (lexer, '=');
+      lex_match (lexer, T_EQUALS);
     }
 
   if (!parse_variables_const (lexer, dict,
@@ -170,14 +174,14 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
 
   oneway.indep_var = parse_variable_const (lexer, dict);
 
-  while (lex_token (lexer) != '.')
+  while (lex_token (lexer) != T_ENDCMD)
     {
-      lex_match (lexer, '/');
+      lex_match (lexer, T_SLASH);
 
       if (lex_match_id (lexer, "STATISTICS"))
        {
-          lex_match (lexer, '=');
-          while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
+          lex_match (lexer, T_EQUALS);
+          while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
            {
              if (lex_match_id (lexer, "DESCRIPTIVES"))
                {
@@ -199,11 +203,11 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
          struct contrasts_node *cl = xzalloc (sizeof *cl);
 
          struct ll_list *coefficient_list = &cl->coefficient_list;
-          lex_match (lexer, '=');
+          lex_match (lexer, T_EQUALS);
 
          ll_init (coefficient_list);
 
-          while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
+          while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
            {
              if ( lex_is_number (lexer))
                {
@@ -224,8 +228,8 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
        }
       else if (lex_match_id (lexer, "MISSING"))
         {
-          lex_match (lexer, '=');
-          while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
+          lex_match (lexer, T_EQUALS);
+          while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
             {
              if (lex_match_id (lexer, "INCLUDE"))
                {
@@ -263,8 +267,6 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
     struct casereader *group;
     bool ok;
 
-
-
     grouper = casegrouper_create_splits (proc_open (ds), dict);
     while (casegrouper_get_next_group (grouper, &group))
       run_oneway (&oneway, group, ds);
@@ -283,31 +285,82 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
 
 \f
 
-static int
-compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
+
+static struct descriptive_data *
+dd_create (const struct variable *var)
 {
-  const double *a = a_;
-  const double *b = b_;
-  return *a < *b ? -1 : *a > *b;
+  struct descriptive_data *dd = xmalloc (sizeof *dd);
+
+  dd->mom = moments1_create (MOMENT_VARIANCE);
+  dd->minimum = DBL_MAX;
+  dd->maximum = -DBL_MAX;
+  dd->var = var;
+
+  return dd;
 }
 
-static unsigned
-do_hash_double (const void *value_, const void *aux UNUSED)
+static void
+dd_destroy (struct descriptive_data *dd)
 {
-  const double *value = value_;
-  return hash_double (*value, 0);
+  moments1_destroy (dd->mom);
+  free (dd);
 }
 
-static void
-free_double (void *value_, const void *aux UNUSED)
+static void *
+makeit (void *aux1, void *aux2 UNUSED)
 {
-  double *value = value_;
-  free (value);
+  const struct variable *var = aux1;
+
+  struct descriptive_data *dd = dd_create (var);
+
+  return dd;
 }
 
-static void postcalc (const struct oneway_spec *cmd);
-static void  precalc (const struct oneway_spec *cmd);
+static void 
+updateit (void *user_data, 
+         enum mv_class exclude,
+         const struct variable *wv, 
+         const struct variable *catvar UNUSED,
+         const struct ccase *c,
+         void *aux1, void *aux2)
+{
+  struct descriptive_data *dd = user_data;
+
+  const struct variable *varp = aux1;
+
+  const union value *valx = case_data (c, varp);
+
+  struct descriptive_data *dd_total = aux2;
 
+  double weight;
+
+  if ( var_is_value_missing (varp, valx, exclude))
+    return;
+
+  weight = wv != NULL ? case_data (c, wv)->f : 1.0;
+
+  moments1_add (dd->mom, valx->f, weight);
+  if (valx->f < dd->minimum)
+    dd->minimum = valx->f;
+
+  if (valx->f > dd->maximum)
+    dd->maximum = valx->f;
+
+  {
+    const struct variable *var = dd_total->var;
+    const union value *val = case_data (c, var);
+
+    moments1_add (dd_total->mom,
+                 val->f,
+                 weight);
+
+    if (val->f < dd_total->minimum)
+      dd_total->minimum = val->f;
+
+    if (val->f > dd_total->maximum)
+      dd_total->maximum = val->f;
+  }
+}
 
 static void
 run_oneway (const struct oneway_spec *cmd,
@@ -320,42 +373,39 @@ run_oneway (const struct oneway_spec *cmd,
   struct casereader *reader;
   struct ccase *c;
 
-
   struct oneway_workspace ws;
 
-  ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
+  ws.actual_number_of_groups = 0;
+  ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
+  ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
 
+  for (v = 0 ; v < cmd->n_vars; ++v)
+    ws.dd_total[v] = dd_create (cmd->vars[v]);
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
-      struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
-                                                  cmd->wv, cmd->exclude);
+      ws.vws[v].cat = categoricals_create (&cmd->indep_var, 1, cmd->wv,
+                                           cmd->exclude, makeit, updateit,
+                                           CONST_CAST (struct variable *,
+                                                       cmd->vars[v]),
+                                           ws.dd_total[v]);
 
       ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
-                                              cats
+                                              ws.vws[v].cat
                                               cmd->wv, cmd->exclude);
-      ws.vws[v].cc = 0;
     }
 
   c = casereader_peek (input, 0);
   if (c == NULL)
     {
       casereader_destroy (input);
-      return;
+      goto finish;
     }
   output_split_file_values (ds, c);
   case_unref (c);
 
   taint = taint_clone (casereader_get_taint (input));
 
-  ws.group_hash = hsh_create (4,
-                                 compare_double_3way,
-                                 do_hash_double,
-                                 free_double,
-                                 cmd->indep_var);
-
-  precalc (cmd);
-
   input = casereader_create_filter_missing (input, &cmd->indep_var, 1,
                                             cmd->exclude, NULL, NULL);
   if (cmd->missing_type == MISS_LISTWISE)
@@ -363,84 +413,35 @@ run_oneway (const struct oneway_spec *cmd,
                                               cmd->exclude, NULL, NULL);
   input = casereader_create_filter_weight (input, dict, NULL, NULL);
 
+
+  if (cmd->stats & STATS_HOMOGENEITY)
+    for (v = 0; v < cmd->n_vars; ++v)
+      {
+       struct per_var_ws *pvw = &ws.vws[v];
+
+       pvw->levene_w = levene (input, cmd->indep_var, cmd->vars[v], cmd->wv, cmd->exclude);
+      }
+
   reader = casereader_clone (input);
 
   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
     {
-      size_t i;
-
-      const double weight = dict_get_case_weight (dict, c, NULL);
-
-      const union value *indep_val = case_data (c, cmd->indep_var);
-      void **p = hsh_probe (ws.group_hash, &indep_val->f);
-      if (*p == NULL)
-        {
-          double *value = *p = xmalloc (sizeof *value);
-          *value = indep_val->f;
-        }
+      int i;
 
       for (i = 0; i < cmd->n_vars; ++i)
        {
-         {
-           struct per_var_ws *pvw = &ws.vws[i];
-
-           pvw->cc += weight;
-           covariance_accumulate_pass1 (pvw->cov, c);
-         }
-
+         struct per_var_ws *pvw = &ws.vws[i];
          const struct variable *v = cmd->vars[i];
-
          const union value *val = case_data (c, v);
 
-          struct group_proc *gp = group_proc_get (cmd->vars[i]);
-         struct hsh_table *group_hash = gp->group_hash;
-
-         struct group_statistics *gs;
-
-         gs = hsh_find (group_hash, indep_val );
-
-         if ( ! gs )
+         if ( MISS_ANALYSIS == cmd->missing_type)
            {
-             gs = xmalloc (sizeof *gs);
-             gs->id = *indep_val;
-             gs->sum = 0;
-             gs->n = 0;
-             gs->ssq = 0;
-             gs->sum_diff = 0;
-             gs->minimum = DBL_MAX;
-             gs->maximum = -DBL_MAX;
-
-             hsh_insert ( group_hash, gs );
+             if ( var_is_value_missing (v, val, cmd->exclude))
+               continue;
            }
 
-         if (!var_is_value_missing (v, val, cmd->exclude))
-           {
-             struct group_statistics *totals = &gp->ugs;
-
-             totals->n += weight;
-             totals->sum += weight * val->f;
-             totals->ssq += weight * pow2 (val->f);
-
-             if ( val->f * weight  < totals->minimum )
-               totals->minimum = val->f * weight;
-
-             if ( val->f * weight  > totals->maximum )
-               totals->maximum = val->f * weight;
-
-             gs->n += weight;
-             gs->sum += weight * val->f;
-             gs->ssq += weight * pow2 (val->f);
-
-             if ( val->f * weight  < gs->minimum )
-               gs->minimum = val->f * weight;
-
-             if ( val->f * weight  > gs->maximum )
-               gs->maximum = val->f * weight;
-           }
-
-         gp->n_groups = hsh_count (group_hash );
+         covariance_accumulate_pass1 (pvw->cov, c);
        }
-
     }
   casereader_destroy (reader);
   reader = casereader_clone (input);
@@ -450,6 +451,15 @@ run_oneway (const struct oneway_spec *cmd,
       for (i = 0; i < cmd->n_vars; ++i)
        {
          struct per_var_ws *pvw = &ws.vws[i];
+         const struct variable *v = cmd->vars[i];
+         const union value *val = case_data (c, v);
+
+         if ( MISS_ANALYSIS == cmd->missing_type)
+           {
+             if ( var_is_value_missing (v, val, cmd->exclude))
+               continue;
+           }
+
          covariance_accumulate_pass2 (pvw->cov, c);
        }
     }
@@ -461,8 +471,13 @@ run_oneway (const struct oneway_spec *cmd,
       gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
 
+      double n;
+      moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
+
       pvw->sst = gsl_matrix_get (cm, 0, 0);
 
+      //      gsl_matrix_fprintf (stdout, cm, "%g ");
+
       reg_sweep (cm, 0);
 
       pvw->sse = gsl_matrix_get (cm, 0, 0);
@@ -470,91 +485,38 @@ run_oneway (const struct oneway_spec *cmd,
       pvw->ssa = pvw->sst - pvw->sse;
 
       pvw->n_groups = categoricals_total (cats);
+
+      pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
+
+      gsl_matrix_free (cm);
     }
 
-  postcalc (cmd);
+  for (v = 0; v < cmd->n_vars; ++v)
+    {
+      const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
 
-  if ( cmd->stats & STATS_HOMOGENEITY )
-    levene (dict, casereader_clone (input), cmd->indep_var,
-           cmd->n_vars, cmd->vars, cmd->exclude);
+      categoricals_done (cats);
+      
+      if (categoricals_total (cats) > ws.actual_number_of_groups)
+       ws.actual_number_of_groups = categoricals_total (cats);
+    }
 
   casereader_destroy (input);
 
-  ws.actual_number_of_groups = hsh_count (ws.group_hash);
-
   if (!taint_has_tainted_successor (taint))
     output_oneway (cmd, &ws);
 
   taint_destroy (taint);
-}
-
-/* Pre calculations */
-static void
-precalc (const struct oneway_spec *cmd)
-{
-  size_t i = 0;
 
-  for (i = 0; i < cmd->n_vars; ++i)
+ finish:
+  for (v = 0; v < cmd->n_vars; ++v)
     {
-      struct group_proc *gp = group_proc_get (cmd->vars[i]);
-      struct group_statistics *totals = &gp->ugs;
-
-      /* Create a hash for each of the dependent variables.
-        The hash contains a group_statistics structure,
-        and is keyed by value of the independent variable */
-
-      gp->group_hash = hsh_create (4, compare_group, hash_group,
-                                  (hsh_free_func *) free_group,
-                                  cmd->indep_var);
-
-      totals->sum = 0;
-      totals->n = 0;
-      totals->ssq = 0;
-      totals->sum_diff = 0;
-      totals->maximum = -DBL_MAX;
-      totals->minimum = DBL_MAX;
+      covariance_destroy (ws.vws[v].cov);
+      dd_destroy (ws.dd_total[v]);
     }
-}
-
-/* Post calculations for the ONEWAY command */
-static void
-postcalc (const struct oneway_spec *cmd)
-{
-  size_t i = 0;
+  free (ws.vws);
+  free (ws.dd_total);
 
-  for (i = 0; i < cmd->n_vars; ++i)
-    {
-      struct group_proc *gp = group_proc_get (cmd->vars[i]);
-      struct hsh_table *group_hash = gp->group_hash;
-      struct group_statistics *totals = &gp->ugs;
-
-      struct hsh_iterator g;
-      struct group_statistics *gs;
-
-      for (gs =  hsh_first (group_hash, &g);
-          gs != 0;
-          gs = hsh_next (group_hash, &g))
-       {
-         gs->mean = gs->sum / gs->n;
-         gs->s_std_dev = sqrt (gs->ssq / gs->n - pow2 (gs->mean));
-
-         gs->std_dev = sqrt (
-                             gs->n / (gs->n - 1) *
-                             ( gs->ssq / gs->n - pow2 (gs->mean))
-                             );
-
-         gs->se_mean = gs->std_dev / sqrt (gs->n);
-         gs->mean_diff = gs->sum_diff / gs->n;
-       }
-
-      totals->mean = totals->sum / totals->n;
-      totals->std_dev = sqrt (
-                             totals->n / (totals->n - 1) *
-                             (totals->ssq / totals->n - pow2 (totals->mean))
-                             );
-
-      totals->se_mean = totals->std_dev / sqrt (totals->n);
-    }
 }
 
 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
@@ -590,30 +552,18 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
     }
 
   if (cmd->stats & STATS_DESCRIPTIVES)
-    show_descriptives (cmd);
+    show_descriptives (cmd, ws);
 
   if (cmd->stats & STATS_HOMOGENEITY)
     show_homogeneity (cmd, ws);
 
   show_anova_table (cmd, ws);
 
-
   if (ll_count (&cmd->contrast_list) > 0)
     {
       show_contrast_coeffs (cmd, ws);
       show_contrast_tests (cmd, ws);
     }
-
-
-  /* Clean up */
-  for (i = 0; i < cmd->n_vars; ++i )
-    {
-      struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
-
-      hsh_destroy (group_hash);
-    }
-
-  hsh_destroy (ws->group_hash);
 }
 
 
@@ -648,13 +598,17 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
 
   for (i = 0; i < cmd->n_vars; ++i)
     {
+      double n;
+      double df1, df2;
+      double msa;
+      const char *s = var_to_string (cmd->vars[i]);
       const struct per_var_ws *pvw = &ws->vws[i];
-      struct group_proc *gp = group_proc_get (cmd->vars[i]);
-      const double df1 = pvw->n_groups - 1;
-      const double df2 = pvw->cc - pvw->n_groups;
-      const double msa = pvw->ssa / df1;
 
-      const char *s = var_to_string (cmd->vars[i]);
+      moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
+
+      df1 = pvw->n_groups - 1;
+      df2 = n - pvw->n_groups;
+      msa = pvw->ssa / df1;
 
       tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
       tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
@@ -665,8 +619,6 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
        tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
 
 
-      gp->mse  = (pvw->sst - pvw->ssa) / df2;
-
       /* Sums of Squares */
       tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL);
       tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL);
@@ -676,14 +628,14 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
       /* Degrees of freedom */
       tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0);
       tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0);
-      tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0);
+      tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0);
 
       /* Mean Squares */
       tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
-      tab_double (t, 4, i * 3 + 2, TAB_RIGHT, gp->mse, NULL);
+      tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
 
       {
-       const double F = msa / gp->mse ;
+       const double F = msa / pvw->mse ;
 
        /* The F value */
        tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
@@ -700,7 +652,7 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
 
 /* Show the descriptives table */
 static void
-show_descriptives (const struct oneway_spec *cmd)
+show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
 {
   size_t v;
   int n_cols = 10;
@@ -710,17 +662,16 @@ show_descriptives (const struct oneway_spec *cmd)
   const double confidence = 0.95;
   const double q = (1.0 - confidence) / 2.0;
 
-  const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : & F_8_0;
+  const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
 
   int n_rows = 2;
 
-  for ( v = 0; v < cmd->n_vars; ++v )
-    n_rows += group_proc_get (cmd->vars[v])->n_groups + 1;
+  for (v = 0; v < cmd->n_vars; ++v)
+    n_rows += ws->actual_number_of_groups + 1;
 
   t = tab_create (n_cols, n_rows);
   tab_headers (t, 2, 0, 2, 0);
 
-
   /* Put a frame around the entire box, and vertical lines inside */
   tab_box (t,
           TAL_2, TAL_2,
@@ -750,39 +701,42 @@ show_descriptives (const struct oneway_spec *cmd)
   tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
   tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
 
-
   tab_title (t, _("Descriptives"));
 
-
   row = 2;
   for (v = 0; v < cmd->n_vars; ++v)
     {
-      double T;
-      double std_error;
-
-      struct group_proc *gp = group_proc_get (cmd->vars[v]);
-
-      struct group_statistics *gs;
-      struct group_statistics *totals = &gp->ugs;
-
       const char *s = var_to_string (cmd->vars[v]);
       const struct fmt_spec *fmt = var_get_print_format (cmd->vars[v]);
 
-      struct group_statistics *const *gs_array =
-       (struct group_statistics *const *) hsh_sort (gp->group_hash);
       int count = 0;
 
+      struct per_var_ws *pvw = &ws->vws[v];
+      const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
+
       tab_text (t, 0, row, TAB_LEFT | TAT_TITLE, s);
       if ( v > 0)
        tab_hline (t, TAL_1, 0, n_cols - 1, row);
 
-      for (count = 0; count < hsh_count (gp->group_hash); ++count)
+      for (count = 0; count < categoricals_total (cats); ++count)
        {
+         double T;
+         double n, mean, variance;
+         double std_dev, std_error ;
+
          struct string vstr;
+
+         const union value *gval = categoricals_get_value_by_subscript (cats, count);
+         const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, count);
+
+         moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
+
+         std_dev = sqrt (variance);
+         std_error = std_dev / sqrt (n) ;
+
          ds_init_empty (&vstr);
-         gs = gs_array[count];
 
-         var_append_value_name (cmd->indep_var, &gs->id, &vstr);
+         var_append_value_name (cmd->indep_var, gval, &vstr);
 
          tab_text (t, 1, row + count,
                    TAB_LEFT | TAT_TITLE,
@@ -792,61 +746,68 @@ show_descriptives (const struct oneway_spec *cmd)
 
          /* Now fill in the numbers ... */
 
-         tab_fixed (t, 2, row + count, 0, gs->n, 8, 0);
+         tab_double (t, 2, row + count, 0, n, wfmt);
+
+         tab_double (t, 3, row + count, 0, mean, NULL);
 
-         tab_double (t, 3, row + count, 0, gs->mean, NULL);
+         tab_double (t, 4, row + count, 0, std_dev, NULL);
 
-         tab_double (t, 4, row + count, 0, gs->std_dev, NULL);
 
-         std_error = gs->std_dev / sqrt (gs->n) ;
-         tab_double (t, 5, row + count, 0,
-                     std_error, NULL);
+         tab_double (t, 5, row + count, 0, std_error, NULL);
 
          /* Now the confidence interval */
 
-         T = gsl_cdf_tdist_Qinv (q, gs->n - 1);
+         T = gsl_cdf_tdist_Qinv (q, n - 1);
 
          tab_double (t, 6, row + count, 0,
-                     gs->mean - T * std_error, NULL);
+                     mean - T * std_error, NULL);
 
          tab_double (t, 7, row + count, 0,
-                     gs->mean + T * std_error, NULL);
+                     mean + T * std_error, NULL);
 
          /* Min and Max */
 
-         tab_double (t, 8, row + count, 0,  gs->minimum, fmt);
-         tab_double (t, 9, row + count, 0,  gs->maximum, fmt);
+         tab_double (t, 8, row + count, 0,  dd->minimum, fmt);
+         tab_double (t, 9, row + count, 0,  dd->maximum, fmt);
        }
 
-      tab_text (t, 1, row + count,
-               TAB_LEFT | TAT_TITLE, _("Total"));
+      {
+       double T;
+       double n, mean, variance;
+       double std_dev;
+       double std_error;
 
-      tab_double (t, 2, row + count, 0, totals->n, wfmt);
+       moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
 
-      tab_double (t, 3, row + count, 0, totals->mean, NULL);
+       std_dev = sqrt (variance);
+       std_error = std_dev / sqrt (n) ;
 
-      tab_double (t, 4, row + count, 0, totals->std_dev, NULL);
+       tab_text (t, 1, row + count,
+                 TAB_LEFT | TAT_TITLE, _("Total"));
 
-      std_error = totals->std_dev / sqrt (totals->n) ;
+       tab_double (t, 2, row + count, 0, n, wfmt);
 
-      tab_double (t, 5, row + count, 0, std_error, NULL);
+       tab_double (t, 3, row + count, 0, mean, NULL);
 
-      /* Now the confidence interval */
+       tab_double (t, 4, row + count, 0, std_dev, NULL);
 
-      T = gsl_cdf_tdist_Qinv (q, totals->n - 1);
+       tab_double (t, 5, row + count, 0, std_error, NULL);
 
-      tab_double (t, 6, row + count, 0,
-                 totals->mean - T * std_error, NULL);
+       /* Now the confidence interval */
+       T = gsl_cdf_tdist_Qinv (q, n - 1);
 
-      tab_double (t, 7, row + count, 0,
-                 totals->mean + T * std_error, NULL);
+       tab_double (t, 6, row + count, 0,
+                   mean - T * std_error, NULL);
 
-      /* Min and Max */
+       tab_double (t, 7, row + count, 0,
+                   mean + T * std_error, NULL);
 
-      tab_double (t, 8, row + count, 0,  totals->minimum, fmt);
-      tab_double (t, 9, row + count, 0,  totals->maximum, fmt);
+       /* Min and Max */
+       tab_double (t, 8, row + count, 0,  ws->dd_total[v]->minimum, fmt);
+       tab_double (t, 9, row + count, 0,  ws->dd_total[v]->maximum, fmt);
+      }
 
-      row += gp->n_groups + 1;
+      row += categoricals_total (cats) + 1;
     }
 
   tab_submit (t);
@@ -860,13 +821,9 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *
   int n_cols = 5;
   size_t n_rows = cmd->n_vars + 1;
 
-  struct tab_table *t;
-
-
-  t = tab_create (n_cols, n_rows);
+  struct tab_table *t = tab_create (n_cols, n_rows);
   tab_headers (t, 1, 0, 1, 0);
 
-
   /* Put a frame around the entire box, and vertical lines inside */
   tab_box (t,
           TAL_2, TAL_2,
@@ -887,19 +844,20 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
+      double n;
       const struct per_var_ws *pvw = &ws->vws[v];
-      const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
+      double F = pvw->levene_w;
 
       const struct variable *var = cmd->vars[v];
-      const struct group_proc *gp = group_proc_get (cmd->vars[v]);
       const char *s = var_to_string (var);
+      double df1, df2;
 
-      const double df1 = pvw->n_groups - 1;
-      const double df2 = pvw->cc - pvw->n_groups;
-      double F = gp->levene;
+      moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
 
-      tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
+      df1 = pvw->n_groups - 1;
+      df2 = n - pvw->n_groups;
 
+      tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
 
       tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL);
       tab_fixed (t, 2, v + 1, TAB_RIGHT, df1, 8, 0);
@@ -1035,7 +993,6 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
   tab_vline (t, TAL_2, 3, 0, n_rows - 1);
 
-
   tab_title (t, _("Contrast Tests"));
 
   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
@@ -1047,6 +1004,8 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
+      const struct per_var_ws *pvw = &ws->vws[v];
+      const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
       struct ll *cli;
       int i = 0;
       int lines_per_variable = 2 * n_contrasts;
@@ -1059,14 +1018,10 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
            ++i, cli = ll_next (cli))
        {
          struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
-         struct ll *coeffi = ll_head (&cn->coefficient_list);
-         int ci;
+         struct ll *coeffi ;
+         int ci = 0;
          double contrast_value = 0.0;
          double coef_msq = 0.0;
-         struct group_proc *grp_data = group_proc_get (cmd->vars[v]);
-         struct hsh_table *group_hash = grp_data->group_hash;
-
-         void *const *group_stat_array;
 
          double T;
          double std_error_contrast;
@@ -1086,6 +1041,11 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
 
          double df_denominator = 0.0;
          double df_numerator = 0.0;
+
+         double grand_n;
+         moments1_calculate (ws->dd_total[v]->mom, &grand_n, NULL, NULL, NULL, NULL);
+         df = grand_n - pvw->n_groups;
+
          if ( i == 0 )
            {
              tab_text (t,  1, (v * lines_per_variable) + i + 1,
@@ -1108,27 +1068,28 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
          if (cn->bad_count)
            continue;
 
-         group_stat_array = hsh_sort (group_hash);
-
-         for (ci = 0;
-              coeffi != ll_null (&cn->coefficient_list) && 
-                ci < hsh_count (group_hash);
+         for (coeffi = ll_head (&cn->coefficient_list);
+              coeffi != ll_null (&cn->coefficient_list);
               ++ci, coeffi = ll_next (coeffi))
            {
+             double n, mean, variance;
+             const struct descriptive_data *dd = categoricals_get_user_data_by_subscript (cats, ci);
              struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
              const double coef = cn->coeff; 
-             struct group_statistics *gs = group_stat_array[ci];
+             double winv ;
+
+             moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
 
-             const double winv = pow2 (gs->std_dev) / gs->n;
+             winv = variance / n;
 
-             contrast_value += coef * gs->mean;
+             contrast_value += coef * mean;
 
-             coef_msq += (coef * coef) / gs->n;
+             coef_msq += (pow2 (coef)) / n;
 
-             sec_vneq += (coef * coef) * pow2 (gs->std_dev) /gs->n;
+             sec_vneq += (pow2 (coef)) * variance / n;
 
-             df_numerator += (coef * coef) * winv;
-             df_denominator += pow2((coef * coef) * winv) / (gs->n - 1);
+             df_numerator += (pow2 (coef)) * winv;
+             df_denominator += pow2((pow2 (coef)) * winv) / (n - 1);
            }
 
          sec_vneq = sqrt (sec_vneq);
@@ -1142,7 +1103,7 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
                      n_contrasts,
                      TAB_RIGHT, contrast_value, NULL);
 
-         std_error_contrast = sqrt (grp_data->mse * coef_msq);
+         std_error_contrast = sqrt (pvw->mse * coef_msq);
 
          /* Std. Error */
          tab_double (t,  4, (v * lines_per_variable) + i + 1,
@@ -1157,7 +1118,6 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
                      TAB_RIGHT, T,
                      NULL);
 
-         df = grp_data->ugs.n - grp_data->n_groups;
 
          /* Degrees of Freedom */
          tab_fixed (t,  6, (v * lines_per_variable) + i + 1,