fixed look up of the number of variables
[pspp-builds.git] / src / language / stats / regression.q
index 737a25fdce4b4b2f03008ca8279d9b9b70a6ba73..58822812648a03767314e2b19ee35b3c1a4cfc42 100644 (file)
@@ -501,43 +501,67 @@ subcommand_statistics (int *keywords, pspp_linreg_cache * c)
   statistics_keyword_output (reg_stats_tol, keywords[tol], c);
   statistics_keyword_output (reg_stats_selection, keywords[selection], c);
 }
-static void
-subcommand_save (int save, pspp_linreg_cache *lc, const struct casefile *cf, int *is_missing)
+static int
+regression_trns_proc (void *m, struct ccase *c, int case_idx UNUSED)
 {
-  int i;
-  int case_num;
-  double residual;
+  size_t i;
+  size_t n_vars;
+  pspp_linreg_cache *model = m;
+  union value *output;
   const union value **vals = NULL;
   const union value *obs = NULL;
-  struct casereader *r;
-  struct ccase c;
-
-  assert (lc != NULL);
-  assert (lc->depvar != NULL);
-  assert (is_missing != NULL);
+  struct variable **vars = NULL;
+  
+  assert (model != NULL);
+  assert (model->depvar != NULL);
+  assert (model->resid != NULL);
+  
+  dict_get_vars (default_dict, &vars, &n_vars, 1u << DC_ORDINARY);
+  vals = xnmalloc (n_vars, sizeof (*vals));
+  assert (vals != NULL);
+  output = case_data_rw (c, model->resid->fv);
+  assert (output != NULL);
 
-  if (save)
+  for (i = 0; i < n_vars; i++)
     {
-      vals = xnmalloc (n_variables, sizeof (*vals));
-      for (r = casefile_get_reader (cf); casereader_read (r, &c);
-          case_destroy (&c))
+      /* Do not use the residual variable. */
+      if (vars[i]->index != model->resid->index) 
        {
-         case_num = casereader_cnum (r) - 1;
-         if (!is_missing[case_num])
+         /* Do not use the dependent variable as a predictor. */
+         if (vars[i]->index == model->depvar->index) 
            {
-             for (i = 0; i < n_variables; ++i)
-               {
-                 vals[i] = case_data (&c, v_variables[i]->fv);
-                 if (v_variables[i]->index == lc->depvar->index)
-                   {
-                     obs = vals[i];
-                   }
-               }
-             residual = (*lc->residual) ((const struct variable **) v_variables, 
-                                         (const union value **) vals, obs, lc, n_variables);
+             obs = case_data (c, i);
+             assert (obs != NULL);
+           }
+         else
+           {
+             vals[i] = case_data (c, i);
            }
        }
-      free (vals);
+    }
+  output->f = (*model->residual) ((const struct variable **) vars, 
+                                 vals, obs, model, i);
+  free (vals);
+  return TRNS_CONTINUE;
+}
+static void
+subcommand_save (int save, pspp_linreg_cache *lc)
+{
+  struct variable *residuals = NULL;
+
+  assert (lc != NULL);
+  assert (lc->depvar != NULL);
+
+  if (save)
+    {
+      residuals = dict_create_var (default_dict, "residuals", 0);
+      assert (residuals != NULL);
+      lc->resid = residuals;
+      add_transformation (regression_trns_proc, pspp_linreg_cache_free, lc);
+    }
+  else 
+    {
+      pspp_linreg_cache_free (lc);
     }
 }
 static int
@@ -1042,12 +1066,11 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
        */
       pspp_linreg ((const gsl_vector *) Y, X->m, &lopts, lcache);
       subcommand_statistics (cmd.a_statistics, lcache);
-      subcommand_save (cmd.sbc_save, lcache, cf, is_missing_case);
       subcommand_export (cmd.sbc_export, lcache);
+      subcommand_save (cmd.sbc_save, lcache);
       gsl_vector_free (Y);
       design_matrix_destroy (X);
       free (indep_vars);
-      pspp_linreg_cache_free (lcache);
       free (lopts.get_indep_mean_std);
       casereader_destroy (r);
     }