Oneway: Remove group_values struct from show_contrast_coeffs
[pspp] / src / language / stats / oneway.c
index 3995a1a4cd81d2c2316afd49ed6ee8603dbe148e..2423215f3ac228c5d866801fec53194d2cf94dde 100644 (file)
 #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>
@@ -76,7 +81,7 @@ struct contrasts_node
   bool bad_count; /* True if the number of coefficients does not equal the number of groups */
 };
 
-struct oneway 
+struct oneway_spec
 {
   size_t n_vars;
   const struct variable **vars;
@@ -88,6 +93,30 @@ struct oneway
   enum missing_type missing_type;
   enum mv_class exclude;
 
+  /* List of contrasts */
+  struct ll_list contrast_list;
+
+  /* The weight variable */
+  const struct variable *wv;
+};
+
+
+/* Workspace variable for each dependent variable */
+struct per_var_ws
+{
+  struct covariance *cov;
+
+  double sst;
+  double sse;
+  double ssa;
+
+  int n_groups;
+
+  double cc;
+};
+
+struct oneway_workspace
+{
   /* The number of distinct values of the independent variable, when all
      missing values are disregarded */
   int actual_number_of_groups;
@@ -96,32 +125,29 @@ struct oneway
      variable */
   struct hsh_table *group_hash;
 
-  /* List of contrasts */
-  struct ll_list contrast_list;
+  struct per_var_ws *vws;
 };
 
 /* Routines to show the output tables */
-static void show_anova_table (const struct oneway *);
-static void show_descriptives (const struct oneway *, const struct dictionary *dict);
-static void show_homogeneity (const struct oneway *);
+static void show_anova_table (const struct oneway_spec *, const struct oneway_workspace *);
+static void show_descriptives (const struct oneway_spec *);
+static void show_homogeneity (const struct oneway_spec *);
 
-static void output_oneway (const struct oneway *, const struct dictionary *dict);
-static void run_oneway (struct oneway *cmd, struct casereader *input, const struct dataset *ds);
+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);
 
 int
 cmd_oneway (struct lexer *lexer, struct dataset *ds)
 {
-  const struct dictionary *dict = dataset_dict (ds);
-  
-  struct oneway oneway ;
+  const struct dictionary *dict = dataset_dict (ds);  
+  struct oneway_spec oneway ;
   oneway.n_vars = 0;
   oneway.vars = NULL;
   oneway.indep_var = NULL;
   oneway.stats = 0;
   oneway.missing_type = MISS_ANALYSIS;
   oneway.exclude = MV_ANY;
-  oneway.actual_number_of_groups = 0;
-  oneway.group_hash = NULL;
+  oneway.wv = dict_get_weight (dict);
 
   ll_init (&oneway.contrast_list);
 
@@ -179,13 +205,13 @@ cmd_oneway (struct lexer *lexer, struct dataset *ds)
 
           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
            {
-             union value val;
-             if ( parse_value (lexer, &val, 0))
+             if ( lex_is_number (lexer))
                {
                  struct coeff_node *cc = xmalloc (sizeof *cc);
-                 cc->coeff = val.f;
+                 cc->coeff = lex_number (lexer);
 
                  ll_push_tail (coefficient_list, &cc->ll);
+                 lex_get (lexer);
                }
              else
                {
@@ -237,6 +263,8 @@ 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);
@@ -277,20 +305,38 @@ free_double (void *value_, const void *aux UNUSED)
   free (value);
 }
 
-static void postcalc (const struct oneway *cmd);
-static void  precalc (const struct oneway *cmd);
+static void postcalc (const struct oneway_spec *cmd);
+static void  precalc (const struct oneway_spec *cmd);
 
 
 static void
-run_oneway (struct oneway *cmd,
+run_oneway (const struct oneway_spec *cmd,
             struct casereader *input,
             const struct dataset *ds)
 {
+  int v;
   struct taint *taint;
   struct dictionary *dict = dataset_dict (ds);
   struct casereader *reader;
   struct ccase *c;
 
+
+  struct oneway_workspace ws;
+
+  ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
+
+
+  for (v = 0; v < cmd->n_vars; ++v)
+    {
+      struct categoricals *cats = categoricals_create (&cmd->indep_var, 1,
+                                                  cmd->wv, cmd->exclude);
+
+      ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v],
+                                              cats, 
+                                              cmd->wv, cmd->exclude);
+      ws.vws[v].cc = 0;
+    }
+
   c = casereader_peek (input, 0);
   if (c == NULL)
     {
@@ -302,7 +348,7 @@ run_oneway (struct oneway *cmd,
 
   taint = taint_clone (casereader_get_taint (input));
 
-  cmd->group_hash = hsh_create (4,
+  ws.group_hash = hsh_create (4,
                                  compare_double_3way,
                                  do_hash_double,
                                  free_double,
@@ -318,6 +364,7 @@ run_oneway (struct oneway *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;
@@ -325,7 +372,7 @@ run_oneway (struct oneway *cmd,
       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 (cmd->group_hash, &indep_val->f);
+      void **p = hsh_probe (ws.group_hash, &indep_val->f);
       if (*p == NULL)
         {
           double *value = *p = xmalloc (sizeof *value);
@@ -334,6 +381,13 @@ run_oneway (struct oneway *cmd,
 
       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);
+         }
+
          const struct variable *v = cmd->vars[i];
 
          const union value *val = case_data (c, v);
@@ -389,6 +443,34 @@ run_oneway (struct oneway *cmd,
 
     }
   casereader_destroy (reader);
+  reader = casereader_clone (input);
+  for ( ; (c = casereader_read (reader) ); case_unref (c))
+    {
+      int i;
+      for (i = 0; i < cmd->n_vars; ++i)
+       {
+         struct per_var_ws *pvw = &ws.vws[i];
+         covariance_accumulate_pass2 (pvw->cov, c);
+       }
+    }
+  casereader_destroy (reader);
+
+  for (v = 0; v < cmd->n_vars; ++v)
+    {
+      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);
+
+      pvw->sst = gsl_matrix_get (cm, 0, 0);
+
+      reg_sweep (cm, 0);
+
+      pvw->sse = gsl_matrix_get (cm, 0, 0);
+
+      pvw->ssa = pvw->sst - pvw->sse;
+
+      pvw->n_groups = categoricals_total (cats);
+    }
 
   postcalc (cmd);
 
@@ -398,17 +480,17 @@ run_oneway (struct oneway *cmd,
 
   casereader_destroy (input);
 
-  cmd->actual_number_of_groups = hsh_count (cmd->group_hash);
+  ws.actual_number_of_groups = hsh_count (ws.group_hash);
 
   if (!taint_has_tainted_successor (taint))
-    output_oneway (cmd, dict);
+    output_oneway (cmd, &ws);
 
   taint_destroy (taint);
 }
 
 /* Pre calculations */
 static void
-precalc (const struct oneway *cmd)
+precalc (const struct oneway_spec *cmd)
 {
   size_t i = 0;
 
@@ -436,7 +518,7 @@ precalc (const struct oneway *cmd)
 
 /* Post calculations for the ONEWAY command */
 static void
-postcalc (const struct oneway *cmd)
+postcalc (const struct oneway_spec *cmd)
 {
   size_t i = 0;
 
@@ -475,11 +557,11 @@ postcalc (const struct oneway *cmd)
     }
 }
 
-static void show_contrast_coeffs (const struct oneway *cmd);
-static void show_contrast_tests (const struct oneway *cmd);
+static void show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws);
+static void show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws);
 
 static void
-output_oneway (const struct oneway *cmd, const struct dictionary *dict)
+output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws)
 {
   size_t i = 0;
 
@@ -492,7 +574,7 @@ output_oneway (const struct oneway *cmd, const struct dictionary *dict)
       struct ll_list *cl = &coeff_list->coefficient_list;
       ++i;
 
-      if (ll_count (cl) != cmd->actual_number_of_groups)
+      if (ll_count (cl) != ws->actual_number_of_groups)
        {
          msg (SW,
               _("Number of contrast coefficients must equal the number of groups"));
@@ -508,18 +590,18 @@ output_oneway (const struct oneway *cmd, const struct dictionary *dict)
     }
 
   if (cmd->stats & STATS_DESCRIPTIVES)
-    show_descriptives (cmd, dict);
+    show_descriptives (cmd);
 
   if (cmd->stats & STATS_HOMOGENEITY)
     show_homogeneity (cmd);
 
-  show_anova_table (cmd);
+  show_anova_table (cmd, ws);
 
 
   if (ll_count (&cmd->contrast_list) > 0)
     {
-      show_contrast_coeffs (cmd);
-      show_contrast_tests (cmd);
+      show_contrast_coeffs (cmd, ws);
+      show_contrast_tests (cmd, ws);
     }
 
 
@@ -531,13 +613,13 @@ output_oneway (const struct oneway *cmd, const struct dictionary *dict)
       hsh_destroy (group_hash);
     }
 
-  hsh_destroy (cmd->group_hash);
+  hsh_destroy (ws->group_hash);
 }
 
 
 /* Show the ANOVA table */
 static void
-show_anova_table (const struct oneway *cmd)
+show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *ws)
 {
   size_t i;
   int n_cols =7;
@@ -566,21 +648,13 @@ show_anova_table (const struct oneway *cmd)
 
   for (i = 0; i < cmd->n_vars; ++i)
     {
-      struct group_statistics *totals = &group_proc_get (cmd->vars[i])->ugs;
-      struct hsh_table *group_hash = group_proc_get (cmd->vars[i])->group_hash;
-      struct hsh_iterator g;
-      struct group_statistics *gs;
-      double ssa = 0;
-      const char *s = var_to_string (cmd->vars[i]);
-
-      for (gs =  hsh_first (group_hash, &g);
-          gs != 0;
-          gs = hsh_next (group_hash, &g))
-       {
-         ssa += pow2 (gs->sum) / gs->n;
-       }
+      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;
 
-      ssa -= pow2 (totals->sum) / totals->n;
+      const char *s = var_to_string (cmd->vars[i]);
 
       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"));
@@ -590,44 +664,35 @@ show_anova_table (const struct oneway *cmd)
       if (i > 0)
        tab_hline (t, TAL_1, 0, n_cols - 1, i * 3 + 1);
 
-      {
-        struct group_proc *gp = group_proc_get (cmd->vars[i]);
-       const double sst = totals->ssq - pow2 (totals->sum) / totals->n;
-       const double df1 = gp->n_groups - 1;
-       const double df2 = totals->n - gp->n_groups;
-       const double msa = ssa / df1;
-
-       gp->mse  = (sst - ssa) / df2;
 
+      gp->mse  = (pvw->sst - pvw->ssa) / df2;
 
-       /* Sums of Squares */
-       tab_double (t, 2, i * 3 + 1, 0, ssa, NULL);
-       tab_double (t, 2, i * 3 + 3, 0, sst, NULL);
-       tab_double (t, 2, i * 3 + 2, 0, sst - ssa, NULL);
+      /* 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);
 
 
-       /* 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, totals->n - 1, 4, 0);
+      /* 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);
 
-       /* 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);
+      /* 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);
 
-       {
-         const double F = msa / gp->mse ;
+      {
+       const double F = msa / gp->mse ;
 
-         /* The F value */
-         tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
+       /* The F value */
+       tab_double (t, 5, i * 3 + 1, 0,  F, NULL);
 
-         /* The significance */
-         tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
-       }
+       /* The significance */
+       tab_double (t, 6, i * 3 + 1, 0, gsl_cdf_fdist_Q (F, df1, df2), NULL);
       }
     }
 
-
   tab_title (t, _("ANOVA"));
   tab_submit (t);
 }
@@ -635,7 +700,7 @@ show_anova_table (const struct oneway *cmd)
 
 /* Show the descriptives table */
 static void
-show_descriptives (const struct oneway *cmd, const struct dictionary *dict)
+show_descriptives (const struct oneway_spec *cmd)
 {
   size_t v;
   int n_cols = 10;
@@ -645,8 +710,7 @@ show_descriptives (const struct oneway *cmd, const struct dictionary *dict)
   const double confidence = 0.95;
   const double q = (1.0 - confidence) / 2.0;
 
-  const struct variable *wv = dict_get_weight (dict);
-  const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
+  const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : & F_8_0;
 
   int n_rows = 2;
 
@@ -790,7 +854,7 @@ show_descriptives (const struct oneway *cmd, const struct dictionary *dict)
 
 /* Show the homogeneity table */
 static void
-show_homogeneity (const struct oneway *cmd)
+show_homogeneity (const struct oneway_spec *cmd)
 {
   size_t v;
   int n_cols = 5;
@@ -850,19 +914,19 @@ show_homogeneity (const struct oneway *cmd)
 
 /* Show the contrast coefficients table */
 static void
-show_contrast_coeffs (const struct oneway *cmd)
+show_contrast_coeffs (const struct oneway_spec *cmd, struct oneway_workspace *ws)
 {
   int c_num = 0;
   struct ll *cli;
 
   int n_contrasts = ll_count (&cmd->contrast_list);
-  int n_cols = 2 + cmd->actual_number_of_groups;
+  int n_cols = 2 + ws->actual_number_of_groups;
   int n_rows = 2 + n_contrasts;
 
-  void *const *group_values;
-
   struct tab_table *t;
 
+  const struct covariance *cov = ws->vws[0].cov ;
+
   t = tab_create (n_cols, n_rows);
   tab_headers (t, 2, 0, 2, 0);
 
@@ -898,34 +962,29 @@ show_contrast_coeffs (const struct oneway *cmd)
   tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE,
                  var_to_string (cmd->indep_var));
 
-  group_values = hsh_sort (cmd->group_hash);
-
   for ( cli = ll_head (&cmd->contrast_list);
        cli != ll_null (&cmd->contrast_list);
        cli = ll_next (cli))
     {
       int count = 0;
       struct contrasts_node *cn = ll_data (cli, struct contrasts_node, ll);
-      struct ll *coeffi = ll_head (&cn->coefficient_list);
+      struct ll *coeffi ;
 
       tab_text_format (t, 1, c_num + 2, TAB_CENTER, "%d", c_num + 1);
 
-      for (count = 0;
-          count < hsh_count (cmd->group_hash) && coeffi != ll_null (&cn->coefficient_list);
-          ++count)
+      for (coeffi = ll_head (&cn->coefficient_list);
+          coeffi != ll_null (&cn->coefficient_list);
+          ++count, coeffi = ll_next (coeffi))
        {
-         double *group_value_p;
-         union value group_value;
+         const struct categoricals *cats = covariance_get_categoricals (cov);
+         const union value *val = categoricals_get_value_by_subscript (cats, count);
          struct string vstr;
 
          ds_init_empty (&vstr);
 
-         group_value_p = group_values[count];
-         group_value.f = *group_value_p;
-         var_append_value_name (cmd->indep_var, &group_value, &vstr);
+         var_append_value_name (cmd->indep_var, val, &vstr);
 
-         tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE,
-                   ds_cstr (&vstr));
+         tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, ds_cstr (&vstr));
 
          ds_destroy (&vstr);
 
@@ -937,8 +996,6 @@ show_contrast_coeffs (const struct oneway *cmd)
 
              tab_text_format (t, count + 2, c_num + 2, TAB_RIGHT, "%g", coeffn->coeff);
            }
-
-         coeffi = ll_next (coeffi);
        }
       ++c_num;
     }
@@ -949,7 +1006,7 @@ show_contrast_coeffs (const struct oneway *cmd)
 
 /* Show the results of the contrast tests */
 static void
-show_contrast_tests (const struct oneway *cmd)
+show_contrast_tests (const struct oneway_spec *cmd, struct oneway_workspace *ws)
 {
   int n_contrasts = ll_count (&cmd->contrast_list);
   size_t v;