added residual computation; improved handling of bad input
[pspp-builds.git] / src / math / linreg / coefficient.c
1 /* lib/linreg/coefficient.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 /*
23   Accessor functions for matching coefficients and variables.
24  */
25 #include <math/linreg/coefficient.h>
26 #include <math/linreg/linreg.h>
27 #include "src/math/design-matrix.h"
28
29 #include <gl/xalloc.h>
30
31
32 struct varinfo
33 {
34   const struct variable *v;     /* Variable associated with this
35                                    coefficient. Note this variable may not
36                                    be unique. In other words, a
37                                    coefficient structure may have other
38                                    v_info's, each with its own variable.
39                                  */
40   const union value *val;       /* Value of the variable v which this
41                                    varinfo refers to. This member is relevant
42                                    only to categorical variables.
43                                  */
44 };
45
46 void
47 pspp_linreg_coeff_free (struct pspp_linreg_coeff *c)
48 {
49   free (c);
50 }
51
52 /*
53   Initialize the variable and value pointers inside the
54   coefficient structures for the linear model.
55  */
56 void
57 pspp_linreg_coeff_init (pspp_linreg_cache * c, struct design_matrix *X)
58 {
59   size_t i;
60   size_t j;
61   int n_vals = 1;
62   struct pspp_linreg_coeff *coeff;
63
64   c->coeff = xnmalloc (X->m->size2 + 1, sizeof (*c->coeff));
65   for (i = 0; i < X->m->size2; i++)
66     {
67       j = i + 1;                /* The first coefficient is the intercept. */
68       coeff = c->coeff + j;
69       coeff->n_vars = n_vals;   /* Currently, no procedures allow
70                                    interactions.  This will have to
71                                    change when procedures that allow
72                                    interaction terms are written.
73                                  */
74       coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
75       assert (coeff->v_info != NULL);
76       coeff->v_info->v =
77         (const struct variable *) design_matrix_col_to_var (X, i);
78
79       if (coeff->v_info->v->type == ALPHA)
80         {
81           size_t k;
82           k = design_matrix_var_to_column (X, coeff->v_info->v);
83           assert (k <= i);
84           k = i - k;
85           coeff->v_info->val =
86             cat_subscript_to_value (k, (struct variable *) coeff->v_info->v);
87         }
88     }
89 }
90 void
91 pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c, double estimate)
92 {
93   c->estimate = estimate;
94 }
95
96 void
97 pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
98 {
99   c->std_err = std_err;
100 }
101
102 /*
103   Return the estimated value of the coefficient.
104  */
105 double
106 pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
107 {
108   if (c == NULL)
109     {
110       return 0.0;
111     }
112   return c->estimate;
113 }
114 /*
115   Return the standard error of the estimated coefficient.
116 */
117 double 
118 pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
119 {
120   if (c == NULL)
121     {
122       return 0.0;
123     }
124   return c->std_err;
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 comination? 
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   while (i < c->n_vars)
168     {
169       candidate = pspp_linreg_coeff_get_var (c, i);
170       if (v->index == candidate->index)
171         {
172           return (c->v_info + i)->val;
173         }
174       i++;
175     }
176   return NULL;
177 }
178 /*
179   Which coefficient is associated with V? The VAL argument is relevant
180   only to categorical variables.
181  */
182 const struct pspp_linreg_coeff *
183 pspp_linreg_get_coeff (const pspp_linreg_cache *c,
184                        const struct variable *v,
185                        const union value *val)
186 {
187   int i = 1;
188   struct pspp_linreg_coeff *result;
189   const struct variable *tmp;
190
191   if (c == NULL || c->coeff == NULL || c->n_indeps == NULL || v == NULL)
192     {
193       return NULL;
194     }
195   
196   result = c->coeff + i;
197   tmp = pspp_linreg_coeff_get_var (result, 0);
198   while (tmp->index != v->index && i < c->n_coeffs)
199     {
200       i++;
201       result = c->coeff + i;
202       tmp = pspp_linreg_coeff_get_var (result, 0);
203     }
204   if (i == c->n_coeffs)
205     {
206       return NULL;
207     }
208   if (v->type == NUMERIC)
209     {
210       return result;
211     }
212   else if (val != NULL)
213     {
214       /*
215         If v is categorical, we need to ensure the coefficient
216         matches the VAL.
217        */
218       while (tmp->index != v->index && i < c->n_coeffs &&
219              compare_values (pspp_linreg_coeff_get_value (result, tmp), val, v->width))/*FIX THIS*/
220         {
221           i++;
222           result = c->coeff + i;
223           tmp = pspp_linreg_coeff_get_var (result, 0);
224         }
225       if (i == c->n_coeffs)
226         {
227           return NULL;
228         }
229       return result;
230     }
231   return NULL;
232 }
233