1 /* PSPP - binary encodings for categorical variables.
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 Functions and data structures to store values of a categorical
22 variable, and to recode those values into binary vectors.
24 For some statistical models, it is necessary to change each value
25 of a categorical variable to a vector with binary entries. These
26 vectors are then stored as sub-rows within a matrix during
27 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
29 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
31 matrix, we need to know which sub-row corresponds to the variable
42 #include <libpspp/alloc.h>
43 #include <libpspp/message.h>
48 #define CAT_VALUE_NOT_FOUND -2
50 #define N_INITIAL_CATEGORIES 1
53 This structure contains the observed values of a
60 size_t n_allocated_categories; /* This is used only during
61 initialization to keep
62 track of the number of
68 cat_stored_values_create (const struct variable *v)
70 if (!var_has_obs_vals (v))
72 struct cat_vals *obs_vals = xmalloc (sizeof *obs_vals);
74 obs_vals->n_categories = 0;
75 obs_vals->n_allocated_categories = N_INITIAL_CATEGORIES;
76 obs_vals->vals = xnmalloc (N_INITIAL_CATEGORIES, sizeof *obs_vals->vals);
77 var_set_obs_vals (v, obs_vals);
82 cat_stored_values_destroy (struct cat_vals *obs_vals)
86 if (obs_vals->n_allocated_categories > 0)
87 free (obs_vals->vals);
93 Which subscript corresponds to val?
96 cat_value_find (const struct variable *v, const union value *val)
98 struct cat_vals *obs_vals = var_get_obs_vals (v);
100 const union value *candidate;
102 for (i = 0; i < obs_vals->n_categories; i++)
104 candidate = obs_vals->vals + i;
105 assert (candidate != NULL);
106 if (!compare_values (candidate, val, var_get_width (v)))
111 return CAT_VALUE_NOT_FOUND;
115 Add the new value unless it is already present.
118 cat_value_update (const struct variable *v, const union value *val)
120 if (var_is_alpha (v))
122 struct cat_vals *cv = var_get_obs_vals (v);
123 if (cat_value_find (v, val) == CAT_VALUE_NOT_FOUND)
125 if (cv->n_categories >= cv->n_allocated_categories)
127 cv->n_allocated_categories *= 2;
128 cv->vals = xnrealloc (cv->vals,
129 cv->n_allocated_categories,
132 cv->vals[cv->n_categories] = *val;
139 cat_subscript_to_value (const size_t s, const struct variable *v)
141 struct cat_vals *obs_vals = var_get_obs_vals (v);
142 return s < obs_vals->n_categories ? obs_vals->vals + s : NULL;
146 Return the number of categories of a categorical variable.
149 cat_get_n_categories (const struct variable *v)
151 return var_get_obs_vals (v)->n_categories;