updated residual function; fixed bug in coefficient.c
[pspp-builds.git] / src / math / linreg / coefficient.c
1 /*
2   lib/linreg/coefficient.c
3   
4   Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H Stover.
5   
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)
9   any later version.
10   
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
14   more details.
15   
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.
19 */
20
21 /*
22   Accessor functions for matching coefficients and variables.
23  */
24 #include <math/linreg/coefficient.h>
25 #include <math/linreg/linreg.h>
26 #include "src/math/design-matrix.h"
27
28 #include <gl/xalloc.h>
29
30
31 struct varinfo
32 {
33   const struct variable *v;     /* Variable associated with this
34                                    coefficient. Note this variable
35                                    may not be unique. In other words,
36                                    a coefficient structure may have
37                                    other v_info's, each with its own
38                                    variable. */
39   const union value *val;       /* Value of the variable v which this varinfo
40                                    refers to. This member is relevant only to
41                                    categorical variables. */
42 };
43
44 void
45 pspp_linreg_coeff_free (struct pspp_linreg_coeff *c)
46 {
47   free (c);
48 }
49
50 /*
51   Initialize the variable and value pointers inside the
52   coefficient structures for the linear model.
53  */
54 void
55 pspp_linreg_coeff_init (pspp_linreg_cache * c, struct design_matrix *X)
56 {
57   size_t i;
58   size_t j;
59   int n_vals = 1;
60   struct pspp_linreg_coeff *coeff;
61
62   c->coeff = xnmalloc (X->m->size2 + 1, sizeof (*c->coeff));
63   for (i = 0; i < X->m->size2; i++)
64     {
65       j = i + 1;                /* The first coefficient is the intercept. */
66       coeff = c->coeff + j;
67       coeff->n_vars = n_vals;   /* Currently, no procedures allow
68                                    interactions.  This line will have to
69                                    change when procedures that allow
70                                    interaction terms are written. 
71                                 */
72       coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
73       assert (coeff->v_info != NULL);
74       coeff->v_info->v =
75         (const struct variable *) design_matrix_col_to_var (X, i);
76
77       if (coeff->v_info->v->type == ALPHA)
78         {
79           size_t k;
80           k = design_matrix_var_to_column (X, coeff->v_info->v);
81           assert (k <= i);
82           k = i - k;
83           coeff->v_info->val =
84             cat_subscript_to_value (k, (struct variable *) coeff->v_info->v);
85         }
86     }
87 }
88 void
89 pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c, double estimate)
90 {
91   c->estimate = estimate;
92 }
93
94 void
95 pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
96 {
97   c->std_err = std_err;
98 }
99
100 /*
101   Return the estimated value of the coefficient.
102  */
103 double
104 pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
105 {
106   if (c == NULL)
107     {
108       return 0.0;
109     }
110   return c->estimate;
111 }
112
113 /*
114   Return the standard error of the estimated coefficient.
115 */
116 double
117 pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
118 {
119   if (c == NULL)
120     {
121       return 0.0;
122     }
123   return c->std_err;
124 }
125
126 /*
127   How many variables are associated with this coefficient?
128  */
129 int
130 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
131 {
132   if (c == NULL)
133     {
134       return 0;
135     }
136   return c->n_vars;
137 }
138
139 /*
140   Which variable does this coefficient match?
141  */
142 const struct variable *
143 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
144 {
145   if (c == NULL)
146     {
147       return NULL;
148     }
149   assert (i < c->n_vars);
150   return (c->v_info + i)->v;
151 }
152
153 /*
154   Which value is associated with this coefficient/variable combination?
155  */
156 const union value *
157 pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
158                              const struct variable *v)
159 {
160   int i = 0;
161   const struct variable *candidate;
162
163   if (c == NULL || v == NULL)
164     {
165       return NULL;
166     }
167   if (v->type == NULL)
168     {
169       return NULL;
170     }
171   while (i < c->n_vars)
172     {
173       candidate = pspp_linreg_coeff_get_var (c, i);
174       if (v->index == candidate->index)
175         {
176           return (c->v_info + i)->val;
177         }
178       i++;
179     }
180   return NULL;
181 }
182
183 /*
184   Which coefficient is associated with V? The VAL argument is relevant
185   only to categorical variables.
186  */
187 const struct pspp_linreg_coeff *
188 pspp_linreg_get_coeff (const pspp_linreg_cache * c,
189                        const struct variable *v, const union value *val)
190 {
191   int i = 1;
192   struct pspp_linreg_coeff *result = NULL;
193   const struct variable *tmp = NULL;
194
195   if (c == NULL)
196     {
197       return NULL;
198     }
199   if (c->coeff == NULL || c->n_indeps == NULL || v == NULL)
200     {
201       return NULL;
202     }
203
204   result = c->coeff + i;
205   tmp = pspp_linreg_coeff_get_var (result, 0);
206   while (tmp->index != v->index && i < c->n_coeffs)
207     {
208       result = c->coeff + i;
209       tmp = pspp_linreg_coeff_get_var (result, 0);
210       i++;
211     }
212   if (i == c->n_coeffs)
213     {
214       return NULL;
215     }
216   if (v->type == NUMERIC)
217     {
218       return result;
219     }
220   else if (val != NULL)
221     {
222       /*
223          If v is categorical, we need to ensure the coefficient
224          matches the VAL.
225        */
226       while (tmp->index != v->index && i < c->n_coeffs
227              && compare_values (pspp_linreg_coeff_get_value (result, tmp),
228                                 val, v->width))
229         {                       /* FIX THIS */
230           i++;
231           result = c->coeff + i;
232           tmp = pspp_linreg_coeff_get_var (result, 0);
233         }
234       if (i == c->n_coeffs)
235         {
236           return NULL;
237         }
238       return result;
239     }
240   return NULL;
241 }