+2006-04-05 Jason Stover <jhs@math.gcsu.edu>
+
+ * predict.c: New file. pspp_linreg_predict() uses a linear model
+ to return a predicted value of the dependent variable.
+
+ * coefficient.c: Added pspp_linreg_coeff_get_est() and
+ pspp_linreg_coeff_get_std_err() to access estimated values of
+ coefficients and standard errors.
+
2006-04-03 Jason Stover <jhs@math.gcsu.edu>
* coefficient.c: Added pspp_linreg_get_coeff() to find a
- coefficient corresponding to a variabl/value combination.
+ coefficient corresponding to a variable/value combination.
Thu Mar 2 08:40:33 WST 2006 John Darrington <john@darrington.wattle.id.au>
noinst_LIBRARIES += src/math/linreg/libpspp_linreg.a
src_math_linreg_libpspp_linreg_a_SOURCES = \
+ src/math/linreg/predict.c \
src/math/linreg/coefficient.c \
src/math/linreg/coefficient.h \
src/math/linreg/linreg.c \
- src/math/linreg/linreg.h
+ src/math/linreg/linreg.h
+
/*
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>
c->std_err = std_err;
}
+/*
+ Return the estimated value of the coefficient.
+ */
+double
+pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
+{
+ assert (c != NULL);
+ return c->estimate;
+}
+/*
+ Return the standard error of the estimated coefficient.
+*/
+double
+pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
+{
+ assert (c != NULL);
+ return c->std_err;
+}
/*
How many variables are associated with this coefficient?
*/
#ifndef COEFFICIENT_H
#define COEFFICIENT_H
-
+#include <assert.h>
#include <math/linreg/linreg.h>
+#include <src/data/variable.h>
+#include <src/data/value.h>
struct design_matrix;
Accessor functions for matching coefficients and variables.
*/
-void pspp_linreg_coeff_free (struct pspp_linreg_coeff *c);
+void pspp_linreg_coeff_free (struct pspp_linreg_coeff *);
/*
Initialize the variable and value pointers inside the
coefficient structures for the linear model.
*/
void
-pspp_linreg_coeff_init (pspp_linreg_cache *c,
- struct design_matrix *X);
+pspp_linreg_coeff_init (pspp_linreg_cache *,
+ struct design_matrix *);
void
-pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c,
+pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *,
double estimate);
void
-pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c,
+pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *,
double std_err);
+/*
+ Return the estimated value of the coefficient.
+ */
+double
+pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *);
+
+/*
+ Return the standard error of the estimated coefficient.
+*/
+double
+pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *);
+
/*
How many variables are associated with this coefficient?
*/
int
-pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c);
+pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *);
/*
Which variable does this coefficient match?
*/
const struct variable *
-pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i);
+pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *, int );
/*
Which value is associated with this coefficient/variable comination?
*/
const union value *
-pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
- const struct variable *v);
+pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *,
+ const struct variable *);
const struct pspp_linreg_coeff *
pspp_linreg_get_coeff (const pspp_linreg_cache *,
struct variable ;
struct pspp_linreg_coeff;
-
+union value;
enum
{
const pspp_linreg_opts * opts,
pspp_linreg_cache * cache);
-
+double
+pspp_linreg_predict (const struct variable *, const union value *,
+ const pspp_linreg_cache *, int);
#endif
--- /dev/null
+/* lib/linreg/predict.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.
+*/
+
+#include <math/linreg/linreg.h>
+#include <math/linreg/coefficient.h>
+
+/*
+ Predict the value of the dependent variable with the
+ new set of predictors. PREDICTORS must point to a list
+ of variables, each of whose values are stored in VALS,
+ in the same order.
+ */
+double
+pspp_linreg_predict (const struct variable *predictors,
+ const union value *vals,
+ const pspp_linreg_cache *c,
+ int n_vals)
+{
+ int i;
+ double result;
+ double tmp;
+
+ assert (predictors != NULL);
+ assert (vals != NULL);
+ assert (c != NULL);
+
+ result = c->coeff->estimate; /* Intercept. */
+
+ /*
+ Stop at the minimum of c->n_coeffs and n_vals in case
+ the caller passed us inadequate information, such as too
+ few or too many values.
+ */
+ for (i = 1; i < c->n_coeffs && i < n_vals; i++)
+ {
+ tmp = pspp_linreg_coeff_get_est (pspp_linreg_get_coeff (c, predictors + i, vals + i));
+ if ((predictors + i)->type == NUMERIC)
+ {
+ tmp *= (vals + i)->f;
+ }
+ result += tmp;
+ }
+ return result;
+}