4 Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H. Stover.
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 51
18 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
22 #include <math/linreg/linreg.h>
23 #include <math/coefficient.h>
24 #include <gl/xalloc.h>
27 Predict the value of the dependent variable with the
28 new set of predictors. PREDICTORS must point to a list
29 of variables, each of whose values are stored in VALS,
33 pspp_linreg_predict (const struct variable **predictors,
34 const union value **vals, const void *c_, int n_vals)
36 const pspp_linreg_cache *c = c_;
39 const struct pspp_coeff **found;
40 const struct pspp_coeff *coe;
44 if (predictors == NULL || vals == NULL || c == NULL)
50 /* The stupid model: just guess the mean. */
51 return c->depvar_mean;
53 found = xnmalloc (c->n_coeffs, sizeof (*found));
55 result = c->coeff[0]->estimate; /* Intercept. */
58 The loops guard against the possibility that the caller passed us
59 inadequate information, such as too few or too many values, or
60 a redundant list of variable names.
62 for (j = 0; j < n_vals; j++)
64 coe = pspp_linreg_get_coeff (c, predictors[j], vals[j]);
66 while (found[i] == coe && i < c->n_coeffs)
73 tmp = pspp_coeff_get_est (coe);
74 if (var_is_numeric (predictors[j]))
87 pspp_linreg_residual (const struct variable **predictors,
88 const union value **vals,
89 const union value *obs, const void *c, int n_vals)
94 if (predictors == NULL || vals == NULL || c == NULL || obs == NULL)
98 pred = pspp_linreg_predict (predictors, vals, c, n_vals);
100 result = gsl_isnan (pred) ? GSL_NAN : (obs->f - pred);