2 lib/linreg/coefficient.c
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/linreg/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_linreg_coeff_free (struct pspp_linreg_coeff *c)
51 Initialize the variable and value pointers inside the
52 coefficient structures for the linear model.
55 pspp_linreg_coeff_init (pspp_linreg_cache * c, struct design_matrix *X)
60 struct pspp_linreg_coeff *coeff;
62 c->coeff = xnmalloc (X->m->size2 + 1, sizeof (*c->coeff));
63 c->coeff->v_info = NULL; /* Intercept has no associated variable. */
64 for (i = 0; i < X->m->size2; i++)
66 j = i + 1; /* The first coefficient is the intercept. */
68 coeff->n_vars = n_vals; /* Currently, no procedures allow
69 interactions. This line will have to
70 change when procedures that allow
71 interaction terms are written.
73 coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
74 assert (coeff->v_info != NULL);
76 (const struct variable *) design_matrix_col_to_var (X, i);
78 if (coeff->v_info->v->type == ALPHA)
81 k = design_matrix_var_to_column (X, coeff->v_info->v);
85 cat_subscript_to_value (k, (struct variable *) coeff->v_info->v);
90 pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c, double estimate)
92 c->estimate = estimate;
96 pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
102 Return the estimated value of the coefficient.
105 pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
115 Return the standard error of the estimated coefficient.
118 pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
128 How many variables are associated with this coefficient?
131 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
141 Which variable does this coefficient match? I should be
142 0 unless the coefficient refers to an interaction term.
144 const struct variable *
145 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
151 assert (i < c->n_vars);
152 return (c->v_info + i)->v;
156 Which value is associated with this coefficient/variable combination?
159 pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
160 const struct variable *v)
163 const struct variable *candidate;
165 if (c == NULL || v == NULL)
169 if (v->type == NUMERIC)
173 while (i < c->n_vars)
175 candidate = pspp_linreg_coeff_get_var (c, i);
176 if (v->index == candidate->index)
178 return (c->v_info + i)->val;
186 Which coefficient is associated with V? The VAL argument is relevant
187 only to categorical variables.
189 const struct pspp_linreg_coeff *
190 pspp_linreg_get_coeff (const pspp_linreg_cache * c,
191 const struct variable *v, const union value *val)
194 struct pspp_linreg_coeff *result = NULL;
195 const struct variable *tmp = NULL;
201 if (c->coeff == NULL || c->n_indeps == 0 || v == NULL)
206 result = c->coeff + i;
207 tmp = pspp_linreg_coeff_get_var (result, 0);
208 while (tmp->index != v->index && i < c->n_coeffs)
210 result = c->coeff + i;
211 tmp = pspp_linreg_coeff_get_var (result, 0);
218 if (v->type == NUMERIC)
222 else if (val != NULL)
225 If v is categorical, we need to ensure the coefficient
228 while (tmp->index != v->index && i < c->n_coeffs
229 && compare_values (pspp_linreg_coeff_get_value (result, tmp),
233 result = c->coeff + i;
234 tmp = pspp_linreg_coeff_get_var (result, 0);
236 if (i == c->n_coeffs)