fixed predict argument list
[pspp-builds.git] / src / language / stats / regression.q
index 0ffe998b8480b90b3177d7992ed9b4c61ac4df57..3037f72cdd0714b71a83c9cc44a78330db45c765 100644 (file)
 #include <gsl/gsl_vector.h>
 #include <gsl/gsl_matrix.h>
 #include <math.h>
-#include "libpspp/alloc.h"
-#include "data/case.h"
-#include "data/casefile.h"
-#include "data/category.h"
-#include "data/cat-routines.h"
-#include "language/command.h"
-#include "libpspp/compiler.h"
-#include "math/design-matrix.h"
-#include "data/dictionary.h"
-#include "libpspp/message.h"
-#include "language/data-io/file-handle.h"
+#include <libpspp/alloc.h>
+#include <data/case.h>
+#include <data/casefile.h>
+#include <data/category.h>
+#include <data/cat-routines.h>
+#include <language/command.h>
+#include <libpspp/compiler.h>
+#include <math/design-matrix.h>
+#include <data/dictionary.h>
+#include <libpspp/message.h>
+#include <language/data-io/file-handle.h>
 #include "gettext.h"
-#include "language/lexer/lexer.h"
-#include "math/linreg/linreg.h"
-#include "math/linreg/coefficient.h"
-#include "data/missing-values.h"
+#include <language/lexer/lexer.h>
+#include <math/linreg/linreg.h>
+#include <math/linreg/coefficient.h>
+#include <data/missing-values.h>
 #include "regression-export.h"
-#include "output/table.h"
-#include "data/value-labels.h"
-#include "data/variable.h"
+#include <output/table.h>
+#include <data/value-labels.h>
+#include <data/variable.h>
 #include "procedure.h"
 
 #define REG_LARGE_DATA 1000
@@ -71,6 +71,7 @@
    all;
    export=custom;
    ^dependent=varlist;
+   save=residuals;
    method=enter.
 */
 /* (declarations) */
@@ -150,7 +151,7 @@ reg_stats_r (pspp_linreg_cache * c)
   tab_float (t, 2, 1, TAB_RIGHT, rsq, 10, 2);
   tab_float (t, 3, 1, TAB_RIGHT, adjrsq, 10, 2);
   tab_float (t, 4, 1, TAB_RIGHT, std_error, 10, 2);
-  tab_title (t, 0, _("Model Summary"));
+  tab_title (t, _("Model Summary"));
   tab_submit (t);
 }
 
@@ -252,7 +253,7 @@ reg_stats_coeff (pspp_linreg_cache * c)
       pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), 1.0);
       tab_float (t, 6, j + 1, 0, pval, 10, 2);
     }
-  tab_title (t, 0, _("Coefficients"));
+  tab_title (t, _("Coefficients"));
   tab_submit (t);
   free (tmp);
 }
@@ -313,7 +314,7 @@ reg_stats_anova (pspp_linreg_cache * c)
 
   tab_float (t, 6, 1, 0, pval, 8, 3);
 
-  tab_title (t, 0, _("ANOVA"));
+  tab_title (t, _("ANOVA"));
   tab_submit (t);
 }
 static void
@@ -384,7 +385,7 @@ reg_stats_bcov (pspp_linreg_cache * c)
                     gsl_matrix_get (c->cov, row, col), 8, 3);
        }
     }
-  tab_title (t, 0, _("Coefficient Correlations"));
+  tab_title (t, _("Coefficient Correlations"));
   tab_submit (t);
 }
 static void
@@ -500,6 +501,39 @@ 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)
+{
+  int i;
+  int case_num;
+  double residual;
+  const union value **vals;
+  struct casereader *r;
+  struct ccase c;
+
+  assert (lc != NULL);
+  assert (is_missing != NULL);
+
+  if (save)
+    {
+      vals = xnmalloc (n_variables, sizeof (*vals));
+      for (r = casefile_get_reader (cf); casereader_read (r, &c);
+          case_destroy (&c))
+       {
+         case_num = casereader_cnum (r) - 1;
+         if (!is_missing[case_num])
+           {
+             for (i = 0; i < n_variables; ++i)
+               {
+                 vals[i] = case_data (&c, v_variables[i]->fv);
+               }
+             residual = (*lc->predict) ((const struct variable **) v_variables, 
+                                        (const union value **) vals, lc, n_variables);
+           }
+       }
+      free (vals);
+    }
+}
 static int
 reg_inserted (const struct variable *v, struct variable **varlist, int n_vars)
 {
@@ -599,6 +633,23 @@ reg_print_getvar (FILE * fp, pspp_linreg_cache * c)
           "if (strncmp (v_name, model_depvars[i], PSPP_REG_MAXLEN) == 0)\n\t\t{\n\t\t\t");
   fprintf (fp, "return i;\n\t\t}\n\t}\n}\n");
 }
+static int
+reg_has_categorical (pspp_linreg_cache * c)
+{
+  int i;
+  const struct variable *v;
+  
+  for (i = 1; i < c->n_coeffs; i++)
+    {
+      v = pspp_linreg_coeff_get_var (c->coeff + i, 0);
+      if (v->type == ALPHA)
+       {
+         return 1;
+       }
+    }
+  return 0;
+}
+
 static void
 subcommand_export (int export, pspp_linreg_cache * c)
 {
@@ -618,7 +669,10 @@ subcommand_export (int export, pspp_linreg_cache * c)
       assert (fp != NULL);
       fprintf (fp, "%s", reg_preamble);
       reg_print_getvar (fp, c);
-      reg_print_categorical_encoding (fp, c);
+      if (reg_has_categorical (c))
+       {
+         reg_print_categorical_encoding (fp, c);
+       }
       fprintf (fp, "%s", reg_export_t_quantiles_1);
       increment = 0.5 / (double) increment;
       for (i = 0; i < n_quantiles - 1; i++)
@@ -982,6 +1036,7 @@ 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);
       gsl_vector_free (Y);
       design_matrix_destroy (X);