added predict function for linear model
[pspp-builds.git] / src / math / linreg / predict.c
1 /* lib/linreg/predict.c
2
3  Copyright (C) 2005 Free Software Foundation, Inc.
4  Written by Jason H. Stover.
5
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at
9  your option) any later version.
10
11  This program is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02111-1307, USA.
20 */
21
22 #include <math/linreg/linreg.h>
23 #include <math/linreg/coefficient.h>
24
25 /*
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,
29   in the same order.
30  */
31 double
32 pspp_linreg_predict (const struct variable *predictors, 
33                      const union value *vals, 
34                      const pspp_linreg_cache *c,
35                      int n_vals)
36 {
37   int i;
38   double result;
39   double tmp;
40   
41   assert (predictors != NULL);
42   assert (vals != NULL);
43   assert (c != NULL);
44
45   result = c->coeff->estimate; /* Intercept. */
46
47   /*
48     Stop at the minimum of c->n_coeffs and n_vals in case
49     the caller passed us inadequate information, such as too
50     few or too many values.
51    */
52   for (i = 1; i < c->n_coeffs && i < n_vals; i++)
53     {
54       tmp = pspp_linreg_coeff_get_est (pspp_linreg_get_coeff (c, predictors + i, vals + i));
55       if ((predictors + i)->type == NUMERIC)
56         {
57           tmp *= (vals + i)->f;
58         }
59       result += tmp;
60     }
61   return result;
62 }