ONEWAY: Fix declaration-after-statement warning.
[pspp] / src / language / stats / oneway.c
index f421371c1d43201c8e1112717e16ba3c266a8aa1..aa8a255211192852f78318940d68179e046ec28c 100644 (file)
@@ -99,8 +99,18 @@ 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
@@ -113,7 +123,7 @@ struct per_var_ws
 
   int n_groups;
 
-  double cc;
+  double mse;
 };
 
 struct oneway_workspace
@@ -128,9 +138,8 @@ struct oneway_workspace
 
   struct per_var_ws *vws;
 
-  struct moments1 *totals;
-  double minimum;
-  double maximum;
+  /* An array of descriptive data.  One for each dependent variable */
+  struct descriptive_data **dd_total;
 };
 
 /* Routines to show the output tables */
@@ -268,8 +277,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);
@@ -315,51 +322,74 @@ 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
-{
-  struct moments1 *mom;
-  double minimum;
-  double maximum;
-};
-
-static void *
-makeit (void)
+static struct descriptive_data *
+dd_create (const struct variable *var)
 {
   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 void *
+makeit (void *aux1, void *aux2 UNUSED)
+{
+  const struct variable *var = aux1;
+
+  struct descriptive_data *dd = dd_create (var);
 
   return dd;
 }
 
 static void 
-updateit (void *user_data, const struct variable *wv, 
-         const struct variable *catvar, const struct ccase *c, void *aux)
+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)
 {
-  const union value *val = case_data_idx (c, 0);
   struct descriptive_data *dd = user_data;
-  struct oneway_workspace *ws = aux;
 
-  double weight = 1.0;
-  if (wv)
-    weight = case_data (c, wv)->f;
+  const struct variable *varp = aux1;
+
+  const union value *valx = case_data (c, varp);
+
+  struct descriptive_data *dd_total = aux2;
 
-  moments1_add (dd->mom, val->f, weight);
-  moments1_add (ws->totals, val->f, weight);
+  double weight;
 
-  if (val->f * weight < dd->minimum)
-    dd->minimum = val->f * weight;
+  if ( var_is_value_missing (varp, valx, exclude))
+    return;
 
-  if (val->f * weight > dd->maximum)
-    dd->maximum = val->f * weight;
+  weight = wv != NULL ? case_data (c, wv)->f : 1.0;
 
+  moments1_add (dd->mom, valx->f, weight);
+  if (valx->f * weight < dd->minimum)
+    dd->minimum = valx->f * weight;
 
-  if (val->f * weight < ws->minimum)
-    ws->minimum = val->f * weight;
+  if (valx->f * weight > dd->maximum)
+    dd->maximum = valx->f * weight;
+
+  {
+    const struct variable *var = dd_total->var;
+    const union value *val = case_data (c, var);
 
-  if (val->f * weight > ws->maximum)
-    ws->maximum = val->f * weight;
+    moments1_add (dd_total->mom,
+                 val->f,
+                 weight);
+
+    if (val->f * weight < dd_total->minimum)
+      dd_total->minimum = val->f * weight;
+
+    if (val->f * weight > dd_total->maximum)
+      dd_total->maximum = val->f * weight;
+  }
 }
 
 static void
@@ -375,24 +405,24 @@ 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.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
 
-  ws.totals = moments1_create (MOMENT_VARIANCE);
-  ws.minimum = DBL_MAX;
-  ws.maximum = -DBL_MAX;
-
+  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, 
                                                       makeit,
-                                                      updateit, &ws);
+                                                      updateit,
+                                                      cmd->vars[v], ws.dd_total[v]);
 
       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);
@@ -439,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 )
@@ -508,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);
        }
     }
@@ -519,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);
@@ -528,15 +571,21 @@ 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);
     }
 
   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 )
@@ -545,8 +594,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);
 
@@ -662,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 )
     {
@@ -713,13 +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];
-      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"));
@@ -730,8 +779,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);
@@ -741,14 +788,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);
@@ -835,16 +882,17 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
        {
          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);
 
-         double std_dev = sqrt (variance);
-         double std_error = std_dev / sqrt (n) ;
-
-         struct string vstr;
+         std_dev = sqrt (variance);
+         std_error = std_dev / sqrt (n) ;
 
          ds_init_empty (&vstr);
 
@@ -886,11 +934,13 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
       {
        double T;
        double n, mean, variance;
+       double std_dev;
+       double std_error;
 
-       moments1_calculate (ws->totals, &n, &mean, &variance, NULL, NULL);
+       moments1_calculate (ws->dd_total[v]->mom, &n, &mean, &variance, NULL, NULL);
 
-       double std_dev = sqrt (variance);
-       double std_error = std_dev / sqrt (n) ;
+       std_dev = sqrt (variance);
+       std_error = std_dev / sqrt (n) ;
 
        tab_text (t, 1, row + count,
                  TAB_LEFT | TAT_TITLE, _("Total"));
@@ -904,7 +954,6 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
        tab_double (t, 5, row + count, 0, std_error, NULL);
 
        /* Now the confidence interval */
-
        T = gsl_cdf_tdist_Qinv (q, n - 1);
 
        tab_double (t, 6, row + count, 0,
@@ -914,9 +963,8 @@ show_descriptives (const struct oneway_spec *cmd, const struct oneway_workspace
                    mean + T * std_error, NULL);
 
        /* Min and Max */
-
-       tab_double (t, 8, row + count, 0,  ws->minimum, fmt);
-       tab_double (t, 9, row + count, 0,  ws->maximum, fmt);
+       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 += categoricals_total (cats) + 1;
@@ -956,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);
@@ -1115,6 +1167,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;
@@ -1127,14 +1181,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;
@@ -1154,6 +1204,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,
@@ -1176,27 +1231,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);
@@ -1210,7 +1266,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,
@@ -1225,7 +1281,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,