18 |
02110-1301, USA. */ |
02110-1301, USA. */ |
19 |
|
|
20 |
/* |
/* |
21 |
Functions and data structures to recode categorical variables into |
Functions and data structures to store values of a categorical |
22 |
vectors and sub-rows of matrices. |
variable, and to recode those values into binary vectors. |
23 |
|
|
24 |
For some statistical models, it is necessary to change each value |
For some statistical models, it is necessary to change each value |
25 |
of a categorical variable to a vector with binary entries. These |
of a categorical variable to a vector with binary entries. These |
26 |
vectors are then stored as sub-rows within a matrix during |
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 |
model-fitting. For example, we need functions and data strucutres to map a |
28 |
value, say 'a', of a variable named 'cat_var', to a vector, say (0 |
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 |
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 |
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 |
matrix, we need to know which sub-row corresponds to the variable |
32 |
'cat_var'. |
'cat_var'. |
|
|
|
|
The data structures defined here will be placed in the variable |
|
|
structure in the future. When that happens, the useful code |
|
|
in this file will be that which refers to design matrices. |
|
33 |
*/ |
*/ |
34 |
#include <config.h> |
#include <config.h> |
35 |
#include <stdlib.h> |
#include <stdlib.h> |
72 |
} |
} |
73 |
} |
} |
74 |
|
|
|
#if 0 |
|
|
struct recoded_categorical_array * |
|
|
cr_recoded_cat_ar_create (int n_variables, struct variable *v_variables[]) |
|
|
{ |
|
|
size_t n_categoricals = 0; |
|
|
size_t i; |
|
|
struct recoded_categorical_array *ca; |
|
|
struct variable *v; |
|
|
|
|
|
ca = xmalloc (sizeof *ca); |
|
|
for (i = 0; i < n_variables; i++) |
|
|
{ |
|
|
v = v_variables[i]; |
|
|
if (v->type == ALPHA) |
|
|
{ |
|
|
n_categoricals++; |
|
|
} |
|
|
} |
|
|
ca->n_vars = n_categoricals; |
|
|
ca->a = xnmalloc (n_categoricals, sizeof *ca->a); |
|
|
for (i = 0; i < n_categoricals; i++) |
|
|
{ |
|
|
*(ca->a + i) = cr_recoded_categorical_create (v_variables[i]); |
|
|
} |
|
|
|
|
|
return ca; |
|
|
} |
|
|
|
|
|
int |
|
|
cr_free_recoded_array (struct recoded_categorical_array *r) |
|
|
{ |
|
|
int rc = 0; |
|
|
size_t i; |
|
|
|
|
|
for (i = 0; i < r->n_vars; i++) |
|
|
{ |
|
|
cr_recoded_categorical_destroy (*(r->a + i)); |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
#endif |
|
75 |
static size_t |
static size_t |
76 |
cat_value_find (const struct variable *v, const union value *val) |
cat_value_find (const struct variable *v, const union value *val) |
77 |
{ |
{ |
122 |
} |
} |
123 |
|
|
124 |
/* |
/* |
|
Create a gsl_matrix, whose rows correspond to values of a |
|
|
categorical variable. Since n categories have n-1 degrees of |
|
|
freedom, the gsl_matrix is n-by-(n-1), with the first category |
|
|
encoded as the zero vector. |
|
|
*/ |
|
|
#if 0 |
|
|
void |
|
|
cat_create_value_matrix (struct variable *v) |
|
|
{ |
|
|
size_t i; |
|
|
size_t row; |
|
|
size_t col; |
|
|
size_t n_rows; |
|
|
size_t n_cols; |
|
|
|
|
|
assert (v != NULL); |
|
|
if (v->type == ALPHA) |
|
|
{ |
|
|
assert (v->rc != NULL); |
|
|
n_rows = v->rc->n_categories; |
|
|
n_cols = v->rc->n_categories - 1; |
|
|
v->rc->m = gsl_matrix_calloc (n_rows, n_cols); |
|
|
for (row = 1; row < n_rows; row++) |
|
|
{ |
|
|
col = row - 1; |
|
|
gsl_matrix_set (v->rc->m, row, col, 1.0); |
|
|
} |
|
|
} |
|
|
} |
|
|
#endif |
|
|
/* |
|
125 |
Return the subscript of the binary vector corresponding |
Return the subscript of the binary vector corresponding |
126 |
to this value. |
to this value. |
127 |
*/ |
*/ |
164 |
} |
} |
165 |
} |
} |
166 |
|
|
|
#if 0 |
|
|
/* |
|
|
Return the row of the matrix corresponding |
|
|
to the value v. |
|
|
*/ |
|
|
static gsl_vector_view |
|
|
cr_value_to_vector (const union value *v, struct recoded_categorical *cr) |
|
|
{ |
|
|
size_t row; |
|
|
row = cr_value_to_subscript (v, cr); |
|
|
return gsl_matrix_row (cr->m, row); |
|
|
} |
|
|
#endif |
|
167 |
/* |
/* |
168 |
Which element of a vector is equal to the value x? |
Which element of a vector is equal to the value x? |
169 |
*/ |
*/ |
197 |
} |
} |
198 |
|
|
199 |
/* |
/* |
200 |
Return the value corresponding to the vector. |
Return the value of v corresponding to the vector vec. |
|
To avoid searching the matrix, this routine takes |
|
|
advantage of the fact that element (i,i+1) is 1 |
|
|
when i is between 1 and cr->n_categories - 1 and |
|
|
i is 0 otherwise. |
|
201 |
*/ |
*/ |
202 |
union value * |
union value * |
203 |
cr_vector_to_value (const gsl_vector * vec, struct variable *v) |
cat_vector_to_value (const gsl_vector * vec, struct variable *v) |
204 |
{ |
{ |
205 |
size_t i; |
size_t i; |
206 |
|
|
216 |
return NULL; |
return NULL; |
217 |
} |
} |
218 |
|
|
|
#if 0 |
|
|
/* |
|
|
Given a variable, return a pointer to its recoded |
|
|
structure. BUSTED IN HERE. |
|
|
*/ |
|
|
struct recoded_categorical * |
|
|
cr_var_to_recoded_categorical (const struct variable *v, |
|
|
struct recoded_categorical_array *ca) |
|
|
{ |
|
|
struct recoded_categorical *rc; |
|
|
size_t i; |
|
|
|
|
|
for (i = 0; i < ca->n_vars; i++) |
|
|
{ |
|
|
rc = *(ca->a + i); |
|
|
if (rc->v->index == v->index) |
|
|
{ |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
#endif |
|
219 |
struct design_matrix * |
struct design_matrix * |
220 |
design_matrix_create (int n_variables, |
design_matrix_create (int n_variables, |
221 |
const struct variable *v_variables[], |
const struct variable *v_variables[], |