1 /* lib/linreg/coefficient.c
3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Written by Jason H Stover.
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.
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.
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
23 Accessor functions for matching coefficients and variables.
25 #include <math/linreg/coefficient.h>
26 #include <math/linreg/linreg.h>
27 #include "src/math/design-matrix.h"
29 #include <gl/xalloc.h>
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.
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.
47 pspp_linreg_coeff_free (struct pspp_linreg_coeff *c)
53 Initialize the variable and value pointers inside the
54 coefficient structures for the linear model.
57 pspp_linreg_coeff_init (pspp_linreg_cache * c, struct design_matrix *X)
62 struct pspp_linreg_coeff *coeff;
64 c->coeff = xnmalloc (X->m->size2 + 1, sizeof (*c->coeff));
65 for (i = 0; i < X->m->size2; i++)
67 j = i + 1; /* The first coefficient is the intercept. */
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.
74 coeff->v_info = xnmalloc (coeff->n_vars, sizeof (*coeff->v_info));
75 assert (coeff->v_info != NULL);
77 (const struct variable *) design_matrix_col_to_var (X, i);
79 if (coeff->v_info->v->type == ALPHA)
82 k = design_matrix_var_to_column (X, coeff->v_info->v);
86 cat_subscript_to_value (k, (struct variable *) coeff->v_info->v);
91 pspp_linreg_coeff_set_estimate (struct pspp_linreg_coeff *c, double estimate)
93 c->estimate = estimate;
97 pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
103 Return the estimated value of the coefficient.
106 pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
112 Return the standard error of the estimated coefficient.
115 pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
121 How many variables are associated with this coefficient?
124 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
130 Which variable does this coefficient match?
132 const struct variable *
133 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
135 assert (i < c->n_vars);
136 return (c->v_info + i)->v;
140 Which value is associated with this coefficient/variable comination?
143 pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
144 const struct variable *v)
147 const struct variable *candidate;
149 while (i < c->n_vars)
151 candidate = pspp_linreg_coeff_get_var (c, i);
152 if (v->index == candidate->index)
154 return (c->v_info + i)->val;
161 Which coefficient is associated with V? The VAL argument is relevant
162 only to categorical variables.
164 const struct pspp_linreg_coeff *
165 pspp_linreg_get_coeff (const pspp_linreg_cache *c,
166 const struct variable *v,
167 const union value *val)
170 struct pspp_linreg_coeff *result;
171 const struct variable *tmp;
174 assert (c->coeff != NULL);
175 assert (c->n_indeps > 0);
177 assert (val != NULL);
179 result = c->coeff + i;
180 tmp = pspp_linreg_coeff_get_var (result, 0);
181 while (tmp->index != v->index && i < c->n_coeffs)
184 result = c->coeff + i;
185 tmp = pspp_linreg_coeff_get_var (result, 0);
187 if (i == c->n_coeffs)
191 if (v->type == NUMERIC)
198 If v is categorical, we need to ensure the coefficient
201 while (tmp->index != v->index && i < c->n_coeffs &&
202 compare_values (pspp_linreg_coeff_get_value (result, tmp), val, v->width))/*FIX THIS*/
205 result = c->coeff + i;
206 tmp = pspp_linreg_coeff_get_var (result, 0);
208 if (i == c->n_coeffs)