From dae2296e30e86491c61d824a0bce43abfe376ca6 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Thu, 16 Feb 2012 15:05:07 +0100 Subject: [PATCH] Means: Report totals as well as catagories --- src/language/stats/means.c | 137 +++++++++++++++------------------- tests/language/stats/means.at | 82 +++++++++++++++++--- 2 files changed, 131 insertions(+), 88 deletions(-) diff --git a/src/language/stats/means.c b/src/language/stats/means.c index 3b7bfeef09..c859fb9e69 100644 --- a/src/language/stats/means.c +++ b/src/language/stats/means.c @@ -441,23 +441,26 @@ struct summary }; +struct layer +{ + size_t n_factor_vars; + const struct variable **factor_vars; +}; + /* The thing parsed after TABLES= */ struct mtable { size_t n_dep_vars; const struct variable **dep_vars; - size_t n_interactions; + int n_layers; + struct layer *layers; + struct interaction **interactions; struct summary *summary; - size_t *n_factor_vars; - const struct variable ***factor_vars; - int ii; - int n_layers; - struct categoricals *cats; }; @@ -491,44 +494,14 @@ static void run_means (struct means *cmd, struct casereader *input, const struct dataset *ds); -/* Append all the variables belonging to layer and all subsequent layers - to iact. And then append iact to the means->interaction. - This is a recursive function. - */ -static void -iact_append_factor (struct mtable *means, int layer, - const struct interaction *iact) -{ - int v; - const struct variable **fv; - if (layer >= means->n_layers) - return; - - fv = means->factor_vars[layer]; - - for (v = 0; v < means->n_factor_vars[layer]; ++v) - { - struct interaction *nexti = interaction_clone (iact); - - interaction_add_variable (nexti, fv[v]); - - iact_append_factor (means, layer + 1, nexti); - - if (layer == means->n_layers - 1) - { - means->interactions[means->ii++] = nexti; - } - } -} static bool parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct mtable *table) { table->ii = 0; table->n_layers = 0; - table->factor_vars = NULL; - table->n_factor_vars = NULL; + table->layers = NULL; /* Dependent variable (s) */ if (!parse_variables_const (lexer, cmd->dict, @@ -542,24 +515,33 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct m if (lex_match (lexer, T_BY)) { table->n_layers++; - table->factor_vars = - xrealloc (table->factor_vars, - sizeof (*table->factor_vars) * table->n_layers); - - table->n_factor_vars = - xrealloc (table->n_factor_vars, - sizeof (*table->n_factor_vars) * table->n_layers); - - if (!parse_variables_const (lexer, cmd->dict, - &table->factor_vars[table->n_layers - 1], - &table->n_factor_vars[table->n_layers - - 1], - PV_NO_DUPLICATE)) + table->layers = + xrealloc (table->layers, + sizeof (*table->layers) * table->n_layers); + + if (!parse_variables_const + (lexer, cmd->dict, + &table->layers[table->n_layers - 1].factor_vars, + &table->layers[table->n_layers - 1].n_factor_vars, + PV_NO_DUPLICATE)) return false; } } + /* There is always at least one layer. + However the final layer is the total, and not + normally considered by the user as a + layer. + */ + + table->n_layers++; + table->layers = + xrealloc (table->layers, + sizeof (*table->layers) * table->n_layers); + table->layers[table->n_layers - 1].factor_vars = NULL; + table->layers[table->n_layers - 1].n_factor_vars = 0; + return true; } @@ -768,28 +750,27 @@ cmd_means (struct lexer *lexer, struct dataset *ds) for (t = 0; t < means.n_tables; ++t) { struct mtable *table = &means.table[t]; - table->n_interactions = 1; - for (l = 0; l < table->n_layers; ++l) - { - const int n_vars = table->n_factor_vars[l]; - table->n_interactions *= n_vars; - } table->interactions = - xcalloc (table->n_interactions, sizeof (*table->interactions)); + xcalloc (table->n_layers, sizeof (*table->interactions)); table->summary = - xcalloc (table->n_dep_vars * table->n_interactions, sizeof (*table->summary)); - - - if (table->n_layers > 0) - iact_append_factor (table, 0, interaction_create (NULL)); - else - table->interactions[0] = interaction_create (NULL); + xcalloc (table->n_dep_vars * table->n_layers, sizeof (*table->summary)); + for (l = 0; l < table->n_layers; ++l) + { + int v; + const struct layer *lyr = &table->layers[l]; + const int n_vars = lyr->n_factor_vars; + table->interactions[l] = interaction_create (NULL); + for (v = 0 ; v < n_vars ; ++v) + { + interaction_add_variable (table->interactions[l], + lyr->factor_vars[v]); + } + } } - { struct casegrouper *grouper; struct casereader *group; @@ -894,7 +875,7 @@ update_n (const void *aux1, void *aux2, void *user_data, const struct ccase *c, const double x = case_data (c, table->dep_vars[v])->f; - for (i = 0; i < table->n_interactions; ++i) + for (i = 0; i < table->n_layers; ++i) { if ( is_missing (means, table->dep_vars[v], table->interactions[i], c)) @@ -942,12 +923,11 @@ calculate_n (const void *aux1, void *aux2, void *user_data) } } - static void run_means (struct means *cmd, struct casereader *input, const struct dataset *ds UNUSED) { - int i,t; + int t; const struct variable *wv = dict_get_weight (cmd->dict); struct ccase *c; struct casereader *reader; @@ -962,7 +942,7 @@ run_means (struct means *cmd, struct casereader *input, struct mtable *table = &cmd->table[t]; table->cats = categoricals_create (table->interactions, - table->n_interactions, wv, cmd->exclude); + table->n_layers, wv, cmd->exclude); categoricals_set_payload (table->cats, &payload, cmd, table); } @@ -979,7 +959,7 @@ run_means (struct means *cmd, struct casereader *input, for (v = 0; v < table->n_dep_vars; ++v) { int i; - for (i = 0; i < table->n_interactions; ++i) + for (i = 0; i < table->n_layers; ++i) { const bool missing = is_missing (cmd, table->dep_vars[v], @@ -987,10 +967,10 @@ run_means (struct means *cmd, struct casereader *input, if (missing) { something_missing = true; - table->summary[v * table->n_interactions + i].missing++; + table->summary[v * table->n_layers + i].missing++; } else - table->summary[v * table->n_interactions + i].non_missing++; + table->summary[v * table->n_layers + i].non_missing++; } } if ( something_missing && cmd->listwise_exclude) @@ -1011,20 +991,22 @@ run_means (struct means *cmd, struct casereader *input, for (t = 0; t < cmd->n_tables; ++t) { + int i; const struct mtable *table = &cmd->table[t]; output_case_processing_summary (table); - for (i = 0; i < table->n_interactions; ++i) + for (i = 0; i < table->n_layers; ++i) { output_report (cmd, i, table); } - categoricals_destroy (table->cats); } + } + static void output_case_processing_summary (const struct mtable *table) { @@ -1033,7 +1015,7 @@ output_case_processing_summary (const struct mtable *table) const int heading_rows = 3; struct tab_table *t; - const int nr = heading_rows + table->n_interactions * table->n_dep_vars; + const int nr = heading_rows + table->n_layers * table->n_dep_vars; const int nc = 7; t = tab_create (nc, nr); @@ -1070,9 +1052,9 @@ output_case_processing_summary (const struct mtable *table) { const struct variable *var = table->dep_vars[v]; const char *dv_name = var_to_string (var); - for (i = 0; i < table->n_interactions; ++i) + for (i = 0; i < table->n_layers; ++i) { - const int row = v * table->n_interactions + i; + const int row = v * table->n_layers + i; const struct interaction *iact = table->interactions[i]; casenumber n_total; @@ -1123,7 +1105,6 @@ output_case_processing_summary (const struct mtable *table) } - static void output_report (const struct means *cmd, int iact_idx, const struct mtable *table) diff --git a/tests/language/stats/means.at b/tests/language/stats/means.at index 5bef5e911c..4a82786c11 100644 --- a/tests/language/stats/means.at +++ b/tests/language/stats/means.at @@ -45,11 +45,16 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent score: factor,26,100%,0,0%,26,100% +score: ,26,100%,0,0%,26,100% Table: Report ,factor,Mean,N,Std. Deviation score,1.00000,19.78947,19.00000,4.03566 ,2.00000,24.00000,7.00000,5.50757 + +Table: Report +,Mean,N,Std. Deviation +score,20.92308,26.00000,4.75750 ]) AT_CLEANUP @@ -107,8 +112,8 @@ begin data. end data. MEANS TABLES = - a BY g1 - a BY g2 + a BY g1 g2 + BY g2 /cells = MEAN COUNT . ]) @@ -120,7 +125,8 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1 * g2,6,75%,2,25%,8,100% -a: a * g2,6,75%,2,25%,8,100% +a: g2,6,75%,2,25%,8,100% +a: ,7,87.5%,1,12.5%,8,100% Table: Report ,g1,g2,Mean,N @@ -130,13 +136,14 @@ a,1.00,11.00,4.00,1.00 ,2.00,31.00,5.67,3.00 Table: Report -,a,g2,Mean,N -a,2.00,31.00,2.00,1.00 -,3.00,21.00,3.00,1.00 -,4.00,11.00,4.00,1.00 -,6.00,21.00,6.00,1.00 -,7.00,31.00,7.00,1.00 -,8.00,31.00,8.00,1.00 +,g2,Mean,N +a,11.00,4.00,1.00 +,21.00,4.50,2.00 +,31.00,5.67,3.00 + +Table: Report +,Mean,N +a,5.00,6.00 ]) AT_CLEANUP @@ -176,11 +183,16 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent test1: group,10,100%,0,0%,10,100% +test1: ,10,100%,0,0%,10,100% Table: Report ,group,Mean,N,Std. Deviation,Sum,Min,Max,Range,Variance,Kurtosis,Skewness test1,experimental group,86.2000,5.0000,8.9833,431.0000,75.0000,99.0000,24.0000,80.7000,.2727,.3858 ,control group,61.8000,5.0000,10.0598,309.0000,50.0000,71.0000,21.0000,101.2000,-3.0437,-.4830 + +Table: Report +,Mean,N,Std. Deviation,Sum,Min,Max,Range,Variance,Kurtosis,Skewness +test1,74.0000,10.0000,15.6915,740.0000,50.0000,99.0000,49.0000,246.2222,-.5759,-.1262 ]) AT_CLEANUP @@ -216,11 +228,16 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent test1: group,10,100%,0,0%,10,100% +test1: ,10,100%,0,0%,10,100% Table: Report ,group,Mean,N,S.E. Mean,S.E. Skew,S.E. Kurt test1,1.0000,83.5000,6.0000,4.2485,.8452,1.7408 ,2.0000,59.7500,4.0000,5.1700,1.0142,2.6186 + +Table: Report +,Mean,N,S.E. Mean,S.E. Skew,S.E. Kurt +test1,74.0000,10.0000,4.9621,.6870,1.3342 ]) AT_CLEANUP @@ -363,7 +380,9 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% b: g1,6,85.7143%,1,14.2857%,7,100% +b: ,6,85.7143%,1,14.2857%,7,100% Table: Report ,g1,N @@ -372,23 +391,35 @@ a,1.00,3.00 b,1.00,3.00 ,2.00,3.00 +Table: Report +,N +a,6.00 +b,6.00 + Table: Case Processing Summary ,Cases,,,,, ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% Table: Report ,g1,N a,1.00,3.00 ,2.00,4.00 +Table: Report +,N +a,7.00 + Table: Case Processing Summary ,Cases,,,,, ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% b: g1,6,85.7143%,1,14.2857%,7,100% +b: ,6,85.7143%,1,14.2857%,7,100% Table: Report ,g1,N @@ -397,16 +428,26 @@ a,1.00,3.00 b,1.00,3.00 ,2.00,3.00 +Table: Report +,N +a,7.00 +b,6.00 + Table: Case Processing Summary ,Cases,,,,, ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% Table: Report ,g1,N a,1.00,3.00 ,2.00,4.00 + +Table: Report +,N +a,7.00 ]) AT_CLEANUP @@ -446,7 +487,9 @@ Table: Case Processing Summary ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,6,85.7143%,1,14.2857%,7,100% +a: ,7,100%,0,0%,7,100% b: g1,5,71.4286%,2,28.5714%,7,100% +b: ,6,85.7143%,1,14.2857%,7,100% Table: Report ,g1,N @@ -455,12 +498,19 @@ a,1.00,2.00 b,1.00,2.00 ,2.00,3.00 +Table: Report +,N +a,6.00 +b,5.00 + Table: Case Processing Summary ,Cases,,,,, ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% b: g1,7,100%,0,0%,7,100% +b: ,7,100%,0,0%,7,100% Table: Report ,g1,N @@ -471,12 +521,19 @@ b,1.00,2.00 ,2.00,4.00 ,9.00,1.00 +Table: Report +,N +a,7.00 +b,7.00 + Table: Case Processing Summary ,Cases,,,,, ,Included,,Excluded,,Total, ,N,Percent,N,Percent,N,Percent a: g1,7,100%,0,0%,7,100% +a: ,7,100%,0,0%,7,100% b: g1,6,85.7143%,1,14.2857%,7,100% +b: ,6,85.7143%,1,14.2857%,7,100% Table: Report ,g1,N @@ -486,6 +543,11 @@ a,1.00,2.00 b,1.00,2.00 ,2.00,3.00 ,9.00,1.00 + +Table: Report +,N +a,7.00 +b,6.00 ]) AT_CLEANUP -- 2.30.2