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.
21 #include <math/linreg/linreg.h>
22 #include <math/coefficient.h>
23 #include <gl/xalloc.h>
26 Predict the value of the dependent variable with the
27 new set of predictors. PREDICTORS must point to a list
28 of variables, each of whose values are stored in VALS,
32 pspp_linreg_predict (const struct variable **predictors,
33 const union value **vals, const void *c_, int n_vals)
35 const pspp_linreg_cache *c = c_;
38 const struct pspp_coeff **found;
39 const struct pspp_coeff *coe;
43 if (predictors == NULL || vals == NULL || c == NULL)
49 /* The stupid model: just guess the mean. */
50 return c->depvar_mean;
52 found = xnmalloc (c->n_coeffs, sizeof (*found));
54 result = c->coeff[0]->estimate; /* Intercept. */
57 The loops guard against the possibility that the caller passed us
58 inadequate information, such as too few or too many values, or
59 a redundant list of variable names.
61 for (j = 0; j < n_vals; j++)
63 coe = pspp_linreg_get_coeff (c, predictors[j], vals[j]);
65 while (found[i] == coe && i < c->n_coeffs)
72 tmp = pspp_coeff_get_est (coe);
73 if (var_is_numeric (predictors[j]))
86 pspp_linreg_residual (const struct variable **predictors,
87 const union value **vals,
88 const union value *obs, const void *c, int n_vals)
93 if (predictors == NULL || vals == NULL || c == NULL || obs == NULL)
97 pred = pspp_linreg_predict (predictors, vals, c, n_vals);
99 result = gsl_isnan (pred) ? GSL_NAN : (obs->f - pred);