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 "src/math/design-matrix.h"
24 #include <gl/xalloc.h>
29 const struct variable *v; /* Variable associated with this
30 coefficient. Note this variable
31 may not be unique. In other words,
32 a coefficient structure may have
33 other v_info's, each with its own
35 const union value *val; /* Value of the variable v which this varinfo
36 refers to. This member is relevant only to
37 categorical variables. */
38 double mean; /* Mean for this variable */
39 double sd; /* Standard deviation for this variable */
43 pspp_coeff_free (struct pspp_coeff *c)
50 Initialize the variable and value pointers inside the
51 coefficient structures for the model.
54 pspp_coeff_init (struct pspp_coeff ** coeff, const struct design_matrix *X)
59 assert (coeff != NULL);
60 for (i = 0; i < X->m->size2; i++)
62 coeff[i] = xmalloc (sizeof (*coeff[i]));
63 coeff[i]->n_vars = n_vals; /* Currently, no procedures allow
64 interactions. This line will have to
65 change when procedures that allow
66 interaction terms are written.
68 coeff[i]->v_info = xnmalloc (coeff[i]->n_vars, sizeof (*coeff[i]->v_info));
69 assert (coeff[i]->v_info != NULL);
70 coeff[i]->v_info->v = design_matrix_col_to_var (X, i);
72 if (var_is_alpha (coeff[i]->v_info->v))
75 k = design_matrix_var_to_column (X, coeff[i]->v_info->v);
78 coeff[i]->v_info->val =
79 cat_subscript_to_value (k, coeff[i]->v_info->v);
81 coeff[i]->v_info->mean = 0.0;
82 coeff[i]->v_info->sd = 0.0;
86 pspp_coeff_set_estimate (struct pspp_coeff *coef, double estimate)
88 coef->estimate = estimate;
92 pspp_coeff_set_std_err (struct pspp_coeff *coef, double std_err)
94 coef->std_err = std_err;
98 Return the estimated value of the coefficient.
101 pspp_coeff_get_est (const struct pspp_coeff *coef)
107 return coef->estimate;
111 Return the standard error of the estimated coefficient.
114 pspp_coeff_get_std_err (const struct pspp_coeff *coef)
120 return coef->std_err;
124 How many variables are associated with this coefficient?
127 pspp_coeff_get_n_vars (struct pspp_coeff *coef)
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 *coef, int i)
147 assert (i < coef->n_vars);
148 return (coef->v_info + i)->v;
152 Which coefficient does this variable match? If the variable is
153 categorical, and has more than one coefficient, use the VAL to find
157 pspp_coeff_var_to_coeff (const struct variable *v, struct pspp_coeff **coefs,
158 size_t n_coef, const union value *val)
163 struct pspp_coeff *result = NULL;
167 v_idx = var_get_dict_index (v);
168 while (i < n_coef && var_get_dict_index (coefs[i]->v_info->v) != v_idx)
173 if (var_is_alpha (v))
176 Use the VAL to find the coefficient.
181 while (j < n_coef && compare_values (pspp_coeff_get_value (coefs[j], v),
182 val, var_get_width (v)) != 0)
186 result = ((j < n_coef) ? coefs[j] : NULL);
194 Which value is associated with this coefficient/variable combination?
197 pspp_coeff_get_value (struct pspp_coeff *coef,
198 const struct variable *v)
201 const struct variable *candidate;
203 if (coef == NULL || v == NULL)
207 if (var_is_numeric (v))
211 while (i < coef->n_vars)
213 candidate = pspp_coeff_get_var (coef, i);
216 return (coef->v_info + i)->val;
224 Get or set the standard deviation of the variable associated with this coefficient.
226 double pspp_coeff_get_sd (const struct pspp_coeff *coef)
228 return coef->v_info->sd;
230 void pspp_coeff_set_sd (struct pspp_coeff *coef, double s)
232 coef->v_info->sd = s;
236 Get or set the mean for the variable associated with this coefficient.
238 double pspp_coeff_get_mean (const struct pspp_coeff *coef)
240 return coef->v_info->mean;
243 void pspp_coeff_set_mean (struct pspp_coeff *coef, double m)
245 coef->v_info->mean = m;