40 |
#define CAT_H 1 |
#define CAT_H 1 |
41 |
|
|
42 |
#include <gsl/gsl_matrix.h> |
#include <gsl/gsl_matrix.h> |
43 |
|
#include <stdbool.h> |
44 |
/* |
/* |
45 |
This structure contains the binary encoding of a |
This structure contains the observed values of a |
46 |
categorical variable. |
categorical variable. |
47 |
*/ |
*/ |
48 |
struct recoded_categorical |
struct cat_vals |
49 |
{ |
{ |
|
const struct variable *v; /* Original variable. */ |
|
50 |
union value *vals; |
union value *vals; |
|
gsl_matrix *m; /* Vector-encoded values of the |
|
|
original variable. The ith row of |
|
|
the matrix corresponds to the ith |
|
|
value of a categorical variable. |
|
|
*/ |
|
51 |
size_t n_categories; |
size_t n_categories; |
52 |
size_t first_column; /* First column of the gsl_matrix which |
size_t n_allocated_categories; /* This is used only during |
53 |
contains recoded values of the categorical |
initialization to keep |
54 |
variable. |
track of the number of |
55 |
*/ |
values stored. |
56 |
size_t last_column; /* Last column containing the recoded |
*/ |
|
categories. The practice of |
|
|
keeping only the first and last |
|
|
columns of the matrix implies those |
|
|
columns corresponding to v must be |
|
|
contiguous. |
|
|
*/ |
|
|
size_t n_allocated_categories; /* This is used only during |
|
|
initialization to keep |
|
|
track of the number of |
|
|
values stored. |
|
|
*/ |
|
57 |
}; |
}; |
58 |
|
|
59 |
/* |
/* |
76 |
|
|
77 |
struct design_matrix_var |
struct design_matrix_var |
78 |
{ |
{ |
79 |
int first_column; /* First column for this variable in the |
int first_column; /* First column for this variable in |
80 |
design_matix. If this variable is categorical, |
the design_matix. If this variable |
81 |
its values are stored in multiple, contiguous |
is categorical, its values are |
82 |
columns, as dictated by its vector encoding |
stored in multiple, contiguous |
83 |
in the variable's struct recoded_categorical. |
columns, as dictated by its vector |
84 |
|
encoding in the variable's struct |
85 |
|
recoded_categorical. |
86 |
*/ |
*/ |
87 |
int last_column; |
int last_column; |
88 |
const struct variable *v; |
const struct variable *v; |
90 |
struct design_matrix |
struct design_matrix |
91 |
{ |
{ |
92 |
gsl_matrix *m; |
gsl_matrix *m; |
93 |
struct design_matrix_var *vars; /* Element i is the the variable whose |
struct design_matrix_var *vars; /* Element i corresponds to |
94 |
values are stored in column i of m. If that |
the variable whose values |
95 |
variable is categorical with more than two |
are stored in at least one |
96 |
categories, its values are stored in multiple, |
column of m. If that |
97 |
contiguous columns. In this case, element i is |
variable is categorical |
98 |
the first column for that variable. The |
with more than two |
99 |
variable's values are then stored in the |
categories, its values are |
100 |
columns first_column through |
stored in multiple, |
101 |
last_column. first_column and last_column for |
contiguous columns. The |
102 |
a categorical variable are stored in the |
variable's values are then |
103 |
variable's recoded_categorical structure. |
stored in the columns |
104 |
|
first_column through |
105 |
|
last_column of the |
106 |
|
design_matrix_var |
107 |
|
structure. |
108 |
*/ |
*/ |
109 |
size_t n_vars; |
size_t n_vars; |
110 |
}; |
}; |
111 |
union value *cr_vector_to_value (const gsl_vector *, |
union value *cr_vector_to_value (const gsl_vector *, struct variable *); |
112 |
struct recoded_categorical *); |
|
113 |
|
void cat_stored_values_create (struct variable *); |
114 |
|
|
115 |
void cr_value_update (struct recoded_categorical *, const union value *); |
void cat_value_update (struct variable *, const union value *); |
116 |
|
|
117 |
int cr_free_recoded_array (struct recoded_categorical_array *); |
int cat_free_recoded_array (struct recoded_categorical_array *); |
118 |
|
|
119 |
struct recoded_categorical_array *cr_recoded_cat_ar_create (int, |
struct recoded_categorical_array *cr_recoded_cat_ar_create (int, |
120 |
struct variable |
struct variable |
121 |
*[]); |
*[]); |
122 |
|
|
123 |
struct recoded_categorical *cr_recoded_categorical_create (const struct |
void cat_recoded_categorical_create (struct variable *); |
|
variable *); |
|
124 |
|
|
125 |
void cr_create_value_matrices (struct recoded_categorical_array *); |
void cat_create_value_matrix (struct variable *); |
126 |
|
|
127 |
struct recoded_categorical *cr_var_to_recoded_categorical (const struct |
struct recoded_categorical *cat_var_to_recoded_categorical (const struct |
128 |
variable *, |
variable *, |
129 |
struct |
struct |
130 |
recoded_categorical_array |
recoded_categorical_array |
131 |
*); |
*); |
132 |
|
|
133 |
struct design_matrix *design_matrix_create (int, const struct variable *[], |
struct design_matrix *design_matrix_create (int, const struct variable *[], |
|
struct |
|
|
recoded_categorical_array *, |
|
134 |
const size_t); |
const size_t); |
135 |
|
|
136 |
void design_matrix_destroy (struct design_matrix *); |
void design_matrix_destroy (struct design_matrix *); |
137 |
|
|
138 |
void design_matrix_set_categorical (struct design_matrix *, size_t, |
void design_matrix_set_categorical (struct design_matrix *, size_t, |
139 |
const struct variable *, |
const struct variable *, |
140 |
const union value *, |
const union value *); |
|
struct recoded_categorical *); |
|
141 |
|
|
142 |
void design_matrix_set_numeric (struct design_matrix *, size_t, |
void design_matrix_set_numeric (struct design_matrix *, size_t, |
143 |
const struct variable *, const union value *); |
const struct variable *, const union value *); |
153 |
const struct variable *, const union value *, |
const struct variable *, const union value *, |
154 |
struct recoded_categorical *); |
struct recoded_categorical *); |
155 |
|
|
156 |
void cr_recoded_categorical_destroy (struct recoded_categorical *); |
void cat_stored_values_destroy (struct variable *); |
157 |
|
|
158 |
#endif |
#endif |