ONEWAY: fix crash in contrasts when dataset is empty
[pspp] / src / language / stats / oneway.c
index d08e3114c04d8155d6fe035000ba42745f05dc07..544889dfae4e3580c9ff11f3acb50c8801ea58ae 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, 2012, 2013, 2014 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 <math/moments.h>
+#include <float.h>
+#include <gsl/gsl_cdf.h>
 #include <gsl/gsl_matrix.h>
-#include <linreg/sweep.h>
+#include <math.h>
 
-#include <libpspp/ll.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 "tukey/tukey.h"
+#include "math/categoricals.h"
+#include "math/interaction.h"
+#include "math/covariance.h"
+#include "math/levene.h"
+#include "math/moments.h"
+#include "output/tab.h"
 
-#include <language/lexer/lexer.h>
-#include <language/lexer/variable-parser.h>
-#include <language/lexer/value-parser.h>
-#include <language/command.h>
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
 
-#include <data/procedure.h>
-#include <data/value.h>
-#include <data/dictionary.h>
+/* Workspace variable for each dependent variable */
+struct per_var_ws
+{
+  struct interaction *iact;
+  struct categoricals *cat;
+  struct covariance *cov;
+  struct levene *nl;
 
-#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>
+  double n;
 
-#include <output/tab.h>
+  double sst;
+  double sse;
+  double ssa;
 
-#include <gsl/gsl_cdf.h>
-#include <math.h>
-#include <data/format.h>
+  int n_groups;
 
-#include <libpspp/message.h>
+  double mse;
+};
 
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
+/* Per category data */
+struct descriptive_data
+{
+  const struct variable *var;
+  struct moments1 *mom;
+
+  double minimum;
+  double maximum;
+};
 
 enum missing_type
   {
@@ -78,8 +102,28 @@ struct contrasts_node
 {
   struct ll ll; 
   struct ll_list coefficient_list;
+};
 
-  bool bad_count; /* True if the number of coefficients does not equal the number of groups */
+
+struct oneway_spec;
+
+typedef double df_func (const struct per_var_ws *pvw, const struct moments1 *mom_i, const struct moments1 *mom_j);
+typedef double ts_func (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err);
+typedef double p1tail_func (double ts, double df1, double df2);
+
+typedef double pinv_func (double std_err, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j);
+
+
+struct posthoc
+{
+  const char *syntax;
+  const char *label;
+
+  df_func *dff;
+  ts_func *tsf;
+  p1tail_func *p1f;
+
+  pinv_func *pinv;
 };
 
 struct oneway_spec
@@ -100,31 +144,227 @@ struct oneway_spec
   /* The weight variable */
   const struct variable *wv;
 
+  /* The confidence level for multiple comparisons */
+  double alpha;
+
+  int *posthoc;
+  int n_posthoc;
 };
 
-/* Per category data */
-struct descriptive_data
+static double
+df_common (const struct per_var_ws *pvw, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
 {
-  const struct variable *var;
-  struct moments1 *mom;
+  return  pvw->n - pvw->n_groups;
+}
 
-  double minimum;
-  double maximum;
-};
+static double
+df_individual (const struct per_var_ws *pvw UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j)
+{
+  double n_i, var_i;
+  double n_j, var_j;
+  double nom,denom;
 
-/* Workspace variable for each dependent variable */
-struct per_var_ws
+  moments1_calculate (mom_i, &n_i, NULL, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, NULL, &var_j, 0, 0);
+  
+  if ( n_i <= 1.0 || n_j <= 1.0)
+    return SYSMIS;
+
+  nom = pow2 (var_i/n_i + var_j/n_j);
+  denom = pow2 (var_i/n_i) / (n_i - 1) + pow2 (var_j/n_j) / (n_j - 1);
+
+  return nom / denom;
+}
+
+static double lsd_pinv (double std_err, double alpha, double df, int k UNUSED, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
 {
-  struct covariance *cov;
+  return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / 2.0, df);
+}
 
-  double sst;
-  double sse;
-  double ssa;
+static double bonferroni_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
+{
+  const int m = k * (k - 1) / 2;
+  return std_err * gsl_cdf_tdist_Pinv (1.0 - alpha / (2.0 * m), df);
+}
 
-  int n_groups;
+static double sidak_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
+{
+  const double m = k * (k - 1) / 2;
+  double lp = 1.0 - exp (log (1.0 - alpha) / m ) ;
+  return std_err * gsl_cdf_tdist_Pinv (1.0 - lp / 2.0, df);
+}
+
+static double tukey_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
+{
+  if ( k < 2 || df < 2)
+    return SYSMIS;
+
+  return std_err / sqrt (2.0)  * qtukey (1 - alpha, 1.0, k, df, 1, 0);
+}
+
+static double scheffe_pinv (double std_err, double alpha, double df, int k, const struct moments1 *mom_i UNUSED, const struct moments1 *mom_j UNUSED)
+{
+  double x = (k - 1) * gsl_cdf_fdist_Pinv (1.0 - alpha, k - 1, df);
+  return std_err * sqrt (x);
+}
+
+static double gh_pinv (double std_err UNUSED, double alpha, double df, int k, const struct moments1 *mom_i, const struct moments1 *mom_j)
+{
+  double n_i, mean_i, var_i;
+  double n_j, mean_j, var_j;
+  double m;
+
+  moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
+
+  m = sqrt ((var_i/n_i + var_j/n_j) / 2.0);
+
+  if ( k < 2 || df < 2)
+    return SYSMIS;
+
+  return m * qtukey (1 - alpha, 1.0, k, df, 1, 0);
+}
+
+
+static double 
+multiple_comparison_sig (double std_err,
+                                      const struct per_var_ws *pvw,
+                                      const struct descriptive_data *dd_i, const struct descriptive_data *dd_j,
+                                      const struct posthoc *ph)
+{
+  int k = pvw->n_groups;
+  double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
+  double ts = ph->tsf (k, dd_i->mom, dd_j->mom, std_err);
+  if ( df == SYSMIS)
+    return SYSMIS;
+  return  ph->p1f (ts, k - 1, df);
+}
+
+static double 
+mc_half_range (const struct oneway_spec *cmd, const struct per_var_ws *pvw, double std_err, const struct descriptive_data *dd_i, const struct descriptive_data *dd_j, const struct posthoc *ph)
+{
+  int k = pvw->n_groups;
+  double df = ph->dff (pvw, dd_i->mom, dd_j->mom);
+  if ( df == SYSMIS)
+    return SYSMIS;
+
+  return ph->pinv (std_err, cmd->alpha, df, k, dd_i->mom, dd_j->mom);
+}
+
+static double tukey_1tailsig (double ts, double df1, double df2)
+{
+  double twotailedsig;
+
+  if (df2 < 2 || df1 < 1)
+    return SYSMIS;
+
+  twotailedsig = 1.0 - ptukey (ts, 1.0, df1 + 1, df2, 1, 0);
+
+  return twotailedsig / 2.0;
+}
+
+static double lsd_1tailsig (double ts, double df1 UNUSED, double df2)
+{
+  return ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
+}
+
+static double sidak_1tailsig (double ts, double df1, double df2)
+{
+  double ex = (df1 + 1.0) * df1 / 2.0;
+  double lsd_sig = 2 * lsd_1tailsig (ts, df1, df2);
+
+  return 0.5 * (1.0 - pow (1.0 - lsd_sig, ex));
+}
+
+static double bonferroni_1tailsig (double ts, double df1, double df2)
+{
+  const int m = (df1 + 1) * df1 / 2;
+
+  double p = ts < 0 ? gsl_cdf_tdist_P (ts, df2) : gsl_cdf_tdist_Q (ts, df2);
+  p *= m;
+
+  return p > 0.5 ? 0.5 : p;
+}
+
+static double scheffe_1tailsig (double ts, double df1, double df2)
+{
+  return 0.5 * gsl_cdf_fdist_Q (ts, df1, df2);
+}
+
+
+static double tukey_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
+{
+  double ts;
+  double n_i, mean_i, var_i;
+  double n_j, mean_j, var_j;
+
+  moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
+
+  ts =  (mean_i - mean_j) / std_err;
+  ts = fabs (ts) * sqrt (2.0);
+
+  return ts;
+}
+
+static double lsd_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
+{
+  double n_i, mean_i, var_i;
+  double n_j, mean_j, var_j;
+
+  moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
+
+  return (mean_i - mean_j) / std_err;
+}
+
+static double scheffe_test_stat (int k, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err)
+{
+  double t;
+  double n_i, mean_i, var_i;
+  double n_j, mean_j, var_j;
+
+  moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
+
+  t = (mean_i - mean_j) / std_err;
+  t = pow2 (t);
+  t /= k - 1;
+
+  return t;
+}
+
+static double gh_test_stat (int k UNUSED, const struct moments1 *mom_i, const struct moments1 *mom_j, double std_err UNUSED)
+{
+  double ts;
+  double thing;
+  double n_i, mean_i, var_i;
+  double n_j, mean_j, var_j;
+
+  moments1_calculate (mom_i, &n_i, &mean_i, &var_i, 0, 0);  
+  moments1_calculate (mom_j, &n_j, &mean_j, &var_j, 0, 0);
+
+  thing = var_i / n_i + var_j / n_j;
+  thing /= 2.0;
+  thing = sqrt (thing);
+
+  ts = (mean_i - mean_j) / thing;
+
+  return fabs (ts);
+}
+
+
+
+static const struct posthoc ph_tests [] = 
+  {
+    { "LSD",        N_("LSD"),          df_common, lsd_test_stat,     lsd_1tailsig,          lsd_pinv},
+    { "TUKEY",      N_("Tukey HSD"),    df_common, tukey_test_stat,   tukey_1tailsig,        tukey_pinv},
+    { "BONFERRONI", N_("Bonferroni"),   df_common, lsd_test_stat,     bonferroni_1tailsig,   bonferroni_pinv},
+    { "SCHEFFE",    N_("Scheffé"),      df_common, scheffe_test_stat, scheffe_1tailsig,      scheffe_pinv},
+    { "GH",         N_("Games-Howell"), df_individual, gh_test_stat,  tukey_1tailsig,        gh_pinv},
+    { "SIDAK",      N_("Šidák"),        df_common, lsd_test_stat,     sidak_1tailsig,        sidak_pinv}
+  };
 
-  double mse;
-};
 
 struct oneway_workspace
 {
@@ -132,10 +372,6 @@ 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 */
@@ -150,6 +386,37 @@ static void show_homogeneity (const struct oneway_spec *, const struct oneway_wo
 static void output_oneway (const struct oneway_spec *, struct oneway_workspace *ws);
 static void run_oneway (const struct oneway_spec *cmd, struct casereader *input, const struct dataset *ds);
 
+
+static void
+destroy_coeff_list (struct contrasts_node *coeff_list)
+{
+  struct coeff_node *cn = NULL;
+  struct coeff_node *cnx = NULL;
+  struct ll_list *cl = &coeff_list->coefficient_list;
+  
+  ll_for_each_safe (cn, cnx, struct coeff_node, ll, cl)
+    {
+      free (cn);
+    }
+  
+  free (coeff_list);
+}
+
+static void
+oneway_cleanup (struct oneway_spec *cmd)
+{
+  struct contrasts_node *coeff_list  = NULL;
+  struct contrasts_node *coeff_next  = NULL;
+  ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
+    {
+      destroy_coeff_list (coeff_list);
+    }
+
+  free (cmd->posthoc);
+}
+
+
+
 int
 cmd_oneway (struct lexer *lexer, struct dataset *ds)
 {
@@ -162,17 +429,20 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
   oneway.missing_type = MISS_ANALYSIS;
   oneway.exclude = MV_ANY;
   oneway.wv = dict_get_weight (dict);
+  oneway.alpha = 0.05;
+  oneway.posthoc = NULL;
+  oneway.n_posthoc = 0;
 
   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,
@@ -184,14 +454,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"))
                {
@@ -208,16 +478,55 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
                }
            }
        }
+      else if (lex_match_id (lexer, "POSTHOC"))
+       {
+          lex_match (lexer, T_EQUALS);
+          while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
+           {
+             int p;
+             bool method = false;
+             for (p = 0 ; p < sizeof (ph_tests) / sizeof (struct posthoc); ++p)
+               {
+                 if (lex_match_id (lexer, ph_tests[p].syntax))
+                   {
+                     oneway.n_posthoc++;
+                     oneway.posthoc = xrealloc (oneway.posthoc, sizeof (*oneway.posthoc) * oneway.n_posthoc);
+                     oneway.posthoc[oneway.n_posthoc - 1] = p;
+                     method = true;
+                     break;
+                   }
+               }
+             if ( method == false)
+               {
+                 if (lex_match_id (lexer, "ALPHA"))
+                   {
+                     if ( !lex_force_match (lexer, T_LPAREN))
+                       goto error;
+                     lex_force_num (lexer);
+                     oneway.alpha = lex_number (lexer);
+                     lex_get (lexer);
+                     if ( !lex_force_match (lexer, T_RPAREN))
+                       goto error;
+                   }
+                 else
+                   {
+                     msg (SE, _("The post hoc analysis method %s is not supported."), lex_tokcstr (lexer));
+                     lex_error (lexer, NULL);
+                     goto error;
+                   }
+               }
+           }
+       }
       else if (lex_match_id (lexer, "CONTRAST"))
        {
          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))
                {
@@ -229,6 +538,7 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
                }
              else
                {
+                 destroy_coeff_list (cl);
                  lex_error (lexer, NULL);
                  goto error;
                }
@@ -238,8 +548,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"))
                {
@@ -284,10 +594,12 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
     ok = proc_commit (ds) && ok;
   }
 
+  oneway_cleanup (&oneway);
   free (oneway.vars);
   return CMD_SUCCESS;
 
  error:
+  oneway_cleanup (&oneway);
   free (oneway.vars);
   return CMD_FAILURE;
 }
@@ -295,32 +607,6 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
 
 \f
 
-static int
-compare_double_3way (const void *a_, const void *b_, const void *aux UNUSED)
-{
-  const double *a = a_;
-  const double *b = b_;
-  return *a < *b ? -1 : *a > *b;
-}
-
-static unsigned
-do_hash_double (const void *value_, const void *aux UNUSED)
-{
-  const double *value = value_;
-  return hash_double (*value, 0);
-}
-
-static void
-free_double (void *value_, const void *aux UNUSED)
-{
-  double *value = value_;
-  free (value);
-}
-
-
-
-static void postcalc (const struct oneway_spec *cmd);
-static void  precalc (const struct oneway_spec *cmd);
 
 static struct descriptive_data *
 dd_create (const struct variable *var)
@@ -335,9 +621,15 @@ dd_create (const struct variable *var)
   return dd;
 }
 
+static void
+dd_destroy (struct descriptive_data *dd)
+{
+  moments1_destroy (dd->mom);
+  free (dd);
+}
 
 static void *
-makeit (void *aux1, void *aux2 UNUSED)
+makeit (const void *aux1, void *aux2 UNUSED)
 {
   const struct variable *var = aux1;
 
@@ -347,12 +639,17 @@ makeit (void *aux1, void *aux2 UNUSED)
 }
 
 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)
+killit (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
+{
+  struct descriptive_data *dd = user_data;
+
+  dd_destroy (dd);
+}
+
+
+static void 
+updateit (const void *aux1, void *aux2, void *user_data,
+         const struct ccase *c, double weight)
 {
   struct descriptive_data *dd = user_data;
 
@@ -360,21 +657,14 @@ updateit (void *user_data,
 
   const union value *valx = case_data (c, varp);
 
-  if ( var_is_value_missing (varp, valx, exclude))
-    return;
-
   struct descriptive_data *dd_total = aux2;
 
-  double weight = 1.0;
-  if (wv)
-    weight = case_data (c, wv)->f;
-
   moments1_add (dd->mom, valx->f, weight);
-  if (valx->f * weight < dd->minimum)
-    dd->minimum = valx->f * weight;
+  if (valx->f < dd->minimum)
+    dd->minimum = valx->f;
 
-  if (valx->f * weight > dd->maximum)
-    dd->maximum = valx->f * weight;
+  if (valx->f > dd->maximum)
+    dd->maximum = valx->f;
 
   {
     const struct variable *var = dd_total->var;
@@ -384,11 +674,11 @@ updateit (void *user_data,
                  val->f,
                  weight);
 
-    if (val->f * weight < dd_total->minimum)
-      dd_total->minimum = val->f * weight;
+    if (val->f < dd_total->minimum)
+      dd_total->minimum = val->f;
 
-    if (val->f * weight > dd_total->maximum)
-      dd_total->maximum = val->f * weight;
+    if (val->f > dd_total->maximum)
+      dd_total->maximum = val->f;
   }
 }
 
@@ -406,7 +696,7 @@ run_oneway (const struct oneway_spec *cmd,
   struct oneway_workspace ws;
 
   ws.actual_number_of_groups = 0;
-  ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
+  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)
@@ -414,36 +704,38 @@ run_oneway (const struct oneway_spec *cmd,
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
-      struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
-                                                      cmd->wv, cmd->exclude, 
-                                                      makeit,
-                                                      updateit,
-                                                      cmd->vars[v], ws.dd_total[v]);
+      struct payload payload;
+      payload.create = makeit;
+      payload.update = updateit;
+      payload.calculate = NULL;
+      payload.destroy = killit;
+
+      ws.vws[v].iact = interaction_create (cmd->indep_var);
+      ws.vws[v].cat = categoricals_create (&ws.vws[v].iact, 1, cmd->wv,
+                                           cmd->exclude, cmd->exclude);
+
+      categoricals_set_payload (ws.vws[v].cat, &payload, 
+                               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].nl = levene_create (var_get_width (cmd->indep_var), NULL);
     }
 
   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)
@@ -452,23 +744,14 @@ run_oneway (const struct oneway_spec *cmd,
   input = casereader_create_filter_weight (input, dict, NULL, NULL);
 
   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;
+      double w = dict_get_case_weight (dict, c, NULL);
 
       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);
 
@@ -478,67 +761,41 @@ run_oneway (const struct oneway_spec *cmd,
                continue;
            }
 
-         {
-           struct per_var_ws *pvw = &ws.vws[i];
-
-           covariance_accumulate_pass1 (pvw->cov, c);
-         }
-
-          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 );
+         covariance_accumulate_pass1 (pvw->cov, c);
+         levene_pass_one (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
+       }
+    }
+  casereader_destroy (reader);
 
-         if ( ! gs )
-           {
-             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 );
-           }
+  reader = casereader_clone (input);
+  for ( ; (c = casereader_read (reader) ); case_unref (c))
+    {
+      int i;
+      double w = dict_get_case_weight (dict, c, NULL);
+      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 (!var_is_value_missing (v, val, cmd->exclude))
+         if ( MISS_ANALYSIS == cmd->missing_type)
            {
-             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;
+             if ( var_is_value_missing (v, val, cmd->exclude))
+               continue;
            }
 
-         gp->n_groups = hsh_count (group_hash );
+         covariance_accumulate_pass2 (pvw->cov, c);
+         levene_pass_two (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
        }
-
     }
   casereader_destroy (reader);
+
   reader = casereader_clone (input);
   for ( ; (c = casereader_read (reader) ); case_unref (c))
     {
       int i;
+      double w = dict_get_case_weight (dict, c, NULL);
+
       for (i = 0; i < cmd->n_vars; ++i)
        {
          struct per_var_ws *pvw = &ws.vws[i];
@@ -551,51 +808,61 @@ run_oneway (const struct oneway_spec *cmd,
                continue;
            }
 
-         covariance_accumulate_pass2 (pvw->cov, c);
+         levene_pass_three (pvw->nl, val->f, w, case_data (c, cmd->indep_var));
        }
     }
   casereader_destroy (reader);
 
+
   for (v = 0; v < cmd->n_vars; ++v)
     {
+      const gsl_matrix *ucm;
+      gsl_matrix *cm;
       struct per_var_ws *pvw = &ws.vws[v];
-      gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov);
       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
+      const bool ok = categoricals_sane (cats);
 
-      double n;
-      moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
+      if ( ! ok)
+       {
+         msg (MW, 
+              _("Dependent variable %s has no non-missing values.  No analysis for this variable will be done."),
+              var_get_name (cmd->vars[v]));
+         continue;
+       }
 
-      pvw->sst = gsl_matrix_get (cm, 0, 0);
+      ucm = covariance_calculate_unnormalized (pvw->cov);
+
+      cm = gsl_matrix_alloc (ucm->size1, ucm->size2);
+      gsl_matrix_memcpy (cm, ucm);
 
-      //      gsl_matrix_fprintf (stdout, cm, "%g ");
+      moments1_calculate (ws.dd_total[v]->mom, &pvw->n, NULL, NULL, NULL, NULL);
+
+      pvw->sst = gsl_matrix_get (cm, 0, 0);
 
       reg_sweep (cm, 0);
 
       pvw->sse = gsl_matrix_get (cm, 0, 0);
+      gsl_matrix_free (cm);
 
       pvw->ssa = pvw->sst - pvw->sse;
 
-      pvw->n_groups = categoricals_total (cats);
+      pvw->n_groups = categoricals_n_total (cats);
 
-      pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
+      pvw->mse = (pvw->sst - pvw->ssa) / (pvw->n - pvw->n_groups);
     }
 
-  postcalc (cmd);
-
-  
   for (v = 0; v < cmd->n_vars; ++v)
     {
-      struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
+      const struct categoricals *cats = covariance_get_categoricals (ws.vws[v].cov);
 
-      categoricals_done (cats);
-      
-      if (categoricals_total (cats) > ws.actual_number_of_groups)
-       ws.actual_number_of_groups = categoricals_total (cats);
-    }
+      if ( ! categoricals_is_complete (cats))
+       {
+         continue;
+       }
 
-  if ( cmd->stats & STATS_HOMOGENEITY )
-    levene (dict, casereader_clone (input), cmd->indep_var,
-           cmd->n_vars, cmd->vars, cmd->exclude);
+      if (categoricals_n_total (cats) > ws.actual_number_of_groups)
+       ws.actual_number_of_groups = categoricals_n_total (cats);
+    }
 
   casereader_destroy (input);
 
@@ -603,79 +870,24 @@ run_oneway (const struct oneway_spec *cmd,
     output_oneway (cmd, &ws);
 
   taint_destroy (taint);
-}
 
-/* Pre calculations */
-static void
-precalc (const struct oneway_spec *cmd)
-{
-  size_t i = 0;
+ finish:
 
-  for (i = 0; i < cmd->n_vars; ++i)
+  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);
+      levene_destroy (ws.vws[v].nl);
+      dd_destroy (ws.dd_total[v]);
+      interaction_destroy (ws.vws[v].iact);
     }
-}
-
-/* Post calculations for the ONEWAY command */
-static void
-postcalc (const struct oneway_spec *cmd)
-{
-  size_t i = 0;
-
-  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);
-    }
+  free (ws.vws);
+  free (ws.dd_total);
 }
 
 static void show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
 static void show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspace *ws);
+static void show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int depvar);
 
 static void
 output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
@@ -684,7 +896,8 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
 
   /* Check the sanity of the given contrast values */
   struct contrasts_node *coeff_list  = NULL;
-  ll_for_each (coeff_list, struct contrasts_node, ll, &cmd->contrast_list)
+  struct contrasts_node *coeff_next  = NULL;
+  ll_for_each_safe (coeff_list, coeff_next, struct contrasts_node, ll, &cmd->contrast_list)
     {
       struct coeff_node *cn = NULL;
       double sum = 0;
@@ -694,8 +907,11 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
       if (ll_count (cl) != ws->actual_number_of_groups)
        {
          msg (SW,
-              _("Number of contrast coefficients must equal the number of groups"));
-         coeff_list->bad_count = true;
+              _("In contrast list %zu, the number of coefficients (%zu) does not equal the number of groups (%d). This contrast list will be ignored."),
+              i, ll_count (cl), ws->actual_number_of_groups);
+
+         ll_remove (&coeff_list->ll);
+         destroy_coeff_list (coeff_list);
          continue;
        }
 
@@ -720,15 +936,17 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
       show_contrast_tests (cmd, ws);
     }
 
-  /* Clean up */
-  for (i = 0; i < cmd->n_vars; ++i )
+  if ( cmd->posthoc )
     {
-      struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
+      int v;
+      for (v = 0 ; v < cmd->n_vars; ++v)
+       {
+         const struct categoricals *cats = covariance_get_categoricals (ws->vws[v].cov);
 
-      hsh_destroy (group_hash);
+         if ( categoricals_is_complete (cats))
+           show_comparisons (cmd, ws, v);
+       }
     }
-
-  hsh_destroy (ws->group_hash);
 }
 
 
@@ -758,20 +976,22 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
-  tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
+  tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
 
 
   for (i = 0; i < cmd->n_vars; ++i)
     {
       double n;
-      moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
-
+      double df1, df2;
+      double msa;
+      const char *s = var_to_string (cmd->vars[i]);
       const struct per_var_ws *pvw = &ws->vws[i];
-      const double df1 = pvw->n_groups - 1;
-      const double df2 = n - 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"));
@@ -783,28 +1003,28 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
 
 
       /* 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);
-      tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL);
+      tab_double (t, 2, i * 3 + 1, 0, pvw->ssa, NULL, RC_OTHER);
+      tab_double (t, 2, i * 3 + 3, 0, pvw->sst, NULL, RC_OTHER);
+      tab_double (t, 2, i * 3 + 2, 0, pvw->sse, NULL, RC_OTHER);
 
 
       /* 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, n - 1, 4, 0);
+      tab_double (t, 3, i * 3 + 1, 0, df1, NULL, RC_INTEGER);
+      tab_double (t, 3, i * 3 + 2, 0, df2,  NULL, RC_INTEGER); 
+      tab_double (t, 3, i * 3 + 3, 0, n - 1, NULL, RC_INTEGER); 
 
       /* Mean Squares */
-      tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL);
-      tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL);
+      tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL, RC_OTHER);
+      tab_double (t, 4, i * 3 + 2, TAB_RIGHT, pvw->mse, NULL, RC_OTHER);
 
       {
        const double F = msa / pvw->mse ;
 
        /* The F value */
-       tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
+       tab_double (t, 5, i * 3 + 1, 0,  F, NULL, RC_OTHER);
 
        /* The significance */
-       tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
+       tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL, RC_PVALUE);
       }
     }
 
@@ -833,6 +1053,7 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
     n_rows += ws->actual_number_of_groups + 1;
 
   t = tab_create (n_cols, n_rows);
+  tab_set_format (t, RC_WEIGHT, wfmt);
   tab_headers (t, 2, 0, 2, 0);
 
   /* Put a frame around the entire box, and vertical lines inside */
@@ -881,7 +1102,7 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
       if ( v > 0)
        tab_hline (t, TAL_1, 0, n_cols - 1, row);
 
-      for (count = 0; count < categoricals_total (cats); ++count)
+      for (count = 0; count < categoricals_n_total (cats); ++count)
        {
          double T;
          double n, mean, variance;
@@ -889,8 +1110,8 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
 
          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);
+         const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
+         const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, count);
 
          moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
 
@@ -899,7 +1120,7 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
 
          ds_init_empty (&vstr);
 
-         var_append_value_name (cmd->indep_var, gval, &vstr);
+         var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
 
          tab_text (t, 1, row + count,
                    TAB_LEFT | TAT_TITLE,
@@ -909,31 +1130,32 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
 
          /* Now fill in the numbers ... */
 
-         tab_fixed (t, 2, row + count, 0, n, 8, 0);
+         tab_double (t, 2, row + count, 0, n, NULL, RC_WEIGHT);
 
-         tab_double (t, 3, row + count, 0, mean, NULL);
+         tab_double (t, 3, row + count, 0, mean, NULL, RC_OTHER);
 
-         tab_double (t, 4, row + count, 0, std_dev, NULL);
+         tab_double (t, 4, row + count, 0, std_dev, NULL, RC_OTHER);
 
 
-         tab_double (t, 5, row + count, 0, std_error, NULL);
+         tab_double (t, 5, row + count, 0, std_error, NULL, RC_OTHER);
 
          /* Now the confidence interval */
 
          T = gsl_cdf_tdist_Qinv (q, n - 1);
 
          tab_double (t, 6, row + count, 0,
-                     mean - T * std_error, NULL);
+                     mean - T * std_error, NULL, RC_OTHER);
 
          tab_double (t, 7, row + count, 0,
-                     mean + T * std_error, NULL);
+                     mean + T * std_error, NULL, RC_OTHER);
 
          /* Min and Max */
 
-         tab_double (t, 8, row + count, 0,  dd->minimum, fmt);
-         tab_double (t, 9, row + count, 0,  dd->maximum, fmt);
+         tab_double (t, 8, row + count, 0,  dd->minimum, fmt, RC_OTHER);
+         tab_double (t, 9, row + count, 0,  dd->maximum, fmt, RC_OTHER);
        }
 
+      if (categoricals_is_complete (cats))
       {
        double T;
        double n, mean, variance;
@@ -948,29 +1170,30 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
        tab_text (t, 1, row + count,
                  TAB_LEFT | TAT_TITLE, _("Total"));
 
-       tab_double (t, 2, row + count, 0, n, wfmt);
+       tab_double (t, 2, row + count, 0, n, NULL, RC_WEIGHT);
 
-       tab_double (t, 3, row + count, 0, mean, NULL);
+       tab_double (t, 3, row + count, 0, mean, NULL, RC_OTHER);
 
-       tab_double (t, 4, row + count, 0, std_dev, NULL);
+       tab_double (t, 4, row + count, 0, std_dev, NULL, RC_OTHER);
 
-       tab_double (t, 5, row + count, 0, std_error, NULL);
+       tab_double (t, 5, row + count, 0, std_error, NULL, RC_OTHER);
 
        /* Now the confidence interval */
        T = gsl_cdf_tdist_Qinv (q, n - 1);
 
        tab_double (t, 6, row + count, 0,
-                   mean - T * std_error, NULL);
+                   mean - T * std_error, NULL, RC_OTHER);
 
        tab_double (t, 7, row + count, 0,
-                   mean + T * std_error, NULL);
+                   mean + T * std_error, NULL, RC_OTHER);
+
 
        /* 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);
+       tab_double (t, 8, row + count, 0,  ws->dd_total[v]->minimum, fmt, RC_OTHER);
+       tab_double (t, 9, row + count, 0,  ws->dd_total[v]->maximum, fmt, RC_OTHER);
       }
 
-      row += categoricals_total (cats) + 1;
+      row += categoricals_n_total (cats) + 1;
     }
 
   tab_submit (t);
@@ -1001,35 +1224,33 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *
   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
-  tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
+  tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
 
   tab_title (t, _("Test of Homogeneity of Variances"));
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
       double n;
-      moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
-
       const struct per_var_ws *pvw = &ws->vws[v];
-      const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
+      double F = levene_calculate (pvw->nl);
 
       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 = n - 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);
-      tab_fixed (t, 3, v + 1, TAB_RIGHT, df2, 8, 0);
+      tab_double (t, 1, v + 1, TAB_RIGHT, F, NULL, RC_OTHER);
+      tab_double (t, 2, v + 1, TAB_RIGHT, df1, NULL, RC_INTEGER);
+      tab_double (t, 3, v + 1, TAB_RIGHT, df2, NULL, RC_INTEGER);
 
       /* Now the significance */
-      tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL);
+      tab_double (t, 4, v + 1, TAB_RIGHT, gsl_cdf_fdist_Q (F, df1, df2), NULL, RC_PVALUE);
     }
 
   tab_submit (t);
@@ -1101,25 +1322,20 @@ show_contrast_coeffs (const struct oneway_spec *cmd, const struct oneway_workspa
           ++count, coeffi = ll_next (coeffi))
        {
          const struct categoricals *cats = covariance_get_categoricals (cov);
-         const union value *val = categoricals_get_value_by_subscript (cats, count);
+         const struct ccase *gcc = categoricals_get_case_by_category (cats, count);
+         struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
          struct string vstr;
 
          ds_init_empty (&vstr);
 
-         var_append_value_name (cmd->indep_var, val, &vstr);
+         var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
 
          tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
 
          ds_destroy (&vstr);
 
-         if (cn->bad_count)
-           tab_text (t, count + 2, c_num + 2, TAB_RIGHT, "?" );
-         else
-           {
-             struct coeff_node *coeffn = ll_data (coeffi, struct coeff_node, ll);
-
-             tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
-           }
+         tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%.*g",
+                           DBL_DIG + 1, coeffn->coeff);
        }
       ++c_num;
     }
@@ -1171,6 +1387,8 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
     {
       const struct per_var_ws *pvw = &ws->vws[v];
       const struct categoricals *cats = covariance_get_categoricals (pvw->cov);
+      if (!categoricals_is_complete (cats))
+       continue;
       struct ll *cli;
       int i = 0;
       int lines_per_variable = 2 * n_contrasts;
@@ -1230,22 +1448,19 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
                            (v * lines_per_variable) + i + 1 + n_contrasts,
                            TAB_CENTER | TAT_TITLE, "%d", i + 1);
 
-         if (cn->bad_count)
-           continue;
-
          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);
-
-             moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
-
+             const struct descriptive_data *dd = categoricals_get_user_data_by_category (cats, ci);
              struct coeff_node *cn = ll_data (coeffi, struct coeff_node, ll);
              const double coef = cn->coeff; 
+             double winv ;
+
+             moments1_calculate (dd->mom, &n, &mean, &variance, NULL, NULL);
 
-             const double winv = variance / n;
+             winv = variance / n;
 
              contrast_value += coef * mean;
 
@@ -1262,18 +1477,18 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
          df_numerator = pow2 (df_numerator);
 
          tab_double (t,  3, (v * lines_per_variable) + i + 1,
-                     TAB_RIGHT, contrast_value, NULL);
+                     TAB_RIGHT, contrast_value, NULL, RC_OTHER);
 
          tab_double (t,  3, (v * lines_per_variable) + i + 1 +
                      n_contrasts,
-                     TAB_RIGHT, contrast_value, NULL);
+                     TAB_RIGHT, contrast_value, NULL, RC_OTHER);
 
          std_error_contrast = sqrt (pvw->mse * coef_msq);
 
          /* Std. Error */
          tab_double (t,  4, (v * lines_per_variable) + i + 1,
                      TAB_RIGHT, std_error_contrast,
-                     NULL);
+                     NULL, RC_OTHER);
 
          T = fabs (contrast_value / std_error_contrast);
 
@@ -1281,19 +1496,18 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
 
          tab_double (t,  5, (v * lines_per_variable) + i + 1,
                      TAB_RIGHT, T,
-                     NULL);
+                     NULL, RC_OTHER);
 
 
          /* Degrees of Freedom */
-         tab_fixed (t,  6, (v * lines_per_variable) + i + 1,
-                    TAB_RIGHT,  df,
-                    8, 0);
+         tab_double (t,  6, (v * lines_per_variable) + i + 1,
+                    TAB_RIGHT,  df, NULL, RC_INTEGER);
 
 
          /* Significance TWO TAILED !!*/
          tab_double (t,  7, (v * lines_per_variable) + i + 1,
                      TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T, df),
-                     NULL);
+                     NULL, RC_PVALUE);
 
          /* Now for the Variances NOT Equal case */
 
@@ -1301,25 +1515,30 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
          tab_double (t,  4,
                      (v * lines_per_variable) + i + 1 + n_contrasts,
                      TAB_RIGHT, sec_vneq,
-                     NULL);
+                     NULL, RC_OTHER);
 
          T = contrast_value / sec_vneq;
          tab_double (t,  5,
                      (v * lines_per_variable) + i + 1 + n_contrasts,
                      TAB_RIGHT, T,
-                     NULL);
+                     NULL, RC_OTHER);
 
          df = df_numerator / df_denominator;
 
          tab_double (t,  6,
                      (v * lines_per_variable) + i + 1 + n_contrasts,
                      TAB_RIGHT, df,
-                     NULL);
+                     NULL, RC_OTHER);
 
-         /* The Significance */
-         tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
-                     TAB_RIGHT,  2 * gsl_cdf_tdist_Q (T,df),
-                     NULL);
+         {
+           double p = gsl_cdf_tdist_P (T, df);
+           double q = gsl_cdf_tdist_Q (T, df);
+
+           /* The Significance */
+           tab_double (t, 7, (v * lines_per_variable) + i + 1 + n_contrasts,
+                       TAB_RIGHT,  2 * ((T > 0) ? q : p),
+                       NULL, RC_PVALUE);
+         }
        }
 
       if ( v > 0 )
@@ -1330,3 +1549,127 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
 }
 
 
+
+static void
+show_comparisons (const struct oneway_spec *cmd, const struct oneway_workspace *ws, int v)
+{
+  const int n_cols = 8;
+  const int heading_rows = 2;
+  const int heading_cols = 3;
+
+  int p;
+  int r = heading_rows ;
+
+  const struct per_var_ws *pvw = &ws->vws[v];
+  const struct categoricals *cat = pvw->cat;
+  const int n_rows = heading_rows + cmd->n_posthoc * pvw->n_groups * (pvw->n_groups - 1);
+
+  struct tab_table *t = tab_create (n_cols, n_rows);
+
+  tab_headers (t, heading_cols, 0, heading_rows, 0);
+
+  /* Put a frame around the entire box, and vertical lines inside */
+  tab_box (t,
+          TAL_2, TAL_2,
+          -1, -1,
+          0, 0,
+          n_cols - 1, n_rows - 1);
+
+  tab_box (t,
+          -1, -1,
+          -1, TAL_1,
+          heading_cols, 0,
+          n_cols - 1, n_rows - 1);
+
+  tab_vline (t, TAL_2, heading_cols, 0, n_rows - 1);
+
+  tab_title (t, _("Multiple Comparisons (%s)"), var_to_string (cmd->vars[v]));
+
+  tab_text_format (t,  1, 1, TAB_LEFT | TAT_TITLE, _("(I) %s"), var_to_string (cmd->indep_var));
+  tab_text_format (t,  2, 1, TAB_LEFT | TAT_TITLE, _("(J) %s"), var_to_string (cmd->indep_var));
+  tab_text (t,  3, 0, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
+  tab_text (t,  3, 1, TAB_CENTER | TAT_TITLE, _("(I - J)"));
+  tab_text (t,  4, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
+  tab_text (t,  5, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
+
+  tab_joint_text_format (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE,
+                         _("%g%% Confidence Interval"),
+                         (1 - cmd->alpha) * 100.0);
+
+  tab_text (t,  6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
+  tab_text (t,  7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
+
+
+  for (p = 0; p < cmd->n_posthoc; ++p)
+    {
+      int i;
+      const struct posthoc *ph = &ph_tests[cmd->posthoc[p]];
+
+      tab_hline (t, TAL_2, 0, n_cols - 1, r);
+
+      tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, gettext (ph->label));
+
+      for (i = 0; i < pvw->n_groups ; ++i)
+       {
+         double weight_i, mean_i, var_i;
+         int rx = 0;
+         struct string vstr;
+         int j;
+         struct descriptive_data *dd_i = categoricals_get_user_data_by_category (cat, i);
+         const struct ccase *gcc = categoricals_get_case_by_category (cat, i);
+         
+
+         ds_init_empty (&vstr);
+         var_append_value_name (cmd->indep_var, case_data (gcc, cmd->indep_var), &vstr);
+
+         if ( i != 0)
+           tab_hline (t, TAL_1, 1, n_cols - 1, r);
+         tab_text (t, 1, r, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
+
+         moments1_calculate (dd_i->mom, &weight_i, &mean_i, &var_i, 0, 0);
+
+         for (j = 0 ; j < pvw->n_groups; ++j)
+           {
+             double std_err;
+             double weight_j, mean_j, var_j;
+             double half_range;
+             const struct ccase *cc;
+             struct descriptive_data *dd_j = categoricals_get_user_data_by_category (cat, j);
+             if (j == i)
+               continue;
+
+             ds_clear (&vstr);
+             cc = categoricals_get_case_by_category (cat, j);
+             var_append_value_name (cmd->indep_var, case_data (cc, cmd->indep_var), &vstr);
+             tab_text (t, 2, r + rx, TAB_LEFT | TAT_TITLE, ds_cstr (&vstr));
+
+             moments1_calculate (dd_j->mom, &weight_j, &mean_j, &var_j, 0, 0);
+
+             tab_double  (t, 3, r + rx, 0, mean_i - mean_j, NULL, RC_OTHER);
+
+             std_err = pvw->mse;
+             std_err *= weight_i + weight_j;
+             std_err /= weight_i * weight_j;
+             std_err = sqrt (std_err);
+
+             tab_double  (t, 4, r + rx, 0, std_err, NULL, RC_OTHER);
+         
+             tab_double (t, 5, r + rx, 0, 2 * multiple_comparison_sig (std_err, pvw, dd_i, dd_j, ph), NULL, RC_PVALUE);
+
+             half_range = mc_half_range (cmd, pvw, std_err, dd_i, dd_j, ph);
+
+             tab_double (t, 6, r + rx, 0,
+                         (mean_i - mean_j) - half_range, NULL, RC_OTHER);
+
+             tab_double (t, 7, r + rx, 0,
+                         (mean_i - mean_j) + half_range, NULL, RC_OTHER);
+
+             rx++;
+           }
+         ds_destroy (&vstr);
+         r += pvw->n_groups - 1;
+       }
+    }
+
+  tab_submit (t);
+}