fixed prediction bug
[pspp-builds.git] / src / math / linreg / coefficient.c
index be189baa7c81ab8f586853158f1b29eea16b00b5..2154ffe4ae0329610dc4d60bec0736bc33710ece 100644 (file)
@@ -1,33 +1,29 @@
-/* lib/linreg/coefficient.c
-
-   Copyright (C) 2005 Free Software Foundation, Inc.
-   Written by Jason H Stover.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or (at
-   your option) any later version.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02111-1307, USA.
- */
+/*
+  lib/linreg/coefficient.c
+  
+  Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H Stover.
+  
+  This program is free software; you can redistribute it and/or modify it under
+  the terms of the GNU General Public License as published by the Free
+  Software Foundation; either version 2 of the License, or (at your option)
+  any later version.
+  
+  This program is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+  more details.
+  
+  You should have received a copy of the GNU General Public License along with
+  this program; if not, write to the Free Software Foundation, Inc., 51
+  Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
+*/
 
 /*
   Accessor functions for matching coefficients and variables.
  */
-#include <assert.h>
 #include <math/linreg/coefficient.h>
 #include <math/linreg/linreg.h>
 #include "src/math/design-matrix.h"
-#include "src/data/variable.h"
-#include "src/data/value.h"
 
 #include <gl/xalloc.h>
 
 struct varinfo
 {
   const struct variable *v;    /* Variable associated with this
-                                  coefficient. Note this variable may not
-                                  be unique. In other words, a
-                                  coefficient structure may have other
-                                  v_info's, each with its own variable.
-                                */
-  const union value *val;      /* Value of the variable v which this
-                                  varinfo refers to. This member is relevant
-                                  only to categorical variables.
-                                */
+                                  coefficient. Note this variable
+                                  may not be unique. In other words,
+                                  a coefficient structure may have
+                                  other v_info's, each with its own
+                                  variable. */
+  const union value *val;      /* Value of the variable v which this varinfo
+                                  refers to. This member is relevant only to
+                                  categorical variables. */
 };
 
 void
@@ -70,10 +65,10 @@ pspp_linreg_coeff_init (pspp_linreg_cache * c, struct design_matrix *X)
       j = i + 1;               /* The first coefficient is the intercept. */
       coeff = c->coeff + j;
       coeff->n_vars = n_vals;  /* Currently, no procedures allow
-                                  interactions.  This will have to
+                                  interactions.  This line will have to
                                   change when procedures that allow
-                                  interaction terms are written.
-                                */
+                                  interaction terms are written. 
+                               */
       coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
       assert (coeff->v_info != NULL);
       coeff->v_info->v =
@@ -102,12 +97,42 @@ pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
   c->std_err = std_err;
 }
 
+/*
+  Return the estimated value of the coefficient.
+ */
+double
+pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
+{
+  if (c == NULL)
+    {
+      return 0.0;
+    }
+  return c->estimate;
+}
+
+/*
+  Return the standard error of the estimated coefficient.
+*/
+double
+pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
+{
+  if (c == NULL)
+    {
+      return 0.0;
+    }
+  return c->std_err;
+}
+
 /*
   How many variables are associated with this coefficient?
  */
 int
 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
 {
+  if (c == NULL)
+    {
+      return 0;
+    }
   return c->n_vars;
 }
 
@@ -117,13 +142,17 @@ pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
 const struct variable *
 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
 {
+  if (c == NULL)
+    {
+      return NULL;
+    }
   assert (i < c->n_vars);
   return (c->v_info + i)->v;
 }
 
-/* 
-   Which value is associated with this coefficient/variable comination? 
-*/
+/*
+  Which value is associated with this coefficient/variable combination?
+ */
 const union value *
 pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
                             const struct variable *v)
@@ -131,6 +160,14 @@ pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
   int i = 0;
   const struct variable *candidate;
 
+  if (c == NULL || v == NULL)
+    {
+      return NULL;
+    }
+  if (v->type == NULL)
+    {
+      return NULL;
+    }
   while (i < c->n_vars)
     {
       candidate = pspp_linreg_coeff_get_var (c, i);
@@ -142,34 +179,37 @@ pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
     }
   return NULL;
 }
+
 /*
   Which coefficient is associated with V? The VAL argument is relevant
   only to categorical variables.
  */
 const struct pspp_linreg_coeff *
-pspp_linreg_get_coeff (const pspp_linreg_cache *c,
-                      const struct variable *v,
-                      const union value *val)
+pspp_linreg_get_coeff (const pspp_linreg_cache * c,
+                      const struct variable *v, const union value *val)
 {
   int i = 1;
-  struct pspp_linreg_coeff *result;
-  const struct variable *tmp;
-
-  assert (c != NULL);
-  assert (c->coeff != NULL);
-  assert (c->n_indeps > 0);
-  assert (v != NULL);
-  assert (val != NULL);
-  
+  struct pspp_linreg_coeff *result = NULL;
+  const struct variable *tmp = NULL;
+
+  if (c == NULL)
+    {
+      return NULL;
+    }
+  if (c->coeff == NULL || c->n_indeps == NULL || v == NULL)
+    {
+      return NULL;
+    }
+
   result = c->coeff + i;
   tmp = pspp_linreg_coeff_get_var (result, 0);
   while (tmp->index != v->index && i < c->n_coeffs)
     {
-      i++;
       result = c->coeff + i;
       tmp = pspp_linreg_coeff_get_var (result, 0);
+      i++;
     }
-  if (i == c->n_coeffs)
+  if (i > c->n_coeffs)
     {
       return NULL;
     }
@@ -177,15 +217,16 @@ pspp_linreg_get_coeff (const pspp_linreg_cache *c,
     {
       return result;
     }
-  else
+  else if (val != NULL)
     {
       /*
-       If v is categorical, we need to ensure the coefficient
-       matches the VAL.
+         If v is categorical, we need to ensure the coefficient
+         matches the VAL.
        */
-      while (tmp->index != v->index && i < c->n_coeffs &&
-            compare_values (pspp_linreg_coeff_get_value (result, tmp), val, v->width))/*FIX THIS*/
-       {
+      while (tmp->index != v->index && i < c->n_coeffs
+            && compare_values (pspp_linreg_coeff_get_value (result, tmp),
+                               val, v->width))
+       {                       /* FIX THIS */
          i++;
          result = c->coeff + i;
          tmp = pspp_linreg_coeff_get_var (result, 0);
@@ -196,5 +237,5 @@ pspp_linreg_get_coeff (const pspp_linreg_cache *c,
        }
       return result;
     }
+  return NULL;
 }
-