1 /* PSPP - linear regression.
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 Functions and data structures to recode categorical variables into
22 vectors and sub-rows of matrices.
24 For some statistical models, it is necessary to change each value
25 of a categorical variable to a vector with binary entries. These
26 vectors are then stored as sub-rows within a matrix during
27 model-fitting. E.g., we need functions and data strucutres to map a
28 value, say 'a', of a variable named 'cat_var', to a vector, say (0
29 1 0 0 0), and vice versa. We also need to be able to map the
30 vector back to the value 'a', and if the vector is a sub-row of a
31 matrix, we need to know which sub-row corresponds to the variable
34 The data structures defined here will be placed in the variable
35 structure in the future. When that happens, the useful code
36 in this file will be that which refers to design matrices.
47 #include <gsl/gsl_machine.h>
48 #include <gsl/gsl_vector.h>
49 #include <gsl/gsl_matrix.h>
51 #define N_INITIAL_CATEGORIES 1
52 #define CR_COLUMN_NOT_FOUND -1
53 #define CR_VALUE_NOT_FOUND -2
54 #define CR_INDEX_NOT_FOUND -3
56 static gsl_vector_view cr_value_to_vector (const union value *,
57 struct recoded_categorical *);
59 struct recoded_categorical *
60 cr_recoded_categorical_create (const struct variable *v)
62 struct recoded_categorical *rc;
64 rc = xmalloc (sizeof (*rc));
67 rc->n_allocated_categories = N_INITIAL_CATEGORIES;
68 rc->vals = xmalloc (N_INITIAL_CATEGORIES *
75 cr_recoded_categorical_destroy (struct recoded_categorical *r)
81 struct recoded_categorical_array *
82 cr_recoded_cat_ar_create (int n_variables, struct variable *v_variables[])
84 size_t n_categoricals = 0;
86 struct recoded_categorical_array *ca;
89 ca = (struct recoded_categorical_array *) xmalloc (sizeof (*ca));
90 for (i = 0; i < n_variables; i++)
98 ca->n_vars = n_categoricals;
99 ca->a = xmalloc (n_categoricals * sizeof (*(ca->a)));
100 for (i = 0; i < n_categoricals; i++)
102 *(ca->a + i) = cr_recoded_categorical_create (v_variables[i]);
109 cr_free_recoded_array (struct recoded_categorical_array *r)
114 for (i = 0; i < r->n_vars; i++)
116 cr_recoded_categorical_destroy (*(r->a + i));
122 cr_value_find (struct recoded_categorical *rc, const union value *v)
125 const union value *val;
127 for (i = 0; i < rc->n_categories; i++)
130 if (!compare_values (val, v, rc->v->width))
135 return CR_VALUE_NOT_FOUND;
139 Add the new value unless it is already present.
142 cr_value_update (struct recoded_categorical *rc, const union value *v)
144 if (cr_value_find (rc, v) == CR_VALUE_NOT_FOUND)
146 if (rc->n_categories >= rc->n_allocated_categories)
148 rc->n_allocated_categories *= 2;
149 rc->vals = xrealloc (rc->vals, rc->n_allocated_categories
150 * sizeof (*(rc->vals)));
152 rc->vals[rc->n_categories] = *v;
158 Create a set of gsl_matrix's, each of whose rows correspond to
159 values of a categorical variable. Since n categories have n-1
160 degrees of freedom, the gsl_matrix is n-by-(n-1), with the first
161 category encoded as the zero vector.
164 cr_create_value_matrices (struct recoded_categorical_array *r)
172 for (i = 0; i < r->n_vars; i++)
174 n_rows = (*(r->a + i))->n_categories;
175 n_cols = (*(r->a + i))->n_categories - 1;
176 (*(r->a + i))->m = gsl_matrix_calloc (n_rows, n_cols);
177 for (row = 1; row < n_rows; row++)
180 gsl_matrix_set ((*(r->a + i))->m, row, col, 1.0);
186 cr_value_to_subscript (const union value *val, struct recoded_categorical *cr)
188 const union value *v;
192 subscript = cr->n_categories - 1;
193 while (subscript > 0)
195 v = cr->vals + subscript;
196 different = compare_values (val, v, cr->v->width);
207 cr_subscript_to_value (const size_t s, struct recoded_categorical *cr)
209 if (s < cr->n_categories)
211 return (cr->vals + s);
220 Return the row of the matrix corresponding
223 static gsl_vector_view
224 cr_value_to_vector (const union value * v, struct recoded_categorical * cr)
227 row = cr_value_to_subscript (v, cr);
228 return gsl_matrix_row (cr->m, row);
232 Which element of a vector is equal to the value x?
235 cr_which_element_eq (const gsl_vector * vec, double x)
239 for (i = 0; i < vec->size; i++)
241 if (fabs (gsl_vector_get (vec, i) - x) < GSL_DBL_EPSILON)
246 return CR_VALUE_NOT_FOUND;
249 cr_is_zero_vector (const gsl_vector * vec)
253 for (i = 0; i < vec->size; i++)
255 if (gsl_vector_get (vec, i) != 0.0)
264 Return the value corresponding to the vector.
265 To avoid searching the matrix, this routine takes
266 advantage of the fact that element (i,i+1) is 1
267 when i is between 1 and cr->n_categories - 1 and
271 cr_vector_to_value (const gsl_vector * vec, struct recoded_categorical *cr)
275 i = cr_which_element_eq (vec, 1.0);
276 if (i != CR_VALUE_NOT_FOUND)
278 return cr_subscript_to_value (i + 1, cr);
280 if (cr_is_zero_vector (vec))
282 return cr_subscript_to_value (0, cr);
288 Given a variable, return a pointer to its recoded
289 structure. BUSTED IN HERE.
291 struct recoded_categorical *
292 cr_var_to_recoded_categorical (const struct variable *v,
293 struct recoded_categorical_array *ca)
295 struct recoded_categorical *rc;
298 for (i = 0; i < ca->n_vars; i++)
301 if (rc->v->index == v->index)
309 struct design_matrix *
310 design_matrix_create (int n_variables,
311 const struct variable *v_variables[],
312 struct recoded_categorical_array *ca,
315 struct design_matrix *dm;
316 struct design_matrix_var *tmp;
317 struct recoded_categorical *rc;
318 const struct variable *v;
323 dm = xmalloc (sizeof (*dm));
324 dm->vars = xmalloc (n_variables * sizeof (struct variable *));
325 dm->n_vars = n_variables;
327 for (i = 0; i < n_variables; i++)
330 if (v->type == NUMERIC)
334 else if (v->type == ALPHA)
337 rc = cr_var_to_recoded_categorical (v, ca);
339 rc->first_column = n_cols;
340 rc->last_column = rc->first_column + rc->n_categories - 2;
341 n_cols += rc->n_categories - 1;
344 dm->m = gsl_matrix_calloc (n_data, n_cols);
345 dm->vars = xmalloc (dm->n_vars * sizeof (*(dm->vars)));
346 assert (dm->vars != NULL);
349 for (i = 0; i < n_variables; i++)
353 if (v->type == NUMERIC)
355 tmp = &(dm->vars[col]);
357 tmp->first_column = col;
360 else if (v->type == ALPHA)
363 rc = cr_var_to_recoded_categorical (v, ca);
365 tmp = &(dm->vars[col]);
367 tmp->last_column = rc->last_column;
368 col = rc->last_column + 1;
375 design_matrix_destroy (struct design_matrix *dm)
378 gsl_matrix_free (dm->m);
383 Return the index of the variable for the
387 design_matrix_col_to_var_index (const struct design_matrix *dm, size_t col)
390 struct design_matrix_var v;
392 for (i = 0; i < dm->n_vars; i++)
395 if (v.first_column <= col && col <= v.last_column)
398 return CR_INDEX_NOT_FOUND;
402 Return a pointer to the variable whose values
403 are stored in column col.
406 design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
410 struct design_matrix_var dmv;
412 index = design_matrix_col_to_var_index (dm, col);
413 for (i = 0; i < dm->n_vars; i++)
416 if ((dmv.v)->index == index)
425 cmp_dm_var_index (const struct design_matrix_var *dmv, size_t index)
427 if (dmv->v->index == index)
433 Return the number of the first column storing the
434 values for variable v.
437 design_matrix_var_to_column (const struct design_matrix * dm,
438 const struct variable * v)
441 struct design_matrix_var tmp;
443 for (i = 0; i < dm->n_vars; i++)
446 if (cmp_dm_var_index (&tmp, v->index))
448 return tmp.first_column;
451 return CR_COLUMN_NOT_FOUND;
455 Set the appropriate value in the design matrix,
456 whether that value is from a categorical or numeric
460 design_matrix_set_categorical (struct design_matrix *dm, size_t row,
461 const struct variable *var,
462 const union value *val,
463 struct recoded_categorical *rc)
468 assert (var->type == ALPHA);
469 const gsl_vector_view vec = cr_value_to_vector (val, rc);
472 Copying values here is not the 'most efficient' way,
473 but it will work even if we change the vector encoding later.
475 for (col = rc->first_column; col <= rc->last_column; col++)
477 x = gsl_vector_get (&vec.vector, col);
478 gsl_matrix_set (dm->m, row, col, x);
482 design_matrix_set_numeric (struct design_matrix *dm, size_t row,
483 const struct variable *var, const union value *val)
487 assert (var->type == NUMERIC);
488 col = design_matrix_var_to_column ((const struct design_matrix *) dm, var);
489 assert (col != CR_COLUMN_NOT_FOUND);
490 gsl_matrix_set (dm->m, row, col, val->f);