-struct design_matrix *
-design_matrix_create (int n_variables,
- const struct variable *v_variables[],
- struct recoded_categorical_array *ca,
- const size_t n_data)
-{
- struct design_matrix *dm;
- struct design_matrix_var *tmp;
- struct recoded_categorical *rc;
- const struct variable *v;
- size_t i;
- size_t n_cols = 0;
- size_t col;
-
- dm = xmalloc (sizeof (*dm));
- dm->vars = xmalloc (n_variables * sizeof (struct variable *));
- dm->n_vars = n_variables;
-
- for (i = 0; i < n_variables; i++)
- {
- v = v_variables[i];
- if (v->type == NUMERIC)
- {
- n_cols++;
- }
- else if (v->type == ALPHA)
- {
- assert (ca != NULL);
- rc = cr_var_to_recoded_categorical (v, ca);
- assert (rc != NULL);
- rc->first_column = n_cols;
- rc->last_column = rc->first_column + rc->n_categories - 2;
- n_cols += rc->n_categories - 1;
- }
- }
- dm->m = gsl_matrix_calloc (n_data, n_cols);
- dm->vars = xmalloc (dm->n_vars * sizeof (*(dm->vars)));
- assert (dm->vars != NULL);
- col = 0;
-
- for (i = 0; i < n_variables; i++)
- {
- v = v_variables[i];
- (dm->vars[i]).v = v;
- if (v->type == NUMERIC)
- {
- tmp = &(dm->vars[col]);
- tmp->v = v;
- tmp->first_column = col;
- col++;
- }
- else if (v->type == ALPHA)
- {
- assert (ca != NULL);
- rc = cr_var_to_recoded_categorical (v, ca);
- assert (rc != NULL);
- tmp = &(dm->vars[col]);
- tmp->v = v;
- tmp->last_column = rc->last_column;
- col = rc->last_column + 1;
- }
- }
- return dm;
-}
-
-void
-design_matrix_destroy (struct design_matrix *dm)
-{
- free (dm->vars);
- gsl_matrix_free (dm->m);
- free (dm);
-}
-
-/*
- Return the index of the variable for the
- given column.
- */
-static const size_t
-design_matrix_col_to_var_index (const struct design_matrix *dm, size_t col)
-{
- size_t i;
- struct design_matrix_var v;
-
- for (i = 0; i < dm->n_vars; i++)
- {
- v = dm->vars[i];
- if (v.first_column <= col && col <= v.last_column)
- return (v.v)->index;
- }
- return CR_INDEX_NOT_FOUND;
-}
-
-/*
- Return a pointer to the variable whose values
- are stored in column col.
- */
-const struct variable *
-design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
-{
- size_t index;
- size_t i;
- struct design_matrix_var dmv;
- const struct variable *v;
-
- index = design_matrix_col_to_var_index (dm, col);
- for (i = 0; i < dm->n_vars; i++)
- {
- dmv = dm->vars[i];
- v = (dmv.v)->index;
- if (v->index == index)
- {
- return v;
- }
- }
- return NULL;
-}
-
-static size_t
-cmp_dm_var_index (const struct design_matrix_var *dmv, size_t index)
-{
- if (dmv->v->index == index)
- return 1;
- return 0;
-}
-
-/*
- Return the number of the first column storing the
- values for variable v.
- */
-size_t
-design_matrix_var_to_column (const struct design_matrix * dm,
- const struct variable * v)
-{
- size_t i;
- struct design_matrix_var tmp;
-
- for (i = 0; i < dm->n_vars; i++)
- {
- tmp = dm->vars[i];
- if (cmp_dm_var_index (&tmp, v->index))
- {
- return tmp.first_column;
- }
- }
- return CR_COLUMN_NOT_FOUND;
-}
-
-/*
- Set the appropriate value in the design matrix,
- whether that value is from a categorical or numeric
- variable.
- */
-void
-design_matrix_set_categorical (struct design_matrix *dm, size_t row,
- const struct variable *var,
- const union value *val,
- struct recoded_categorical *rc)
-{
- size_t col;
- double x;
-
- assert (var->type == ALPHA);
- gsl_vector_const_view vec = cr_value_to_vector (val, rc);
-
- /*
- Copying values here is not the 'most efficient' way,
- but it will work even if we change the vector encoding later.
- */
- for (col = rc->first_column; col <= rc->last_column; col++)
- {
- x = gsl_vector_get (&vec.vector, col);
- gsl_matrix_set (dm->m, row, col, x);
- }
-}
-void
-design_matrix_set_numeric (struct design_matrix *dm, size_t row,
- const struct variable *var, const union value *val)
-{
- size_t col;
-
- assert (var->type == NUMERIC);
- col = design_matrix_var_to_column ((const struct design_matrix *) dm, var);
- assert (col != CR_COLUMN_NOT_FOUND);
- gsl_matrix_set (dm->m, row, col, val->f);
-}