1 /* PSPP - binary encodings for categorical variables.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 Functions and data structures to store values of a categorical
21 variable, and to recode those values into binary vectors.
23 For some statistical models, it is necessary to change each value
24 of a categorical variable to a vector with binary entries. These
25 vectors are then stored as sub-rows within a matrix during
26 model-fitting. For example, we need functions and data strucutres to map a
27 value, say 'a', of a variable named 'cat_var', to a vector, say (0
28 1 0 0 0), and vice versa. We also need to be able to map the
29 vector back to the value 'a', and if the vector is a sub-row of a
30 matrix, we need to know which sub-row corresponds to the variable
41 #include <libpspp/alloc.h>
42 #include <libpspp/message.h>
47 #define CAT_VALUE_NOT_FOUND -2
49 #define N_INITIAL_CATEGORIES 1
52 This structure contains the observed values of a
59 size_t n_allocated_categories; /* This is used only during
60 initialization to keep
61 track of the number of
67 cat_stored_values_create (const struct variable *v)
69 if (!var_has_obs_vals (v))
71 struct cat_vals *obs_vals = xmalloc (sizeof *obs_vals);
73 obs_vals->n_categories = 0;
74 obs_vals->n_allocated_categories = N_INITIAL_CATEGORIES;
75 obs_vals->vals = xnmalloc (N_INITIAL_CATEGORIES, sizeof *obs_vals->vals);
76 var_set_obs_vals (v, obs_vals);
81 cat_stored_values_destroy (struct cat_vals *obs_vals)
85 if (obs_vals->n_allocated_categories > 0)
86 free (obs_vals->vals);
92 Which subscript corresponds to val?
95 cat_value_find (const struct variable *v, const union value *val)
97 struct cat_vals *obs_vals = var_get_obs_vals (v);
99 const union value *candidate;
101 for (i = 0; i < obs_vals->n_categories; i++)
103 candidate = obs_vals->vals + i;
104 assert (candidate != NULL);
105 if (!compare_values (candidate, val, var_get_width (v)))
110 return CAT_VALUE_NOT_FOUND;
114 Add the new value unless it is already present.
117 cat_value_update (const struct variable *v, const union value *val)
119 if (var_is_alpha (v))
121 struct cat_vals *cv = var_get_obs_vals (v);
122 if (cat_value_find (v, val) == CAT_VALUE_NOT_FOUND)
124 if (cv->n_categories >= cv->n_allocated_categories)
126 cv->n_allocated_categories *= 2;
127 cv->vals = xnrealloc (cv->vals,
128 cv->n_allocated_categories,
131 cv->vals[cv->n_categories] = *val;
138 cat_subscript_to_value (const size_t s, const struct variable *v)
140 struct cat_vals *obs_vals = var_get_obs_vals (v);
141 return s < obs_vals->n_categories ? obs_vals->vals + s : NULL;
145 Return the number of categories of a categorical variable.
148 cat_get_n_categories (const struct variable *v)
150 return var_get_obs_vals (v)->n_categories;