From 956933cf2545aa67692fd72ef8e4b3e00e524281 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sat, 14 Aug 2010 11:16:21 +0200 Subject: [PATCH] Covariance matrix interface change. covariance_2pass_create now takes a pointer to a struct categoricals which must be created by the caller, instead of creating this object for itself. This will (hopefully) make the implementation of interactions and other features easier. --- src/language/stats/correlations.c | 2 +- src/language/stats/glm.q | 9 ++++++++- src/language/stats/oneway.c | 6 +++++- src/math/covariance.c | 17 +++++++++++------ src/math/covariance.h | 3 ++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/language/stats/correlations.c b/src/language/stats/correlations.c index ea7d6909bb..6af8ba32ff 100644 --- a/src/language/stats/correlations.c +++ b/src/language/stats/correlations.c @@ -281,7 +281,7 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr const gsl_matrix *cov_matrix; gsl_matrix *corr_matrix; struct covariance *cov = covariance_2pass_create (corr->n_vars_total, corr->vars, - 0, NULL, + NULL, opts->wv, opts->exclude); struct casereader *rc = casereader_clone (r); diff --git a/src/language/stats/glm.q b/src/language/stats/glm.q index 5f20a2e3f0..13b097f3b8 100644 --- a/src/language/stats/glm.q +++ b/src/language/stats/glm.q @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -358,7 +359,13 @@ run_glm (struct casereader *input, k++; } - cov = covariance_2pass_create (n_numerics, numerics, n_categoricals, categoricals, NULL, MV_NEVER); + struct categoricals *cats = categoricals_create (categoricals, + n_categoricals, + NULL, MV_NEVER); + + cov = covariance_2pass_create (n_numerics, numerics, + cats, + NULL, MV_NEVER); reader = casereader_clone (input); reader = casereader_create_filter_missing (reader, numerics, n_numerics, diff --git a/src/language/stats/oneway.c b/src/language/stats/oneway.c index c9ae255ad7..da4669bcd8 100644 --- a/src/language/stats/oneway.c +++ b/src/language/stats/oneway.c @@ -325,10 +325,14 @@ run_oneway (const struct oneway_spec *cmd, ws.vws = xmalloc (cmd->n_vars * sizeof (*ws.vws)); + for (v = 0; v < cmd->n_vars; ++v) { + struct categoricals *cats = categoricals_create (&cmd->indep_var, 1, + cmd->wv, cmd->exclude); + ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v], - 1, &cmd->indep_var, + cats, cmd->wv, cmd->exclude); ws.vws[v].cc = 0; } diff --git a/src/math/covariance.c b/src/math/covariance.c index f0161a54c5..aa7f41771c 100644 --- a/src/math/covariance.c +++ b/src/math/covariance.c @@ -171,7 +171,7 @@ covariance_1pass_create (size_t n_vars, const struct variable *const *vars, */ struct covariance * covariance_2pass_create (size_t n_vars, const struct variable *const *vars, - size_t n_catvars, const struct variable *const *catvars, + struct categoricals *cats, const struct variable *wv, enum mv_class exclude) { size_t i; @@ -197,7 +197,7 @@ covariance_2pass_create (size_t n_vars, const struct variable *const *vars, cov->n_cm = -1; cov->cm = NULL; - cov->categoricals = categoricals_create (catvars, n_catvars, wv, exclude); + cov->categoricals = cats; return cov; } @@ -296,7 +296,8 @@ covariance_accumulate_pass1 (struct covariance *cov, const struct ccase *c) cov->state = 1; } - categoricals_update (cov->categoricals, c); + if (cov->categoricals) + categoricals_update (cov->categoricals, c); for (i = 0 ; i < cov->dim; ++i) { @@ -342,8 +343,11 @@ covariance_accumulate_pass2 (struct covariance *cov, const struct ccase *c) assert (cov->state == 1); cov->state = 2; - cov->dim = cov->n_vars + - categoricals_total (cov->categoricals) - categoricals_get_n_variables (cov->categoricals); + cov->dim = cov->n_vars; + + if (cov->categoricals) + cov->dim += categoricals_total (cov->categoricals) + - categoricals_get_n_variables (cov->categoricals); cov->n_cm = (cov->dim * (cov->dim - 1) ) / 2; cov->cm = xcalloc (sizeof *cov->cm, cov->n_cm); @@ -355,7 +359,8 @@ covariance_accumulate_pass2 (struct covariance *cov, const struct ccase *c) cov->moments[i] = resize_matrix (cov->moments[i], cov->dim); } - categoricals_done (cov->categoricals); + if (cov->categoricals) + categoricals_done (cov->categoricals); /* Populate the moments matrices with the categorical value elements */ for (i = cov->n_vars; i < cov->dim; ++i) diff --git a/src/math/covariance.h b/src/math/covariance.h index fb59535718..cb83e15140 100644 --- a/src/math/covariance.h +++ b/src/math/covariance.h @@ -26,13 +26,14 @@ struct covariance; struct variable; struct ccase ; +struct categoricals; struct covariance * covariance_1pass_create (size_t n_vars, const struct variable *const *vars, const struct variable *wv, enum mv_class excl); struct covariance * covariance_2pass_create (size_t n_vars, const struct variable *const *vars, - size_t n_catvars, const struct variable *const *catvars, + struct categoricals *cats, const struct variable *wv, enum mv_class excl); void covariance_accumulate (struct covariance *, const struct ccase *); -- 2.30.2