Move global initialization and cleanup code into main.c.
[pspp-builds.git] / src / regression.q
index ee7c0075442288cc32afb9e2aa9f8ff48982cd64..30224a6263e46fcf42c62c9facf33ab23f5d9c0f 100644 (file)
@@ -26,7 +26,9 @@
 #include "case.h"
 #include "casefile.h"
 #include "cat.h"
+#include "cat-routines.h"
 #include "command.h"
+#include "design-matrix.h"
 #include "dictionary.h"
 #include "error.h"
 #include "file-handle.h"
@@ -38,6 +40,8 @@
 #include "var.h"
 #include "vfm.h"
 
+#define REG_LARGE_DATA 1000
+
 /* (headers) */
 
 
@@ -497,7 +501,6 @@ static void
 run_regression (const struct casefile *cf, void *cmd_ UNUSED)
 {
   size_t i;
-  size_t k;
   size_t n_data = 0;
   size_t row;
   size_t case_num;
@@ -512,6 +515,7 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
   struct casereader *r2;
   struct ccase c;
   struct variable *v;
+  struct variable **indep_vars;
   struct design_matrix *X;
   gsl_vector *Y;
   pspp_linreg_cache *lcache;
@@ -534,29 +538,35 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
      Read from the active file. The first pass encodes categorical
      variables and drops cases with missing values.
    */
+  j = 0;
   for (i = 0; i < cmd.n_variables; i++)
     {
-      v = cmd.v_variables[i];
-      if (v->type == ALPHA)
-       {
-         /* Make a place to hold the binary vectors 
-            corresponding to this variable's values. */
-         cat_stored_values_create (v);
-       }
-      for (r = casefile_get_reader (cf);
-          casereader_read (r, &c); case_destroy (&c))
+      if (!is_depvar (i))
        {
-         row = casereader_cnum (r) - 1;
-
-         val = case_data (&c, v->fv);
-         cat_value_update (v, val);
-         if (mv_is_value_missing (&v->miss, val))
+         v = cmd.v_variables[i];
+         indep_vars[j] = v;
+         j++;
+         if (v->type == ALPHA)
            {
-             if (!is_missing_case[row])
+             /* Make a place to hold the binary vectors 
+                corresponding to this variable's values. */
+             cat_stored_values_create (v);
+           }
+         for (r = casefile_get_reader (cf);
+              casereader_read (r, &c); case_destroy (&c))
+           {
+             row = casereader_cnum (r) - 1;
+             
+             val = case_data (&c, v->fv);
+             cat_value_update (v, val);
+             if (mv_is_value_missing (&v->miss, val))
                {
-                 /* Now it is missing. */
-                 n_data--;
-                 is_missing_case[row] = 1;
+                 if (!is_missing_case[row])
+                   {
+                     /* Now it is missing. */
+                     n_data--;
+                     is_missing_case[row] = 1;
+                   }
                }
            }
        }
@@ -564,7 +574,7 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
 
   Y = gsl_vector_alloc (n_data);
   X =
-    design_matrix_create (n_indep, (const struct variable **) cmd.v_variables,
+    design_matrix_create (n_indep, (const struct variable **) indep_vars,
                          n_data);
   lcache = pspp_linreg_cache_alloc (X->m->size1, X->m->size2);
   lcache->indep_means = gsl_vector_alloc (X->m->size2);
@@ -578,7 +588,6 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
        case_destroy (&c))
     /* Iterate over the cases. */
     {
-      k = 0;
       case_num = casereader_cnum (r2) - 1;
       if (!is_missing_case[case_num])
        {
@@ -604,7 +613,7 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
                      pspp_reg_rc = CMD_FAILURE;
                      return;
                    }
-                 lcache->depvar = (const struct var *) v;
+                 lcache->depvar = (const struct variable *) v;
                  gsl_vector_set (Y, row, val->f);
                }
              else
@@ -618,8 +627,6 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
                      design_matrix_set_numeric (X, row, v, val);
                    }
 
-                 indep_vars[k] = i;
-                 k++;
                  lopts.get_indep_mean_std[i] = 1;
                }
            }
@@ -639,10 +646,17 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED)
        (const struct variable *) design_matrix_col_to_var (X, i);
       assert (lcache->coeff[j].v != NULL);
     }
+  /*
+    For large data sets, use QR decomposition.
+   */
+  if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA)
+    {
+      lcache->method = PSPP_LINREG_SVD;
+    }
   /* 
      Find the least-squares estimates and other statistics.
    */
-  pspp_linreg ((const gsl_vector *) Y, X->m, &lopts, lcache);
+    pspp_linreg ((const gsl_vector *) Y, X->m, &lopts, lcache);
   subcommand_statistics (cmd.a_statistics, lcache);
   gsl_vector_free (Y);
   design_matrix_destroy (X);