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 Accessor functions for matching coefficients and variables.
24 #include <math/coefficient.h>
25 #include <math/linreg/linreg.h>
26 #include "src/math/design-matrix.h"
28 #include <gl/xalloc.h>
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
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. */
45 pspp_coeff_free (struct pspp_coeff *c)
52 Initialize the variable and value pointers inside the
53 coefficient structures for the linear model.
56 pspp_coeff_init (struct pspp_coeff ** c, const struct design_matrix *X)
61 for (i = 0; i < X->m->size2; i++)
63 c[i] = xmalloc (sizeof (*c[i]));
64 c[i]->n_vars = n_vals; /* Currently, no procedures allow
65 interactions. This line will have to
66 change when procedures that allow
67 interaction terms are written.
69 c[i]->v_info = xnmalloc (c[i]->n_vars, sizeof (*c[i]->v_info));
70 assert (c[i]->v_info != NULL);
72 (const struct variable *) design_matrix_col_to_var (X, i);
74 if (c[i]->v_info->v->type == ALPHA)
77 k = design_matrix_var_to_column (X, c[i]->v_info->v);
81 cat_subscript_to_value (k, (struct variable *) c[i]->v_info->v);
86 pspp_coeff_set_estimate (struct pspp_coeff *c, double estimate)
88 c->estimate = estimate;
92 pspp_coeff_set_std_err (struct pspp_coeff *c, double std_err)
98 Return the estimated value of the coefficient.
101 pspp_coeff_get_est (const struct pspp_coeff *c)
111 Return the standard error of the estimated coefficient.
114 pspp_coeff_get_std_err (const struct pspp_coeff *c)
124 How many variables are associated with this coefficient?
127 pspp_coeff_get_n_vars (struct pspp_coeff *c)
137 Which variable does this coefficient match? I should be
138 0 unless the coefficient refers to an interaction term.
140 const struct variable *
141 pspp_coeff_get_var (struct pspp_coeff *c, int i)
147 assert (i < c->n_vars);
148 return (c->v_info + i)->v;
152 Which value is associated with this coefficient/variable combination?
155 pspp_coeff_get_value (struct pspp_coeff *c,
156 const struct variable *v)
159 const struct variable *candidate;
161 if (c == NULL || v == NULL)
165 if (v->type == NUMERIC)
169 while (i < c->n_vars)
171 candidate = pspp_coeff_get_var (c, i);
172 if (v->index == candidate->index)
174 return (c->v_info + i)->val;
182 Which coefficient is associated with V? The VAL argument is relevant
183 only to categorical variables.
185 const struct pspp_coeff *
186 pspp_linreg_get_coeff (const pspp_linreg_cache * c,
187 const struct variable *v, const union value *val)
190 struct pspp_coeff *result = NULL;
191 const struct variable *tmp = NULL;
197 if (c->coeff == NULL || c->n_indeps == 0 || v == NULL)
202 result = c->coeff[i];
203 tmp = pspp_coeff_get_var (result, 0);
204 while (tmp->index != v->index && i < c->n_coeffs)
206 result = c->coeff[i];
207 tmp = pspp_coeff_get_var (result, 0);
214 if (v->type == NUMERIC)
218 else if (val != NULL)
221 If v is categorical, we need to ensure the coefficient
224 while (tmp->index != v->index && i < c->n_coeffs
225 && compare_values (pspp_coeff_get_value (result, tmp),
229 result = c->coeff[i];
230 tmp = pspp_coeff_get_var (result, 0);
232 if (i == c->n_coeffs)