From f4ac26fb5880f9aeba2c7303d0e687b183bc3da1 Mon Sep 17 00:00:00 2001 From: Jason Stover Date: Wed, 5 Apr 2006 19:18:51 +0000 Subject: [PATCH] added predict function for linear model --- src/math/linreg/ChangeLog | 11 ++++++- src/math/linreg/automake.mk | 4 ++- src/math/linreg/coefficient.c | 21 ++++++++++-- src/math/linreg/coefficient.h | 34 +++++++++++++------ src/math/linreg/linreg.h | 6 ++-- src/math/linreg/predict.c | 62 +++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 src/math/linreg/predict.c diff --git a/src/math/linreg/ChangeLog b/src/math/linreg/ChangeLog index dfcea1f0..7b6ee73d 100644 --- a/src/math/linreg/ChangeLog +++ b/src/math/linreg/ChangeLog @@ -1,7 +1,16 @@ +2006-04-05 Jason Stover + + * 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 * 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 diff --git a/src/math/linreg/automake.mk b/src/math/linreg/automake.mk index 61263bb8..5519ba98 100644 --- a/src/math/linreg/automake.mk +++ b/src/math/linreg/automake.mk @@ -3,7 +3,9 @@ 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 + diff --git a/src/math/linreg/coefficient.c b/src/math/linreg/coefficient.c index be189baa..d09432b3 100644 --- a/src/math/linreg/coefficient.c +++ b/src/math/linreg/coefficient.c @@ -22,12 +22,9 @@ /* Accessor functions for matching coefficients and variables. */ -#include #include #include #include "src/math/design-matrix.h" -#include "src/data/variable.h" -#include "src/data/value.h" #include @@ -102,6 +99,24 @@ 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) +{ + 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? */ diff --git a/src/math/linreg/coefficient.h b/src/math/linreg/coefficient.h index 2537c197..c2c7200c 100644 --- a/src/math/linreg/coefficient.h +++ b/src/math/linreg/coefficient.h @@ -23,8 +23,10 @@ #ifndef COEFFICIENT_H #define COEFFICIENT_H - +#include #include +#include +#include struct design_matrix; @@ -63,42 +65,54 @@ struct pspp_linreg_coeff 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 *, diff --git a/src/math/linreg/linreg.h b/src/math/linreg/linreg.h index 63f980b1..2852dd77 100644 --- a/src/math/linreg/linreg.h +++ b/src/math/linreg/linreg.h @@ -28,7 +28,7 @@ struct variable ; struct pspp_linreg_coeff; - +union value; enum { @@ -178,5 +178,7 @@ int pspp_linreg (const gsl_vector * Y, const gsl_matrix * X, 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 diff --git a/src/math/linreg/predict.c b/src/math/linreg/predict.c new file mode 100644 index 00000000..f2539c0e --- /dev/null +++ b/src/math/linreg/predict.c @@ -0,0 +1,62 @@ +/* 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 +#include + +/* + 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; +} -- 2.30.2