Oneway: Fix problems with MISSING=ANALYSIS
[pspp] / src / language / stats / oneway.c
index f481c69dc13c765cfd55cbc346ca2089a15df7f2..d08e3114c04d8155d6fe035000ba42745f05dc07 100644 (file)
@@ -102,6 +102,15 @@ struct oneway_spec
 
 };
 
+/* 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
@@ -114,7 +123,6 @@ struct per_var_ws
 
   int n_groups;
 
-  double cc;
   double mse;
 };
 
@@ -314,15 +322,6 @@ free_double (void *value_, const void *aux UNUSED)
 static void postcalc (const struct oneway_spec *cmd);
 static void  precalc (const struct oneway_spec *cmd);
 
-struct descriptive_data
-{
-  const struct variable *var;
-  struct moments1 *mom;
-
-  double minimum;
-  double maximum;
-};
-
 static struct descriptive_data *
 dd_create (const struct variable *var)
 {
@@ -348,7 +347,9 @@ makeit (void *aux1, void *aux2 UNUSED)
 }
 
 static void 
-updateit (void *user_data, const struct variable *wv, 
+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)
@@ -359,6 +360,9 @@ updateit (void *user_data, const struct variable *wv,
 
   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;
@@ -400,17 +404,13 @@ run_oneway (const struct oneway_spec *cmd,
   struct ccase *c;
 
   struct oneway_workspace ws;
-  
-  {
-    ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws));
 
-    ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
+  ws.actual_number_of_groups = 0;
+  ws.vws = xmalloc (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)
+    ws.dd_total[v] = dd_create (cmd->vars[v]);
 
   for (v = 0; v < cmd->n_vars; ++v)
     {
@@ -423,7 +423,6 @@ run_oneway (const struct oneway_spec *cmd,
       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);
@@ -470,17 +469,21 @@ run_oneway (const struct oneway_spec *cmd,
 
       for (i = 0; i < cmd->n_vars; ++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;
+           }
+
          {
            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);
-
           struct group_proc *gp = group_proc_get (cmd->vars[i]);
          struct hsh_table *group_hash = gp->group_hash;
 
@@ -539,6 +542,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);
        }
     }
@@ -550,8 +562,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);
@@ -560,16 +577,20 @@ run_oneway (const struct oneway_spec *cmd,
 
       pvw->n_groups = categoricals_total (cats);
 
-      pvw->mse = (pvw->sst - pvw->ssa) / (pvw->cc - pvw->n_groups);
+      pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups);
     }
 
   postcalc (cmd);
 
+  
   for (v = 0; v < cmd->n_vars; ++v)
     {
       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 ( cmd->stats & STATS_HOMOGENEITY )
@@ -578,8 +599,6 @@ run_oneway (const struct oneway_spec *cmd,
 
   casereader_destroy (input);
 
-  ws.actual_number_of_groups = hsh_count (ws.group_hash);
-
   if (!taint_has_tainted_successor (taint))
     output_oneway (cmd, &ws);
 
@@ -695,14 +714,12 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *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 )
     {
@@ -746,9 +763,12 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace *
 
   for (i = 0; i < cmd->n_vars; ++i)
     {
+      double n;
+      moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL);
+
       const struct per_var_ws *pvw = &ws->vws[i];
       const double df1 = pvw->n_groups - 1;
-      const double df2 = pvw->cc - pvw->n_groups;
+      const double df2 = n - pvw->n_groups;
       const double msa = pvw->ssa / df1;
 
       const char *s = var_to_string (cmd->vars[i]);
@@ -771,7 +791,7 @@ 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);
@@ -987,6 +1007,9 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *
 
   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);
 
@@ -995,7 +1018,7 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace *
       const char *s = var_to_string (var);
 
       const double df1 = pvw->n_groups - 1;
-      const double df2 = pvw->cc - pvw->n_groups;
+      const double df2 = n - pvw->n_groups;
       double F = gp->levene;
 
       tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);