1 /* PSPP - Creates design-matrices.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Jason H Stover <jason@sakla.net>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Create design matrices for procedures that need them.
30 #include "design-matrix.h"
33 #include <gsl/gsl_machine.h>
34 #include <gsl/gsl_vector.h>
35 #include <gsl/gsl_matrix.h>
37 #define DM_COLUMN_NOT_FOUND -1
38 #define DM_INDEX_NOT_FOUND -3
41 Which element of a vector is equal to the value x?
44 cat_which_element_eq (const gsl_vector * vec, double x)
48 for (i = 0; i < vec->size; i++)
50 if (fabs (gsl_vector_get (vec, i) - x) < GSL_DBL_EPSILON)
55 return CAT_VALUE_NOT_FOUND;
58 cat_is_zero_vector (const gsl_vector * vec)
62 for (i = 0; i < vec->size; i++)
64 if (gsl_vector_get (vec, i) != 0.0)
73 Return the value of v corresponding to the vector vec.
76 cat_vector_to_value (const gsl_vector * vec, struct variable *v)
80 i = cat_which_element_eq (vec, 1.0);
81 if (i != CAT_VALUE_NOT_FOUND)
83 return cat_subscript_to_value (i + 1, v);
85 if (cat_is_zero_vector (vec))
87 return cat_subscript_to_value (0, v);
92 struct design_matrix *
93 design_matrix_create (int n_variables,
94 const struct variable *v_variables[],
97 struct design_matrix *dm;
98 const struct variable *v;
103 dm = xmalloc (sizeof *dm);
104 dm->vars = xnmalloc (n_variables, sizeof *dm->vars);
105 dm->n_vars = n_variables;
107 for (i = 0; i < n_variables; i++)
110 assert ((dm->vars + i) != NULL);
111 (dm->vars + i)->v = v; /* Allows us to look up the variable from
112 the design matrix. */
113 (dm->vars + i)->first_column = n_cols;
114 if (v->type == NUMERIC)
116 (dm->vars + i)->last_column = n_cols;
119 else if (v->type == ALPHA)
121 assert (v->obs_vals != NULL);
122 (dm->vars + i)->last_column =
123 (dm->vars + i)->first_column + v->obs_vals->n_categories - 2;
124 n_cols += v->obs_vals->n_categories - 1;
127 dm->m = gsl_matrix_calloc (n_data, n_cols);
134 design_matrix_destroy (struct design_matrix *dm)
137 gsl_matrix_free (dm->m);
142 Return the index of the variable for the
146 design_matrix_col_to_var_index (const struct design_matrix *dm, size_t col)
149 struct design_matrix_var v;
151 for (i = 0; i < dm->n_vars; i++)
154 if (v.first_column <= col && col <= v.last_column)
157 return DM_INDEX_NOT_FOUND;
161 Return a pointer to the variable whose values
162 are stored in column col.
165 design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
169 struct design_matrix_var dmv;
171 index = design_matrix_col_to_var_index (dm, col);
172 for (i = 0; i < dm->n_vars; i++)
175 if ((dmv.v)->index == index)
177 return (struct variable *) dmv.v;
184 cmp_dm_var_index (const struct design_matrix_var *dmv, size_t index)
186 if (dmv->v->index == index)
192 Return the number of the first column which holds the
193 values for variable v.
196 design_matrix_var_to_column (const struct design_matrix * dm,
197 const struct variable * v)
200 struct design_matrix_var tmp;
202 for (i = 0; i < dm->n_vars; i++)
205 if (cmp_dm_var_index (&tmp, v->index))
207 return tmp.first_column;
210 return DM_COLUMN_NOT_FOUND;
215 dm_var_to_last_column (const struct design_matrix *dm,
216 const struct variable *v)
219 struct design_matrix_var tmp;
221 for (i = 0; i < dm->n_vars; i++)
224 if (cmp_dm_var_index (&tmp, v->index))
226 return tmp.last_column;
229 return DM_COLUMN_NOT_FOUND;
233 Set the appropriate value in the design matrix,
234 whether that value is from a categorical or numeric
235 variable. For a categorical variable, only the usual
236 binary encoding is allowed.
239 design_matrix_set_categorical (struct design_matrix *dm, size_t row,
240 const struct variable *var,
241 const union value *val)
249 assert (var->type == ALPHA);
250 fc = design_matrix_var_to_column (dm, var);
251 lc = dm_var_to_last_column (dm, var);
252 assert (lc != DM_COLUMN_NOT_FOUND);
253 assert (fc != DM_COLUMN_NOT_FOUND);
254 is_one = fc + cat_value_find (var, val);
255 for (col = fc; col <= lc; col++)
257 entry = (col == is_one) ? 1.0 : 0.0;
258 gsl_matrix_set (dm->m, row, col, entry);
262 design_matrix_set_numeric (struct design_matrix *dm, size_t row,
263 const struct variable *var, const union value *val)
267 assert (var->type == NUMERIC);
268 col = design_matrix_var_to_column ((const struct design_matrix *) dm, var);
269 assert (col != DM_COLUMN_NOT_FOUND);
270 gsl_matrix_set (dm->m, row, col, val->f);