1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 Accessor functions for matching coefficients and variables.
21 #include <math/coefficient.h>
22 #include <math/linreg/linreg.h>
23 #include "src/math/design-matrix.h"
25 #include <gl/xalloc.h>
30 const struct variable *v; /* Variable associated with this
31 coefficient. Note this variable
32 may not be unique. In other words,
33 a coefficient structure may have
34 other v_info's, each with its own
36 const union value *val; /* Value of the variable v which this varinfo
37 refers to. This member is relevant only to
38 categorical variables. */
42 pspp_coeff_free (struct pspp_coeff *c)
49 Initialize the variable and value pointers inside the
50 coefficient structures for the linear model.
53 pspp_coeff_init (struct pspp_coeff ** c, const struct design_matrix *X)
58 for (i = 0; i < X->m->size2; i++)
60 c[i] = xmalloc (sizeof (*c[i]));
61 c[i]->n_vars = n_vals; /* Currently, no procedures allow
62 interactions. This line will have to
63 change when procedures that allow
64 interaction terms are written.
66 c[i]->v_info = xnmalloc (c[i]->n_vars, sizeof (*c[i]->v_info));
67 assert (c[i]->v_info != NULL);
68 c[i]->v_info->v = design_matrix_col_to_var (X, i);
70 if (var_is_alpha (c[i]->v_info->v))
73 k = design_matrix_var_to_column (X, c[i]->v_info->v);
77 cat_subscript_to_value (k, c[i]->v_info->v);
82 pspp_coeff_set_estimate (struct pspp_coeff *c, double estimate)
84 c->estimate = estimate;
88 pspp_coeff_set_std_err (struct pspp_coeff *c, double std_err)
94 Return the estimated value of the coefficient.
97 pspp_coeff_get_est (const struct pspp_coeff *c)
107 Return the standard error of the estimated coefficient.
110 pspp_coeff_get_std_err (const struct pspp_coeff *c)
120 How many variables are associated with this coefficient?
123 pspp_coeff_get_n_vars (struct pspp_coeff *c)
133 Which variable does this coefficient match? I should be
134 0 unless the coefficient refers to an interaction term.
136 const struct variable *
137 pspp_coeff_get_var (struct pspp_coeff *c, int i)
143 assert (i < c->n_vars);
144 return (c->v_info + i)->v;
148 Which value is associated with this coefficient/variable combination?
151 pspp_coeff_get_value (struct pspp_coeff *c,
152 const struct variable *v)
155 const struct variable *candidate;
157 if (c == NULL || v == NULL)
161 if (var_is_numeric (v))
165 while (i < c->n_vars)
167 candidate = pspp_coeff_get_var (c, i);
170 return (c->v_info + i)->val;
178 Which coefficient is associated with V? The VAL argument is relevant
179 only to categorical variables.
181 const struct pspp_coeff *
182 pspp_linreg_get_coeff (const pspp_linreg_cache * c,
183 const struct variable *v, const union value *val)
186 struct pspp_coeff *result = NULL;
187 const struct variable *tmp = NULL;
193 if (c->coeff == NULL || c->n_indeps == 0 || v == NULL)
198 result = c->coeff[i];
199 tmp = pspp_coeff_get_var (result, 0);
200 while (tmp != v && i < c->n_coeffs)
202 result = c->coeff[i];
203 tmp = pspp_coeff_get_var (result, 0);
210 if (var_is_numeric (v))
214 else if (val != NULL)
217 If v is categorical, we need to ensure the coefficient
220 while (tmp != v && i < c->n_coeffs
221 && compare_values (pspp_coeff_get_value (result, tmp),
222 val, var_get_width (v)))
225 result = c->coeff[i];
226 tmp = pspp_coeff_get_var (result, 0);
228 if (i == c->n_coeffs)