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.
25 #include "design-matrix.h"
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.h>
34 #include <data/variable.h>
35 #include <data/category.h>
37 #include <gsl/gsl_machine.h>
38 #include <gsl/gsl_vector.h>
39 #include <gsl/gsl_matrix.h>
41 #define DM_COLUMN_NOT_FOUND -1
42 #define DM_INDEX_NOT_FOUND -3
45 Which element of a vector is equal to the value x?
48 cat_which_element_eq (const gsl_vector * vec, double x)
52 for (i = 0; i < vec->size; i++)
54 if (fabs (gsl_vector_get (vec, i) - x) < GSL_DBL_EPSILON)
59 return CAT_VALUE_NOT_FOUND;
62 cat_is_zero_vector (const gsl_vector * vec)
66 for (i = 0; i < vec->size; i++)
68 if (gsl_vector_get (vec, i) != 0.0)
77 Return the value of v corresponding to the vector vec.
80 cat_vector_to_value (const gsl_vector * vec, struct variable *v)
84 i = cat_which_element_eq (vec, 1.0);
85 if (i != CAT_VALUE_NOT_FOUND)
87 return cat_subscript_to_value (i + 1, v);
89 if (cat_is_zero_vector (vec))
91 return cat_subscript_to_value (0, v);
96 struct design_matrix *
97 design_matrix_create (int n_variables,
98 const struct variable *v_variables[],
101 struct design_matrix *dm;
102 const struct variable *v;
107 dm = xmalloc (sizeof *dm);
108 dm->vars = xnmalloc (n_variables, sizeof *dm->vars);
109 dm->n_vars = n_variables;
111 for (i = 0; i < n_variables; i++)
114 assert ((dm->vars + i) != NULL);
115 (dm->vars + i)->v = v; /* Allows us to look up the variable from
116 the design matrix. */
117 (dm->vars + i)->first_column = n_cols;
118 if (var_is_numeric (v))
120 (dm->vars + i)->last_column = n_cols;
123 else if (var_is_alpha (v))
125 assert (v->obs_vals != NULL);
126 (dm->vars + i)->last_column =
127 (dm->vars + i)->first_column + v->obs_vals->n_categories - 2;
128 n_cols += v->obs_vals->n_categories - 1;
131 dm->m = gsl_matrix_calloc (n_data, n_cols);
138 design_matrix_destroy (struct design_matrix *dm)
141 gsl_matrix_free (dm->m);
146 Return the index of the variable for the
150 design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
153 struct design_matrix_var v;
155 for (i = 0; i < dm->n_vars; i++)
158 if (v.first_column <= col && col <= v.last_column)
159 return (struct variable *) v.v;
165 Return the number of the first column which holds the
166 values for variable v.
169 design_matrix_var_to_column (const struct design_matrix * dm,
170 const struct variable * v)
173 struct design_matrix_var tmp;
175 for (i = 0; i < dm->n_vars; i++)
180 return tmp.first_column;
183 return DM_COLUMN_NOT_FOUND;
188 dm_var_to_last_column (const struct design_matrix *dm,
189 const struct variable *v)
192 struct design_matrix_var tmp;
194 for (i = 0; i < dm->n_vars; i++)
199 return tmp.last_column;
202 return DM_COLUMN_NOT_FOUND;
206 Set the appropriate value in the design matrix,
207 whether that value is from a categorical or numeric
208 variable. For a categorical variable, only the usual
209 binary encoding is allowed.
212 design_matrix_set_categorical (struct design_matrix *dm, size_t row,
213 const struct variable *var,
214 const union value *val)
222 assert (var_is_alpha (var));
223 fc = design_matrix_var_to_column (dm, var);
224 lc = dm_var_to_last_column (dm, var);
225 assert (lc != DM_COLUMN_NOT_FOUND);
226 assert (fc != DM_COLUMN_NOT_FOUND);
227 is_one = fc + cat_value_find (var, val);
228 for (col = fc; col <= lc; col++)
230 entry = (col == is_one) ? 1.0 : 0.0;
231 gsl_matrix_set (dm->m, row, col, entry);
235 design_matrix_set_numeric (struct design_matrix *dm, size_t row,
236 const struct variable *var, const union value *val)
240 assert (var_is_numeric (var));
241 col = design_matrix_var_to_column ((const struct design_matrix *) dm, var);
242 assert (col != DM_COLUMN_NOT_FOUND);
243 gsl_matrix_set (dm->m, row, col, val->f);