ONEWAY: Fix declaration-after-statement warning.
[pspp] / src / language / stats / oneway.c
index e88a9542ecd33855f4370c6b10cd76a8ef86ffc0..aa8a255211192852f78318940d68179e046ec28c 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)
@@ -361,9 +362,12 @@ updateit (void *user_data, const struct variable *wv,
 
   struct descriptive_data *dd_total = aux2;
 
-  double weight = 1.0;
-  if (wv)
-    weight = case_data (c, wv)->f;
+  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 * weight < dd->minimum)
@@ -419,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);
@@ -466,22 +469,21 @@ run_oneway (const struct oneway_spec *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);
-         }
-
+         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;
 
+         if ( MISS_ANALYSIS == cmd->missing_type)
+           {
+             if ( var_is_value_missing (v, val, cmd->exclude))
+               continue;
+           }
+
+         covariance_accumulate_pass1 (pvw->cov, c);
+
          gs = hsh_find (group_hash, indep_val );
 
          if ( ! gs )
@@ -535,6 +537,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);
        }
     }
@@ -546,8 +557,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);
@@ -556,7 +572,7 @@ 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);
@@ -693,14 +709,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 )
     {
@@ -744,12 +758,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];
-      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"));
@@ -769,7 +788,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);
@@ -985,19 +1004,23 @@ 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);
 
       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);
-
-      const double df1 = pvw->n_groups - 1;
-      const double df2 = pvw->cc - pvw->n_groups;
+      double df1, df2;
       double F = gp->levene;
 
-      tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
+      moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL);
 
+      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);
@@ -1214,13 +1237,13 @@ show_contrast_tests (const struct oneway_spec *cmd, const struct oneway_workspac
            {
              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);
-
              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;