From 3025fc73778a733bfd2aeefd57da1b1c1955a675 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Mon, 23 Aug 2010 16:45:27 +0200 Subject: [PATCH] Categoricals: Use moments instead of keeping cc count ourselves --- src/language/stats/oneway.c | 49 +++++++++++++++++++++++-------------- src/math/categoricals.c | 10 +++----- src/math/categoricals.h | 1 + 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/language/stats/oneway.c b/src/language/stats/oneway.c index e88a9542ec..69570bbc6b 100644 --- a/src/language/stats/oneway.c +++ b/src/language/stats/oneway.c @@ -102,6 +102,15 @@ struct oneway_spec }; +/* Per category data */ +struct descriptive_data +{ + const struct variable *var; + struct moments1 *mom; + + double minimum; + double maximum; +}; /* Workspace variable for each dependent variable */ struct per_var_ws @@ -114,7 +123,6 @@ struct per_var_ws int n_groups; - double cc; double mse; }; @@ -314,15 +322,6 @@ free_double (void *value_, const void *aux UNUSED) static void postcalc (const struct oneway_spec *cmd); static void precalc (const struct oneway_spec *cmd); -struct descriptive_data -{ - const struct variable *var; - struct moments1 *mom; - - double minimum; - double maximum; -}; - static struct descriptive_data * dd_create (const struct variable *var) { @@ -348,7 +347,9 @@ makeit (void *aux1, void *aux2 UNUSED) } static void -updateit (void *user_data, const struct variable *wv, +updateit (void *user_data, + enum mv_class exclude, + const struct variable *wv, const struct variable *catvar UNUSED, const struct ccase *c, void *aux1, void *aux2) @@ -359,6 +360,9 @@ updateit (void *user_data, const struct variable *wv, const union value *valx = case_data (c, varp); + if ( var_is_value_missing (varp, valx, exclude)) + return; + struct descriptive_data *dd_total = aux2; double weight = 1.0; @@ -419,7 +423,6 @@ run_oneway (const struct oneway_spec *cmd, ws.vws[v].cov = covariance_2pass_create (1, &cmd->vars[v], cats, cmd->wv, cmd->exclude); - ws.vws[v].cc = 0; } c = casereader_peek (input, 0); @@ -469,7 +472,6 @@ run_oneway (const struct oneway_spec *cmd, { struct per_var_ws *pvw = &ws.vws[i]; - pvw->cc += weight; covariance_accumulate_pass1 (pvw->cov, c); } @@ -546,8 +548,13 @@ run_oneway (const struct oneway_spec *cmd, gsl_matrix *cm = covariance_calculate_unnormalized (pvw->cov); const struct categoricals *cats = covariance_get_categoricals (pvw->cov); + double n; + moments1_calculate (ws.dd_total[v]->mom, &n, NULL, NULL, NULL, NULL); + pvw->sst = gsl_matrix_get (cm, 0, 0); + // gsl_matrix_fprintf (stdout, cm, "%g "); + reg_sweep (cm, 0); pvw->sse = gsl_matrix_get (cm, 0, 0); @@ -556,7 +563,7 @@ run_oneway (const struct oneway_spec *cmd, pvw->n_groups = categoricals_total (cats); - pvw->mse = (pvw->sst - pvw->ssa) / (pvw->cc - pvw->n_groups); + pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups); } postcalc (cmd); @@ -693,14 +700,12 @@ output_oneway (const struct oneway_spec *cmd, struct oneway_workspace *ws) show_anova_table (cmd, ws); - if (ll_count (&cmd->contrast_list) > 0) { show_contrast_coeffs (cmd, ws); show_contrast_tests (cmd, ws); } - /* Clean up */ for (i = 0; i < cmd->n_vars; ++i ) { @@ -744,9 +749,12 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace * for (i = 0; i < cmd->n_vars; ++i) { + double n; + moments1_calculate (ws->dd_total[i]->mom, &n, NULL, NULL, NULL, NULL); + const struct per_var_ws *pvw = &ws->vws[i]; const double df1 = pvw->n_groups - 1; - const double df2 = pvw->cc - pvw->n_groups; + const double df2 = n - pvw->n_groups; const double msa = pvw->ssa / df1; const char *s = var_to_string (cmd->vars[i]); @@ -769,7 +777,7 @@ show_anova_table (const struct oneway_spec *cmd, const struct oneway_workspace * /* Degrees of freedom */ tab_fixed (t, 3, i * 3 + 1, 0, df1, 4, 0); tab_fixed (t, 3, i * 3 + 2, 0, df2, 4, 0); - tab_fixed (t, 3, i * 3 + 3, 0, pvw->cc - 1, 4, 0); + tab_fixed (t, 3, i * 3 + 3, 0, n - 1, 4, 0); /* Mean Squares */ tab_double (t, 4, i * 3 + 1, TAB_RIGHT, msa, NULL); @@ -985,6 +993,9 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace * for (v = 0; v < cmd->n_vars; ++v) { + double n; + moments1_calculate (ws->dd_total[v]->mom, &n, NULL, NULL, NULL, NULL); + const struct per_var_ws *pvw = &ws->vws[v]; const struct categoricals *cats = covariance_get_categoricals (pvw->cov); @@ -993,7 +1004,7 @@ show_homogeneity (const struct oneway_spec *cmd, const struct oneway_workspace * const char *s = var_to_string (var); const double df1 = pvw->n_groups - 1; - const double df2 = pvw->cc - pvw->n_groups; + const double df2 = n - pvw->n_groups; double F = gp->levene; tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s); diff --git a/src/math/categoricals.c b/src/math/categoricals.c index 1968b332f8..576e19b1d3 100644 --- a/src/math/categoricals.c +++ b/src/math/categoricals.c @@ -88,16 +88,12 @@ struct categoricals /* Missing values to be excluded */ enum mv_class exclude; - - /* Function to be called on each update */ update_func *update; - /* Function specified by the caller to create user_data */ user_data_create_func *user_data_create; - /* Auxilliary data to be passed to update and user_data_create_func*/ void *aux1; void *aux2; @@ -119,7 +115,7 @@ categoricals_destroy ( struct categoricals *cat) } -#if 0 +#if 1 void categoricals_dump (const struct categoricals *cat) { @@ -280,7 +276,7 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) cat->vp[i].cc += weight; if (cat->update) - cat->update (node->user_data, cat->wv, var, c, cat->aux1, cat->aux2); + cat->update (node->user_data, cat->exclude, cat->wv, var, c, cat->aux1, cat->aux2); } } @@ -341,6 +337,8 @@ categoricals_done (struct categoricals *cat) } assert (cat->n_vars <= cat->n_vp); + + // categoricals_dump (cat); } diff --git a/src/math/categoricals.h b/src/math/categoricals.h index 02fef13a30..09ced7d7ac 100644 --- a/src/math/categoricals.h +++ b/src/math/categoricals.h @@ -28,6 +28,7 @@ struct ccase; union value ; typedef void update_func (void *user_data, + enum mv_class exclude, const struct variable *wv, const struct variable *catvar, const struct ccase *c, -- 2.30.2