1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
18 An interaction is a gsl_vector containing a "product" of other
19 variables. The variables can be either categorical or numeric.
20 If the variables are all numeric, the interaction is just the
21 scalar product. If any of the variables are categorical, their
22 product is a vector containing 0's in all but one entry. This entry
23 is found by combining the vectors corresponding to the variables'
24 OBS_VALS member. If there are K categorical variables, each with
25 N_1, N_2, ..., N_K categories, then the interaction will have
26 N_1 * N_2 * N_3 *...* N_K - 1 entries.
28 When using these functions, make sure the orders of variables and
29 values match when appropriate.
34 #include <gsl/gsl_math.h>
35 #include <gsl/gsl_vector.h>
36 #include <data/value.h>
37 #include <data/variable.h>
38 #include <math/interaction.h>
42 struct interaction_variable
45 const struct variable **members;
46 struct variable *intr;
50 struct interaction_value
52 const struct interaction_variable *intr;
53 union value *val; /* Concatenation of the string values in this
54 interaction's value, or the product of a bunch
55 of numeric values for a purely numeric
58 double f; /* Product of the numerical values in this interaction's value. */
62 An interaction_variable has type alpha if any of members have type
63 alpha. Otherwise, its type is numeric.
65 struct interaction_variable *
66 interaction_variable_create (const struct variable **vars, int n_vars)
68 struct interaction_variable *result = NULL;
73 result = xmalloc (sizeof (*result));
75 result->members = xnmalloc (n_vars, sizeof (*result->members));
76 result->n_vars = n_vars;
77 for (i = 0; i < n_vars; i++)
79 result->members[i] = vars[i];
80 if (var_is_alpha (vars[i]))
86 result->intr = var_create_internal (0);
90 void interaction_variable_destroy (struct interaction_variable *iv)
92 var_destroy (iv->intr);
98 Get one of the member variables.
100 const struct variable *
101 interaction_variable_get_member (const struct interaction_variable *iv, size_t i)
103 return iv->members[i];
107 interaction_get_n_vars (const struct interaction_variable *iv)
109 return (iv == NULL) ? 0 : iv->n_vars;
113 interaction_get_n_alpha (const struct interaction_variable *iv)
119 interaction_get_n_numeric (const struct interaction_variable *iv)
121 return (interaction_get_n_vars (iv) - interaction_get_n_alpha (iv));
125 Get the interaction varibale itself.
127 const struct variable *
128 interaction_variable_get_var (const struct interaction_variable *iv)
133 Given list of values, compute the value of the corresponding
134 interaction. This "value" is not stored as the typical vector of
135 0's and one double, but rather the string values are concatenated to
136 make one big string value, and the numerical values are multiplied
137 together to give the non-zero entry of the corresponding vector.
139 struct interaction_value *
140 interaction_value_create (const struct interaction_variable *var, const union value **vals)
142 struct interaction_value *result = NULL;
143 const struct variable *member;
149 result = xmalloc (sizeof (*result));
151 n_vars = interaction_get_n_vars (var);
152 result->val = value_create (n_vars * MAX_SHORT_STRING + 1);
154 for (i = 0; i < n_vars; i++)
156 member = interaction_variable_get_member (var, i);
158 if (var_is_value_missing (member, vals[i], MV_ANY))
160 value_set_missing (result->val, MAX_SHORT_STRING);
166 if (var_is_alpha (var->members[i]))
168 strncat (result->val->s, vals[i]->s, MAX_SHORT_STRING);
170 else if (var_is_numeric (var->members[i]))
172 result->f *= vals[i]->f;
176 if (interaction_get_n_alpha (var) == 0)
179 If there are no categorical variables, then the
180 interaction consists of only numeric data. In this case,
181 code that uses this interaction_value will see the union
182 member as the numeric value. If we were to store that
183 numeric value in result->f as well, the calling code may
184 inadvertently square this value by multiplying by
185 result->val->f. Such multiplication would be correct for an
186 interaction consisting of both categorical and numeric
187 data, but a mistake for purely numerical interactions. To
188 avoid the error, we set result->f to 1.0 for numeric
191 result->val->f = result->f;
199 interaction_value_get (const struct interaction_value *val)
205 Returns the numeric value of the non-zero entry for the vector
206 corresponding to this interaction. Do not use this function to get
207 the numeric value of a purley numeric interaction. Instead, use the
208 union value * returned by interaction_value_get.
211 interaction_value_get_nonzero_entry (const struct interaction_value *val)
219 interaction_value_destroy (struct interaction_value *val)
229 Return a value from a variable that is an interaction.
231 struct interaction_value *
232 interaction_case_data (const struct ccase *ccase, const struct variable *var,
233 const struct interaction_variable **intr_vars, size_t n_intr)
237 const struct interaction_variable *iv;
238 const struct variable *intr;
239 const struct variable *member;
240 const union value **vals = NULL;
242 for (i = 0; i < n_intr; i++)
245 intr = interaction_variable_get_var (iv);
246 if (var_get_dict_index (intr) == var_get_dict_index (var))
251 n_vars = interaction_get_n_vars (iv);
252 vals = xnmalloc (n_vars, sizeof (*vals));
253 for (i = 0; i < n_vars; i++)
255 member = interaction_variable_get_member (iv, i);
256 vals[i] = case_data (ccase, member);
258 return interaction_value_create (iv, vals);
262 is_interaction (const struct variable *var, const struct interaction_variable **iv, size_t n_intr)
265 const struct variable *intr;
267 for (i = 0; i < n_intr; i++)
269 intr = interaction_variable_get_var (iv[i]);
270 if (var_get_dict_index (intr) == var_get_dict_index (var))