From: John Darrington Date: Tue, 5 Jul 2011 13:14:18 +0000 (+0200) Subject: GLM: Rewrite interactions module and update glm.c to use it in some places X-Git-Tag: v0.7.9~205 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3fa2fade9820dcb3c7ee3883839f8848d248559;p=pspp-builds.git GLM: Rewrite interactions module and update glm.c to use it in some places --- diff --git a/src/language/stats/glm.c b/src/language/stats/glm.c index fa418583..0c85965e 100644 --- a/src/language/stats/glm.c +++ b/src/language/stats/glm.c @@ -39,6 +39,7 @@ #include "linreg/sweep.h" #include "math/categoricals.h" #include "math/covariance.h" +#include "math/interaction.h" #include "math/moments.h" #include "output/tab.h" @@ -61,6 +62,9 @@ struct glm_spec size_t n_design_vars; const struct variable **design_vars; + size_t n_interactions; + const struct interaction **interactions; + enum mv_class exclude; /* The weight variable */ @@ -107,6 +111,8 @@ cmd_glm (struct lexer *lexer, struct dataset *ds) glm.n_dep_vars = 0; glm.n_factor_vars = 0; glm.n_design_vars = 0; + glm.n_interactions = 0; + glm.interactions = NULL; glm.dep_vars = NULL; glm.factor_vars = NULL; glm.design_vars = NULL; @@ -252,7 +258,7 @@ cmd_glm (struct lexer *lexer, struct dataset *ds) if (! parse_design_spec (lexer, &glm)) goto error; - if ( glm.n_design_vars == 0) + if ( glm.n_interactions == 0) { msg (ME, _("One or more design variables must be given")); goto error; @@ -287,7 +293,7 @@ cmd_glm (struct lexer *lexer, struct dataset *ds) const_var_set_destroy (factors); free (glm.factor_vars); - free (glm.design_vars); + free (glm.interactions); free (glm.dep_vars); @@ -297,7 +303,7 @@ error: const_var_set_destroy (factors); free (glm.factor_vars); - free (glm.design_vars); + free (glm.interactions); free (glm.dep_vars); return CMD_FAILURE; @@ -455,8 +461,8 @@ run_glm (struct glm_spec *cmd, struct casereader *input, reg_sweep (cm, 0); /* - Store the overall SSE. - */ + Store the overall SSE. + */ ws.ssq = gsl_vector_alloc (cm->size1); gsl_vector_set (ws.ssq, 0, gsl_matrix_get (cm, 0, 0)); get_ssq (cov, ws.ssq, cmd); @@ -493,7 +499,7 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) struct tab_table *t; const int nc = 6; - int nr = heading_rows + 4 + cmd->n_design_vars; + int nr = heading_rows + 4 + cmd->n_interactions; if (cmd->intercept) nr++; @@ -522,7 +528,7 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) if (cmd->intercept) df_corr += 1.0; - for (f = 0; f < cmd->n_design_vars; ++f) + for (f = 0; f < cmd->n_interactions; ++f) df_corr += categoricals_n_count (ws->cats, f) - 1.0; mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr); @@ -547,13 +553,15 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) r++; } - for (f = 0; f < cmd->n_design_vars; ++f) + for (f = 0; f < cmd->n_interactions; ++f) { + struct string str = DS_EMPTY_INITIALIZER; const double df = categoricals_n_count (ws->cats, f) - 1.0; const double ssq = gsl_vector_get (ws->ssq, f + 1); const double F = ssq / df / mse; - tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, - var_to_string (cmd->design_vars[f])); + interaction_to_string (cmd->interactions[f], &str); + tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str)); + ds_destroy (&str); tab_double (t, 1, r, 0, ssq, NULL); tab_double (t, 2, r, 0, df, wfmt); @@ -562,8 +570,6 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL); - - r++; } @@ -651,9 +657,11 @@ lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struc /* An interaction is a variable followed by {*, BY} followed by an interaction */ static bool -parse_design_interaction (struct lexer *lexer, struct glm_spec *glm) +parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact) { const struct variable *v = NULL; + assert (iact); + switch (lex_next_token (lexer, 1)) { case T_ENDCMD: @@ -669,12 +677,23 @@ parse_design_interaction (struct lexer *lexer, struct glm_spec *glm) } if (! lex_match_variable (lexer, glm, &v)) - return false; + { + interaction_destroy (*iact); + *iact = NULL; + return false; + } + + assert (v); + + if ( *iact == NULL) + *iact = interaction_create (v); + else + interaction_add_variable (*iact, v); if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY)) { lex_error (lexer, "Interactions are not yet implemented"); return false; - return parse_design_interaction (lexer, glm); + return parse_design_interaction (lexer, glm, iact); } glm->n_design_vars++; @@ -708,8 +727,14 @@ parse_nested_variable (struct lexer *lexer, struct glm_spec *glm) static bool parse_design_term (struct lexer *lexer, struct glm_spec *glm) { - if (parse_design_interaction (lexer, glm)) - return true; + struct interaction *iact = NULL; + if (parse_design_interaction (lexer, glm, &iact)) + { + /* Interaction parsing successful. Add to list of interactions */ + glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions); + glm->interactions[glm->n_interactions - 1] = iact; + return true; + } if ( parse_nested_variable (lexer, glm)) return true; diff --git a/src/math/interaction.c b/src/math/interaction.c index 87f4836d..c06acde8 100644 --- a/src/math/interaction.c +++ b/src/math/interaction.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,269 +14,134 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -/* - An interaction is a structure containing a "product" of other - variables. The variables can be either categorical or numeric. - If the variables are all numeric, the interaction is just the - scalar product. If any of the variables are categorical, their - product is a vector containing 0's in all but one entry. This entry - is found by combining the vectors corresponding to the variables' - OBS_VALS member. If there are K categorical variables, each with - N_1, N_2, ..., N_K categories, then the interaction will have - N_1 * N_2 * N_3 *...* N_K - 1 entries. - - When using these functions, make sure the orders of variables and - values match when appropriate. - */ - #include -#include "math/interaction.h" +#include "interaction.h" -#include -#include -#include - -#include "data/dictionary.h" #include "data/value.h" #include "data/variable.h" +#include "libpspp/str.h" #include "gl/xalloc.h" -struct interaction_variable -{ - int n_vars; - const struct variable **members; - struct variable *intr; - size_t n_alpha; -}; +#include -struct interaction_value -{ - const struct interaction_variable *intr; - union value val; /* Concatenation of the string values in this - interaction's value, or the product of a bunch - of numeric values for a purely numeric - interaction. - */ - double f; /* Product of the numerical values in this interaction's value. */ -}; /* - An interaction_variable has type alpha if any of members have type - alpha. Otherwise, its type is numeric. - */ -struct interaction_variable * -interaction_variable_create (const struct variable **vars, int n_vars) -{ - struct interaction_variable *result = NULL; - size_t i; + An interaction is a structure containing a "product" of other + variables. The variables can be either string or numeric. - if (n_vars > 0) - { - int width = 0; + Interaction is commutative. That means, that from a mathematical point of + view, the order of the variables is irrelevant. However, for display + purposes, and for matching with an interaction's value the order is + pertinent. + + Therefore, when using these functions, make sure the orders of variables + and values match when appropriate. +*/ - result = xmalloc (sizeof (*result)); - result->n_alpha = 0; - result->members = xnmalloc (n_vars, sizeof (*result->members)); - result->n_vars = n_vars; - for (i = 0; i < n_vars; i++) - { - result->members[i] = vars[i]; - if (var_is_alpha (vars[i])) - { - result->n_alpha++; - width += var_get_width (vars[i]); - } - } - result->intr = dict_create_internal_var (0, width); - } - return result; -} -void interaction_variable_destroy (struct interaction_variable *iv) -{ - dict_destroy_internal_var (iv->intr); - free (iv->members); - free (iv); -} -/* - Get one of the member variables. - */ -const struct variable * -interaction_get_member (const struct interaction_variable *iv, size_t i) +struct interaction * +interaction_create (const struct variable *v) { - return iv->members[i]; + struct interaction *i = xmalloc (sizeof *i); + i->vars = xmalloc (sizeof *i->vars); + i->vars[0] = v; + i->n_vars = 1; + return i; } -size_t -interaction_get_n_vars (const struct interaction_variable *iv) +void +interaction_destroy (struct interaction *i) { - return (iv == NULL) ? 0 : iv->n_vars; + free (i->vars); + free (i); } -size_t -interaction_get_n_alpha (const struct interaction_variable *iv) +void +interaction_add_variable (struct interaction *i, const struct variable *v) { - return iv->n_alpha; + i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars); + i->vars[i->n_vars - 1] = v; } -size_t -interaction_get_n_numeric (const struct interaction_variable *iv) -{ - return (interaction_get_n_vars (iv) - interaction_get_n_alpha (iv)); -} -/* - Get the interaction variable itself. - */ -const struct variable * -interaction_get_variable (const struct interaction_variable *iv) -{ - return iv->intr; -} -/* - Given list of values, compute the value of the corresponding - interaction. This "value" is not stored as the typical vector of - 0's and one double, but rather the string values are concatenated to - make one big string value, and the numerical values are multiplied - together to give the non-zero entry of the corresponding vector. - */ -struct interaction_value * -interaction_value_create (const struct interaction_variable *var, const union value **vals) +void +interaction_dump (const struct interaction *i) { - struct interaction_value *result = NULL; - - if (var != NULL) + int v = 0; + printf ("%s", var_get_name (i->vars[v])); + for (v = 1; v < i->n_vars; ++v) { - size_t i; - int val_width = var_get_width (interaction_get_variable (var)); - int offset = 0; - size_t n_vars = interaction_get_n_vars (var); - - result = xmalloc (sizeof (*result)); - result->intr = var; - - value_init (&result->val, val_width); - - result->f = 1.0; - for (i = 0; i < n_vars; i++) - { - const struct variable *member = interaction_get_member (var, i); - - if (var_is_value_missing (member, vals[i], MV_ANY)) - { - value_set_missing (&result->val, val_width); - result->f = SYSMIS; - break; - } - else - { - if (var_is_alpha (var->members[i])) - { - uint8_t *val = value_str_rw (&result->val, val_width); - int w = var_get_width (var->members[i]); - u8_cpy (val + offset, value_str (vals[i], w), w); - offset += w; - } - else if (var_is_numeric (var->members[i])) - { - result->f *= vals[i]->f; - } - } - } - if (interaction_get_n_alpha (var) == 0) - { - /* - If there are no categorical variables, then the - interaction consists of only numeric data. In this case, - code that uses this interaction_value will see the union - member as the numeric value. If we were to store that - numeric value in result->f as well, the calling code may - inadvertently square this value by multiplying by - result->val->f. Such multiplication would be correct for an - interaction consisting of both categorical and numeric - data, but a mistake for purely numerical interactions. To - avoid the error, we set result->f to 1.0 for numeric - interactions. - */ - result->val.f = result->f; - result->f = 1.0; - } + printf (" * %s", var_get_name (i->vars[v])); } - return result; + printf ("\n"); } -const union value * -interaction_value_get (const struct interaction_value *val) -{ - return &val->val; -} +/* Appends STR with a representation of the interaction, suitable for user + display. -/* - Returns the numeric value of the non-zero entry for the vector - corresponding to this interaction. Do not use this function to get - the numeric value of a purely numeric interaction. Instead, use the - union value * returned by interaction_value_get. - */ -double -interaction_value_get_nonzero_entry (const struct interaction_value *val) + STR must have been initialised prior to calling this function. +*/ +void +interaction_to_string (const struct interaction *iact, struct string *str) { - if (val != NULL) - return val->f; - return 1.0; + int v = 0; + ds_put_cstr (str, var_to_string (iact->vars[v])); + for (v = 1; v < iact->n_vars; ++v) + { + ds_put_cstr (str, " * "); + ds_put_cstr (str, var_to_string (iact->vars[v])); + } } -void -interaction_value_destroy (struct interaction_value *val) +unsigned int +interaction_value_hash (const struct interaction *iact, const union value *val) { - if (val != NULL) + int i; + size_t hash = 0; + for (i = 0; i < iact->n_vars; ++i) { - int val_width = var_get_width (interaction_get_variable (val->intr)); - - value_destroy (&val->val, val_width); - free (val); + hash = value_hash (&val[i], var_get_width (iact->vars[i]), hash); } + + return hash; } -/* - Return a value from a variable that is an interaction. - */ -struct interaction_value * -interaction_case_data (const struct ccase *ccase, const struct interaction_variable *iv) +bool +interaction_value_equal (const struct interaction *iact, const union value *val1, const union value *val2) { - size_t i; - size_t n_vars; - const struct variable *member; - const union value **vals = NULL; - - n_vars = interaction_get_n_vars (iv); - vals = xnmalloc (n_vars, sizeof (*vals)); + int i; + bool same = true; - for (i = 0; i < n_vars; i++) + for (i = 0; i < iact->n_vars; ++i) + { + if ( ! value_equal (&val1[i], &val2[i], var_get_width (iact->vars[i]))) { - member = interaction_get_member (iv, i); - vals[i] = case_data (ccase, member); + same = false; + break; } + } - return interaction_value_create (iv, vals); + return same; } + bool -is_interaction (const struct variable *var, const struct interaction_variable **iv, size_t n_intr) +interaction_value_is_missing (const struct interaction *iact, const union value *val, enum mv_class exclude) { - size_t i; - const struct variable *intr; - - for (i = 0; i < n_intr; i++) + int i; + bool missing = false; + + for (i = 0; i < iact->n_vars; ++i) { - intr = interaction_get_variable (iv[i]); - if (intr == var) + if ( var_is_value_missing (iact->vars[i], &val[i], exclude)) { - return true; + missing = true; + break; } } - return false; + + return missing; } - diff --git a/src/math/interaction.h b/src/math/interaction.h index 13b13690..bffe7518 100644 --- a/src/math/interaction.h +++ b/src/math/interaction.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2011 Free Software Foundation, Inc. + Copyright (C) 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,28 +14,37 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef INTERACTION_H -#define INTERACTION_H - -#include "data/case.h" - -struct interaction_variable; -struct interaction_value; - -struct interaction_variable * interaction_variable_create (const struct variable **, int); -void interaction_variable_destroy (struct interaction_variable *); -struct interaction_value * interaction_value_create (const struct interaction_variable *, const union value **); -void interaction_value_destroy (struct interaction_value *); -size_t interaction_variable_get_n_vars (const struct interaction_variable *); -double interaction_value_get_nonzero_entry (const struct interaction_value *); -const union value *interaction_value_get (const struct interaction_value *); -const struct variable * interaction_get_variable (const struct interaction_variable *); -size_t interaction_get_n_numeric (const struct interaction_variable *); -size_t interaction_get_n_alpha (const struct interaction_variable *); -size_t interaction_get_n_vars (const struct interaction_variable *); -const struct variable * interaction_get_member (const struct interaction_variable *, size_t); -bool is_interaction (const struct variable *, const struct interaction_variable **, size_t); -struct interaction_value * -interaction_case_data (const struct ccase *, const struct interaction_variable *); -double interaction_value_get_nonzero_entry (const struct interaction_value *); + +#ifndef _INTERACTION_H__ +#define _INTERACTION_H__ 1 + +#include +#include "data/missing-values.h" + +struct interaction; +struct variable; +struct string; + +#include +struct interaction +{ + size_t n_vars; + const struct variable **vars; +}; + + + +struct interaction * interaction_create (const struct variable *); +void interaction_destroy (struct interaction *); +void interaction_add_variable (struct interaction *, const struct variable *); +void interaction_dump (const struct interaction *); +void interaction_to_string (const struct interaction *iact, struct string *str); + + +union value; + +unsigned int interaction_value_hash (const struct interaction *, const union value *); +bool interaction_value_equal (const struct interaction *, const union value *, const union value *); +bool interaction_value_is_missing (const struct interaction *, const union value *, enum mv_class); + #endif